summaryrefslogtreecommitdiff
path: root/test/clang1.rl
blob: 85532c6879f4af4ada5c9c8f2f09fdd14385dc42 (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
/*
 * @LANG: c
 * A mini C-like language scanner.
 */

#include <stdio.h>
#include <string.h>
#define IDENT_BUFLEN 256

%%{
	machine clang;

	# Function to buffer a character.
	action bufChar {
		if ( identLen < IDENT_BUFLEN ) {
			identBuf[identLen] = fc;
			identLen += 1;
		}
	}

	# Function to clear the buffer.
	action clearBuf {
		identLen = 0;
	}

	# Functions to dump tokens as they are matched.
	action ident {
		identBuf[identLen] = 0;
		printf("ident(%i): %s\n", curLine, identBuf);
	}
	action literal {
		identBuf[identLen] = 0;
		printf("literal(%i): %s\n", curLine, identBuf);
	}
	action float {
		identBuf[identLen] = 0;
		printf("float(%i): %s\n", curLine, identBuf);
	}
	action int {
		identBuf[identLen] = 0;
		printf("int(%i): %s\n", curLine, identBuf);
	}
	action hex {
		identBuf[identLen] = 0;
		printf("hex(%i): 0x%s\n", curLine, identBuf);
	}
	action symbol {
		identBuf[identLen] = 0;
		printf("symbol(%i): %s\n", curLine, identBuf);
	}

	# Alpha numberic characters or underscore.
	alnumu = alnum | '_';

	# Alpha charactres or underscore.
	alphau = alpha | '_';

	# Symbols. Upon entering clear the buffer. On all transitions
	# buffer a character. Upon leaving dump the symbol.
	symbol = ( punct - [_'"] ) >clearBuf $bufChar %symbol;

	# Identifier. Upon entering clear the buffer. On all transitions
	# buffer a character. Upon leaving, dump the identifier.
	ident = (alphau . alnumu*) >clearBuf $bufChar %ident;

	# Match single characters inside literal strings. Or match 
	# an escape sequence. Buffers the charater matched.
	sliteralChar =
			( extend - ['\\] ) @bufChar |
			( '\\' . extend @bufChar );
	dliteralChar =
			( extend - ["\\] ) @bufChar |
			( '\\' . extend @bufChar );

	# Single quote and double quota literals. At the start clear
	# the buffer. Upon leaving dump the literal.
	sliteral = ('\'' @clearBuf . sliteralChar* . '\'' ) %literal;
	dliteral = ('"' @clearBuf . dliteralChar* . '"' ) %literal;
	literal = sliteral | dliteral;

	# Whitespace is standard ws, newlines and control codes.
	whitespace = any - 0x21..0x7e;

	# Describe both c style comments and c++ style comments. The
	# priority bump on tne terminator of the comments brings us
	# out of the extend* which matches everything.
	ccComment = '//' . extend* $0 . '\n' @1;
	cComment = '/*' . extend* $0 . '*/' @1;

	# Match an integer. We don't bother clearing the buf or filling it.
	# The float machine overlaps with int and it will do it.
	int = digit+ %int;

	# Match a float. Upon entering the machine clear the buf, buffer
	# characters on every trans and dump the float upon leaving.
	float =  ( digit+ . '.' . digit+ ) >clearBuf $bufChar %float;

	# Match a hex. Upon entering the hex part, clear the buf, buffer characters
	# on every trans and dump the hex on leaving transitions.
	hex = '0x' . xdigit+ >clearBuf $bufChar %hex;

	# Or together all the lanuage elements.
	fin = ( ccComment |
		cComment |
		symbol |
		ident |
		literal |
		whitespace |
		int |
		float |
		hex );

	# Star the language elements. It is critical in this type of application
	# that we decrease the priority of out transitions before doing so. This
	# is so that when we see 'aa' we stay in the fin machine to match an ident
	# of length two and not wrap around to the front to match two idents of 
	# length one.
	clang_main = ( fin $1 %0 )*;

	# This machine matches everything, taking note of newlines.
	newline = ( any | '\n' @{ curLine += 1; } )*;

	# The final fsm is the lexer intersected with the newline machine which
	# will count lines for us. Since the newline machine accepts everything,
	# the strings accepted is goverened by the clang_main machine, onto which
	# the newline machine overlays line counting.
	main := clang_main & newline;
}%%

#include <stdio.h>

%% write data noerror;


char data[] = 
	"/*\n"
	" *  Copyright\n"
	" */\n"
	"\n"
	"/*  Aapl.\n"
	" */\n"
	"\n"
	"#define _AAPL_RESIZE_H\n"
	"\n"
	"#include <assert.h>\n"
	"\n"
	"#ifdef AAPL_NAMESPACE\n"
	"namespace Aapl {\n"
	"#endif\n"
	"#define LIN_DEFAULT_STEP 256\n"
	"#define EXPN_UP( existing, needed ) \\\n"
	"		need > eng ? (ned<<1) : eing\n"
	"	\n"
	"\n"
	"/*@}*/\n"
	"#undef EXPN_UP\n"
	"#ifdef AAPL_NAMESPACE\n"
	"#endif /* _AAPL_RESIZE_H */\n";

void test( char *buf )
{
	int len = strlen( buf );
	char *p = buf, *pe = buf + len;
	char *eof = pe;
	char identBuf[IDENT_BUFLEN+1];
	int identLen;
	int curLine;
	int cs;

	identLen = 0;
	curLine = 1;

	%% write init;
	%% write exec;

	if ( cs >= clang_first_final )
		printf("ACCEPT\n");
	else
		printf("FAIL\n");
}

int main()
{
	test( 
		"999 0xaAFF99 99.99 /*\n"
		"*/ 'lksdj' //\n"
		"\"\n"
		"\n"
		"literal\n"
		"\n"
		"\n"
		"\"0x00aba foobardd.ddsf 0x0.9\n" );
	test( 
		"wordwithnum00asdf\n"
		"000wordfollowsnum,makes new symbol\n"
		"\n"
		"finishing early /* unfinished ...\n" );
	test( data );
	return 0;
}

#ifdef _____OUTPUT_____
int(1): 999
hex(1): 0xaAFF99
float(1): 99.99
literal(2): lksdj
literal(8): 

literal



hex(8): 0x00aba
ident(8): foobardd
symbol(8): .
ident(8): ddsf
hex(8): 0x0
symbol(8): .
int(8): 9
ACCEPT
ident(1): wordwithnum00asdf
int(2): 000
ident(2): wordfollowsnum
symbol(2): ,
ident(2): makes
ident(2): new
ident(2): symbol
ident(4): finishing
ident(4): early
FAIL
symbol(8): #
ident(8): define
ident(8): _AAPL_RESIZE_H
symbol(10): #
ident(10): include
symbol(10): <
ident(10): assert
symbol(10): .
ident(10): h
symbol(10): >
symbol(12): #
ident(12): ifdef
ident(12): AAPL_NAMESPACE
ident(13): namespace
ident(13): Aapl
symbol(13): {
symbol(14): #
ident(14): endif
symbol(15): #
ident(15): define
ident(15): LIN_DEFAULT_STEP
int(15): 256
symbol(16): #
ident(16): define
ident(16): EXPN_UP
symbol(16): (
ident(16): existing
symbol(16): ,
ident(16): needed
symbol(16): )
symbol(16): \
ident(17): need
symbol(17): >
ident(17): eng
symbol(17): ?
symbol(17): (
ident(17): ned
symbol(17): <
symbol(17): <
int(17): 1
symbol(17): )
symbol(17): :
ident(17): eing
symbol(21): #
ident(21): undef
ident(21): EXPN_UP
symbol(22): #
ident(22): ifdef
ident(22): AAPL_NAMESPACE
symbol(23): #
ident(23): endif
ACCEPT
#endif