From: helly Date: Mon, 17 Apr 2006 15:39:02 +0000 (+0000) Subject: - Clean separation of steps X-Git-Tag: 0.13.6~369 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2a8f363a75d138ea640b77e55286d7895b51e923;p=re2c - Clean separation of steps --- diff --git a/lessons/002_strip_comments/strip_002.s.c b/lessons/002_strip_comments/strip_002.s.c index be5cff40..ad14ef04 100755 --- a/lessons/002_strip_comments/strip_002.s.c +++ b/lessons/002_strip_comments/strip_002.s.c @@ -1,7 +1,7 @@ /* Generated by re2c */ #line 1 "strip_002.s.re" /* re2c lesson 002_strip_comments, strip_002.s, (c) M. Boerger 2006 */ -#line 34 "strip_002.s.re" +#line 32 "strip_002.s.re" #include #include @@ -68,7 +68,6 @@ void echo(Scanner *s) int scan(FILE *fp) { int res = 0; - int nlcomment = 0; Scanner s; if (!fp) @@ -84,7 +83,7 @@ int scan(FILE *fp) { s.tok = s.cur; -#line 88 "" +#line 87 "" { YYCTYPE yych; @@ -95,29 +94,29 @@ int scan(FILE *fp) if((yych = *YYCURSOR) == '*') goto yy5; if(yych == '/') goto yy7; yy3: -#line 124 "strip_002.s.re" +#line 121 "strip_002.s.re" { fputc(*s.tok, stdout); continue; } -#line 101 "" +#line 100 "" yy4: yych = *++YYCURSOR; goto yy3; yy5: ++YYCURSOR; -#line 123 "strip_002.s.re" +#line 120 "strip_002.s.re" { goto comment; } -#line 109 "" +#line 108 "" yy7: ++YYCURSOR; -#line 122 "strip_002.s.re" +#line 119 "strip_002.s.re" { goto cppcomment; } -#line 114 "" +#line 113 "" } -#line 125 "strip_002.s.re" +#line 122 "strip_002.s.re" comment: s.tok = s.cur; -#line 121 "" +#line 120 "" { YYCTYPE yych; if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); @@ -126,24 +125,24 @@ comment: ++YYCURSOR; if((yych = *YYCURSOR) == '/') goto yy14; yy12: -#line 130 "strip_002.s.re" +#line 127 "strip_002.s.re" { goto comment; } -#line 132 "" +#line 131 "" yy13: yych = *++YYCURSOR; goto yy12; yy14: ++YYCURSOR; -#line 129 "strip_002.s.re" +#line 126 "strip_002.s.re" { goto commentws; } -#line 140 "" +#line 139 "" } -#line 131 "strip_002.s.re" +#line 128 "strip_002.s.re" commentws: s.tok = s.cur; -#line 147 "" +#line 146 "" { YYCTYPE yych; if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); @@ -162,41 +161,34 @@ yy18: ++YYCURSOR; if((yych = *YYCURSOR) == 0x0A) goto yy25; yy19: -#line 143 "strip_002.s.re" +#line 133 "strip_002.s.re" { goto commentws; } -#line 168 "" +#line 167 "" yy20: ++YYCURSOR; yy21: -#line 135 "strip_002.s.re" - { - if (!nlcomment) - { - echo(&s); - } - nlcomment = 0; - continue; - } -#line 181 "" +#line 132 "strip_002.s.re" + { echo(&s); continue; } +#line 173 "" yy22: yych = *++YYCURSOR; goto yy19; yy23: ++YYCURSOR; -#line 144 "strip_002.s.re" - { echo(&s); nlcomment = 0; continue; } -#line 189 "" +#line 134 "strip_002.s.re" + { echo(&s); continue; } +#line 181 "" yy25: ++YYCURSOR; yych = *YYCURSOR; goto yy21; } -#line 145 "strip_002.s.re" +#line 135 "strip_002.s.re" cppcomment: s.tok = s.cur; -#line 200 "" +#line 192 "" { YYCTYPE yych; if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); @@ -206,15 +198,15 @@ cppcomment: ++YYCURSOR; if((yych = *YYCURSOR) == 0x0A) goto yy33; yy29: -#line 150 "strip_002.s.re" +#line 140 "strip_002.s.re" { goto cppcomment; } -#line 212 "" +#line 204 "" yy30: ++YYCURSOR; yy31: -#line 149 "strip_002.s.re" +#line 139 "strip_002.s.re" { echo(&s); continue; } -#line 218 "" +#line 210 "" yy32: yych = *++YYCURSOR; goto yy29; @@ -223,7 +215,7 @@ yy33: yych = *YYCURSOR; goto yy31; } -#line 151 "strip_002.s.re" +#line 141 "strip_002.s.re" } diff --git a/lessons/002_strip_comments/strip_002.s.re b/lessons/002_strip_comments/strip_002.s.re index b440a6b4..21f11d0b 100755 --- a/lessons/002_strip_comments/strip_002.s.re +++ b/lessons/002_strip_comments/strip_002.s.re @@ -4,6 +4,13 @@ - complexity . When a comemnt is preceeded by a new line and followed by whitespace and a new line then we can drop the trailing whitespace and new line. + . Additional to what we strip out already what about two consequtive comment + blocks? When two comments are only separated by whitespace we want to drop + both. In other words when detecting the end of a comment block we need to + check whether it is followed by only whitespace and the a new comment in + which case we continure ignoring the input. If it is followed only by white + space and a new line we strip out the new white space and new line. In any + other case we start outputting all that follows. But we cannot simply use the following two rules: "*" "/" WS* "/" "*" { continue; } "*" "/" WS* NL { continue; } @@ -21,15 +28,6 @@ . When shifting buffer contents as done in our fill function the marker needs to be corrected, too. -- formatting - . Until now we only used single line expression code and we always had the - opening { on the same line as the rule itself. If we have multiline rule - code and care for formatting we can nolonger rely on re2c. Now we have - to indent the rule code ourself. Also we need to take care of the opening - {. If we keep it on the same line as the rule then re2c will indent it - correctly and the emitted #line informations will be correct. If we place - it on the next line then the #line directivy will also point to that line - and not to the rule. */ #include @@ -97,7 +95,6 @@ void echo(Scanner *s) int scan(FILE *fp) { int res = 0; - int nlcomment = 0; Scanner s; if (!fp) @@ -132,16 +129,9 @@ comment: commentws: s.tok = s.cur; /*!re2c - NL { - if (!nlcomment) - { - echo(&s); - } - nlcomment = 0; - continue; - } + NL { echo(&s); continue; } WS { goto commentws; } - ANY { echo(&s); nlcomment = 0; continue; } + ANY { echo(&s); continue; } */ cppcomment: s.tok = s.cur; diff --git a/lessons/002_strip_comments/strip_003.b.c b/lessons/002_strip_comments/strip_003.b.c index 21c7ff86..eb166021 100755 --- a/lessons/002_strip_comments/strip_003.b.c +++ b/lessons/002_strip_comments/strip_003.b.c @@ -1,7 +1,7 @@ /* Generated by re2c */ #line 1 "strip_003.b.re" /* re2c lesson 002_strip_comments, strip_003.b, (c) M. Boerger 2006 */ -#line 29 "strip_003.b.re" +#line 37 "strip_003.b.re" #include #include @@ -104,7 +104,7 @@ int scan(FILE *fp) if((yych = *YYCURSOR) == '*') goto yy12; if(yych == '/') goto yy14; yy3: -#line 122 "strip_003.b.re" +#line 130 "strip_003.b.re" { fputc(*s.tok, stdout); continue; } #line 110 "" yy4: @@ -128,7 +128,7 @@ yy8: yy9: ++YYCURSOR; YYCURSOR = YYCTXMARKER; -#line 120 "strip_003.b.re" +#line 128 "strip_003.b.re" { echo(&s); nlcomment = 1; continue; } #line 134 "" yy11: @@ -138,17 +138,17 @@ yy11: goto yy8; yy12: ++YYCURSOR; -#line 121 "strip_003.b.re" +#line 129 "strip_003.b.re" { goto comment; } #line 144 "" yy14: ++YYCURSOR; -#line 119 "strip_003.b.re" +#line 127 "strip_003.b.re" { goto cppcomment; } #line 149 "" } } -#line 123 "strip_003.b.re" +#line 131 "strip_003.b.re" comment: s.tok = s.cur; @@ -163,7 +163,7 @@ comment: ++YYCURSOR; if((yych = *YYCURSOR) == '/') goto yy21; yy19: -#line 128 "strip_003.b.re" +#line 136 "strip_003.b.re" { goto comment; } #line 169 "" yy20: @@ -171,12 +171,12 @@ yy20: goto yy19; yy21: ++YYCURSOR; -#line 127 "strip_003.b.re" +#line 135 "strip_003.b.re" { goto commentws; } #line 177 "" } } -#line 129 "strip_003.b.re" +#line 137 "strip_003.b.re" commentws: s.tok = s.cur; @@ -207,14 +207,14 @@ commentws: ++YYCURSOR; if((yych = *YYCURSOR) == 0x0A) goto yy37; yy26: -#line 142 "strip_003.b.re" +#line 150 "strip_003.b.re" { goto commentws; } #line 213 "" yy27: yych = *(YYMARKER = ++YYCURSOR); if(yych == '/') goto yy35; yy28: -#line 134 "strip_003.b.re" +#line 142 "strip_003.b.re" { if (!nlcomment) { @@ -228,7 +228,7 @@ yy29: ++YYCURSOR; if((yych = *YYCURSOR) == '*') goto yy33; yy30: -#line 143 "strip_003.b.re" +#line 151 "strip_003.b.re" { echo(&s); nlcomment = 0; continue; } #line 234 "" yy31: @@ -239,7 +239,7 @@ yy32: goto yy30; yy33: ++YYCURSOR; -#line 133 "strip_003.b.re" +#line 141 "strip_003.b.re" { goto comment; } #line 245 "" yy35: @@ -253,7 +253,7 @@ yy37: goto yy28; } } -#line 144 "strip_003.b.re" +#line 152 "strip_003.b.re" cppcomment: s.tok = s.cur; @@ -269,13 +269,13 @@ cppcomment: ++YYCURSOR; if((yych = *YYCURSOR) == 0x0A) goto yy45; yy41: -#line 149 "strip_003.b.re" +#line 157 "strip_003.b.re" { goto cppcomment; } #line 275 "" yy42: ++YYCURSOR; yy43: -#line 148 "strip_003.b.re" +#line 156 "strip_003.b.re" { echo(&s); continue; } #line 281 "" yy44: @@ -287,7 +287,7 @@ yy45: goto yy43; } } -#line 150 "strip_003.b.re" +#line 158 "strip_003.b.re" } diff --git a/lessons/002_strip_comments/strip_003.b.re b/lessons/002_strip_comments/strip_003.b.re index 7b1f8748..065d31ab 100755 --- a/lessons/002_strip_comments/strip_003.b.re +++ b/lessons/002_strip_comments/strip_003.b.re @@ -2,14 +2,10 @@ /*!ignore:re2c - more complexity - . Additional to what we strip out already what about two consequtive comment - blocks? When two comments are only separated by whitespace we want to drop - both. In other words when detecting the end of a comment block we need to - check whether it is followed by only whitespace and the a new comment in - which case we continure ignoring the input. If it is followed only by white - space and a new line we strip out the new white space and new line. In any - other case we start outputting all that follows. - . The solution to the above is to use trailing contexts. + . Right now we strip out trailing white space and new lines after a comment + block. This can be a problem when the comment block was not preceeded by + a new line. + . The solution is to use trailing contexts. - trailing contexts . Re2c allows to check for a portion of input and only recognize it when it @@ -24,7 +20,19 @@ a pointer variable needs to be provided. . As with YYMARKER the corrsponding variable needs to be corrected if we shift in some buffer. - + . Still this is not all we need to solve the problem. What is left is that + the information whether we detected a trailing context was detected has to + be stored somewhere. This is done by the new variable nlcomment. + +- formatting + . Until now we only used single line expression code and we always had the + opening { on the same line as the rule itself. If we have multiline rule + code and care for formatting we can nolonger rely on re2c. Now we have + to indent the rule code ourself. Also we need to take care of the opening + {. If we keep it on the same line as the rule then re2c will indent it + correctly and the emitted #line informations will be correct. If we place + it on the next line then the #line directivy will also point to that line + and not to the rule. */ #include