1 /* Generated by re2c */
2 #line 1 "strip_003.re"
3 // re2c $INPUT -o $OUTPUT -b
4 /* re2c lesson 002_strip_comments, strip_003, (c) M. Boerger 2006 - 2007 */
5 #line 37 "strip_003.re"
6 
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 #line 13 "strip_003.c"
13 #define YYMAXFILL 4
14 #line 43 "strip_003.re"
15 
16 #define	BSIZE	128
17 
18 #if BSIZE < YYMAXFILL
19 # error BSIZE must be greater YYMAXFILL
20 #endif
21 
22 #define	YYCTYPE		unsigned char
23 #define	YYCURSOR	s.cur
24 #define	YYLIMIT		s.lim
25 #define YYMARKER	s.mrk
26 #define YYCTXMARKER s.ctx
27 #define	YYFILL(n)	{ if ((res = fill(&s, n)) >= 0) break; }
28 
29 typedef struct Scanner
30 {
31 	FILE			*fp;
32 	unsigned char	*cur, *tok, *lim, *eof, *ctx, *mrk;
33 	unsigned char 	buffer[BSIZE];
34 } Scanner;
35 
fill(Scanner * s,int len)36 int fill(Scanner *s, int len)
37 {
38 	if (!len)
39 	{
40 		s->cur = s->tok = s->lim = s->mrk = s->buffer;
41 		s->eof = 0;
42 	}
43 	if (!s->eof)
44 	{
45 		int got, cnt = s->tok - s->buffer;
46 
47 		if (cnt > 0)
48 		{
49 			memcpy(s->buffer, s->tok, s->lim - s->tok);
50 			s->tok -= cnt;
51 			s->cur -= cnt;
52 			s->lim -= cnt;
53 			s->mrk -= cnt;
54 			s->ctx -= cnt;
55 		}
56 		cnt = BSIZE - cnt;
57 		if ((got = fread(s->lim, 1, cnt, s->fp)) != cnt)
58 		{
59 			s->eof = &s->lim[got];
60 		}
61 		s->lim += got;
62 	}
63 	else if (s->cur + len > s->eof)
64 	{
65 		return 0; /* not enough input data */
66 	}
67 	return -1;
68 }
69 
echo(Scanner * s)70 void echo(Scanner *s)
71 {
72 	fwrite(s->tok, 1, s->cur - s->tok, stdout);
73 }
74 
scan(FILE * fp)75 int scan(FILE *fp)
76 {
77 	int  res = 0;
78 	int  nlcomment = 0;
79     Scanner s;
80 
81 	if (!fp)
82 	{
83 		return 1; /* no file was opened */
84 	}
85 
86     s.fp = fp;
87 
88 	fill(&s, 0);
89 
90 	for(;;)
91 	{
92 		s.tok = s.cur;
93 
94 #line 95 "strip_003.c"
95 		{
96 			YYCTYPE yych;
97 			if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
98 			yych = *YYCURSOR;
99 			if (yych <= '\f') {
100 				if (yych == '\n') goto yy4;
101 			} else {
102 				if (yych <= '\r') goto yy5;
103 				if (yych == '/') goto yy6;
104 			}
105 			++YYCURSOR;
106 yy3:
107 #line 131 "strip_003.re"
108 			{ fputc(*s.tok, stdout); continue; }
109 #line 110 "strip_003.c"
110 yy4:
111 			yych = *(YYMARKER = ++YYCURSOR);
112 			if (yych == '/') goto yy7;
113 			goto yy3;
114 yy5:
115 			yych = *(YYMARKER = ++YYCURSOR);
116 			if (yych == '\n') goto yy9;
117 			goto yy3;
118 yy6:
119 			yych = *++YYCURSOR;
120 			if (yych == '*') goto yy10;
121 			if (yych == '/') goto yy12;
122 			goto yy3;
123 yy7:
124 			yych = *++YYCURSOR;
125 			if (yych == '*') goto yy14;
126 yy8:
127 			YYCURSOR = YYMARKER;
128 			goto yy3;
129 yy9:
130 			yych = *++YYCURSOR;
131 			if (yych == '/') goto yy7;
132 			goto yy8;
133 yy10:
134 			++YYCURSOR;
135 #line 130 "strip_003.re"
136 			{ goto comment; }
137 #line 138 "strip_003.c"
138 yy12:
139 			++YYCURSOR;
140 #line 128 "strip_003.re"
141 			{ goto cppcomment; }
142 #line 143 "strip_003.c"
143 yy14:
144 			++YYCURSOR;
145 			YYCURSOR -= 2;
146 #line 129 "strip_003.re"
147 			{ echo(&s); nlcomment = 1; continue; }
148 #line 149 "strip_003.c"
149 		}
150 #line 132 "strip_003.re"
151 
152 comment:
153 		s.tok = s.cur;
154 
155 #line 156 "strip_003.c"
156 		{
157 			YYCTYPE yych;
158 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
159 			yych = *YYCURSOR;
160 			if (yych == '*') goto yy20;
161 			++YYCURSOR;
162 yy19:
163 #line 137 "strip_003.re"
164 			{ goto comment; }
165 #line 166 "strip_003.c"
166 yy20:
167 			yych = *++YYCURSOR;
168 			if (yych != '/') goto yy19;
169 			++YYCURSOR;
170 #line 136 "strip_003.re"
171 			{ goto commentws; }
172 #line 173 "strip_003.c"
173 		}
174 #line 138 "strip_003.re"
175 
176 commentws:
177 		s.tok = s.cur;
178 
179 #line 180 "strip_003.c"
180 		{
181 			YYCTYPE yych;
182 			if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
183 			yych = *YYCURSOR;
184 			if (yych <= '\r') {
185 				if (yych <= '\t') {
186 					if (yych >= '\t') goto yy27;
187 				} else {
188 					if (yych <= '\n') goto yy29;
189 					if (yych >= '\r') goto yy31;
190 				}
191 			} else {
192 				if (yych <= ' ') {
193 					if (yych >= ' ') goto yy27;
194 				} else {
195 					if (yych == '/') goto yy32;
196 				}
197 			}
198 			++YYCURSOR;
199 yy26:
200 #line 152 "strip_003.re"
201 			{ echo(&s); nlcomment = 0; continue; }
202 #line 203 "strip_003.c"
203 yy27:
204 			++YYCURSOR;
205 yy28:
206 #line 151 "strip_003.re"
207 			{ goto commentws; }
208 #line 209 "strip_003.c"
209 yy29:
210 			yych = *(YYMARKER = ++YYCURSOR);
211 			if (yych == '/') goto yy33;
212 yy30:
213 #line 143 "strip_003.re"
214 			{
215 					if (!nlcomment)
216 					{
217 						echo(&s);
218 					}
219 					nlcomment = 0;
220 					continue;
221 				}
222 #line 223 "strip_003.c"
223 yy31:
224 			yych = *++YYCURSOR;
225 			if (yych == '\n') goto yy29;
226 			goto yy28;
227 yy32:
228 			yych = *++YYCURSOR;
229 			if (yych == '*') goto yy35;
230 			goto yy26;
231 yy33:
232 			yych = *++YYCURSOR;
233 			if (yych == '*') goto yy35;
234 			YYCURSOR = YYMARKER;
235 			goto yy30;
236 yy35:
237 			++YYCURSOR;
238 #line 142 "strip_003.re"
239 			{ goto comment; }
240 #line 241 "strip_003.c"
241 		}
242 #line 153 "strip_003.re"
243 
244 cppcomment:
245 		s.tok = s.cur;
246 
247 #line 248 "strip_003.c"
248 		{
249 			YYCTYPE yych;
250 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
251 			yych = *YYCURSOR;
252 			if (yych == '\n') goto yy41;
253 			if (yych == '\r') goto yy43;
254 			++YYCURSOR;
255 yy40:
256 #line 158 "strip_003.re"
257 			{ goto cppcomment; }
258 #line 259 "strip_003.c"
259 yy41:
260 			++YYCURSOR;
261 #line 157 "strip_003.re"
262 			{ echo(&s); continue; }
263 #line 264 "strip_003.c"
264 yy43:
265 			yych = *++YYCURSOR;
266 			if (yych == '\n') goto yy41;
267 			goto yy40;
268 		}
269 #line 159 "strip_003.re"
270 
271 	}
272 
273 	if (fp != stdin)
274 	{
275 		fclose(fp); /* close only if not stdin */
276 	}
277 	return res; /* return result */
278 }
279 
main(int argc,char ** argv)280 int main(int argc, char **argv)
281 {
282 	if (argc > 1)
283 	{
284 		return scan(!strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"));
285 	}
286 	else
287 	{
288 		fprintf(stderr, "%s <expr>\n", argv[0]);
289 		return 1;
290 	}
291 }
292