From edb42c0da8f087003af8f7b60d72107edb4feb71 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 1 Jun 2020 08:39:06 +0900 Subject: [PATCH] lex: Add functions to the lexer so the tokens can be accessed more flexibly --- src/lex.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++------- src/lex.h | 7 +++- 2 files changed, 107 insertions(+), 15 deletions(-) diff --git a/src/lex.c b/src/lex.c index 6555098..dc0366e 100644 --- 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') @@ -43,12 +41,19 @@ (_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); +} diff --git a/src/lex.h b/src/lex.h index a4fc46e..d614d5f 100644 --- 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 */ -- 2.47.3