1 /* Generated by re2c */
2 #line 1 "strip_002.re"
3 // re2c $INPUT -o $OUTPUT -s
4 /* re2c lesson 002_strip_comments, strip_002, (c) M. Boerger 2006 - 2007 */
5 #line 32 "strip_002.re"
6 
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 #line 13 "strip_002.c"
13 #define YYMAXFILL 2
14 #line 38 "strip_002.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	YYFILL(n)	{ if ((res = fill(&s, n)) >= 0) break; }
27 
28 typedef struct Scanner
29 {
30 	FILE			*fp;
31 	unsigned char	*cur, *tok, *lim, *eof, *mrk;
32 	unsigned char 	buffer[BSIZE];
33 } Scanner;
34 
fill(Scanner * s,int len)35 int fill(Scanner *s, int len)
36 {
37 	if (!len)
38 	{
39 		s->cur = s->tok = s->lim = s->mrk = s->buffer;
40 		s->eof = 0;
41 	}
42 	if (!s->eof)
43 	{
44 		int got, cnt = s->tok - s->buffer;
45 
46 		if (cnt > 0)
47 		{
48 			memcpy(s->buffer, s->tok, s->lim - s->tok);
49 			s->tok -= cnt;
50 			s->cur -= cnt;
51 			s->lim -= cnt;
52 			s->mrk -= cnt;
53 		}
54 		cnt = BSIZE - cnt;
55 		if ((got = fread(s->lim, 1, cnt, s->fp)) != cnt)
56 		{
57 			s->eof = &s->lim[got];
58 		}
59 		s->lim += got;
60 	}
61 	else if (s->cur + len > s->eof)
62 	{
63 		return 0; /* not enough input data */
64 	}
65 	return -1;
66 }
67 
echo(Scanner * s)68 void echo(Scanner *s)
69 {
70 	fwrite(s->tok, 1, s->cur - s->tok, stdout);
71 }
72 
scan(FILE * fp)73 int scan(FILE *fp)
74 {
75 	int  res = 0;
76     Scanner s;
77 
78 	if (!fp)
79 	{
80 		return 1; /* no file was opened */
81 	}
82 
83     s.fp = fp;
84 
85 	fill(&s, 0);
86 
87 	for(;;)
88 	{
89 		s.tok = s.cur;
90 
91 #line 92 "strip_002.c"
92 		{
93 			YYCTYPE yych;
94 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
95 			yych = *YYCURSOR;
96 			if (yych == '/') goto yy4;
97 			++YYCURSOR;
98 yy3:
99 #line 122 "strip_002.re"
100 			{ fputc(*s.tok, stdout); continue; }
101 #line 102 "strip_002.c"
102 yy4:
103 			yych = *++YYCURSOR;
104 			if (yych == '*') goto yy5;
105 			if (yych == '/') goto yy7;
106 			goto yy3;
107 yy5:
108 			++YYCURSOR;
109 #line 121 "strip_002.re"
110 			{ goto comment; }
111 #line 112 "strip_002.c"
112 yy7:
113 			++YYCURSOR;
114 #line 120 "strip_002.re"
115 			{ goto cppcomment; }
116 #line 117 "strip_002.c"
117 		}
118 #line 123 "strip_002.re"
119 
120 comment:
121 		s.tok = s.cur;
122 
123 #line 124 "strip_002.c"
124 		{
125 			YYCTYPE yych;
126 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
127 			yych = *YYCURSOR;
128 			if (yych == '*') goto yy13;
129 			++YYCURSOR;
130 yy12:
131 #line 128 "strip_002.re"
132 			{ goto comment; }
133 #line 134 "strip_002.c"
134 yy13:
135 			yych = *++YYCURSOR;
136 			if (yych != '/') goto yy12;
137 			++YYCURSOR;
138 #line 127 "strip_002.re"
139 			{ goto commentws; }
140 #line 141 "strip_002.c"
141 		}
142 #line 129 "strip_002.re"
143 
144 commentws:
145 		s.tok = s.cur;
146 
147 #line 148 "strip_002.c"
148 		{
149 			YYCTYPE yych;
150 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
151 			yych = *YYCURSOR;
152 			if (yych <= '\f') {
153 				if (yych <= 0x08) goto yy18;
154 				if (yych <= '\t') goto yy20;
155 				if (yych <= '\n') goto yy22;
156 			} else {
157 				if (yych <= '\r') goto yy24;
158 				if (yych == ' ') goto yy20;
159 			}
160 yy18:
161 			++YYCURSOR;
162 #line 135 "strip_002.re"
163 			{ echo(&s); continue; }
164 #line 165 "strip_002.c"
165 yy20:
166 			++YYCURSOR;
167 yy21:
168 #line 134 "strip_002.re"
169 			{ goto commentws; }
170 #line 171 "strip_002.c"
171 yy22:
172 			++YYCURSOR;
173 #line 133 "strip_002.re"
174 			{ echo(&s); continue; }
175 #line 176 "strip_002.c"
176 yy24:
177 			yych = *++YYCURSOR;
178 			if (yych == '\n') goto yy22;
179 			goto yy21;
180 		}
181 #line 136 "strip_002.re"
182 
183 cppcomment:
184 		s.tok = s.cur;
185 
186 #line 187 "strip_002.c"
187 		{
188 			YYCTYPE yych;
189 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
190 			yych = *YYCURSOR;
191 			if (yych == '\n') goto yy29;
192 			if (yych == '\r') goto yy31;
193 			++YYCURSOR;
194 yy28:
195 #line 141 "strip_002.re"
196 			{ goto cppcomment; }
197 #line 198 "strip_002.c"
198 yy29:
199 			++YYCURSOR;
200 #line 140 "strip_002.re"
201 			{ echo(&s); continue; }
202 #line 203 "strip_002.c"
203 yy31:
204 			yych = *++YYCURSOR;
205 			if (yych == '\n') goto yy29;
206 			goto yy28;
207 		}
208 #line 142 "strip_002.re"
209 
210 	}
211 
212 	if (fp != stdin)
213 	{
214 		fclose(fp); /* close only if not stdin */
215 	}
216 	return res; /* return result */
217 }
218 
main(int argc,char ** argv)219 int main(int argc, char **argv)
220 {
221 	if (argc > 1)
222 	{
223 		return scan(!strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"));
224 	}
225 	else
226 	{
227 		fprintf(stderr, "%s <expr>\n", argv[0]);
228 		return 1;
229 	}
230 }
231