]> ocean-lang.org Git - ocean/commitdiff
parsergen: centralise (some of) the collecting of next token.
authorNeilBrown <neilb@suse.de>
Sun, 24 Nov 2013 07:14:11 +0000 (18:14 +1100)
committerNeilBrown <neilb@suse.de>
Sun, 24 Nov 2013 07:17:10 +0000 (18:17 +1100)
A future patch will introduce next sites where we want to
discard the current token.
Rather than calling "token_next" at each site, make it possible
to just set "tk = NULL", and the next token will automatically
be collected when needed.

Signed-off-by: NeilBrown <neilb@suse.de>
csrc/parsergen.mdc

index 252b3d6f6c88724c90b93163f392b0b4bfd23279..eb63507a4915c34661baefe02a87ebad2b3a44dd 100644 (file)
@@ -2396,18 +2396,20 @@ drop input tokens until we find one we can shift into the new error state.
                         FILE *trace, const char *non_term[], int knowns)
        {
                struct parser p = { 0 };
-               struct token *tk;
+               struct token *tk = NULL;
                int accepted = 0;
                void *ret;
 
-               tk = tok_copy(token_next(tokens));
                while (!accepted) {
+                       struct token *err_tk;
+                       if (!tk)
+                               tk = tok_copy(token_next(tokens));
                        p.next.sym = tk->num;
                        if (trace)
                                parser_trace(trace, &p, tk, states, non_term, knowns);
 
                        if (shift(&p, tk, states)) {
-                               tk = tok_copy(token_next(tokens));
+                               tk = NULL;
                                continue;
                        }
                        if (states[p.next.state].reduce_prod >= 0) {
@@ -2436,12 +2438,18 @@ drop input tokens until we find one we can shift into the new error state.
                         * Then we discard input tokens until
                         * we find one that is acceptable.
                         */
+
+                       err_tk = tok_copy(*tk);
                        p.next.sym = TK_error;
-                       while (shift(&p, tk, states) == 0
+                       while (shift(&p, err_tk, states) == 0
                               && p.tos > 0)
                                // discard this state
                                pop(&p, 1, do_free);
-                       tk = tok_copy(*tk);
+                       if (p.tos == 0) {
+                               free(err_tk);
+                               // no state accepted TK_error
+                               break;
+                       }
                        while (search(&states[p.next.state], tk->num) < 0 &&
                               tk->num != TK_eof) {
                                free(tk);