/* 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 <stdlib.h>
#include <stdio.h>
int scan(FILE *fp)
{
int res = 0;
- int nlcomment = 0;
Scanner s;
if (!fp)
{
s.tok = s.cur;
-#line 88 "<stdout>"
+#line 87 "<stdout>"
{
YYCTYPE yych;
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 "<stdout>"
+#line 100 "<stdout>"
yy4:
yych = *++YYCURSOR;
goto yy3;
yy5:
++YYCURSOR;
-#line 123 "strip_002.s.re"
+#line 120 "strip_002.s.re"
{ goto comment; }
-#line 109 "<stdout>"
+#line 108 "<stdout>"
yy7:
++YYCURSOR;
-#line 122 "strip_002.s.re"
+#line 119 "strip_002.s.re"
{ goto cppcomment; }
-#line 114 "<stdout>"
+#line 113 "<stdout>"
}
-#line 125 "strip_002.s.re"
+#line 122 "strip_002.s.re"
comment:
s.tok = s.cur;
-#line 121 "<stdout>"
+#line 120 "<stdout>"
{
YYCTYPE yych;
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
++YYCURSOR;
if((yych = *YYCURSOR) == '/') goto yy14;
yy12:
-#line 130 "strip_002.s.re"
+#line 127 "strip_002.s.re"
{ goto comment; }
-#line 132 "<stdout>"
+#line 131 "<stdout>"
yy13:
yych = *++YYCURSOR;
goto yy12;
yy14:
++YYCURSOR;
-#line 129 "strip_002.s.re"
+#line 126 "strip_002.s.re"
{ goto commentws; }
-#line 140 "<stdout>"
+#line 139 "<stdout>"
}
-#line 131 "strip_002.s.re"
+#line 128 "strip_002.s.re"
commentws:
s.tok = s.cur;
-#line 147 "<stdout>"
+#line 146 "<stdout>"
{
YYCTYPE yych;
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
++YYCURSOR;
if((yych = *YYCURSOR) == 0x0A) goto yy25;
yy19:
-#line 143 "strip_002.s.re"
+#line 133 "strip_002.s.re"
{ goto commentws; }
-#line 168 "<stdout>"
+#line 167 "<stdout>"
yy20:
++YYCURSOR;
yy21:
-#line 135 "strip_002.s.re"
- {
- if (!nlcomment)
- {
- echo(&s);
- }
- nlcomment = 0;
- continue;
- }
-#line 181 "<stdout>"
+#line 132 "strip_002.s.re"
+ { echo(&s); continue; }
+#line 173 "<stdout>"
yy22:
yych = *++YYCURSOR;
goto yy19;
yy23:
++YYCURSOR;
-#line 144 "strip_002.s.re"
- { echo(&s); nlcomment = 0; continue; }
-#line 189 "<stdout>"
+#line 134 "strip_002.s.re"
+ { echo(&s); continue; }
+#line 181 "<stdout>"
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 "<stdout>"
+#line 192 "<stdout>"
{
YYCTYPE yych;
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
++YYCURSOR;
if((yych = *YYCURSOR) == 0x0A) goto yy33;
yy29:
-#line 150 "strip_002.s.re"
+#line 140 "strip_002.s.re"
{ goto cppcomment; }
-#line 212 "<stdout>"
+#line 204 "<stdout>"
yy30:
++YYCURSOR;
yy31:
-#line 149 "strip_002.s.re"
+#line 139 "strip_002.s.re"
{ echo(&s); continue; }
-#line 218 "<stdout>"
+#line 210 "<stdout>"
yy32:
yych = *++YYCURSOR;
goto yy29;
yych = *YYCURSOR;
goto yy31;
}
-#line 151 "strip_002.s.re"
+#line 141 "strip_002.s.re"
}
- 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; }
. 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 <stdlib.h>
int scan(FILE *fp)
{
int res = 0;
- int nlcomment = 0;
Scanner s;
if (!fp)
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;
/* 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 <stdlib.h>
#include <stdio.h>
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 "<stdout>"
yy4:
yy9:
++YYCURSOR;
YYCURSOR = YYCTXMARKER;
-#line 120 "strip_003.b.re"
+#line 128 "strip_003.b.re"
{ echo(&s); nlcomment = 1; continue; }
#line 134 "<stdout>"
yy11:
goto yy8;
yy12:
++YYCURSOR;
-#line 121 "strip_003.b.re"
+#line 129 "strip_003.b.re"
{ goto comment; }
#line 144 "<stdout>"
yy14:
++YYCURSOR;
-#line 119 "strip_003.b.re"
+#line 127 "strip_003.b.re"
{ goto cppcomment; }
#line 149 "<stdout>"
}
}
-#line 123 "strip_003.b.re"
+#line 131 "strip_003.b.re"
comment:
s.tok = s.cur;
++YYCURSOR;
if((yych = *YYCURSOR) == '/') goto yy21;
yy19:
-#line 128 "strip_003.b.re"
+#line 136 "strip_003.b.re"
{ goto comment; }
#line 169 "<stdout>"
yy20:
goto yy19;
yy21:
++YYCURSOR;
-#line 127 "strip_003.b.re"
+#line 135 "strip_003.b.re"
{ goto commentws; }
#line 177 "<stdout>"
}
}
-#line 129 "strip_003.b.re"
+#line 137 "strip_003.b.re"
commentws:
s.tok = s.cur;
++YYCURSOR;
if((yych = *YYCURSOR) == 0x0A) goto yy37;
yy26:
-#line 142 "strip_003.b.re"
+#line 150 "strip_003.b.re"
{ goto commentws; }
#line 213 "<stdout>"
yy27:
yych = *(YYMARKER = ++YYCURSOR);
if(yych == '/') goto yy35;
yy28:
-#line 134 "strip_003.b.re"
+#line 142 "strip_003.b.re"
{
if (!nlcomment)
{
++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 "<stdout>"
yy31:
goto yy30;
yy33:
++YYCURSOR;
-#line 133 "strip_003.b.re"
+#line 141 "strip_003.b.re"
{ goto comment; }
#line 245 "<stdout>"
yy35:
goto yy28;
}
}
-#line 144 "strip_003.b.re"
+#line 152 "strip_003.b.re"
cppcomment:
s.tok = s.cur;
++YYCURSOR;
if((yych = *YYCURSOR) == 0x0A) goto yy45;
yy41:
-#line 149 "strip_003.b.re"
+#line 157 "strip_003.b.re"
{ goto cppcomment; }
#line 275 "<stdout>"
yy42:
++YYCURSOR;
yy43:
-#line 148 "strip_003.b.re"
+#line 156 "strip_003.b.re"
{ echo(&s); continue; }
#line 281 "<stdout>"
yy44:
goto yy43;
}
}
-#line 150 "strip_003.b.re"
+#line 158 "strip_003.b.re"
}
/*!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
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 <stdlib.h>