]> git.corax.cc Git - ccc/commitdiff
lex: Add functions to the lexer so the tokens can be accessed more flexibly
authorMatthias Kruk <m@m10k.eu>
Sun, 31 May 2020 23:39:06 +0000 (08:39 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 31 May 2020 23:39:06 +0000 (08:39 +0900)
src/lex.c
src/lex.h

index 655509857b5391e8699b0373a81b385856e677c6..dc0366edc161b4d6c0fda7a52db83fffb30f6602 100644 (file)
--- a/src/lex.c
+++ b/src/lex.c
@@ -34,8 +34,6 @@
 #define STATE_ZERO        23
 #define STATE_NUM         24
 
-#define STATE_DONE    8
-
 #define identifier_firstchr(_c) ((_c) == '_' ||                        \
                                 (_c) >= 'a' && (_c) <= 'z' ||  \
                                 (_c) >= 'A' && (_c) <= 'Z')
                                 (_c) >= '0' && (_c) <= '9')
 
 static char _next = 0;
-static int _state = STATE_NONE;
 static int _line = 1;
 static int _col = 0;
 static int _pline = 1;
 static int _pcol = 1;
-static struct token *_next_token = NULL;
+
+struct token **_tokens;
+int _num_tokens;
+int _cur_token;
+
+struct tokenlist {
+       struct token *token;
+       struct tokenlist *next;
+};
 
 static int _getnextchar(void)
 {
@@ -107,16 +112,12 @@ static int _putnextchar(const char c)
        return(ret_val);
 }
 
-struct token *lex_gettoken(void)
+struct token *_nexttoken(void)
 {
        struct token *tok;
        int state;
        char c;
 
-       if(_next_token) {
-               return(_next_token);
-       }
-
        state = STATE_NONE;
        tok = NULL;
 
@@ -531,16 +532,102 @@ struct token *lex_gettoken(void)
        return(tok);
 }
 
-int lex_puttoken(struct token *tok)
+void _initialize(void)
+{
+       struct tokenlist *list;
+       struct tokenlist *last;
+       struct token *tok;
+       int i;
+
+       list = NULL;
+       last = NULL;
+       _num_tokens = 0;
+
+       while((tok = _nexttoken())) {
+               struct tokenlist *item;
+
+               item = malloc(sizeof(*item));
+               assert(item);
+
+               item->token = tok;
+               item->next = NULL;
+
+               if(last) {
+                       last->next = item;
+                       last = item;
+               } else {
+                       list = item;
+                       last = item;
+               }
+
+               _num_tokens++;
+       }
+
+       _tokens = malloc(sizeof(struct token*) * (_num_tokens  + 1));
+       assert(_tokens);
+
+       for(i = 0; list; ) {
+               struct tokenlist *cur;
+
+               cur = list;
+               _tokens[i++] = cur->token;
+               list = cur->next;
+
+               free(cur);
+       }
+
+       _tokens[i] = NULL;
+       _cur_token = 0;
+
+       return;
+}
+
+int lex_getpos(void)
+{
+       return(_cur_token);
+}
+
+int lex_setpos(int pos)
 {
        int ret_val;
 
-       if(_next_token) {
-               ret_val = -EALREADY;
-       } else {
-               _next_token = tok;
+       ret_val = -ERANGE;
+
+       if(pos >= 0 && pos <= _num_tokens) {
+               _cur_token = pos;
                ret_val = 0;
        }
 
        return(ret_val);
 }
+
+int lex_next(void)
+{
+        return(lex_setpos(_cur_token + 1));
+}
+
+int lex_prev(void)
+{
+       return(lex_setpos(_cur_token - 1));
+}
+
+struct token *lex_gettoken(void)
+{
+       static int initialized = 0;
+
+       if(!initialized) {
+               _initialize();
+               initialized = 1;
+       }
+
+       if(_cur_token > _num_tokens) {
+               return(NULL);
+       }
+
+       return(_tokens[_cur_token++]);
+}
+
+struct token *lex_peek(void)
+{
+       return(_cur_token < _num_tokens ? _tokens[_cur_token] : NULL);
+}
index a4fc46efa467597556554a6a0cefca4ce2cb5121..d614d5f848c1204f0ac6c79337cb4a00dfe542c4 100644 (file)
--- a/src/lex.h
+++ b/src/lex.h
@@ -3,7 +3,12 @@
 
 #include "token.h"
 
+int lex_getpos(void);
+int lex_setpos(int);
+int lex_prev(void);
+int lex_next(void);
+
 struct token *lex_gettoken(void);
-int lex_puttoken(struct token*);
+struct token *lex_peek(void);
 
 #endif /* LEX_H */