From db4c347e32c2820809fe133592a55770f0f63e66 Mon Sep 17 00:00:00 2001 From: Peter Johnson <peter@tortall.net> Date: Sat, 5 Oct 2002 09:10:58 +0000 Subject: [PATCH] Generate #line in the output in a useful way. Now it's much more usable for debugging, code coverage, etc. (with a little post-processing sed help). svn path=/trunk/yasm/; revision=744 --- tools/re2c/code.c | 64 +++++++++-------- tools/re2c/dfa.c | 4 +- tools/re2c/globals.h | 1 + tools/re2c/main.c | 1 + tools/re2c/parse.h | 1 + tools/re2c/re2c-parser.y | 32 +++++---- tools/re2c/scanner.c | 145 ++++++++++++++++++++++++--------------- tools/re2c/scanner.re | 3 +- tools/re2c/substr.c | 5 ++ 9 files changed, 156 insertions(+), 100 deletions(-) diff --git a/tools/re2c/code.c b/tools/re2c/code.c index 4dbda04a..bb52e8e4 100644 --- a/tools/re2c/code.c +++ b/tools/re2c/code.c @@ -144,11 +144,11 @@ void BitMap_gen(FILE *o, uint lb, uint ub){ doGen(b->go, b->on, bm-lb, m); } for(j = 0; j < n; ++j){ - if(j%8 == 0) fputs("\n\t", o); + if(j%8 == 0) {fputs("\n\t", o); oline++;} fprintf(o, "%3u, ", (uint) bm[j]); } } - fputs("\n\t};\n", o); + fputs("\n\t};\n", o); oline+=2; } } @@ -166,7 +166,7 @@ void BitMap_stats(void){ #endif static void genGoTo(FILE *o, State *to){ - fprintf(o, "\tgoto yy%u;\n", to->label); + fprintf(o, "\tgoto yy%u;\n", to->label); oline++; } static void genIf(FILE *o, const char *cmp, uint v){ @@ -181,11 +181,13 @@ static void indent(FILE *o, uint i){ } static void need(FILE *o, uint n){ - if(n == 1) - fputs("\tif(YYLIMIT == YYCURSOR) YYFILL(1);\n", o); - else + if(n == 1) { + fputs("\tif(YYLIMIT == YYCURSOR) YYFILL(1);\n", o); oline++; + } else { fprintf(o, "\tif((YYLIMIT - YYCURSOR) < %u) YYFILL(%u);\n", n, n); - fputs("\tyych = *YYCURSOR;\n", o); + oline++; + } + fputs("\tyych = *YYCURSOR;\n", o); oline++; } void @@ -198,29 +200,29 @@ Action_emit(Action *a, FILE *o) switch (a->type) { case MATCHACT: if(a->state->link){ - fputs("\t++YYCURSOR;\n", o); + fputs("\t++YYCURSOR;\n", o); oline++; need(o, a->state->depth); } else { - fputs("\tyych = *++YYCURSOR;\n", o); + fputs("\tyych = *++YYCURSOR;\n", o); oline++; } break; case ENTERACT: if(a->state->link){ fputs("\t++YYCURSOR;\n", o); - fprintf(o, "yy%u:\n", a->d.label); + fprintf(o, "yy%u:\n", a->d.label); oline+=2; need(o, a->state->depth); } else { fputs("\tyych = *++YYCURSOR;\n", o); - fprintf(o, "yy%u:\n", a->d.label); + fprintf(o, "yy%u:\n", a->d.label); oline+=2; } break; case SAVEMATCHACT: - fprintf(o, "\tyyaccept = %u;\n", a->d.selector); + fprintf(o, "\tyyaccept = %u;\n", a->d.selector); oline++; if(a->state->link){ - fputs("\tYYMARKER = ++YYCURSOR;\n", o); + fputs("\tYYMARKER = ++YYCURSOR;\n", o); oline++; need(o, a->state->depth); } else { - fputs("\tyych = *(YYMARKER = ++YYCURSOR);\n", o); + fputs("\tyych = *(YYMARKER = ++YYCURSOR);\n", o); oline++; } break; case MOVEACT: @@ -231,21 +233,24 @@ Action_emit(Action *a, FILE *o) if(first){ first = 0; fputs("\tYYCURSOR = YYMARKER;\n", o); - fputs("\tswitch(yyaccept){\n", o); + fputs("\tswitch(yyaccept){\n", o); oline+=2; } fprintf(o, "\tcase %u:", a->d.Accept.saves[i]); genGoTo(o, a->d.Accept.rules[i]); } - if(!first) - fputs("\t}\n", o); + if(!first) { + fputs("\t}\n", o); oline++; + } break; case RULEACT: back = RegExp_fixedLength(a->d.rule->d.RuleOp.ctx); if(back != ~0u && back > 0u) fprintf(o, "\tYYCURSOR -= %u;", back); - fprintf(o, "\n#line %u\n\t", a->d.rule->d.RuleOp.code->line); + fprintf(o, "\n"); oline++; + line_source(o, a->d.rule->d.RuleOp.code->line); SubStr_out(&a->d.rule->d.RuleOp.code->text, o); - fprintf(o, "\n"); + fprintf(o, "\n"); oline++; + fprintf(o, "#line %u \"-\"\n", ++oline); break; } } @@ -301,7 +306,7 @@ static void genCases(FILE *o, uint lb, Span *s){ fputs("\tcase '", o); prtCh(o, lb); fputs("':", o); if(++lb == s->ub) break; - fputs("\n", o); + fputs("\n", o); oline++; } } } @@ -320,7 +325,7 @@ Go_genSwitch(Go *g, FILE *o, State *next){ if(g->span[i].to != def) *(t++) = &g->span[i]; - fputs("\tswitch(yych){\n", o); + fputs("\tswitch(yych){\n", o); oline++; while(t != &sP[0]){ State *to; r = s = &sP[0]; @@ -340,7 +345,7 @@ Go_genSwitch(Go *g, FILE *o, State *next){ } fputs("\tdefault:", o); genGoTo(o, def); - fputs("\t}\n", o); + fputs("\t}\n", o); oline++; free(sP); } @@ -351,11 +356,11 @@ static void doBinary(FILE *o, uint i, Span *s, uint n, State *next){ doLinear(o, i, s, n, next); } else { uint h = n/2; - indent(o, i); genIf(o, "<=", s[h-1].ub - 1); fputs("{\n", o); + indent(o, i); genIf(o, "<=", s[h-1].ub - 1); fputs("{\n", o); oline++; doBinary(o, i+1, &s[0], h, next); - indent(o, i); fputs("\t} else {\n", o); + indent(o, i); fputs("\t} else {\n", o); oline++; doBinary(o, i+1, &s[h], n - h, next); - indent(o, i); fputs("\t}\n", o); + indent(o, i); fputs("\t}\n", o); oline++; } } @@ -697,12 +702,15 @@ void DFA_emit(DFA *d, FILE *o){ free(d->head->action); - fputs("{\n\tYYCTYPE yych;\n\tunsigned int yyaccept;\n", o); + oline++; + fprintf(o, "\n#line %u \"-\"\n", ++oline); + + fputs("{\n\tYYCTYPE yych;\n\tunsigned int yyaccept;\n", o); oline+=3; if(bFlag) BitMap_gen(o, d->lbChar, d->ubChar); - fprintf(o, "\tgoto yy%u;\n", label); + fprintf(o, "\tgoto yy%u;\n", label); oline++; Action_new_Enter(d->head, label++); for(s = d->head; s; s = s->next) @@ -712,7 +720,7 @@ void DFA_emit(DFA *d, FILE *o){ State_emit(s, o); Go_genGoto(&s->go, o, s->next); } - fputs("}\n", o); + fputs("}\n", o); oline++; BitMap_first = NULL; diff --git a/tools/re2c/dfa.c b/tools/re2c/dfa.c index 057ef661..e3e6d83c 100644 --- a/tools/re2c/dfa.c +++ b/tools/re2c/dfa.c @@ -59,7 +59,7 @@ State_out(FILE *o, const State *s){ fprintf(o, "state %u", s->label); if(s->rule) fprintf(o, " accepts %u", s->rule->d.RuleOp.accept); - fputs("\n", o); + fputs("\n", o); oline++; lb = 0; for(i = 0; i < s->go.nSpans; ++i) lb = Span_show(&s->go.span[i], o, lb); @@ -70,7 +70,7 @@ DFA_out(FILE *o, const DFA *dfa){ State *s; for(s = dfa->head; s; s = s->next) { State_out(o, s); - fputs("\n\n", o); + fputs("\n\n", o); oline+=2; } } diff --git a/tools/re2c/globals.h b/tools/re2c/globals.h index a9e5eb73..676a9fd1 100644 --- a/tools/re2c/globals.h +++ b/tools/re2c/globals.h @@ -6,6 +6,7 @@ extern const char *fileName; extern int sFlag; extern int bFlag; +extern unsigned int oline; extern uchar asc2ebc[256]; extern uchar ebc2asc[256]; diff --git a/tools/re2c/main.c b/tools/re2c/main.c index 4537bca6..5cfa2031 100644 --- a/tools/re2c/main.c +++ b/tools/re2c/main.c @@ -10,6 +10,7 @@ const char *fileName; int sFlag = 0; int bFlag = 0; +unsigned int oline = 1; int main(int argc, char *argv[]){ FILE *f; diff --git a/tools/re2c/parse.h b/tools/re2c/parse.h index ffeb4054..3588398e 100644 --- a/tools/re2c/parse.h +++ b/tools/re2c/parse.h @@ -15,6 +15,7 @@ void Symbol_init(Symbol *, const SubStr*); static inline Symbol *Symbol_new(const SubStr*); Symbol *Symbol_find(const SubStr*); +void line_source(FILE *, unsigned int); void parse(FILE *, FILE *); static inline Symbol * diff --git a/tools/re2c/re2c-parser.y b/tools/re2c/re2c-parser.y index d2d5406e..374d4f23 100644 --- a/tools/re2c/re2c-parser.y +++ b/tools/re2c/re2c-parser.y @@ -141,20 +141,12 @@ mystrdup(const char *str) return (copy); } -void parse(FILE *i, FILE *o){ +void line_source(FILE *o, unsigned int line) +{ char * fnamebuf; char * token; - time_t now; - time(&now); - - fputs("/* Generated by re2c 0.9.1-C on ", o); - fprintf(o, "%-24s", ctime(&now)); - fputs(" */\n", o); - - in = Scanner_new(i); - - fprintf(o, "#line %u \"", Scanner_line(in)); + fprintf(o, "#line %u \"", line); if( fileName != NULL ) { fnamebuf = mystrdup( fileName ); } else { @@ -167,13 +159,27 @@ void parse(FILE *i, FILE *o){ if( token == NULL ) break; fputs("\\\\", o); } - fputs("\"\n", o); + fputs("\"\n", o); oline++; free( fnamebuf ); +} + +void parse(FILE *i, FILE *o){ + time_t now; + + time(&now); + + fputs("/* Generated by re2c 0.9.1-C on ", o); + fprintf(o, "%-24s", ctime(&now)); + fputs(" */\n", o); oline+=2; + + in = Scanner_new(i); + + line_source(o, Scanner_line(in)); while(Scanner_echo(in, o)){ yyparse(); if(spec) genCode(o, spec); - fprintf(o, "#line %u\n", Scanner_line(in)); + line_source(o, Scanner_line(in)); } } diff --git a/tools/re2c/scanner.c b/tools/re2c/scanner.c index 4f520d6e..1467de3c 100644 --- a/tools/re2c/scanner.c +++ b/tools/re2c/scanner.c @@ -1,12 +1,13 @@ -/* Generated by re2c 0.9.1-C on Thu Aug 15 22:11:10 2002 +/* Generated by re2c 0.9.1-C on Sat Oct 5 02:09:57 2002 */ -#line 1 "tools/re2c/scanner.re" +#line 1 "scanner.re" #include <config.h> #include <stdlib.h> #include <string.h> #include "tools/re2c/scanner.h" #include "tools/re2c/parse.h" +#include "tools/re2c/globals.h" #include "re2c-parser.h" #define BSIZE 8192 @@ -65,7 +66,7 @@ fill(Scanner *s, uchar *cursor) return cursor; } -#line 74 +#line 75 "scanner.re" int @@ -74,6 +75,8 @@ Scanner_echo(Scanner *s, FILE *out) uchar *cursor = s->cur; s->tok = cursor; echo: + +#line 80 "scanner.c" { YYCTYPE yych; unsigned int yyaccept; @@ -88,15 +91,17 @@ yy0: yych = *(YYMARKER = ++YYCURSOR); if(yych == '*') goto yy7; yy3: -#line 90 - { goto echo; } +#line 91 "scanner.re" +{ goto echo; } +#line 97 "scanner.c" yy4: yych = *++YYCURSOR; -#line 86 - { if(cursor == s->eof) RETURN(0); +#line 87 "scanner.re" +{ if(cursor == s->eof) RETURN(0); fwrite(s->tok, 1, cursor - s->tok, out); - s->tok = s->pos = cursor; s->cline++; + s->tok = s->pos = cursor; s->cline++; oline++; goto echo; } +#line 105 "scanner.c" yy6: yych = *++YYCURSOR; goto yy3; yy7: yych = *++YYCURSOR; @@ -115,12 +120,13 @@ yy9: yych = *++YYCURSOR; if(yych != 'c') goto yy8; yych = *++YYCURSOR; -#line 83 - { fwrite(s->tok, 1, &cursor[-7] - s->tok, out); +#line 84 "scanner.re" +{ fwrite(s->tok, 1, &cursor[-7] - s->tok, out); s->tok = cursor; RETURN(1); } +#line 128 "scanner.c" } -#line 91 +#line 92 "scanner.re" } @@ -135,6 +141,8 @@ scan: s->tchar = cursor - s->pos; s->tline = s->cline; s->tok = cursor; + +#line 146 "scanner.c" { YYCTYPE yych; unsigned int yyaccept; @@ -190,33 +198,38 @@ yy15: } yy17: yych = *++YYCURSOR; -#line 106 - { depth = 1; +#line 107 "scanner.re" +{ depth = 1; goto code; } +#line 206 "scanner.c" yy19: yych = *++YYCURSOR; if(yych == '*') goto yy54; yy20: -#line 125 - { RETURN(*s->tok); } +#line 126 "scanner.re" +{ RETURN(*s->tok); } +#line 212 "scanner.c" yy21: yych = *++YYCURSOR; if(yych == '/') goto yy52; yy22: -#line 127 - { yylval.op = *s->tok; +#line 128 "scanner.re" +{ yylval.op = *s->tok; RETURN(CLOSE); } +#line 219 "scanner.c" yy23: yyaccept = 0; yych = *(YYMARKER = ++YYCURSOR); if(yych != '\n') goto yy48; yy24: -#line 118 - { Scanner_fatal(s, "bad string"); } +#line 119 "scanner.re" +{ Scanner_fatal(s, "bad string"); } +#line 226 "scanner.c" yy25: yyaccept = 1; yych = *(YYMARKER = ++YYCURSOR); if(yych != '\n') goto yy42; yy26: -#line 123 - { Scanner_fatal(s, "bad character constant"); } +#line 124 "scanner.re" +{ Scanner_fatal(s, "bad character constant"); } +#line 233 "scanner.c" yy27: yych = *++YYCURSOR; goto yy20; yy28: yych = *++YYCURSOR; @@ -224,30 +237,34 @@ yy28: yych = *++YYCURSOR; yy29: yych = *++YYCURSOR; goto yy40; yy30: -#line 130 - { SubStr substr; +#line 131 "scanner.re" +{ SubStr substr; s->cur = cursor; substr = Scanner_token(s); yylval.symbol = Symbol_find(&substr); return ID; } +#line 247 "scanner.c" yy31: yych = *++YYCURSOR; goto yy38; yy32: -#line 136 - { goto scan; } +#line 137 "scanner.re" +{ goto scan; } +#line 253 "scanner.c" yy33: yych = *++YYCURSOR; -#line 138 - { if(cursor == s->eof) RETURN(0); +#line 139 "scanner.re" +{ if(cursor == s->eof) RETURN(0); s->pos = cursor; s->cline++; goto scan; } +#line 261 "scanner.c" yy35: yych = *++YYCURSOR; -#line 143 - { fprintf(stderr, "unexpected character: '%c'\n", *s->tok); +#line 144 "scanner.re" +{ fprintf(stderr, "unexpected character: '%c'\n", *s->tok); goto scan; } +#line 268 "scanner.c" yy37: ++YYCURSOR; if(YYLIMIT == YYCURSOR) YYFILL(1); yych = *YYCURSOR; @@ -289,10 +306,11 @@ yy44: ++YYCURSOR; goto yy41; yy45: yych = *++YYCURSOR; -#line 120 - { s->cur = cursor; +#line 121 "scanner.re" +{ s->cur = cursor; yylval.regexp = ranToRE(Scanner_token(s)); return RANGE; } +#line 314 "scanner.c" yy47: ++YYCURSOR; if(YYLIMIT == YYCURSOR) YYFILL(1); yych = *YYCURSOR; @@ -310,25 +328,30 @@ yy48: if(yych <= '!'){ goto yy47; yy50: yych = *++YYCURSOR; -#line 115 - { s->cur = cursor; +#line 116 "scanner.re" +{ s->cur = cursor; yylval.regexp = strToRE(Scanner_token(s)); return STRING; } +#line 336 "scanner.c" yy52: yych = *++YYCURSOR; -#line 112 - { s->tok = cursor; +#line 113 "scanner.re" +{ s->tok = cursor; RETURN(0); } +#line 342 "scanner.c" yy54: yych = *++YYCURSOR; -#line 109 - { depth = 1; +#line 110 "scanner.re" +{ depth = 1; goto comment; } +#line 348 "scanner.c" } -#line 146 +#line 147 "scanner.re" code: + +#line 355 "scanner.c" { YYCTYPE yych; unsigned int yyaccept; @@ -356,29 +379,33 @@ yy56: } yych = *++YYCURSOR; -#line 150 - { if(--depth == 0){ +#line 151 "scanner.re" +{ if(--depth == 0){ s->cur = cursor; yylval.token = Token_new(Scanner_token(s), s->tline); return CODE; } goto code; } +#line 390 "scanner.c" yy60: yych = *++YYCURSOR; -#line 156 - { ++depth; +#line 157 "scanner.re" +{ ++depth; goto code; } +#line 396 "scanner.c" yy62: yych = *++YYCURSOR; -#line 158 - { if(cursor == s->eof) Scanner_fatal(s, "missing '}'"); +#line 159 "scanner.re" +{ if(cursor == s->eof) Scanner_fatal(s, "missing '}'"); s->pos = cursor; s->cline++; goto code; } +#line 404 "scanner.c" yy64: yych = *++YYCURSOR; yy65: -#line 162 - { goto code; } +#line 163 "scanner.re" +{ goto code; } +#line 409 "scanner.c" yy66: yyaccept = 0; yych = *(YYMARKER = ++YYCURSOR); if(yych == '\n') goto yy65; @@ -422,10 +449,12 @@ yy73: if(yych <= '!'){ if(yych == '\n') goto yy70; goto yy72; } -#line 163 +#line 164 "scanner.re" comment: + +#line 458 "scanner.c" { YYCTYPE yych; unsigned int yyaccept; @@ -445,34 +474,38 @@ yy75: yy77: yych = *++YYCURSOR; if(yych == '/') goto yy85; yy78: -#line 177 - { goto comment; } +#line 178 "scanner.re" +{ goto comment; } +#line 480 "scanner.c" yy79: yych = *++YYCURSOR; if(yych == '*') goto yy83; goto yy78; yy80: yych = *++YYCURSOR; -#line 173 - { if(cursor == s->eof) RETURN(0); +#line 174 "scanner.re" +{ if(cursor == s->eof) RETURN(0); s->tok = s->pos = cursor; s->cline++; goto comment; } +#line 491 "scanner.c" yy82: yych = *++YYCURSOR; goto yy78; yy83: yych = *++YYCURSOR; -#line 171 - { ++depth; +#line 172 "scanner.re" +{ ++depth; goto comment; } +#line 499 "scanner.c" yy85: yych = *++YYCURSOR; -#line 167 - { if(--depth == 0) +#line 168 "scanner.re" +{ if(--depth == 0) goto scan; else goto comment; } +#line 507 "scanner.c" } -#line 178 +#line 179 "scanner.re" } diff --git a/tools/re2c/scanner.re b/tools/re2c/scanner.re index 4b94ed47..5897271d 100644 --- a/tools/re2c/scanner.re +++ b/tools/re2c/scanner.re @@ -4,6 +4,7 @@ #include <string.h> #include "tools/re2c/scanner.h" #include "tools/re2c/parse.h" +#include "tools/re2c/globals.h" #include "re2c-parser.h" #define BSIZE 8192 @@ -85,7 +86,7 @@ echo: RETURN(1); } "\n" { if(cursor == s->eof) RETURN(0); fwrite(s->tok, 1, cursor - s->tok, out); - s->tok = s->pos = cursor; s->cline++; + s->tok = s->pos = cursor; s->cline++; oline++; goto echo; } any { goto echo; } */ diff --git a/tools/re2c/substr.c b/tools/re2c/substr.c index 3be3c875..57558b2c 100644 --- a/tools/re2c/substr.c +++ b/tools/re2c/substr.c @@ -2,11 +2,16 @@ #include <string.h> #include "tools/re2c/substr.h" +#include "tools/re2c/globals.h" void SubStr_out(const SubStr *s, FILE *o) { + int i; fwrite(s->str, s->len, 1, o); + for (i=0; i<s->len; i++) + if (s->str[i] == '\n') + oline++; } int -- 2.40.0