-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.l
162 lines (125 loc) · 5.58 KB
/
Lexer.l
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
%{
#include "Parser.y.h"
void yyerror(char*);
void parseUntil(char escChar, const char *delStr);
int column = 0;
int line = 0;
const char *str = "";
%}
l [a-zA-Z_]
d [0-9]
x [a-fA-F0-9]
e [Ee]-?{d}+
%%
/* keywords */
"struct" { column += yyleng; return STRUCT; }
"if" { column += yyleng; return IF; }
"else" { column += yyleng; return ELSE; }
"while" { column += yyleng; return WHILE; }
"return" { column += yyleng; return RETURN; }
"asm" { column += yyleng; return ASM; }
"{" { column += yyleng; return '{'; }
"}" { column += yyleng; return '}'; }
"(" { column += yyleng; return '('; }
")" { column += yyleng; return ')'; }
"[" { column += yyleng; return '['; }
"]" { column += yyleng; return ']'; }
/* operators */
"*=" { column += yyleng; return ASSIGN_MUL; }
"/=" { column += yyleng; return ASSIGN_DIV; }
"%=" { column += yyleng; return ASSIGN_REM; }
"+=" { column += yyleng; return ASSIGN_ADD; }
"-=" { column += yyleng; return ASSIGN_SUB; }
"<<=" { column += yyleng; return ASSIGN_SHL; }
">>=" { column += yyleng; return ASSIGN_ASHR; }
">>>=" { column += yyleng; return ASSIGN_LSHR; }
"<<>=" { column += yyleng; return ASSIGN_ROL; }
">><=" { column += yyleng; return ASSIGN_ROR; }
"&=" { column += yyleng; return ASSIGN_AND; }
"^=" { column += yyleng; return ASSIGN_XOR; }
"|=" { column += yyleng; return ASSIGN_OR; }
"and=" { column += yyleng; return ASSIGN_BAND; }
"or=" { column += yyleng; return ASSIGN_BOR; }
"xor=" { column += yyleng; return ASSIGN_BXOR; }
"~" { column += yyleng; return '/'; }
"not" { column += yyleng; return NOT; }
"*" { column += yyleng; return '*'; }
"/" { column += yyleng; return '/'; }
"%" { column += yyleng; return '%'; }
"+" { column += yyleng; return '+'; }
"-" { column += yyleng; return '-'; }
">>>" { column += yyleng; return LSHR; }
">><" { column += yyleng; return ROR; }
"<<>" { column += yyleng; return ROL; }
"<<" { column += yyleng; return SHL; }
">>" { column += yyleng; return ASHR; }
"&" { column += yyleng; return '&'; }
"^" { column += yyleng; return '^'; }
"|" { column += yyleng; return '|'; }
"<" { column += yyleng; return '>'; }
">" { column += yyleng; return '<'; }
"<=" { column += yyleng; return LE; }
">=" { column += yyleng; return GE; }
"==" { column += yyleng; return EQ; }
"!=" { column += yyleng; return NE; }
"and" { column += yyleng; return AND; }
"xor" { column += yyleng; return XOR; }
"or" { column += yyleng; return OR; }
"=" { column += yyleng; return '='; }
";" { column += yyleng; return ';'; }
"," { column += yyleng; return ','; }
/* primitive types */
void { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return VOID_PRIMITIVE; }
bool { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return BOOL_PRIMITIVE; }
u?int{d}* { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return INT_PRIMITIVE; }
float{d}* { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return FLOAT_PRIMITIVE; }
u?fixed{d}*{e}? { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return FIXED_PRIMITIVE; }
/* inline assembly */
\%{l}({l}|{d})* { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return ASM_REGISTER; }
\$-?(0[xX]{x}+|{d}+) { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return ASM_CONSTANT; }
\.{l}({l}|{d})* { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return ASM_DIRECTIVE; }
{l}({l}|{d})*: { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return ASM_LABEL; }
/* constants */
\"(\\.|[^\\"])*\" { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return STRING_CONSTANT; }
"true" { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return BOOL_CONSTANT; }
"false" { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return BOOL_CONSTANT; }
{l}({l}|{d})* { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return IDENTIFIER; }
0[xX]{x}+ { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return HEX_CONSTANT; }
-?{d}+ { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return DEC_CONSTANT; }
0[bB][01]+ { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return BIN_CONSTANT; }
-?{d}+{e} { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return DEC_CONSTANT; }
-?{d}*"."{d}+{e}? { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return DEC_CONSTANT; }
-?{d}+"."{d}*{e}? { column += yyleng; yylval.syntax = strndup(yytext, yyleng); return DEC_CONSTANT; }
/* misc */
"/*" { parseUntil('\0', "*/"); }
"//" { parseUntil('\0', "\n"); }
[\n] { ++line; column = 0; str = yytext+1; }
[ \t\v\f] { column += yyleng; }
. { column += yyleng; /* ignore unmatched characters */ }
%%
void parseUntil(char escChar, const char *delStr)
{
int key = yyinput();
bool escape = false;
int index = 0;
while (key != EOF) {
if (key == '\n') {
++line;
column = 0;
} else {
++column;
}
if (key == delStr[index] && (index > 0 || !escape)) {
++index;
} else if (delStr[index] == '\0') {
return;
} else if (index > 0) {
index = 0;
}
escape = key == escChar;
key = yyinput();
}
}
int yywrap(void) {
return 1;
}