annotatec/lang.l -rw-r--r-- 7.7 KiB View raw
                                                                                
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
O   [0-7]
D   [0-9]
NZ  [1-9]
L   [a-zA-Z_]
A   [a-zA-Z_0-9]
H   [a-fA-F0-9]
HP  (0[xX])
E   ([Ee][+-]?{D}+)
P   ([Pp][+-]?{D}+)
FS  (f|F|l|L)
IS  (((u|U)(l|L|ll|LL)?)|((l|L|ll|LL)(u|U)?))
CP  (u|U|L)
SP  (u8|u|U|L)
ES  (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
WS  [ \t\v\n\f]

%{
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "parse.h"
#include "lang.tab.h"

static int lineno = 1, colno = 1;
char *filename = "";
const char *original_filename = "";
struct source_file *current_source;

int yyerror(char *s);
static void _lineno(void);
static void _lineno_c(char c);

static void linemarker(void);
static int check_type(void);
static void multi_comment(void);

void yyrestart(FILE *);

struct source_file *lexreset(const char *fn, FILE *input) {
	lineno = 1, colno = 1;
	original_filename = fn;
	filename = strdup(original_filename);
	current_source = calloc(1, sizeof(struct source_file));
	current_source->path = strdup(fn);
	yyrestart(input);
	return current_source;
}

%}

%%

"/*"   { multi_comment(); }
"//".* { _lineno(); }
"#".*  { _lineno(); linemarker(); }

"break"				{ _lineno(); return CZ_BREAK; }
"case" 				{ _lineno(); return CZ_CASE; }
"continue"			{ _lineno(); return CZ_CONTINUE; }
"default"			{ _lineno(); return CZ_DEFAULT; }
"do"				{ _lineno(); return CZ_DO; }
"else"				{ _lineno(); return CZ_ELSE; }
"for"				{ _lineno(); return CZ_FOR; }
"goto"				{ _lineno(); return CZ_GOTO; }
"if"				{ _lineno(); return CZ_IF; }
"return"			{ _lineno(); return CZ_RETURN; }
"switch"			{ _lineno(); return CZ_SWITCH; }
"while"				{ _lineno(); return CZ_WHILE; }
"char"				{ _lineno(); return CZ_CHAR; }
"enum"				{ _lineno(); return CZ_ENUM; }
"double"			{ _lineno(); return CZ_DOUBLE; }
"float"				{ _lineno(); return CZ_FLOAT; }
"int"				{ _lineno(); return CZ_INT; }
"long"				{ _lineno(); return CZ_LONG; }
"short"				{ _lineno(); return CZ_SHORT; }
"struct"			{ _lineno(); return CZ_STRUCT; }
"typedef"			{ _lineno(); return CZ_TYPEDEF; }
"union"				{ _lineno(); return CZ_UNION; }
"void"				{ _lineno(); return CZ_VOID; }
"auto"				{ _lineno(); return CZ_AUTO; }
"const"				{ _lineno(); return CZ_CONST; }
"extern"			{ _lineno(); return CZ_EXTERN; }
"inline"			{ _lineno(); return CZ_INLINE; }
"register"			{ _lineno(); return CZ_REGISTER; }
"restrict"			{ _lineno(); return CZ_RESTRICT; }
"signed"			{ _lineno(); return CZ_SIGNED; }
"static"			{ _lineno(); return CZ_STATIC; }
"unsigned"			{ _lineno(); return CZ_UNSIGNED; }
"volatile"			{ _lineno(); return CZ_VOLATILE; }
"sizeof"			{ _lineno(); return CZ_SIZEOF; }
"_Alignas" 			{ _lineno(); return CZ_ALIGNAS; }
"_Alignof" 			{ _lineno(); return CZ_ALIGNOF; }
"_Atomic" 			{ _lineno(); return CZ_ATOMIC; }
"_Bool" 			{ _lineno(); return CZ_BOOL; }
"_Complex" 			{ _lineno(); return CZ_COMPLEX; }
"_Generic" 			{ _lineno(); return CZ_GENERIC; }
"_Imaginary" 		{ _lineno(); return CZ_IMAGINARY; }
"_Noreturn" 		{ _lineno(); return CZ_NORETURN; }
"_Static_assert" 	{ _lineno(); return CZ_STATIC_ASSERT; }
"_Thread_local" 	{ _lineno(); return CZ_THREAD_LOCAL; }
"__builtin_va_list"	{ _lineno(); return CZ_BUILTIN_VA_LIST; }
"__builtin_va_arg"	{ _lineno(); return CZ_BUILTIN_VA_ARG; }

"__restrict"		{ _lineno(); return CZ_RESTRICT; } /* GNU extensions */
"__inline"			{ _lineno(); return CZ_INLINE; }
"__alignof__"		{ _lineno(); return CZ_ALIGNOF; }
"__attribute__"		{ _lineno(); return CZ_GNU_ATTRIBUTE; }
"__extension__"		{ _lineno(); }
"__signed__"		{ _lineno(); return CZ_SIGNED; }
"__volatile__"		{ _lineno(); return CZ_VOLATILE; }
"asm"				{ _lineno(); return CZ_GNU_ASM; }
"__asm__"			{ _lineno(); return CZ_GNU_ASM; }
"_Float128"			{ _lineno(); return CZ_FLOAT; }
"_float128"			{ _lineno(); return CZ_FLOAT; }
"_Float80"			{ _lineno(); return CZ_FLOAT; }
"_float80"			{ _lineno(); return CZ_FLOAT; }
"_Float64x"			{ _lineno(); return CZ_DOUBLE; }
"_float64x"			{ _lineno(); return CZ_DOUBLE; }
"_ibm128"			{ _lineno(); return CZ_DOUBLE; }

{L}{A}*				{
	yylval.strloc.name = strdup(yytext);
	yylval.strloc.lineno = lineno;
	yylval.strloc.colno = colno;
	_lineno();
	return check_type();
}

{HP}{H}+{IS}?				{ _lineno(); return CZ_CONSTANT; }
{NZ}{D}*{IS}?				{ _lineno(); return CZ_CONSTANT; }
"0"{O}*{IS}?				{ _lineno(); return CZ_CONSTANT; }
{CP}?"'"([^'\\\n]|{ES})+"'"	{ _lineno(); return CZ_CONSTANT; }

{D}+{E}{FS}?				{ _lineno(); return CZ_CONSTANT; }
{D}*"."{D}+{E}?{FS}?		{ _lineno(); return CZ_CONSTANT; }
{D}+"."{E}?{FS}?			{ _lineno(); return CZ_CONSTANT; }
{HP}{H}+{P}{FS}?			{ _lineno(); return CZ_CONSTANT; }
{HP}{H}*"."{H}+{P}{FS}?		{ _lineno(); return CZ_CONSTANT; }
{HP}{H}+"."{P}{FS}?			{ _lineno(); return CZ_CONSTANT; }

({SP}?\"([^"\\\n]|{ES})*\"{WS}*)+	{
	_lineno();
	yylval.sval = yytext;
	return CZ_STRING_LITERAL;
}

"..."		{ _lineno(); return CZ_ELLIPSIS; }
">>="		{ _lineno(); return CZ_RSHIFT_ASSIGN; }
"<<="		{ _lineno(); return CZ_LSHIFT_ASSIGN; }
"+="		{ _lineno(); return CZ_ADD_ASSIGN; }
"-="		{ _lineno(); return CZ_SUB_ASSIGN; }
"*="		{ _lineno(); return CZ_MUL_ASSIGN; }
"/="		{ _lineno(); return CZ_DIV_ASSIGN; }
"%="		{ _lineno(); return CZ_MOD_ASSIGN; }
"&="		{ _lineno(); return CZ_BAND_ASSIGN; }
"^="		{ _lineno(); return CZ_BXOR_ASSIGN; }
"|="		{ _lineno(); return CZ_BOR_ASSIGN; }
">>"		{ _lineno(); return CZ_RSHIFT; }
"<<"		{ _lineno(); return CZ_LSHIFT; }
"++"		{ _lineno(); return CZ_INCREMENT; }
"--"		{ _lineno(); return CZ_DECREMENT; }
"->"		{ _lineno(); return CZ_PTR_DEREF; }
"&&"		{ _lineno(); return CZ_LAND; }
"||"		{ _lineno(); return CZ_LOR; }
"<="		{ _lineno(); return CZ_LESS_EQL; }
">="		{ _lineno(); return CZ_GTR_EQL; }
"=="		{ _lineno(); return CZ_EQUAL; }
"!="		{ _lineno(); return CZ_NEQUAL; }
";"			{ _lineno(); return ';'; }
("{"|"<%")	{ _lineno(); return '{'; }
("}"|"%>")	{ _lineno(); return '}'; }
","			{ _lineno(); return ','; }
":"			{ _lineno(); return ':'; }
"="			{ _lineno(); return '='; }
"("			{ _lineno(); return '('; }
")"			{ _lineno(); return ')'; }
("["|"<:")	{ _lineno(); return '['; }
("]"|":>")	{ _lineno(); return ']'; }
"."			{ _lineno(); return '.'; }
"&"			{ _lineno(); return '&'; }
"!"			{ _lineno(); return '!'; }
"~"			{ _lineno(); return '~'; }
"-"			{ _lineno(); return '-'; }
"+"			{ _lineno(); return '+'; }
"*"			{ _lineno(); return '*'; }
"/"			{ _lineno(); return '/'; }
"%"			{ _lineno(); return '%'; }
"<"			{ _lineno(); return '<'; }
">"			{ _lineno(); return '>'; }
"^"			{ _lineno(); return '^'; }
"|"			{ _lineno(); return '|'; }
"?"			{ _lineno(); return '?'; }

{WS}+		{ _lineno(); /* whitespace separates tokens */ }
.			{ _lineno(); /* discard bad characters */ }

%%

static void _lineno_c(char c) {
	switch (c) {
	case '\n':
		colno = 1;
		++lineno;
		break;
	case '\t':
		colno += 8 - (colno % 8);
		break;
	default:
		++colno;
		break;
	}
}

static void _lineno(void) {
	for (const char *p = yytext; *p; ++p) {
		_lineno_c(*p);
	}
}

bool preprocessed_linenos = false;

static void linemarker(void) {
	char *endptr;
	if (preprocessed_linenos) {
		strtol(&yytext[1], &endptr, 10);
	} else {
		lineno = strtol(&yytext[1], &endptr, 10) - 1;
	}
	while (*endptr && *endptr++ != '"') {
		/* This space deliberately left blank */
	}
	free(filename);
	filename = strdup(endptr);
	endptr = strchr(filename, '"');
	if (endptr) {
		*endptr = '\0';
	}
}

static int check_type(void) {
	struct typedef_declaration *td = current_source->typedefs;
	while (td) {
		if (strcmp(td->name, yytext) == 0) {
			return TYPEDEF_NAME;
		}
		td = td->next;
	}

	return CZ_IDENTIFIER;
}

static void multi_comment(void) {
	int c;
	while ((c = input()) != 0) {
		_lineno_c(c);
		if (c == '*') {
			while ((c = input()) == '*') {
				_lineno_c(c);
			}
			if (c == '/') {
				return;
			}
			if (c == 0) {
				break;
			}
		}
	}
	yyerror("unterminated comment");
}

int yywrap(void) {
    return 1;
}

int yyerror(char *err) {
	fprintf(stderr, "Error %s:%d:%d: %s\n", filename, lineno, colno, err);
	return 0;
}