-int lex_token(struct lex_file *file) {
- int ch = lex_getsource(file);
- int it;
-
- /* valid identifier */
- if (ch > 0 && (ch == '_' || isalpha(ch))) {
- lex_clear(file);
- while (ch > 0 && (isalpha(ch) || ch == '_')) {
- lex_addch(ch, file);
- ch = lex_getsource(file);
- }
- lex_unget(ch, file);
- lex_addch('\0', file);
-
- /* look inside the table for a keyword .. */
- for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
- if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
- return it;
-
- /* try the hashtable for typedefs? */
- if (typedef_find(file->lastok))
- for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
- if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it])))
- return it;
-
-
- return LEX_IDENT;
- }
- return ch;
+int lex_token(lex_file *file) {
+ int ch = lex_getsource(file);
+ int it;
+
+ /* valid identifier */
+ if (ch > 0 && (ch == '_' || isalpha(ch))) {
+ lex_clear(file);
+
+ while (ch > 0 && (ch == '_' || isalpha(ch))) {
+ lex_addch(ch, file);
+ ch = lex_getsource(file);
+ }
+ lex_unget(ch, file);
+ lex_addch('\0', file);
+
+ /* look inside the table for a keyword .. */
+ for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
+ if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
+ return it;
+
+ /* try a type? */
+ #define TEST_TYPE(X) \
+ do { \
+ if (!strncmp(X, "float", sizeof("float"))) \
+ return TOKEN_FLOAT; \
+ if (!strncmp(X, "vector", sizeof("vector"))) \
+ return TOKEN_VECTOR; \
+ if (!strncmp(X, "string", sizeof("string"))) \
+ return TOKEN_STRING; \
+ if (!strncmp(X, "entity", sizeof("entity"))) \
+ return TOKEN_ENTITY; \
+ if (!strncmp(X, "void" , sizeof("void"))) \
+ return TOKEN_VOID; \
+ } while(0)
+
+ TEST_TYPE(file->lastok);
+
+ /* try the hashtable for typedefs? */
+ if (typedef_find(file->lastok))
+ TEST_TYPE(typedef_find(file->lastok)->name);
+
+ #undef TEST_TYPE
+ return LEX_IDENT;
+ }
+ return (ch != ' ') ? ch : lex_token(file);
+}
+
+void lex_reset(lex_file *file) {
+ file->current = 0;
+ file->last = 0;
+ file->length = file->size;
+ fseek(file->file, 0, SEEK_SET);
+
+ memset(file->peek, 0, sizeof(file->peek ));
+ memset(file->lastok, 0, sizeof(file->lastok));
+}
+
+void lex_parse(lex_file *file) {
+ if (!file) return;
+ parse_gen(file); /* run parser */