Interface change

This commit is contained in:
Dale Weiler 2012-04-28 05:35:32 -04:00
parent 33b1995fa5
commit 72053e16d2
5 changed files with 40 additions and 32 deletions

View file

@ -167,8 +167,9 @@ enum {
int lex_token (lex_file *);
void lex_reset (lex_file *);
void lex_close (lex_file *);
lex_file *lex_include(lex_file *, char *);
lex_file *lex_open (FILE *);
void lex_parse (lex_file *);
lex_file *lex_include(lex_file *, const char *);
void lex_init (const char *, lex_file **);
//===================================================================
//========================== error.c ================================
@ -208,8 +209,8 @@ void util_meminfo ();
bool util_strupper (const char *);
bool util_strdigit (const char *);
char *util_strdup (const char *);
char *util_strrq (char *);
char *util_strrnl (char *);
char *util_strrq (const char *);
char *util_strrnl (const char *);
char *util_strsws (const char *);
char *util_strchp (const char *, const char *);
void util_debug (const char *, const char *, ...);

37
lex.c
View file

@ -32,12 +32,17 @@ static const char *const lex_keywords[] = {
"for", "typedef"
};
lex_file *lex_open(FILE *fp) {
void lex_init(const char *file, lex_file **set) {
lex_file *lex = mem_a(sizeof(lex_file));
if (!lex || !fp)
return NULL;
lex->file = fp;
if (!lex)
return;
lex->file = fopen(file, "r");
if (!lex->file) {
mem_d(lex);
return;
}
fseek(lex->file, 0, SEEK_END);
lex->length = ftell(lex->file);
lex->size = lex->length; /* copy, this is never changed */
@ -46,7 +51,7 @@ lex_file *lex_open(FILE *fp) {
lex->line = 0;
memset(lex->peek, 0, sizeof(lex->peek));
return lex;
*set = lex;
}
void lex_close(lex_file *file) {
@ -333,23 +338,25 @@ void lex_reset(lex_file *file) {
memset(file->lastok, 0, sizeof(file->lastok));
}
void lex_parse(lex_file *file) {
if (!file) return;
parse_gen(file); /* run parser */
}
/*
* Include a file into the lexer / parsing process: This really
* should check if names are the same to prevent endless include
* recrusion.
*/
lex_file *lex_include(lex_file *lex, char *file) {
lex_file *lex_include(lex_file *lex, const char *file) {
util_strrq(file);
if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
error(lex, ERROR_LEX, "Source file cannot include itself\n");
exit (-1);
}
FILE *fp = fopen(file, "r");
if (!fp) {
error(lex, ERROR_LEX, "Include file `%s` doesn't exist\n", file);
exit (-1);
}
return lex_open(fp);
lex_file *set = NULL;
lex_init(file, &set);
return set;
}

12
main.c
View file

@ -58,9 +58,10 @@ static const int usage(const char *const app) {
}
int main(int argc, char **argv) {
size_t itr = 0;
char *app = &argv[0][0];
FILE *fpp = NULL;
size_t itr = 0;
char *app = &argv[0][0];
FILE *fpp = NULL;
lex_file *lex = NULL;
/*
* Parse all command line arguments. This is rather annoying to do
@ -137,9 +138,8 @@ int main(int argc, char **argv) {
for (; itr < items_elements; itr++) {
switch (items_data[itr].type) {
case 0:
fpp = fopen(items_data[itr].name, "r");
lex_file *lex = lex_open(fpp);
parse_gen(lex);
lex_init (items_data[itr].name, &lex);
lex_parse(lex);
lex_close(lex);
break;
case 1:

View file

@ -263,7 +263,7 @@ int parse_gen(lex_file *file) {
return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
char *copy = util_strdup(file->lastok);
lex_file *next = lex_include(file, copy);
lex_file *next = lex_include(file, copy);
if (!next) {
error(file, ERROR_INTERNAL, "Include subsystem failure\n");

12
util.c
View file

@ -102,9 +102,9 @@ char *util_strdup(const char *s) {
* as well. This function shouldn't be used to create a
* char array that is later freed (it uses pointer arith)
*/
char *util_strrq(char *s) {
char *dst = s;
char *src = s;
char *util_strrq(const char *s) {
char *dst = (char*)s;
char *src = (char*)s;
char chr;
while ((chr = *src++) != '\0') {
if (chr == '\\') {
@ -141,14 +141,14 @@ char *util_strchp(const char *s, const char *e) {
* done pointer wise instead of strlen(), and an array
* access.
*/
char *util_strrnl(char *src) {
char *util_strrnl(const char *src) {
if (!src) return NULL;
char *cpy = src;
char *cpy = (char*)src;
while (*cpy && *cpy != '\n')
cpy++;
*cpy = '\0';
return src;
return (char*)src;
}
/*