#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)
{
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;
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);
+}