-
Notifications
You must be signed in to change notification settings - Fork 1
/
mini-javascript.l
197 lines (179 loc) · 8.12 KB
/
mini-javascript.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
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
%{
#include <stdio.h>
#include <stdlib.h>
int column;
void yyerror(const char *s);
void count();
// enum TOKEN {
// TOKEN_EOF, VAR, LET, CONST, FUNCTION, ARROW_FUNCTION, SEMICOLON, COLON, IDENTIFIER,
// FOR, WHILE, DO, IF, ELSE, SWITCH, CASE, BREAK, CONTINUE, NEW, DELETE, TYPEOF, INSTANCEOF,
// LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET,
// LITERAL_TRUE, LITERAL_FALSE, LITERAL_NAN, LITERAL_NULL, LITERAL_UNDEFINED,
// PLUS, MINUS, MULTIPLY, DIVIDE, EQ, NOT_EQ, NOT, DOT, AND, OR, COMMA, MODULO, TERNARY, INCREASE, DECREASE,
// ADD_ASSIGN, SUBTRACT_ASSIGN, MULTIPLY_ASSIGN, DIVIDE_ASSIGN, MODULO_ASSIGN,
// LT, GT, LTE, GTE, NUMBER STRING
// };
%}
letter [a-zA-Z_$]
digit [0-9]
space [" "\t\r]
%%
var { count(); return VAR; }
let { count(); return LET; }
const { count(); return CONST; }
function { count(); return FUNCTION; }
"=>" { count(); return ARROW_FUNCTION; }
for { count(); return FOR; }
while { count(); return WHILE; }
do { count(); return DO; }
if { count(); return IF; }
else { count(); return ELSE; }
return { count(); return RETURN; }
switch { count(); return SWITCH; }
case { count(); return CASE; }
default { count(); return DEFAULT; }
break { count(); return BREAK; }
continue { count(); return CONTINUE; }
new { count(); return NEW; }
delete { count(); return DELETE; }
typeof { count(); return TYPEOF; }
instanceof { count(); return INSTANCEOF; }
async { count(); return ASYNC; }
await { count(); return AWAIT; }
true {
count();
yylval.val = "true";
return LITERAL_TRUE;
}
false {
count();
yylval.val = "false";
return LITERAL_FALSE;
}
NaN {
count();
yylval.val = "NaN";
return LITERAL_NAN;
}
null {
count();
yylval.val = "null";
return LITERAL_NULL;
}
undefined {
count();
yylval.val = "undefined";
return LITERAL_UNDEFINED;
}
";" { count(); return SEMICOLON; }
":" { count(); return COLON; }
"(" { count(); return LPAREN; }
")" { count(); return RPAREN; }
"{" { count(); return LBRACE; }
"}" { count(); return RBRACE; }
"[" { count(); return LBRACKET; }
"]" { count(); return RBRACKET; }
"+" { count(); return PLUS; }
"-" { count(); return MINUS; }
"*" { count(); return MULTIPLY; }
"/" { count(); return DIVIDE; }
"%" { count(); return MODULO; }
"=" { count(); return ASSIGN; }
"==" { count(); return EQ; }
"===" { count(); return EXACTLY_EQ; }
"!=" { count(); return NOT_EQ; }
"!==" { count(); return EXACTLY_NOT_EQ; }
"&&" { count(); return AND; }
"||" { count(); return OR; }
"!" { count(); return NOT; }
"<" { count(); return LT; }
">" { count(); return GT; }
"<=" { count(); return LTE; }
">=" { count(); return GTE; }
"," { count(); return COMMA; }
"?" { count(); return TERNARY; }
"++" { count(); return INCREASE; }
"--" { count(); return DECREASE; }
"+=" { count(); return ADD_ASSIGN; }
"-=" { count(); return SUBTRACT_ASSIGN; }
"*=" { count(); return MULTIPLY_ASSIGN; }
"/=" { count(); return DIVIDE_ASSIGN; }
"%=" { count(); return MODULO_ASSIGN; }
\. { count(); return DOT; }
Infinity {
count();
yylval.val = "Infinity";
return LITERAL_INFINITY;
}
{letter}({letter}|{digit})* {
count();
yylval.val = strdup(yytext);
return IDENTIFIER;
}
{digit}+ {
count();
yylval.val = strdup(yytext);
return NUMBER;
}
{digit}+\.{digit}+ {
count();
yylval.val = strdup(yytext);
return NUMBER;
}
[\-\+]{digit}+ {
count();
yylval.val = strdup(yytext);
return NUMBER;
}
[\-\+]{digit}+\.{digit}+ {
count();
yylval.val = strdup(yytext);
return NUMBER;
}
\"[^"]*\" {
count();
yylval.val = strdup(yytext);
return STRING;
}
\'[^']*\' {
count();
yylval.val = strdup(yytext);
return STRING;
}
\`[^`]*\` {
count();
yylval.val = strdup(yytext);
return STRING;
}
^\'use[ ]strict\'; { count(); return STRICT_MODE; }
^\"use[ ]strict\"; { count(); return STRICT_MODE; }
"//".* { count(); }
{space} { count(); }
\n { count(); }
. { count(); yyerror("Invalid token"); }
%%
int yywrap() {
return 1;
}
void count() {
for (int i = 0; yytext[i] != '\0'; i++) {
if (yytext[i] == '\n') {
yylineno++;
column = 0;
} else {
column++;
}
}
}
/* int main() {
printf("JavaScript Lexer\n\n");
enum TOKEN token;
while (!!(token = yylex()) != TOKEN_EOF) {
if (token == ERR) {
printf("Error occurred at line %d - \"%s\"\n", yylineno, yytext);
} else {
printf("%s recognized\n", yytext);
}
}
return 0;
} */