File: strip_003.c

package info (click to toggle)
re2c 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 50,052 kB
  • sloc: cpp: 32,477; ml: 8,279; sh: 5,265; makefile: 968; haskell: 612; python: 428; ansic: 227; javascript: 111; java: 3
file content (291 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Generated by re2c */
#line 1 "strip_003.re"
// re2c $INPUT -o $OUTPUT -b
/* re2c lesson 002_strip_comments, strip_003, (c) M. Boerger 2006 - 2007 */
#line 37 "strip_003.re"


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#line 13 "strip_003.c"
#define YYMAXFILL 4
#line 43 "strip_003.re"

#define	BSIZE	128

#if BSIZE < YYMAXFILL
# error BSIZE must be greater YYMAXFILL
#endif

#define	YYCTYPE		unsigned char
#define	YYCURSOR	s.cur
#define	YYLIMIT		s.lim
#define YYMARKER	s.mrk
#define YYCTXMARKER s.ctx
#define	YYFILL(n)	{ if ((res = fill(&s, n)) >= 0) break; }

typedef struct Scanner
{
	FILE			*fp;
	unsigned char	*cur, *tok, *lim, *eof, *ctx, *mrk;
	unsigned char 	buffer[BSIZE];
} Scanner;

int fill(Scanner *s, int len)
{
	if (!len)
	{
		s->cur = s->tok = s->lim = s->mrk = s->buffer;
		s->eof = 0;
	}
	if (!s->eof)
	{
		int got, cnt = s->tok - s->buffer;

		if (cnt > 0)
		{
			memcpy(s->buffer, s->tok, s->lim - s->tok);
			s->tok -= cnt;
			s->cur -= cnt;
			s->lim -= cnt;
			s->mrk -= cnt;
			s->ctx -= cnt;
		}
		cnt = BSIZE - cnt;
		if ((got = fread(s->lim, 1, cnt, s->fp)) != cnt)
		{
			s->eof = &s->lim[got];
		}
		s->lim += got;
	}
	else if (s->cur + len > s->eof)
	{
		return 0; /* not enough input data */
	}
	return -1;
}

void echo(Scanner *s)
{
	fwrite(s->tok, 1, s->cur - s->tok, stdout);
}

int scan(FILE *fp)
{
	int  res = 0;
	int  nlcomment = 0;
    Scanner s;

	if (!fp)
	{
		return 1; /* no file was opened */
	}

    s.fp = fp;
	
	fill(&s, 0);

	for(;;)
	{
		s.tok = s.cur;

#line 95 "strip_003.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
			yych = *YYCURSOR;
			if (yych <= '\f') {
				if (yych == '\n') goto yy2;
			} else {
				if (yych <= '\r') goto yy3;
				if (yych == '/') goto yy4;
			}
			++YYCURSOR;
yy1:
#line 131 "strip_003.re"
			{ fputc(*s.tok, stdout); continue; }
#line 110 "strip_003.c"
yy2:
			yych = *(YYMARKER = ++YYCURSOR);
			if (yych == '/') goto yy5;
			goto yy1;
yy3:
			yych = *(YYMARKER = ++YYCURSOR);
			if (yych == '\n') goto yy7;
			goto yy1;
yy4:
			yych = *++YYCURSOR;
			if (yych == '*') goto yy8;
			if (yych == '/') goto yy9;
			goto yy1;
yy5:
			yych = *++YYCURSOR;
			if (yych == '*') goto yy10;
yy6:
			YYCURSOR = YYMARKER;
			goto yy1;
yy7:
			yych = *++YYCURSOR;
			if (yych == '/') goto yy5;
			goto yy6;
yy8:
			++YYCURSOR;
#line 130 "strip_003.re"
			{ goto comment; }
#line 138 "strip_003.c"
yy9:
			++YYCURSOR;
#line 128 "strip_003.re"
			{ goto cppcomment; }
#line 143 "strip_003.c"
yy10:
			++YYCURSOR;
			YYCURSOR -= 2;
#line 129 "strip_003.re"
			{ echo(&s); nlcomment = 1; continue; }
#line 149 "strip_003.c"
		}
#line 132 "strip_003.re"

comment:
		s.tok = s.cur;

#line 156 "strip_003.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych == '*') goto yy13;
			++YYCURSOR;
yy12:
#line 137 "strip_003.re"
			{ goto comment; }
#line 166 "strip_003.c"
yy13:
			yych = *++YYCURSOR;
			if (yych != '/') goto yy12;
			++YYCURSOR;
#line 136 "strip_003.re"
			{ goto commentws; }
#line 173 "strip_003.c"
		}
#line 138 "strip_003.re"

commentws:
		s.tok = s.cur;

#line 180 "strip_003.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
			yych = *YYCURSOR;
			if (yych <= '\r') {
				if (yych <= '\t') {
					if (yych >= '\t') goto yy16;
				} else {
					if (yych <= '\n') goto yy18;
					if (yych >= '\r') goto yy20;
				}
			} else {
				if (yych <= ' ') {
					if (yych >= ' ') goto yy16;
				} else {
					if (yych == '/') goto yy21;
				}
			}
			++YYCURSOR;
yy15:
#line 152 "strip_003.re"
			{ echo(&s); nlcomment = 0; continue; }
#line 203 "strip_003.c"
yy16:
			++YYCURSOR;
yy17:
#line 151 "strip_003.re"
			{ goto commentws; }
#line 209 "strip_003.c"
yy18:
			yych = *(YYMARKER = ++YYCURSOR);
			if (yych == '/') goto yy22;
yy19:
#line 143 "strip_003.re"
			{
					if (!nlcomment)
					{
						echo(&s);
					}
					nlcomment = 0;
					continue;
				}
#line 223 "strip_003.c"
yy20:
			yych = *++YYCURSOR;
			if (yych == '\n') goto yy18;
			goto yy17;
yy21:
			yych = *++YYCURSOR;
			if (yych == '*') goto yy23;
			goto yy15;
yy22:
			yych = *++YYCURSOR;
			if (yych == '*') goto yy23;
			YYCURSOR = YYMARKER;
			goto yy19;
yy23:
			++YYCURSOR;
#line 142 "strip_003.re"
			{ goto comment; }
#line 241 "strip_003.c"
		}
#line 153 "strip_003.re"

cppcomment:
		s.tok = s.cur;

#line 248 "strip_003.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych == '\n') goto yy26;
			if (yych == '\r') goto yy27;
			++YYCURSOR;
yy25:
#line 158 "strip_003.re"
			{ goto cppcomment; }
#line 259 "strip_003.c"
yy26:
			++YYCURSOR;
#line 157 "strip_003.re"
			{ echo(&s); continue; }
#line 264 "strip_003.c"
yy27:
			yych = *++YYCURSOR;
			if (yych == '\n') goto yy26;
			goto yy25;
		}
#line 159 "strip_003.re"

	}

	if (fp != stdin)
	{
		fclose(fp); /* close only if not stdin */
	}
	return res; /* return result */
}

int main(int argc, char **argv)
{
	if (argc > 1)
	{
		return scan(!strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"));
	}
	else
	{
		fprintf(stderr, "%s <expr>\n", argv[0]);
		return 1;
	}
}