]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lex.c
Count file lines
[xonotic/gmqcc.git] / lex.c
diff --git a/lex.c b/lex.c
index d7c7b006738ea248219ce7d22e2a24fe0dd55353..4e84bcec7f42ccf10eb6f78b2881f82819b6153c 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -39,26 +39,25 @@ static const char *const lex_keywords[] = {
 
 struct lex_file *lex_open(FILE *fp) {
        struct lex_file *lex = mem_a(sizeof(struct lex_file));
-       if (lex) {
-               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;
+       if (!lex || !fp)
+               return NULL;
                
-               memset(lex->peek, 0, sizeof(lex->peek));
-       }
+       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;
+       
+       memset(lex->peek, 0, sizeof(lex->peek));
        return lex;
 }
 
-int lex_close(struct lex_file *file) {
-       int ret = -1;
-       if (file) {
-               ret = fclose(file->file);
-               mem_d(file);
-       }
-       return ret;
+void lex_close(struct lex_file *file) {
+       if (!file) return;
+       
+       fclose(file->file); /* may already be closed */
+       mem_d(file);
 }
 
 static void lex_addch(int ch, struct lex_file *file) {
@@ -139,10 +138,16 @@ static int lex_digraph(struct lex_file *file, int first) {
 
 static int lex_getch(struct lex_file *file) {
        int ch = lex_inget(file);
-       if (ch == '?')
-               return lex_trigraph(file);
-       if (ch == '<' || ch == ':' || ch == '%')
-               return lex_digraph (file, ch);
+       
+       switch (ch) {
+               case '?' :
+                       return lex_trigraph(file);
+               case '<' :
+               case ':' :
+               case '%' :
+                       return lex_digraph (file, ch);
+               case '\n': file->line ++;
+       }
                
        return ch;
 }
@@ -151,7 +156,7 @@ static int lex_get(struct lex_file *file) {
        int ch;
        if (!isspace(ch = lex_getch(file)))
                return ch;
-       
+               
        /* skip over all spaces */
        while (isspace(ch) && ch != '\n')
                ch = lex_getch(file);
@@ -238,7 +243,7 @@ static int lex_skipcmt(struct lex_file *file) {
                lex_addch(ch, file);
                while ((ch = lex_getch(file)) != '*') {
                        if (ch == EOF)
-                               return error(ERROR_LEX, "malformatted comment at line %d", file->line);
+                               return error(ERROR_LEX, "malformatted comment at line", "");
                        else
                                lex_addch(ch, file);
                }
@@ -259,7 +264,8 @@ static int lex_getsource(struct lex_file *file) {
                case '\'': return lex_skipchr(file);
                case '"':  return lex_skipstr(file);
                case '/':  return lex_skipcmt(file);
-               default:   return ch;
+               default:
+                       return ch;
        }
 }
 
@@ -294,7 +300,7 @@ int lex_token(struct lex_file *file) {
                        if (!strncmp(X, "entity", sizeof("entity"))) \
                            return TOKEN_ENTITY;                     \
                        if (!strncmp(X, "void"  , sizeof("void")))   \
-                               return TOKEN_VOID;                       \
+                           return TOKEN_VOID;                       \
                    } while(0)
                
                TEST_TYPE(file->lastok);
@@ -303,6 +309,7 @@ int lex_token(struct lex_file *file) {
                if (typedef_find(file->lastok))
                        TEST_TYPE(typedef_find(file->lastok)->name);
                        
+               #undef TEST_TYPE
                return LEX_IDENT;
        }
        return ch;