From f6dc93c88d0f6cd3132cbc66db68518e0a68b636 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Mon, 19 Nov 2018 23:22:33 +0000 Subject: [PATCH] Dump header on stdout if filename is not set, but /*!header:re2c:on*/ is used. --- re2c/bootstrap/src/ast/lex.cc | 336 +++++++++++++++++----------------- re2c/src/ast/lex.re | 1 + re2c/src/code/output.cc | 58 +++--- re2c/src/code/output.h | 4 +- re2c/test/header/header_03.c | 28 +++ re2c/test/header/header_03.re | 16 ++ 6 files changed, 241 insertions(+), 202 deletions(-) create mode 100644 re2c/test/header/header_03.c create mode 100644 re2c/test/header/header_03.re diff --git a/re2c/bootstrap/src/ast/lex.cc b/re2c/bootstrap/src/ast/lex.cc index 82e586b2..f8095737 100644 --- a/re2c/bootstrap/src/ast/lex.cc +++ b/re2c/bootstrap/src/ast/lex.cc @@ -1,4 +1,3 @@ -/* Generated by re2c 1.1.1 on Sun Nov 18 12:18:36 2018 */ #line 1 "../src/ast/lex.re" #include "src/util/c99_stdint.h" #include @@ -47,7 +46,7 @@ echo: ptr = cur; -#line 51 "src/ast/lex.cc" +#line 50 "src/ast/lex.cc" { YYCTYPE yych; unsigned int yyaccept = 0; @@ -105,19 +104,19 @@ echo: } } ++YYCURSOR; -#line 148 "../src/ast/lex.re" +#line 149 "../src/ast/lex.re" { if (cur != eof) goto echo; out.wraw(tok, ptr); return Stop; } -#line 115 "src/ast/lex.cc" +#line 114 "src/ast/lex.cc" yy4: ++YYCURSOR; yy5: -#line 166 "../src/ast/lex.re" +#line 167 "../src/ast/lex.re" { goto echo; } -#line 121 "src/ast/lex.cc" +#line 120 "src/ast/lex.cc" yy6: yyaccept = 0; yych = (YYCTYPE)*(YYMARKER = ++YYCURSOR); @@ -126,13 +125,13 @@ yy6: } if (yych == '#') goto yy14; yy7: -#line 160 "../src/ast/lex.re" +#line 161 "../src/ast/lex.re" { cline++; pos = cur; goto echo; } -#line 136 "src/ast/lex.cc" +#line 135 "src/ast/lex.cc" yy8: yych = (YYCTYPE)*++YYCURSOR; if (yych == '\n') goto yy6; @@ -180,7 +179,7 @@ yy16: out.wraw(tok, ptr); return Parse; } -#line 184 "src/ast/lex.cc" +#line 183 "src/ast/lex.cc" yy18: yych = (YYCTYPE)*++YYCURSOR; if (yych == '!') goto yy20; @@ -459,13 +458,13 @@ yy76: yy78: ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 154 "../src/ast/lex.re" +#line 155 "../src/ast/lex.re" { out.wraw(tok, ptr + 1); set_sourceline(); goto echo; } -#line 469 "src/ast/lex.cc" +#line 468 "src/ast/lex.cc" yy80: yych = (YYCTYPE)*++YYCURSOR; if (yych == '\n') goto yy78; @@ -593,7 +592,7 @@ yy108: lex_end_of_comment(out); goto echo; } -#line 597 "src/ast/lex.cc" +#line 596 "src/ast/lex.cc" yy110: yych = (YYCTYPE)*++YYCURSOR; if (yych == 'h') goto yy120; @@ -621,7 +620,7 @@ yy115: out.wraw(tok, ptr); return Reuse; } -#line 625 "src/ast/lex.cc" +#line 624 "src/ast/lex.cc" yy117: yych = (YYCTYPE)*++YYCURSOR; if (yych == 'r') goto yy125; @@ -678,7 +677,7 @@ yy129: lex_tags(out, true); goto echo; } -#line 682 "src/ast/lex.cc" +#line 681 "src/ast/lex.cc" yy131: ++YYCURSOR; #line 75 "../src/ast/lex.re" @@ -686,7 +685,7 @@ yy131: out.wraw(tok, ptr); return Rules; } -#line 690 "src/ast/lex.cc" +#line 689 "src/ast/lex.cc" yy133: ++YYCURSOR; #line 121 "../src/ast/lex.re" @@ -695,7 +694,7 @@ yy133: lex_tags(out, false); goto echo; } -#line 699 "src/ast/lex.cc" +#line 698 "src/ast/lex.cc" yy135: ++YYCURSOR; #line 112 "../src/ast/lex.re" @@ -707,7 +706,7 @@ yy135: lex_end_of_comment(out); goto echo; } -#line 711 "src/ast/lex.cc" +#line 710 "src/ast/lex.cc" yy137: yych = (YYCTYPE)*++YYCURSOR; if (yych == '2') goto yy142; @@ -724,7 +723,7 @@ yy139: lex_end_of_comment(out); goto echo; } -#line 728 "src/ast/lex.cc" +#line 727 "src/ast/lex.cc" yy141: yych = (YYCTYPE)*++YYCURSOR; if (yych == 'e') goto yy144; @@ -750,7 +749,7 @@ yy145: lex_end_of_comment(out); goto echo; } -#line 754 "src/ast/lex.cc" +#line 753 "src/ast/lex.cc" yy147: yych = (YYCTYPE)*++YYCURSOR; if (yych == 'f') goto yy149; @@ -770,6 +769,7 @@ yy150: { out.wraw(tok, ptr); out.header_mode(true); + out.need_header = true; lex_end_of_comment(out); goto echo; } @@ -786,7 +786,7 @@ yy152: #line 787 "src/ast/lex.cc" yy154: ++YYCURSOR; -#line 140 "../src/ast/lex.re" +#line 141 "../src/ast/lex.re" { out.wraw(tok, ptr); out.header_mode(false); @@ -796,7 +796,7 @@ yy154: } #line 798 "src/ast/lex.cc" } -#line 167 "../src/ast/lex.re" +#line 168 "../src/ast/lex.re" } @@ -820,18 +820,18 @@ void Scanner::lex_end_of_comment(OutputFile &out) } yy158: ++YYCURSOR; -#line 174 "../src/ast/lex.re" +#line 175 "../src/ast/lex.re" { fatal_lc(get_cline(), get_column(), "expected end of block"); } #line 826 "src/ast/lex.cc" yy160: ++YYCURSOR; yy161: -#line 176 "../src/ast/lex.re" +#line 177 "../src/ast/lex.re" { continue; } #line 832 "src/ast/lex.cc" yy162: ++YYCURSOR; -#line 177 "../src/ast/lex.re" +#line 178 "../src/ast/lex.re" { ++ignored; continue; } #line 837 "src/ast/lex.cc" yy164: @@ -842,7 +842,7 @@ yy165: yych = (YYCTYPE)*++YYCURSOR; if (yych != '/') goto yy161; ++YYCURSOR; -#line 178 "../src/ast/lex.re" +#line 179 "../src/ast/lex.re" { if (ignored > 0) { cline += ignored; @@ -853,7 +853,7 @@ yy165: } #line 855 "src/ast/lex.cc" } -#line 186 "../src/ast/lex.re" +#line 187 "../src/ast/lex.re" } } @@ -919,7 +919,7 @@ void Scanner::lex_tags(OutputFile &out, bool mtags) } ++YYCURSOR; yy171: -#line 193 "../src/ast/lex.re" +#line 194 "../src/ast/lex.re" { fatal_lc(get_cline(), get_column(), "unrecognized configuration"); } #line 925 "src/ast/lex.cc" yy172: @@ -929,12 +929,12 @@ yy172: if (yybm[0+yych] & 128) { goto yy172; } -#line 198 "../src/ast/lex.re" +#line 199 "../src/ast/lex.re" { continue; } #line 935 "src/ast/lex.cc" yy175: ++YYCURSOR; -#line 199 "../src/ast/lex.re" +#line 200 "../src/ast/lex.re" { ++cline; continue; } #line 940 "src/ast/lex.cc" yy177: @@ -955,7 +955,7 @@ yy180: goto yy171; yy181: ++YYCURSOR; -#line 200 "../src/ast/lex.re" +#line 201 "../src/ast/lex.re" { out.wdelay_tags(new ConfTags(fmt, sep), mtags); tok = pos = cur; @@ -998,7 +998,7 @@ yy191: goto yy184; yy192: ++YYCURSOR; -#line 195 "../src/ast/lex.re" +#line 196 "../src/ast/lex.re" { fmt = lex_conf_string(); continue; } #line 1004 "src/ast/lex.cc" yy194: @@ -1009,11 +1009,11 @@ yy194: yych = (YYCTYPE)*++YYCURSOR; if (yych != 'r') goto yy184; ++YYCURSOR; -#line 196 "../src/ast/lex.re" +#line 197 "../src/ast/lex.re" { sep = lex_conf_string(); continue; } #line 1015 "src/ast/lex.cc" } -#line 205 "../src/ast/lex.re" +#line 206 "../src/ast/lex.re" } } @@ -1132,7 +1132,7 @@ scan: yy201: ++YYCURSOR; yy202: -#line 329 "../src/ast/lex.re" +#line 330 "../src/ast/lex.re" { fatal_lc(get_cline(), get_column(), "unexpected character: '%c'", *tok); goto scan; @@ -1145,7 +1145,7 @@ yy203: if (yybm[0+yych] & 16) { goto yy203; } -#line 310 "../src/ast/lex.re" +#line 311 "../src/ast/lex.re" { goto scan; } #line 1151 "src/ast/lex.cc" yy206: @@ -1158,7 +1158,7 @@ yy206: if (yych == '#') goto yy236; } yy207: -#line 317 "../src/ast/lex.re" +#line 318 "../src/ast/lex.re" { pos = cur; cline++; @@ -1177,7 +1177,7 @@ yy208: goto yy202; yy209: ++YYCURSOR; -#line 227 "../src/ast/lex.re" +#line 228 "../src/ast/lex.re" { yylval.regexp = lex_str('"'); return TOKEN_REGEXP; } #line 1183 "src/ast/lex.cc" yy211: @@ -1197,13 +1197,13 @@ yy212: goto yy202; yy213: ++YYCURSOR; -#line 226 "../src/ast/lex.re" +#line 227 "../src/ast/lex.re" { yylval.regexp = lex_str('\''); return TOKEN_REGEXP; } #line 1203 "src/ast/lex.cc" yy215: ++YYCURSOR; yy216: -#line 237 "../src/ast/lex.re" +#line 238 "../src/ast/lex.re" { return *tok; } #line 1209 "src/ast/lex.cc" yy217: @@ -1212,7 +1212,7 @@ yy217: goto yy216; yy218: ++YYCURSOR; -#line 305 "../src/ast/lex.re" +#line 306 "../src/ast/lex.re" { yylval.regexp = ast_dot(cline, get_column()); return TOKEN_REGEXP; @@ -1229,7 +1229,7 @@ yy221: goto yy202; yy222: ++YYCURSOR; -#line 219 "../src/ast/lex.re" +#line 220 "../src/ast/lex.re" { return lex_clist(); } #line 1235 "src/ast/lex.cc" yy224: @@ -1244,7 +1244,7 @@ yy226: if (yybm[0+yych] & 32) { goto yy225; } -#line 282 "../src/ast/lex.re" +#line 283 "../src/ast/lex.re" { if (!globopts->FFlag || lex_namedef_context_re2c()) { yylval.str = new std::string (tok, tok_len()); @@ -1271,7 +1271,7 @@ yy226: yy228: yych = (YYCTYPE)*++YYCURSOR; if (yych == '^') goto yy251; -#line 228 "../src/ast/lex.re" +#line 229 "../src/ast/lex.re" { yylval.regexp = lex_cls(false); return TOKEN_REGEXP; } #line 1277 "src/ast/lex.cc" yy230: @@ -1296,7 +1296,7 @@ yy231: } } yy232: -#line 214 "../src/ast/lex.re" +#line 215 "../src/ast/lex.re" { lex_code_in_braces(); return TOKEN_CODE; } #line 1302 "src/ast/lex.cc" yy233: @@ -1349,7 +1349,7 @@ yy238: } } yy240: -#line 231 "../src/ast/lex.re" +#line 232 "../src/ast/lex.re" { const std::string *name = new std::string(tok + 1, tok_len() - 1); yylval.regexp = ast_tag(cline, get_column(), name, tok[0] == '#'); @@ -1358,33 +1358,33 @@ yy240: #line 1359 "src/ast/lex.cc" yy241: ++YYCURSOR; -#line 224 "../src/ast/lex.re" +#line 225 "../src/ast/lex.re" { tok = cur; return 0; } #line 1364 "src/ast/lex.cc" yy243: ++YYCURSOR; -#line 222 "../src/ast/lex.re" +#line 223 "../src/ast/lex.re" { lex_c_comment(); goto scan; } #line 1369 "src/ast/lex.cc" yy245: ++YYCURSOR; -#line 221 "../src/ast/lex.re" +#line 222 "../src/ast/lex.re" { lex_cpp_comment(); goto scan; } #line 1374 "src/ast/lex.cc" yy247: yych = (YYCTYPE)*++YYCURSOR; if (yych == '>') goto yy261; -#line 215 "../src/ast/lex.re" +#line 216 "../src/ast/lex.re" { lex_code_indented(); return TOKEN_CODE; } #line 1380 "src/ast/lex.cc" yy249: ++YYCURSOR; -#line 217 "../src/ast/lex.re" +#line 218 "../src/ast/lex.re" { lex_cgoto(); return TOKEN_CNEXT; } #line 1385 "src/ast/lex.cc" yy251: ++YYCURSOR; -#line 229 "../src/ast/lex.re" +#line 230 "../src/ast/lex.re" { yylval.regexp = lex_cls(true); return TOKEN_REGEXP; } #line 1390 "src/ast/lex.cc" yy253: @@ -1394,7 +1394,7 @@ yy253: yy254: ++YYCURSOR; yy255: -#line 266 "../src/ast/lex.re" +#line 267 "../src/ast/lex.re" { fatal_lc(get_cline(), get_column(), "illegal closure form, use '{n}', '{n,}', '{n,m}' where n and m are numbers"); @@ -1438,7 +1438,7 @@ yy260: goto yy235; yy261: ++YYCURSOR; -#line 218 "../src/ast/lex.re" +#line 219 "../src/ast/lex.re" { lex_cgoto(); return TOKEN_CJUMP; } #line 1444 "src/ast/lex.cc" yy263: @@ -1454,7 +1454,7 @@ yy264: goto yy255; yy265: ++YYCURSOR; -#line 239 "../src/ast/lex.re" +#line 240 "../src/ast/lex.re" { if (!s_to_u32_unsafe (tok + 1, cur - 1, yylval.bounds.min)) { fatal_lc(get_cline(), get_column(), "repetition count overflow"); @@ -1465,7 +1465,7 @@ yy265: #line 1466 "src/ast/lex.cc" yy267: ++YYCURSOR; -#line 271 "../src/ast/lex.re" +#line 272 "../src/ast/lex.re" { if (!globopts->FFlag) { fatal_lc(get_cline(), get_column(), @@ -1493,7 +1493,7 @@ yy271: goto yy235; yy273: ++YYCURSOR; -#line 258 "../src/ast/lex.re" +#line 259 "../src/ast/lex.re" { if (!s_to_u32_unsafe (tok + 1, cur - 2, yylval.bounds.min)) { fatal_lc(get_cline(), get_column(), "repetition lower bound overflow"); @@ -1508,12 +1508,12 @@ yy275: goto yy235; yy276: ++YYCURSOR; -#line 280 "../src/ast/lex.re" +#line 281 "../src/ast/lex.re" { return TOKEN_CONF; } #line 1514 "src/ast/lex.cc" yy278: ++YYCURSOR; -#line 247 "../src/ast/lex.re" +#line 248 "../src/ast/lex.re" { const char * p = strchr (tok, ','); if (!s_to_u32_unsafe (tok + 1, p, yylval.bounds.min)) { @@ -1580,7 +1580,7 @@ yy285: yy287: ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 312 "../src/ast/lex.re" +#line 313 "../src/ast/lex.re" { set_sourceline (); return TOKEN_LINE_INFO; @@ -1611,7 +1611,7 @@ yy293: if (yych == '\n') goto yy235; goto yy290; } -#line 333 "../src/ast/lex.re" +#line 334 "../src/ast/lex.re" } @@ -1673,7 +1673,7 @@ bool Scanner::lex_namedef_context_re2c() } } yy296: -#line 340 "../src/ast/lex.re" +#line 341 "../src/ast/lex.re" { return false; } #line 1679 "src/ast/lex.cc" yy297: @@ -1692,11 +1692,11 @@ yy300: if (yych == '>') goto yy299; ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 339 "../src/ast/lex.re" +#line 340 "../src/ast/lex.re" { return true; } #line 1698 "src/ast/lex.cc" } -#line 341 "../src/ast/lex.re" +#line 342 "../src/ast/lex.re" } @@ -1750,7 +1750,7 @@ bool Scanner::lex_namedef_context_flex() YYCTXMARKER = YYCURSOR; goto yy306; } -#line 349 "../src/ast/lex.re" +#line 350 "../src/ast/lex.re" { return false; } #line 1756 "src/ast/lex.cc" yy306: @@ -1767,17 +1767,17 @@ yy306: if (yych == '{') goto yy309; } YYCURSOR = YYCTXMARKER; -#line 348 "../src/ast/lex.re" +#line 349 "../src/ast/lex.re" { return true; } #line 1773 "src/ast/lex.cc" yy309: ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 347 "../src/ast/lex.re" +#line 348 "../src/ast/lex.re" { return false; } #line 1779 "src/ast/lex.cc" } -#line 350 "../src/ast/lex.re" +#line 351 "../src/ast/lex.re" } @@ -1836,7 +1836,7 @@ yy311: if (yych <= '!') goto yy315; if (yych == '>') goto yy318; yy314: -#line 360 "../src/ast/lex.re" +#line 361 "../src/ast/lex.re" { goto cond; } #line 1842 "src/ast/lex.cc" yy315: @@ -1845,16 +1845,16 @@ yy315: yych = (YYCTYPE)*YYCURSOR; if (yych == '\t') goto yy315; if (yych == ' ') goto yy315; -#line 358 "../src/ast/lex.re" +#line 359 "../src/ast/lex.re" { kind = TOKEN_CSETUP; goto cond; } #line 1851 "src/ast/lex.cc" yy318: ++YYCURSOR; -#line 359 "../src/ast/lex.re" +#line 360 "../src/ast/lex.re" { kind = TOKEN_CZERO; goto end; } #line 1856 "src/ast/lex.cc" } -#line 361 "../src/ast/lex.re" +#line 362 "../src/ast/lex.re" cond: tok = cur; @@ -1911,12 +1911,12 @@ cond: } yy322: ++YYCURSOR; -#line 367 "../src/ast/lex.re" +#line 368 "../src/ast/lex.re" { goto error; } #line 1917 "src/ast/lex.cc" yy324: ++YYCURSOR; -#line 366 "../src/ast/lex.re" +#line 367 "../src/ast/lex.re" { if (!cl->empty()) goto error; cl->insert("*"); goto next; } #line 1922 "src/ast/lex.cc" yy326: @@ -1926,11 +1926,11 @@ yy326: if (yybm[0+yych] & 128) { goto yy326; } -#line 365 "../src/ast/lex.re" +#line 366 "../src/ast/lex.re" { cl->insert(std::string(tok, tok_len())); goto next; } #line 1932 "src/ast/lex.cc" } -#line 368 "../src/ast/lex.re" +#line 369 "../src/ast/lex.re" next: @@ -1985,7 +1985,7 @@ next: } ++YYCURSOR; yy332: -#line 373 "../src/ast/lex.re" +#line 374 "../src/ast/lex.re" { goto error; } #line 1991 "src/ast/lex.cc" yy333: @@ -2009,12 +2009,12 @@ yy334: if (yybm[0+yych] & 128) { goto yy334; } -#line 371 "../src/ast/lex.re" +#line 372 "../src/ast/lex.re" { goto cond; } #line 2015 "src/ast/lex.cc" yy337: ++YYCURSOR; -#line 372 "../src/ast/lex.re" +#line 373 "../src/ast/lex.re" { goto end; } #line 2020 "src/ast/lex.cc" yy339: @@ -2034,7 +2034,7 @@ yy339: YYCURSOR = YYMARKER; goto yy332; } -#line 374 "../src/ast/lex.re" +#line 375 "../src/ast/lex.re" end: yylval.clist = cl; @@ -2093,11 +2093,11 @@ yy342: if (yybm[0+yych] & 128) { goto yy343; } -#line 386 "../src/ast/lex.re" +#line 387 "../src/ast/lex.re" { goto name; } #line 2099 "src/ast/lex.cc" } -#line 387 "../src/ast/lex.re" +#line 388 "../src/ast/lex.re" name: tok = cur; @@ -2150,7 +2150,7 @@ name: } yy348: ++YYCURSOR; -#line 392 "../src/ast/lex.re" +#line 393 "../src/ast/lex.re" { fatal_l(get_cline(), "syntax error in condition goto"); } #line 2156 "src/ast/lex.cc" yy350: @@ -2160,11 +2160,11 @@ yy350: if (yybm[0+yych] & 128) { goto yy350; } -#line 391 "../src/ast/lex.re" +#line 392 "../src/ast/lex.re" { yylval.str = new std::string (tok, tok_len ()); return; } #line 2166 "src/ast/lex.cc" } -#line 393 "../src/ast/lex.re" +#line 394 "../src/ast/lex.re" } @@ -2202,13 +2202,13 @@ code: } yy355: ++YYCURSOR; -#line 410 "../src/ast/lex.re" +#line 411 "../src/ast/lex.re" { fail_if_eof(); goto code; } #line 2208 "src/ast/lex.cc" yy357: ++YYCURSOR; yy358: -#line 415 "../src/ast/lex.re" +#line 416 "../src/ast/lex.re" { goto code; } #line 2214 "src/ast/lex.cc" yy359: @@ -2221,7 +2221,7 @@ yy359: if (yych == ' ') goto yy367; } yy360: -#line 403 "../src/ast/lex.re" +#line 404 "../src/ast/lex.re" { while (isspace(tok[0])) ++tok; while (cur > tok && isspace(cur[-1])) --cur; @@ -2235,7 +2235,7 @@ yy361: goto yy358; yy362: ++YYCURSOR; -#line 414 "../src/ast/lex.re" +#line 415 "../src/ast/lex.re" { lex_string(cur[-1]); goto code; } #line 2241 "src/ast/lex.cc" yy364: @@ -2245,27 +2245,27 @@ yy364: goto yy358; yy365: ++YYCURSOR; -#line 411 "../src/ast/lex.re" +#line 412 "../src/ast/lex.re" { fatal_l(get_cline(), "Curly braces are not allowed after ':='"); } #line 2251 "src/ast/lex.cc" yy367: ++YYCURSOR; YYCURSOR -= 1; -#line 402 "../src/ast/lex.re" +#line 403 "../src/ast/lex.re" { goto code; } #line 2257 "src/ast/lex.cc" yy369: ++YYCURSOR; -#line 412 "../src/ast/lex.re" +#line 413 "../src/ast/lex.re" { lex_c_comment(); goto code; } #line 2262 "src/ast/lex.cc" yy371: ++YYCURSOR; -#line 413 "../src/ast/lex.re" +#line 414 "../src/ast/lex.re" { lex_cpp_comment(); goto code; } #line 2267 "src/ast/lex.cc" } -#line 416 "../src/ast/lex.re" +#line 417 "../src/ast/lex.re" } @@ -2337,13 +2337,13 @@ code: } yy375: ++YYCURSOR; -#line 436 "../src/ast/lex.re" +#line 437 "../src/ast/lex.re" { fail_if_eof(); goto code; } #line 2343 "src/ast/lex.cc" yy377: ++YYCURSOR; yy378: -#line 440 "../src/ast/lex.re" +#line 441 "../src/ast/lex.re" { goto code; } #line 2349 "src/ast/lex.cc" yy379: @@ -2353,7 +2353,7 @@ yy379: } if (yych == '#') goto yy392; yy380: -#line 435 "../src/ast/lex.re" +#line 436 "../src/ast/lex.re" { pos = cur; cline++; goto code; } #line 2359 "src/ast/lex.cc" yy381: @@ -2362,7 +2362,7 @@ yy381: goto yy378; yy382: ++YYCURSOR; -#line 439 "../src/ast/lex.re" +#line 440 "../src/ast/lex.re" { lex_string(cur[-1]); goto code; } #line 2368 "src/ast/lex.cc" yy384: @@ -2372,12 +2372,12 @@ yy384: goto yy378; yy385: ++YYCURSOR; -#line 433 "../src/ast/lex.re" +#line 434 "../src/ast/lex.re" { ++depth; goto code; } #line 2378 "src/ast/lex.cc" yy387: ++YYCURSOR; -#line 425 "../src/ast/lex.re" +#line 426 "../src/ast/lex.re" { if (--depth == 0) { yylval.code = new Code(get_fname (), line, tok, tok_len ()); @@ -2411,12 +2411,12 @@ yy392: } yy394: ++YYCURSOR; -#line 437 "../src/ast/lex.re" +#line 438 "../src/ast/lex.re" { lex_c_comment(); goto code; } #line 2417 "src/ast/lex.cc" yy396: ++YYCURSOR; -#line 438 "../src/ast/lex.re" +#line 439 "../src/ast/lex.re" { lex_cpp_comment(); goto code; } #line 2422 "src/ast/lex.cc" yy398: @@ -2475,7 +2475,7 @@ yy406: yy408: ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 434 "../src/ast/lex.re" +#line 435 "../src/ast/lex.re" { set_sourceline (); goto code; } #line 2481 "src/ast/lex.cc" yy410: @@ -2503,7 +2503,7 @@ yy414: if (yych == '\n') goto yy391; goto yy411; } -#line 441 "../src/ast/lex.re" +#line 442 "../src/ast/lex.re" } @@ -2537,18 +2537,18 @@ loop: } yy417: ++YYCURSOR; -#line 451 "../src/ast/lex.re" +#line 452 "../src/ast/lex.re" { fail_if_eof(); goto loop; } #line 2543 "src/ast/lex.cc" yy419: ++YYCURSOR; yy420: -#line 452 "../src/ast/lex.re" +#line 453 "../src/ast/lex.re" { goto loop; } #line 2549 "src/ast/lex.cc" yy421: ++YYCURSOR; -#line 450 "../src/ast/lex.re" +#line 451 "../src/ast/lex.re" { pos = cur; ++cline; goto loop; } #line 2554 "src/ast/lex.cc" yy423: @@ -2557,7 +2557,7 @@ yy423: goto yy420; yy424: ++YYCURSOR; -#line 448 "../src/ast/lex.re" +#line 449 "../src/ast/lex.re" { if (cur[-1] == delim) return; else goto loop; } #line 2563 "src/ast/lex.cc" yy426: @@ -2570,11 +2570,11 @@ yy426: } yy427: ++YYCURSOR; -#line 449 "../src/ast/lex.re" +#line 450 "../src/ast/lex.re" { goto loop; } #line 2576 "src/ast/lex.cc" } -#line 453 "../src/ast/lex.re" +#line 454 "../src/ast/lex.re" } @@ -2598,18 +2598,18 @@ loop: } yy431: ++YYCURSOR; -#line 462 "../src/ast/lex.re" +#line 463 "../src/ast/lex.re" { fail_if_eof(); goto loop; } #line 2604 "src/ast/lex.cc" yy433: ++YYCURSOR; yy434: -#line 463 "../src/ast/lex.re" +#line 464 "../src/ast/lex.re" { goto loop; } #line 2610 "src/ast/lex.cc" yy435: ++YYCURSOR; -#line 461 "../src/ast/lex.re" +#line 462 "../src/ast/lex.re" { pos = cur; ++cline; goto loop; } #line 2615 "src/ast/lex.cc" yy437: @@ -2620,11 +2620,11 @@ yy438: yych = (YYCTYPE)*++YYCURSOR; if (yych != '/') goto yy434; ++YYCURSOR; -#line 460 "../src/ast/lex.re" +#line 461 "../src/ast/lex.re" { return; } #line 2626 "src/ast/lex.cc" } -#line 464 "../src/ast/lex.re" +#line 465 "../src/ast/lex.re" } @@ -2647,18 +2647,18 @@ loop: } yy443: ++YYCURSOR; -#line 472 "../src/ast/lex.re" +#line 473 "../src/ast/lex.re" { fail_if_eof(); goto loop; } #line 2653 "src/ast/lex.cc" yy445: ++YYCURSOR; yy446: -#line 473 "../src/ast/lex.re" +#line 474 "../src/ast/lex.re" { goto loop; } #line 2659 "src/ast/lex.cc" yy447: ++YYCURSOR; -#line 471 "../src/ast/lex.re" +#line 472 "../src/ast/lex.re" { pos = cur; ++cline; return; } #line 2664 "src/ast/lex.cc" yy449: @@ -2666,7 +2666,7 @@ yy449: if (yych == '\n') goto yy447; goto yy446; } -#line 474 "../src/ast/lex.re" +#line 475 "../src/ast/lex.re" } @@ -2684,16 +2684,16 @@ fst: if (YYLIMIT <= YYCURSOR) YYFILL(1); yych = (YYCTYPE)*YYCURSOR; if (yych == ']') goto yy453; -#line 486 "../src/ast/lex.re" +#line 487 "../src/ast/lex.re" { l = lex_cls_chr(); goto snd; } #line 2690 "src/ast/lex.cc" yy453: ++YYCURSOR; -#line 485 "../src/ast/lex.re" +#line 486 "../src/ast/lex.re" { return ast_cls(cline, c0, cls, neg); } #line 2695 "src/ast/lex.cc" } -#line 487 "../src/ast/lex.re" +#line 488 "../src/ast/lex.re" snd: @@ -2704,7 +2704,7 @@ snd: yych = (YYCTYPE)*(YYMARKER = YYCURSOR); if (yych == '-') goto yy458; yy457: -#line 490 "../src/ast/lex.re" +#line 491 "../src/ast/lex.re" { u = l; goto add; } #line 2710 "src/ast/lex.cc" yy458: @@ -2715,7 +2715,7 @@ yy458: yy460: ++YYCURSOR; YYCURSOR -= 1; -#line 491 "../src/ast/lex.re" +#line 492 "../src/ast/lex.re" { u = lex_cls_chr(); if (l > u) { @@ -2726,7 +2726,7 @@ yy460: } #line 2728 "src/ast/lex.cc" } -#line 499 "../src/ast/lex.re" +#line 500 "../src/ast/lex.re" add: cls->push_back(ASTRange(l, u, c)); @@ -2755,18 +2755,18 @@ uint32_t Scanner::lex_cls_chr() } yy464: ++YYCURSOR; -#line 511 "../src/ast/lex.re" +#line 512 "../src/ast/lex.re" { fail_if_eof(); return 0; } #line 2761 "src/ast/lex.cc" yy466: ++YYCURSOR; yy467: -#line 517 "../src/ast/lex.re" +#line 518 "../src/ast/lex.re" { return static_cast(tok[0]); } #line 2767 "src/ast/lex.cc" yy468: ++YYCURSOR; -#line 512 "../src/ast/lex.re" +#line 513 "../src/ast/lex.re" { fatal_lc(l, c, "newline in character class"); } #line 2772 "src/ast/lex.cc" yy470: @@ -2827,13 +2827,13 @@ yy471: } } yy472: -#line 515 "../src/ast/lex.re" +#line 516 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in escape sequence"); } #line 2833 "src/ast/lex.cc" yy473: ++YYCURSOR; yy474: -#line 530 "../src/ast/lex.re" +#line 531 "../src/ast/lex.re" { warn.useless_escape(cline, get_column(), tok[1]); return static_cast(tok[1]); @@ -2845,7 +2845,7 @@ yy475: goto yy474; yy476: ++YYCURSOR; -#line 528 "../src/ast/lex.re" +#line 529 "../src/ast/lex.re" { return static_cast('-'); } #line 2851 "src/ast/lex.cc" yy478: @@ -2854,7 +2854,7 @@ yy478: if (yych <= '/') goto yy479; if (yych <= '7') goto yy503; yy479: -#line 514 "../src/ast/lex.re" +#line 515 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in octal escape sequence"); } #line 2860 "src/ast/lex.cc" yy480: @@ -2872,7 +2872,7 @@ yy481: if (yych <= 'f') goto yy505; } yy482: -#line 513 "../src/ast/lex.re" +#line 514 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in hexadecimal escape sequence"); } #line 2878 "src/ast/lex.cc" yy483: @@ -2890,47 +2890,47 @@ yy483: } yy484: ++YYCURSOR; -#line 527 "../src/ast/lex.re" +#line 528 "../src/ast/lex.re" { return static_cast('\\'); } #line 2896 "src/ast/lex.cc" yy486: ++YYCURSOR; -#line 529 "../src/ast/lex.re" +#line 530 "../src/ast/lex.re" { return static_cast(']'); } #line 2901 "src/ast/lex.cc" yy488: ++YYCURSOR; -#line 520 "../src/ast/lex.re" +#line 521 "../src/ast/lex.re" { return static_cast('\a'); } #line 2906 "src/ast/lex.cc" yy490: ++YYCURSOR; -#line 521 "../src/ast/lex.re" +#line 522 "../src/ast/lex.re" { return static_cast('\b'); } #line 2911 "src/ast/lex.cc" yy492: ++YYCURSOR; -#line 522 "../src/ast/lex.re" +#line 523 "../src/ast/lex.re" { return static_cast('\f'); } #line 2916 "src/ast/lex.cc" yy494: ++YYCURSOR; -#line 523 "../src/ast/lex.re" +#line 524 "../src/ast/lex.re" { return static_cast('\n'); } #line 2921 "src/ast/lex.cc" yy496: ++YYCURSOR; -#line 524 "../src/ast/lex.re" +#line 525 "../src/ast/lex.re" { return static_cast('\r'); } #line 2926 "src/ast/lex.cc" yy498: ++YYCURSOR; -#line 525 "../src/ast/lex.re" +#line 526 "../src/ast/lex.re" { return static_cast('\t'); } #line 2931 "src/ast/lex.cc" yy500: ++YYCURSOR; -#line 526 "../src/ast/lex.re" +#line 527 "../src/ast/lex.re" { return static_cast('\v'); } #line 2936 "src/ast/lex.cc" yy502: @@ -2995,7 +2995,7 @@ yy507: } yy508: ++YYCURSOR; -#line 519 "../src/ast/lex.re" +#line 520 "../src/ast/lex.re" { return unesc_oct(tok, cur); } #line 3001 "src/ast/lex.cc" yy510: @@ -3024,7 +3024,7 @@ yy511: } yy512: ++YYCURSOR; -#line 518 "../src/ast/lex.re" +#line 519 "../src/ast/lex.re" { return unesc_hex(tok, cur); } #line 3030 "src/ast/lex.cc" yy514: @@ -3050,7 +3050,7 @@ yy515: goto yy504; } } -#line 534 "../src/ast/lex.re" +#line 535 "../src/ast/lex.re" } @@ -3077,13 +3077,13 @@ uint32_t Scanner::lex_str_chr(char quote, bool &end) } yy518: ++YYCURSOR; -#line 544 "../src/ast/lex.re" +#line 545 "../src/ast/lex.re" { fail_if_eof(); return 0; } #line 3083 "src/ast/lex.cc" yy520: ++YYCURSOR; yy521: -#line 550 "../src/ast/lex.re" +#line 551 "../src/ast/lex.re" { end = tok[0] == quote; return static_cast(tok[0]); @@ -3091,7 +3091,7 @@ yy521: #line 3092 "src/ast/lex.cc" yy522: ++YYCURSOR; -#line 545 "../src/ast/lex.re" +#line 546 "../src/ast/lex.re" { fatal_lc(l, c, "newline in character string"); } #line 3097 "src/ast/lex.cc" yy524: @@ -3147,13 +3147,13 @@ yy525: } } yy526: -#line 548 "../src/ast/lex.re" +#line 549 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in escape sequence"); } #line 3153 "src/ast/lex.cc" yy527: ++YYCURSOR; yy528: -#line 564 "../src/ast/lex.re" +#line 565 "../src/ast/lex.re" { if (tok[1] != quote) { warn.useless_escape(cline, get_column(), tok[1]); @@ -3171,7 +3171,7 @@ yy530: if (yych <= '/') goto yy531; if (yych <= '7') goto yy553; yy531: -#line 547 "../src/ast/lex.re" +#line 548 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in octal escape sequence"); } #line 3177 "src/ast/lex.cc" yy532: @@ -3189,7 +3189,7 @@ yy533: if (yych <= 'f') goto yy555; } yy534: -#line 546 "../src/ast/lex.re" +#line 547 "../src/ast/lex.re" { fatal_lc(l, c, "syntax error in hexadecimal escape sequence"); } #line 3195 "src/ast/lex.cc" yy535: @@ -3207,42 +3207,42 @@ yy535: } yy536: ++YYCURSOR; -#line 563 "../src/ast/lex.re" +#line 564 "../src/ast/lex.re" { return static_cast('\\'); } #line 3213 "src/ast/lex.cc" yy538: ++YYCURSOR; -#line 556 "../src/ast/lex.re" +#line 557 "../src/ast/lex.re" { return static_cast('\a'); } #line 3218 "src/ast/lex.cc" yy540: ++YYCURSOR; -#line 557 "../src/ast/lex.re" +#line 558 "../src/ast/lex.re" { return static_cast('\b'); } #line 3223 "src/ast/lex.cc" yy542: ++YYCURSOR; -#line 558 "../src/ast/lex.re" +#line 559 "../src/ast/lex.re" { return static_cast('\f'); } #line 3228 "src/ast/lex.cc" yy544: ++YYCURSOR; -#line 559 "../src/ast/lex.re" +#line 560 "../src/ast/lex.re" { return static_cast('\n'); } #line 3233 "src/ast/lex.cc" yy546: ++YYCURSOR; -#line 560 "../src/ast/lex.re" +#line 561 "../src/ast/lex.re" { return static_cast('\r'); } #line 3238 "src/ast/lex.cc" yy548: ++YYCURSOR; -#line 561 "../src/ast/lex.re" +#line 562 "../src/ast/lex.re" { return static_cast('\t'); } #line 3243 "src/ast/lex.cc" yy550: ++YYCURSOR; -#line 562 "../src/ast/lex.re" +#line 563 "../src/ast/lex.re" { return static_cast('\v'); } #line 3248 "src/ast/lex.cc" yy552: @@ -3307,7 +3307,7 @@ yy557: } yy558: ++YYCURSOR; -#line 555 "../src/ast/lex.re" +#line 556 "../src/ast/lex.re" { return unesc_oct(tok, cur); } #line 3313 "src/ast/lex.cc" yy560: @@ -3336,7 +3336,7 @@ yy561: } yy562: ++YYCURSOR; -#line 554 "../src/ast/lex.re" +#line 555 "../src/ast/lex.re" { return unesc_hex(tok, cur); } #line 3342 "src/ast/lex.cc" yy564: @@ -3362,7 +3362,7 @@ yy565: goto yy554; } } -#line 570 "../src/ast/lex.re" +#line 571 "../src/ast/lex.re" } @@ -3440,7 +3440,7 @@ sourceline: } } ++YYCURSOR; -#line 607 "../src/ast/lex.re" +#line 608 "../src/ast/lex.re" { --cur; tok = cur; @@ -3450,12 +3450,12 @@ sourceline: yy570: ++YYCURSOR; yy571: -#line 613 "../src/ast/lex.re" +#line 614 "../src/ast/lex.re" { goto sourceline; } #line 3456 "src/ast/lex.cc" yy572: ++YYCURSOR; -#line 602 "../src/ast/lex.re" +#line 603 "../src/ast/lex.re" { tok = cur; return; @@ -3477,7 +3477,7 @@ yy576: if (yybm[0+yych] & 64) { goto yy576; } -#line 589 "../src/ast/lex.re" +#line 590 "../src/ast/lex.re" { if (!s_to_u32_unsafe (tok, cur, cline)) { fatal_lc(get_cline(), get_column(), "line number overflow"); @@ -3501,7 +3501,7 @@ yy581: goto yy571; yy582: ++YYCURSOR; -#line 596 "../src/ast/lex.re" +#line 597 "../src/ast/lex.re" { in.escaped_file_name = std::string (tok + 1, tok_len () - 2); // strip quotes strrreplace (in.escaped_file_name, "\\", "\\\\"); @@ -3516,7 +3516,7 @@ yy584: if (yych == '\n') goto yy581; goto yy579; } -#line 614 "../src/ast/lex.re" +#line 615 "../src/ast/lex.re" } diff --git a/re2c/src/ast/lex.re b/re2c/src/ast/lex.re index f891b09d..693e871b 100644 --- a/re2c/src/ast/lex.re +++ b/re2c/src/ast/lex.re @@ -133,6 +133,7 @@ echo: "/*!header:re2c:on" { out.wraw(tok, ptr); out.header_mode(true); + out.need_header = true; lex_end_of_comment(out); goto echo; } diff --git a/re2c/src/code/output.cc b/re2c/src/code/output.cc index 45490820..7c240dff 100644 --- a/re2c/src/code/output.cc +++ b/re2c/src/code/output.cc @@ -78,6 +78,7 @@ OutputFile::OutputFile(Warn &w) , state_goto(false) , cond_goto(false) , warn_condition_order(true) + , need_header(false) , warn(w) {} @@ -431,13 +432,26 @@ static void foldexpr(std::vector &frags) } } -void OutputFile::emit_blocks(const std::string &filename, - FILE *file, blocks_t &blocks, +bool OutputFile::emit_blocks(const std::string &fname, blocks_t &blocks, const uniq_vector_t &global_types, const std::set &global_stags, const std::set &global_mtags, size_t max_fill, size_t max_nmatch) { + FILE *file = NULL; + std::string filename = fname; + if (filename.empty()) { + filename = ""; + file = stdout; + } + else { + file = fopen(filename.c_str(), "w"); + if (!file) { + error("cannot open output file: %s", filename.c_str()); + return false; + } + } + fix_first_block_opts(blocks); unsigned int line_count = 1; @@ -529,6 +543,9 @@ void OutputFile::emit_blocks(const std::string &filename, line_count += f.count_lines(); } } + + fclose(file); + return true; } static bool have_cond_frag(const blocks_t &blocks) @@ -575,47 +592,24 @@ bool OutputFile::emit(size_t maxfill, size_t nmatch) // global options are last block's options const opt_t *opts = block().opts; + bool ok = true; // emit .h file - const std::string &header = opts->header_file; - if (!header.empty()) { - FILE *file = fopen(header.c_str(), "w"); - - if (!file) { - error("cannot open header file: %s", header.c_str()); - return false; - } - - emit_blocks(header, file, hblocks, conds, stags, mtags, maxfill, nmatch); - + if (!opts->header_file.empty() || need_header) { + // old-style -t, --type-headers usage implies condition generation if (!conds.empty() && !have_cond_frag(hblocks)) { - std::ostringstream os; + std::ostream &os = hblocks.back()->fragments.back ()->stream; os << std::endl; output_types(os, 0, opts, conds); - fwrite(os.str().c_str(), 1, os.str().size(), file); } - fclose(file); + ok &= emit_blocks(opts->header_file, hblocks, conds, stags, mtags, maxfill, nmatch); } // emit .c file - FILE *file = NULL; - std::string filename = cblocks.back()->opts->output_file; - if (filename.empty()) { - filename = ""; - file = stdout; - } - else { - file = fopen(filename.c_str(), "w"); - if (!file) { - error("cannot open output file: %s", filename.c_str()); - return false; - } - } - emit_blocks(filename, file, cblocks, conds, stags, mtags, maxfill, nmatch); - fclose(file); + ok &= emit_blocks(opts->output_file, cblocks, conds, stags, mtags, maxfill, nmatch); - return true; + return ok; } Output::Output(Warn &w) diff --git a/re2c/src/code/output.h b/re2c/src/code/output.h index aea5ae91..965fcdeb 100644 --- a/re2c/src/code/output.h +++ b/re2c/src/code/output.h @@ -122,6 +122,7 @@ public: bool state_goto; bool cond_goto; bool warn_condition_order; + bool need_header; Warn &warn; explicit OutputFile(Warn &w); @@ -167,8 +168,7 @@ public: bool emit(size_t max_fill, size_t max_nmatch); - void emit_blocks(const std::string &filename, - FILE *file, blocks_t &blocks, + bool emit_blocks(const std::string &fname, blocks_t &blocks, const uniq_vector_t &global_types, const std::set &global_stags, const std::set &global_mtags, diff --git a/re2c/test/header/header_03.c b/re2c/test/header/header_03.c new file mode 100644 index 00000000..03278441 --- /dev/null +++ b/re2c/test/header/header_03.c @@ -0,0 +1,28 @@ +/* Generated by re2c */ +#line 1 "header/header_03.re" +#line 1 "header/header_03.re" + + +#line 6 "header/header_03.re" + + +#include "header_03.h" + +void lex(const YYCTYPE *YYCURSOR) +{ + +#line 15 "header/header_03.c" +{ + YYCTYPE yych; + yych = *YYCURSOR++; +#line 14 "header/header_03.re" + { return; } +#line 21 "header/header_03.c" +} +#line 15 "header/header_03.re" + +} +/* Generated by re2c */ + +#define YYCTYPE char +void lex(const YYCTYPE *); diff --git a/re2c/test/header/header_03.re b/re2c/test/header/header_03.re new file mode 100644 index 00000000..fc9fba32 --- /dev/null +++ b/re2c/test/header/header_03.re @@ -0,0 +1,16 @@ +/*!re2c re2c:flags:type-header = "header_03.h"; */ + +/*!header:re2c:on*/ +#define YYCTYPE char +void lex(const YYCTYPE *); +/*!header:re2c:off*/ + +#include "header_03.h" + +void lex(const YYCTYPE *YYCURSOR) +{ + /*!re2c + re2c:yyfill:enable = 0; + * { return; } + */ +} -- 2.40.0