From: NeilBrown Date: Thu, 2 Oct 2014 10:45:32 +0000 (+1000) Subject: Newline handling stuff X-Git-Tag: workingparser~23 X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=70fce43c4b744eccfab0a769c35b71a8a3c38773 Newline handling stuff Signed-off-by: NeilBrown --- diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index 2aa296d..497b3cb 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -2455,7 +2455,7 @@ before the beginning. short sym; short starts_indented; short indents; - short starts_newline; + short since_newline; } *stack; void **asn_stack; int stack_size; @@ -2524,6 +2524,12 @@ So we walk down: next->newline_permitted = 0; if (states[newstate].starts_line) next->newline_permitted = 1; + if (next->since_newline) { + if (p->tos) + next->since_newline = p->stack[p->tos-1].since_newline + 1; + else + next->since_newline = 1; + } p->stack[p->tos] = *next; p->asn_stack[p->tos] = asn; p->tos++; @@ -2547,8 +2553,8 @@ removed. It is called _after_ we reduce a production, just before we p->tos -= num; next->starts_indented = p->stack[p->tos].starts_indented; - next->starts_newline = - p->stack[p->tos].starts_newline; + next->since_newline = + p->stack[p->tos].since_newline; next->indents = 0; for (i = 0; i < num; i++) { next->indents += p->stack[p->tos+i].indents; @@ -2630,7 +2636,6 @@ since the last state which could have been at the start of a line. int accepted = 0; void *ret = NULL; - next.starts_newline = 1; shift(&p, &next, NULL, states); while (!accepted) { struct token *err_tk; @@ -2643,7 +2648,7 @@ since the last state which could have been at the start of a line. if (next.sym == TK_in) { next.starts_indented = 1; next.indents = 1; - next.starts_newline = 1; + next.since_newline = 0; free(tk); tk = NULL; parser_trace_action(trace, "Record"); @@ -2676,16 +2681,19 @@ since the last state which could have been at the start of a line. parser_trace_action(trace, "Discard"); continue; } + if (states[tos->state].reduce_size > 0 && + states[tos->state].reduce_size < tos->since_newline) + goto force_reduce; } if (shift(&p, &next, tk, states)) { - next.starts_newline = - tk->num == TK_newline; + next.since_newline = !(tk->num == TK_newline); next.starts_indented = 0; next.indents = 0; tk = NULL; parser_trace_action(trace, "Shift"); continue; } + force_reduce: if (states[tos->state].reduce_prod >= 0) { void **body; void *res; @@ -2706,7 +2714,7 @@ since the last state which could have been at the start of a line. else { frame.indents = next.indents; frame.starts_indented = frame.indents; - frame.starts_newline = 0; + frame.since_newline = 1; next.indents = 0; next.starts_indented = 0; } @@ -2726,6 +2734,7 @@ since the last state which could have been at the start of a line. struct frame frame = { 0 }; fprintf(stderr, "Synthesize %d to handle indent problem\n", states[tos->state].shift_sym); frame.sym = states[tos->state].shift_sym; + frame.since_newline = 1; shift(&p, &frame, tok_copy(*tk), states); // FIXME need to report this error somehow parser_trace_action(trace, "Synthesize"); @@ -2842,7 +2851,7 @@ end inside square brackets. if (f->indents) fprintf(trace, "%c%d", f->starts_indented?':':'.', f->indents); - if (f->starts_newline) + if (f->since_newline == 0) fputs("/", trace); fputs(" ", trace); } @@ -2857,7 +2866,7 @@ end inside square brackets. if (n->indents) fprintf(trace, "%c%d", n->starts_indented?':':'.', n->indents); - if (n->starts_newline) + if (n->since_newline == 0) fputs("/", trace); fputs("]", trace); }