]> ocean-lang.org Git - ocean/blobdiff - csrc/parsergen.mdc
parsergen: revise rule for NEWLINE forcing reduce
[ocean] / csrc / parsergen.mdc
index d97f98c7b3ff724f2b81488d5a6ffbd6410ef390..48c4d62c6fe58f44164ac1382121806ea1255c20 100644 (file)
@@ -2570,17 +2570,18 @@ removed. It is called _after_ we reduce a production, just before we
                       void(*do_free)(short sym, void *asn))
        {
                int i;
-               short indents;
+               short indents = 0;
+               int sol = 0;
+
                p->tos -= num;
-               if (start_of_line)
-                       *start_of_line =
-                               !p->stack[p->tos].since_newline;
-               indents = 0;
                for (i = 0; i < num; i++) {
+                       sol |= !p->stack[p->tos+1].since_newline;
                        indents += p->stack[p->tos+i].indents;
                        do_free(p->stack[p->tos+i].sym,
                                p->asn_stack[p->tos+i]);
                }
+               if (start_of_line)
+                       *start_of_line = sol;
                return indents;
        }
 
@@ -2652,7 +2653,6 @@ since the last state which could have been at the start of a line.
                         struct token_config *config)
        {
                struct parser p = { 0 };
-               struct frame next = { 0 };
                struct token *tk = NULL;
                int accepted = 0;
                void *ret = NULL;
@@ -2663,11 +2663,10 @@ since the last state which could have been at the start of a line.
                        struct frame *tos = &p.stack[p.tos-1];
                        if (!tk)
                                tk = tok_copy(token_next(tokens));
-                       next.sym = tk->num;
-                       parser_trace(trace, &p, next.indents, !next.since_newline,
+                       parser_trace(trace, &p,
                                     tk, states, non_term, config->known_count);
 
-                       if (next.sym == TK_in) {
+                       if (tk->num == TK_in) {
                                tos->indents += 1;
                                tos->since_newline = 0;
                                tos->since_indent = 0;
@@ -2678,7 +2677,7 @@ since the last state which could have been at the start of a line.
                                parser_trace_action(trace, "Record");
                                continue;
                        }
-                       if (next.sym == TK_out) {
+                       if (tk->num == TK_out) {
                                if (states[tos->state].reduce_size >= 0 &&
                                    states[tos->state].reduce_size <= tos->since_indent)
                                        goto force_reduce;
@@ -2696,7 +2695,7 @@ since the last state which could have been at the start of a line.
                                                        in->newline_permitted = 0;
                                                }
                                                if (states[in->state].starts_line)
-                                                       in->newline_permitted = 0;
+                                                       in->newline_permitted = 1;
                                                while (in < tos) {
                                                        in += 1;
                                                        in->since_indent = in[-1].since_indent + 1;
@@ -2714,20 +2713,19 @@ since the last state which could have been at the start of a line.
                                // fall through and force a REDUCE (as 'shift'
                                // will fail).
                        }
-                       if (next.sym == TK_newline) {
+                       if (tk->num == TK_newline) {
                                if (!tos->newline_permitted) {
                                        free(tk);
                                        tk = NULL;
                                        parser_trace_action(trace, "Discard");
                                        continue;
                                }
-                               if (states[tos->state].reduce_size >= 0 &&
+                               if (tos->since_newline > 1 &&
+                                   states[tos->state].reduce_size >= 0 &&
                                    states[tos->state].reduce_size <= tos->since_newline)
                                        goto force_reduce;
                        }
                        if (shift(&p, tk->num, 0, tk->num == TK_newline, tk, states)) {
-                               next.since_newline = !(tk->num == TK_newline);
-                               next.indents = 0;
                                tk = NULL;
                                parser_trace_action(trace, "Shift");
                                continue;
@@ -2751,9 +2749,8 @@ since the last state which could have been at the start of a line.
                                        indents = pop(&p, size, &start_of_line,
                                                      do_free);
                                else {
-                                       indents = next.indents;
+                                       indents = 0;
                                        start_of_line = 0;
-                                       next.indents = 0;
                                }
                                res = memdup(buf, bufsize);
                                memset(buf, 0, bufsize);
@@ -2788,8 +2785,7 @@ since the last state which could have been at the start of a line.
                        short indents = 0, start_of_line;
 
                        err_tk = tok_copy(*tk);
-                       next.sym = TK_error;
-                       while (shift(&p, TK_error, next.indents, !next.since_newline,
+                       while (shift(&p, TK_error, 0, 0,
                                     err_tk, states) == 0
                               && p.tos > 0)
                                // discard this state
@@ -2870,7 +2866,7 @@ end inside square brackets.
                fprintf(trace, ") ");
        }
 
-       void parser_trace(FILE *trace, struct parser *p, int indents, int start_of_line,
+       void parser_trace(FILE *trace, struct parser *p,
                          struct token *tk, const struct state states[],
                          const char *non_term[], int knowns)
        {
@@ -2904,10 +2900,6 @@ end inside square brackets.
                        fputs(reserved_words[tk->num], trace);
                else
                        text_dump(trace, tk->txt, 20);
-               if (indents)
-                       fprintf(trace, ".%d", indents);
-               if (start_of_line)
-                       fputs("/", trace);
                fputs("]", trace);
        }