- "do", "else", "if", "while",
- "break", "continue", "return", "goto",
- "for", "typedef"
+ "do", "else", "if", "while",
+ "break", "continue", "return", "goto",
+ "for", "typedef"
- struct lex_file *lex = mem_a(sizeof(struct lex_file));
- if (!lex || !fp)
- return NULL;
-
- lex->file = fp;
- fseek(lex->file, 0, SEEK_END);
- lex->length = ftell(lex->file);
- lex->size = lex->length; /* copy, this is never changed */
- fseek(lex->file, 0, SEEK_SET);
- lex->last = 0;
- lex->line = 0;
-
- memset(lex->peek, 0, sizeof(lex->peek));
- return lex;
+ struct lex_file *lex = mem_a(sizeof(struct lex_file));
+ if (!lex || !fp)
+ return NULL;
+
+ lex->file = fp;
+ fseek(lex->file, 0, SEEK_END);
+ lex->length = ftell(lex->file);
+ lex->size = lex->length; /* copy, this is never changed */
+ fseek(lex->file, 0, SEEK_SET);
+ lex->last = 0;
+ lex->line = 0;
+
+ memset(lex->peek, 0, sizeof(lex->peek));
+ return lex;
- if (file->current < sizeof(file->lastok)-1)
- file->lastok[file->current++] = (char)ch;
- if (file->current == sizeof(file->lastok)-1)
- file->lastok[file->current] = (char)'\0';
+ if (file->current < sizeof(file->lastok)-1)
+ file->lastok[file->current++] = (char)ch;
+ if (file->current == sizeof(file->lastok)-1)
+ file->lastok[file->current] = (char)'\0';
- int ch;
- if ((ch = lex_inget(file)) != '?') {
- lex_unget(ch, file);
- return '?';
- }
-
- ch = lex_inget(file);
- switch (ch) {
- case '(' : return '[' ;
- case ')' : return ']' ;
- case '/' : return '\\';
- case '\'': return '^' ;
- case '<' : return '{' ;
- case '>' : return '}' ;
- case '!' : return '|' ;
- case '-' : return '~' ;
- case '=' : return '#' ;
- default:
- lex_unget('?', file);
- lex_unget(ch , file);
- return '?';
- }
- return '?';
+ int ch;
+ if ((ch = lex_inget(file)) != '?') {
+ lex_unget(ch, file);
+ return '?';
+ }
+
+ ch = lex_inget(file);
+ switch (ch) {
+ case '(' : return '[' ;
+ case ')' : return ']' ;
+ case '/' : return '\\';
+ case '\'': return '^' ;
+ case '<' : return '{' ;
+ case '>' : return '}' ;
+ case '!' : return '|' ;
+ case '-' : return '~' ;
+ case '=' : return '#' ;
+ default:
+ lex_unget('?', file);
+ lex_unget(ch , file);
+ return '?';
+ }
+ return '?';
- int ch = lex_inget(file);
- switch (first) {
- case '<':
- if (ch == '%') return '{';
- if (ch == ':') return '[';
- break;
- case '%':
- if (ch == '>') return '}';
- if (ch == ':') return '#';
- break;
- case ':':
- if (ch == '>') return ']';
- break;
- }
-
- lex_unget(ch, file);
- return first;
+ int ch = lex_inget(file);
+ switch (first) {
+ case '<':
+ if (ch == '%') return '{';
+ if (ch == ':') return '[';
+ break;
+ case '%':
+ if (ch == '>') return '}';
+ if (ch == ':') return '#';
+ break;
+ case ':':
+ if (ch == '>') return ']';
+ break;
+ }
+
+ lex_unget(ch, file);
+ return first;
- int ch;
- lex_clear(file);
- ch = lex_getch(file);
-
- if (ch == '/') {
- lex_addch('/', file);
- lex_addch('/', file);
-
- while ((ch = lex_getch(file)) != '\n') {
- if (ch == '\\') {
- lex_addch(ch, file);
- lex_addch(lex_getch(file), file);
- } else {
- lex_addch(ch, file);
- }
- }
- lex_addch('\0', file);
- return LEX_COMMENT;
- }
-
- if (ch != '*') {
- lex_unget(ch, file);
- return '/';
- }
-
- lex_addch('/', file);
-
- /* hate this */
- do {
- lex_addch(ch, file);
- while ((ch = lex_getch(file)) != '*') {
- if (ch == EOF)
- return error(ERROR_LEX, "malformatted comment at line", "");
- else
- lex_addch(ch, file);
- }
- lex_addch(ch, file);
- } while ((ch = lex_getch(file)) != '/');
-
- lex_addch('/', file);
- lex_addch('\0', file);
-
- return LEX_COMMENT;
+ int ch;
+ lex_clear(file);
+ ch = lex_getch(file);
+
+ if (ch == '/') {
+ lex_addch('/', file);
+ lex_addch('/', file);
+
+ while ((ch = lex_getch(file)) != '\n') {
+ if (ch == '\\') {
+ lex_addch(ch, file);
+ lex_addch(lex_getch(file), file);
+ } else {
+ lex_addch(ch, file);
+ }
+ }
+ lex_addch('\0', file);
+ return LEX_COMMENT;
+ }
+
+ if (ch != '*') {
+ lex_unget(ch, file);
+ return '/';
+ }
+
+ lex_addch('/', file);
+
+ /* hate this */
+ do {
+ lex_addch(ch, file);
+ while ((ch = lex_getch(file)) != '*') {
+ if (ch == EOF)
+ return error(file, ERROR_LEX, "malformatted comment");
+ else
+ lex_addch(ch, file);
+ }
+ lex_addch(ch, file);
+ } while ((ch = lex_getch(file)) != '/');
+
+ lex_addch('/', file);
+ lex_addch('\0', file);
+
+ return LEX_COMMENT;
- int ch = lex_get(file);
-
- /* skip char/string/comment */
- switch (ch) {
- case '\'': return lex_skipchr(file);
- case '"': return lex_skipstr(file);
- case '/': return lex_skipcmt(file);
- default:
- return ch;
- }
+ int ch = lex_get(file);
+
+ /* skip char/string/comment */
+ switch (ch) {
+ case '\'': return lex_skipchr(file);
+ case '"': return lex_skipstr(file);
+ case '/': return lex_skipcmt(file);
+ default:
+ return ch;
+ }
- int ch = lex_getsource(file);
- int it;
-
- /* valid identifier */
- if (ch > 0 && (ch == '_' || isalpha(ch))) {
- lex_clear(file);
-
- /*
- * Yes this is dirty, but there is no other _sane_ easy
- * way to do it, this is what I call defensive programming
- * if something breaks, add more defense :-)
- */
- while (ch > 0 && ch != ' ' && ch != '(' &&
- ch != '\n' && 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 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;
+ int ch = lex_getsource(file);
+ int it;
+
+ /* valid identifier */
+ if (ch > 0 && (ch == '_' || isalpha(ch))) {
+ lex_clear(file);
+
+ /*
+ * Yes this is dirty, but there is no other _sane_ easy
+ * way to do it, this is what I call defensive programming
+ * if something breaks, add more defense :-)
+ */
+ while (ch > 0 && ch != ' ' && ch != '(' &&
+ ch != '\n' && 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], 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;
- 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));
+ 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));
- char *find = util_strrq(file);
- /*
- * Dissallow recrusive include: this could easily cause some breakage
- * and instant OOM.
- */
- if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
- error(ERROR_LEX, "%s:%d Source file cannot include itself\n", lex->name, lex->line-1);
- exit (-1);
- }
-
- FILE *fp = fopen(file, "r");
- if (!fp) {
- error(ERROR_LEX, "%s:%d Include file `%s` doesn't exist\n", lex->name, lex->line, file);
- exit (-1);
- }
-
- return lex_open(fp);
+ 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);