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 ================================
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 *, ...);
"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 */
lex->line = 0;
memset(lex->peek, 0, sizeof(lex->peek));
- return lex;
+ *set = lex;
}
void lex_close(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;
}
}
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
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:
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");
* 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 == '\\') {
* 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;
}
/*