summaryrefslogtreecommitdiff
path: root/examples/pullscan.c
blob: a72ca2f2af98879c7d51cd8133e7807d80af6770 (plain)
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
292
293
294

#line 1 "pullscan.rl"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 4096

typedef struct _Scanner {
	/* Scanner state. */
    int cs;
    int act;
    int have;
    int curline;
    char *ts;
    char *te;
    char *p;
    char *pe;
    char *eof;
	FILE *file;
	int done;

	/* Token data */
	char *data;
	int len;
    int value;

	char buf[BUFSIZE];
} Scanner;



#line 34 "pullscan.c"
static const int Scanner_start = 2;
static const int Scanner_first_final = 2;
static const int Scanner_error = -1;

static const int Scanner_en_main = 2;


#line 33 "pullscan.rl"


void scan_init( Scanner *s, FILE *file )
{
	memset (s, '\0', sizeof(Scanner));
	s->curline = 1;
	s->file = file;
	s->eof = 0;
	
#line 52 "pullscan.c"
	{
	 s->cs = Scanner_start;
	 s->ts = 0;
	 s->te = 0;
	 s->act = 0;
	}

#line 42 "pullscan.rl"
}

#define TK_NO_TOKEN (-1)
#define TK_ERR 128
#define TK_EOF 129
#define TK_Identifier 130
#define TK_Number 131
#define TK_String 132

#define ret_tok( _tok ) token = _tok; s->data = s->ts

int scan( Scanner *s )
{
	int token = TK_NO_TOKEN;
	int space, readlen;

	while ( 1 ) {
		if ( s->p == s->pe ) {
			printf("scanner: need more data\n");

			if ( s->ts == 0 )
				s->have = 0;
			else {
				/* There is data that needs to be shifted over. */
				printf("scanner: buffer broken mid token\n");
				s->have = s->pe - s->ts;
				memmove( s->buf, s->ts, s->have );
				s->te -= (s->ts-s->buf);
				s->ts = s->buf;
			}

			s->p = s->buf + s->have;
			space = BUFSIZE - s->have;

			if ( space == 0 ) {
				/* We filled up the buffer trying to scan a token. */
				printf("scanner: out of buffer space\n");
				return TK_ERR;
			}

			if ( s->done ) {
				printf("scanner: end of file\n");
				s->p[0] = 0;
				readlen = 1;
			}
			else {
				readlen = fread( s->p, 1, space, s->file );
				if ( readlen < space )
					s->done = 1;
			}

			s->pe = s->p + readlen;
		}

		
#line 116 "pullscan.c"
	{
	if ( ( s->p) == ( s->pe) )
		goto _test_eof;
	switch (  s->cs )
	{
tr0:
#line 125 "pullscan.rl"
	{{( s->p) = (( s->te))-1;}{ ret_tok( *s->p ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr2:
#line 113 "pullscan.rl"
	{ s->te = ( s->p)+1;{ ret_tok( TK_String ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr4:
#line 125 "pullscan.rl"
	{ s->te = ( s->p)+1;{ ret_tok( *s->p ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr5:
#line 121 "pullscan.rl"
	{ s->te = ( s->p)+1;{ ret_tok( TK_EOF ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr6:
#line 110 "pullscan.rl"
	{ s->te = ( s->p)+1;}
	goto st2;
tr10:
#line 125 "pullscan.rl"
	{ s->te = ( s->p);( s->p)--;{ ret_tok( *s->p ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr11:
#line 117 "pullscan.rl"
	{ s->te = ( s->p);( s->p)--;{ ret_tok( TK_Number ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
tr12:
#line 107 "pullscan.rl"
	{ s->te = ( s->p);( s->p)--;{ ret_tok( TK_Identifier ); {( s->p)++;  s->cs = 2; goto _out;} }}
	goto st2;
st2:
#line 1 "NONE"
	{ s->ts = 0;}
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof2;
case 2:
#line 1 "NONE"
	{ s->ts = ( s->p);}
#line 162 "pullscan.c"
	switch( (*( s->p)) ) {
		case 0: goto tr5;
		case 32: goto tr6;
		case 34: goto tr7;
		case 95: goto st5;
	}
	if ( (*( s->p)) < 48 ) {
		if ( 9 <= (*( s->p)) && (*( s->p)) <= 10 )
			goto tr6;
	} else if ( (*( s->p)) > 57 ) {
		if ( (*( s->p)) > 90 ) {
			if ( 97 <= (*( s->p)) && (*( s->p)) <= 122 )
				goto st5;
		} else if ( (*( s->p)) >= 65 )
			goto st5;
	} else
		goto st4;
	goto tr4;
tr7:
#line 1 "NONE"
	{ s->te = ( s->p)+1;}
	goto st3;
st3:
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof3;
case 3:
#line 189 "pullscan.c"
	switch( (*( s->p)) ) {
		case 34: goto tr2;
		case 92: goto st1;
	}
	goto st0;
st0:
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof0;
case 0:
	switch( (*( s->p)) ) {
		case 34: goto tr2;
		case 92: goto st1;
	}
	goto st0;
st1:
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof1;
case 1:
	goto st0;
st4:
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof4;
case 4:
	if ( 48 <= (*( s->p)) && (*( s->p)) <= 57 )
		goto st4;
	goto tr11;
st5:
	if ( ++( s->p) == ( s->pe) )
		goto _test_eof5;
case 5:
	if ( (*( s->p)) == 95 )
		goto st5;
	if ( (*( s->p)) < 65 ) {
		if ( 48 <= (*( s->p)) && (*( s->p)) <= 57 )
			goto st5;
	} else if ( (*( s->p)) > 90 ) {
		if ( 97 <= (*( s->p)) && (*( s->p)) <= 122 )
			goto st5;
	} else
		goto st5;
	goto tr12;
	}
	_test_eof2:  s->cs = 2; goto _test_eof; 
	_test_eof3:  s->cs = 3; goto _test_eof; 
	_test_eof0:  s->cs = 0; goto _test_eof; 
	_test_eof1:  s->cs = 1; goto _test_eof; 
	_test_eof4:  s->cs = 4; goto _test_eof; 
	_test_eof5:  s->cs = 5; goto _test_eof; 

	_test_eof: {}
	if ( ( s->p) == ( s->eof) )
	{
	switch (  s->cs ) {
	case 3: goto tr10;
	case 0: goto tr0;
	case 1: goto tr0;
	case 4: goto tr11;
	case 5: goto tr12;
	}
	}

	_out: {}
	}

#line 130 "pullscan.rl"


		if ( s->cs == Scanner_error )
			return TK_ERR;

		if ( token != TK_NO_TOKEN ) {
			s->len = s->p - s->data;
			return token;
		}
	}
}


int main (int argc, char** argv)
{
	Scanner ss;
	int tok;

	scan_init(&ss, stdin);

	while ( 1 ) {
		tok = scan (&ss);
		if ( tok == TK_EOF ) {
			printf ("parser: EOF\n");
			break;
		}
		else if ( tok == TK_ERR ) {
			printf ("parser: ERR\n");
			break;
		}
		else {
			printf ("parser: %d \"", tok);
			fwrite ( ss.data, 1, ss.len, stdout );
			printf ("\"\n" );
		}
	}

	return 0;
}