summaryrefslogtreecommitdiff
path: root/ragel-repos/examples/cppscan.rec
blob: 43f297d8b383eb03db62d37eaef2fc36c468e17f (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TK_Dlit 256
#define TK_Slit 257
#define TK_Float 258
#define TK_Id 259
#define TK_NameSep 260
#define TK_Arrow 261
#define TK_PlusPlus 262
#define TK_MinusMinus 263
#define TK_ArrowStar 264
#define TK_DotStar 265
#define TK_ShiftLeft 266
#define TK_ShiftRight 267
#define TK_IntegerDecimal 268
#define TK_IntegerOctal 269
#define TK_IntegerHex 270
#define TK_EqualsEquals 271
#define TK_NotEquals 272
#define TK_AndAnd 273
#define TK_OrOr 274
#define TK_MultAssign 275
#define TK_DivAssign 276
#define TK_PercentAssign 277
#define TK_PlusAssign 278
#define TK_MinusAssign 279
#define TK_AmpAssign 280
#define TK_CaretAssign 281
#define TK_BarAssign 282
#define TK_DotDotDot 283
#define TK_Whitespace 284
#define TK_Comment 285

int line = 1, col = 1;

void token( int tok, char *data, int len )
{
	printf( "<%i> ", tok );
	for ( int i = 0; i < len; i++ )
		fputc( data[i], stdout );
	fputc( '\n', stdout );

	/* Count newlines and columns. This code is here mainly for having some
	 * code in the token routine when commenting out the above output during
	 * performance testing. */
	for ( int i = 0; i < len; i ++ ) {
		if ( data[i] == '\n' ) {
			line += 1;
			col = 1;
		}
		else {
			col += 1;
		}
	}
}

#define BUFSIZE 8192
char buf[BUFSIZE];

void fill( int n )
{
	printf("fill(%i)\n", n);
	exit(1);
}

int main()
{
	char *start, *p = buf, *lim = buf, *marker;
	int len, have, want, shift;
	int done = 0;
	
#define YYCTYPE         char

#define YYCURSOR        p
#define YYLIMIT         lim
#define YYMARKER        marker

#define YYFILL(n)      { \
		if ( ! done ) { \
			have = lim-start; \
			if ( start > buf ) { \
				shift = start-buf; \
				memmove( buf, start, have ); \
				start -= shift; \
				p -= shift; \
				lim -= shift; \
				marker -= shift; \
			} \
			want = BUFSIZE - have - 1; \
			len = fread( lim, 1, want, stdin ); \
			lim += len; \
			if ( len < want ) { \
				*lim++ = 0; \
				done = 1; \
			} \
		} \
	}

again:
	start = p;

/*!re2c

ANY				= [\000-\377];
FRACTCONST		= ( [0-9]* "." [0-9]+ ) | [0-9]+ ".";
EXPONENT		= [eE] [+\-]? [0-9]+;
FLOATSUFFIX		= [flFL];

	"L"? "\'" ( ANY \ [\'\\\n] | "\\" ANY )* "\'" {
		token( TK_Slit, start, p-start );
		goto again;
	}

	"L"? "\"" ( ANY \ [\"\\\n] | "\\" ANY )* "\"" {
		token( TK_Dlit, start, p-start );
		goto again;
	}

	[a-zA-Z_][a-zA-Z0-9_]* {
		token( TK_Id, start, p-start );
		goto again;
	}

	( FRACTCONST EXPONENT? FLOATSUFFIX? ) | ( [0-9]+ EXPONENT FLOATSUFFIX? ) {
		token( TK_Float, start, p-start );
		goto again;
	}


	( "0" | [1-9][0-9]* ) [ulUL]* {
		token( TK_IntegerDecimal, start, p-start );
		goto again;
	}

	"0" [0-9]+ [ulUL]* {
		token( TK_IntegerOctal, start, p-start );
		goto again;
	}

	"0x" [0-9a-fA-F]+[ulUL]* {
		token( TK_IntegerHex, start, p-start );
		goto again;
	}

	"::"	{ token( TK_NameSep, start, p-start ); goto again; }
	"=="	{ token( TK_EqualsEquals, start, p-start ); goto again; }
	"!="	{ token( TK_NotEquals, start, p-start ); goto again; }
	"&&"	{ token( TK_AndAnd, start, p-start ); goto again; }
	"||"	{ token( TK_OrOr, start, p-start ); goto again; }
	"*="	{ token( TK_MultAssign, start, p-start ); goto again; }
	"/="	{ token( TK_DivAssign, start, p-start ); goto again; }
	"%="	{ token( TK_PercentAssign, start, p-start ); goto again; }
	"+="	{ token( TK_PlusAssign, start, p-start ); goto again; }
	"-="	{ token( TK_MinusAssign, start, p-start ); goto again; }
	"&="	{ token( TK_AmpAssign, start, p-start ); goto again; }
	"^="	{ token( TK_CaretAssign, start, p-start ); goto again; }
	"|="	{ token( TK_BarAssign, start, p-start ); goto again; }
	"++"	{ token( TK_PlusPlus, start, p-start ); goto again; }
	"--"	{ token( TK_MinusMinus, start, p-start ); goto again; }
	"->"	{ token( TK_Arrow, start, p-start ); goto again; }
	"->*"	{ token( TK_ArrowStar, start, p-start ); goto again; }
	".*"	{ token( TK_DotStar, start, p-start ); goto again; }
	"..."	{ token( TK_DotDotDot, start, p-start ); goto again; }

	"/*" { goto comment; }
	"//" (ANY\"\n")* "\n"	{ goto again; }
	[\001-\040\177]+		{ goto again; }

	[\041-\057\072-\100\133-\140\173-\176]	{
		token( *start, start, p-start );
		goto again;
	}
	"\000"					{ return 0; }
*/

comment:
/*!re2c
	"*/"					{ goto again; }
	ANY						{ goto comment; }
*/
}