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