summaryrefslogtreecommitdiff
path: root/test/tokstart1.rl
blob: e8c1552b296e18b630d4f2d3c5d9bd51ea86f9ad (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
/*
 * @LANG: c++
 */

#include <iostream>
#include <string.h>
using namespace std;

extern char buf[];

struct Scanner
{
	int cs, act;
	char *ts, *te;

	// Initialize the machine. Invokes any init statement blocks. Returns 0
	// if the machine begins in a non-accepting state and 1 if the machine
	// begins in an accepting state.
	void init( );

	// Execute the machine on a block of data. Returns -1 if after processing
	// the data, the machine is in the error state and can never accept, 0 if
	// the machine is in a non-accepting state and 1 if the machine is in an
	// accepting state.
	int execute( char *data, int len );

	// Indicate that there is no more data. Returns -1 if the machine finishes
	// in the error state and does not accept, 0 if the machine finishes
	// in any other non-accepting state and 1 if the machine finishes in an
	// accepting state.
	int finish( );
};

%%{
	machine Scanner;

	action to_act { 
		cout << "to:   fc = ";
		if ( fc == '\'' )
			cout << (int)fc;
		else
			cout << fc;
		cout << " ts = " << ( ts == 0 ? -1 : ts-buf ) << endl;
	} 
	action from_act {
		cout << "from: fc = ";
		if ( fc == '\'' )
			cout << (int)fc;
		else
			cout << fc;
		cout << " ts = " << ( ts == 0 ? -1 : ts-buf ) << endl;
	}

	c_comm := ( any* $0 '*/' @1 @{ fgoto main; } ) $~to_act $*from_act;
	cxx_comm := ( any* $0 '\n' @1 @{ fgoto main; } ) $~to_act $*from_act;

	main := |*

	# Single and double literals.
	( 'L'? "'" ( [^'\\\n] | /\\./ )* "'" ) $~ to_act $* from_act;
	( 'L'? '"' ( [^"\\\n] | /\\./ )* '"' ) $~ to_act $* from_act;

	# Identifiers
	( [a-zA-Z_] [a-zA-Z0-9_]* ) $~ to_act $* from_act;

	# Floating literals.
	fract_const = digit* '.' digit+ | digit+ '.';
	exponent = [eE] [+\-]? digit+;
	float_suffix = [flFL];

	( fract_const exponent? float_suffix? |
		digit+ exponent float_suffix? ) $~ to_act $* from_act;
	
	# Integer decimal. Leading part buffered by float.
	( ( '0' | [1-9] [0-9]* ) [ulUL]{0,3} ) $~ to_act $* from_act;

	# Integer octal. Leading part buffered by float.
	( '0' [0-9]+ [ulUL]{0,2} ) $~ to_act $* from_act;

	# Integer hex. Leading 0 buffered by float.
	( '0x' [0-9a-fA-F]+ [ulUL]{0,2} ) $~ to_act $* from_act;

	# Three char compounds, first item already buffered. */
	( '...' ) $~ to_act $* from_act;

	# Single char symbols.
	( punct - [_"'] ) $~ to_act $* from_act;

	# Comments and whitespace.
	( '/*' ) $~ to_act $* from_act { fgoto c_comm; };
	( '//' ) $~ to_act $* from_act { fgoto cxx_comm; };

	( any - 33..126 )+ $~ to_act $* from_act;

	*|;
}%%

%% write data;

void Scanner::init( )
{
	%% write init;
}

int Scanner::execute( char *data, int len )
{
	char *p = data;
	char *pe = data + len;
	char *eof = pe;

	%% write exec;

	return 0;
}

int Scanner::finish( )
{
	if ( cs == Scanner_error )
		return -1;
	if ( cs >= Scanner_first_final )
		return 1;
	return 0;
}

void test( )
{
	int len = strlen( buf );
	Scanner scanner;

	scanner.init();
	scanner.execute( buf, len );
	if ( scanner.cs == Scanner_error ) {
		/* Machine failed before finding a token. */
		cout << "PARSE ERROR" << endl;
	}
	scanner.finish();
}

char buf[4096];

int main()
{
	strcpy( buf, 
		"a b 0.98 /*\n"
		"9 */'\\''//hi\n"
		"there\n"
	);
	test();
	return 0;
}

#ifdef _____OUTPUT_____
from: fc = a ts = 0
to:   fc = a ts = 0
from: fc =   ts = 0
to:   fc = a ts = -1
from: fc =   ts = 1
to:   fc =   ts = 1
from: fc = b ts = 1
to:   fc =   ts = -1
from: fc = b ts = 2
to:   fc = b ts = 2
from: fc =   ts = 2
to:   fc = b ts = -1
from: fc =   ts = 3
to:   fc =   ts = 3
from: fc = 0 ts = 3
to:   fc =   ts = -1
from: fc = 0 ts = 4
to:   fc = 0 ts = 4
from: fc = . ts = 4
to:   fc = . ts = 4
from: fc = 9 ts = 4
to:   fc = 9 ts = 4
from: fc = 8 ts = 4
to:   fc = 8 ts = 4
from: fc =   ts = 4
to:   fc = 8 ts = -1
from: fc =   ts = 8
to:   fc =   ts = 8
from: fc = / ts = 8
to:   fc =   ts = -1
from: fc = / ts = 9
to:   fc = / ts = 9
from: fc = * ts = 9
to:   fc = * ts = -1
from: fc = 
 ts = -1
to:   fc = 
 ts = -1
from: fc = 9 ts = -1
to:   fc = 9 ts = -1
from: fc =   ts = -1
to:   fc =   ts = -1
from: fc = * ts = -1
to:   fc = * ts = -1
from: fc = / ts = -1
to:   fc = / ts = -1
from: fc = 39 ts = 16
to:   fc = 39 ts = 16
from: fc = \ ts = 16
to:   fc = \ ts = 16
from: fc = 39 ts = 16
to:   fc = 39 ts = 16
from: fc = 39 ts = 16
to:   fc = 39 ts = -1
from: fc = / ts = 20
to:   fc = / ts = 20
from: fc = / ts = 20
to:   fc = / ts = -1
from: fc = h ts = -1
to:   fc = h ts = -1
from: fc = i ts = -1
to:   fc = i ts = -1
from: fc = 
 ts = -1
to:   fc = 
 ts = -1
from: fc = t ts = 25
to:   fc = t ts = 25
from: fc = h ts = 25
to:   fc = h ts = 25
from: fc = e ts = 25
to:   fc = e ts = 25
from: fc = r ts = 25
to:   fc = r ts = 25
from: fc = e ts = 25
to:   fc = e ts = 25
from: fc = 
 ts = 25
to:   fc = e ts = -1
from: fc = 
 ts = 30
to:   fc = 
 ts = 30
to:   fc = 
 ts = -1
#endif