-
Notifications
You must be signed in to change notification settings - Fork 27
/
rix.l
305 lines (260 loc) · 12.4 KB
/
rix.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
%{
/* need this for the call to atoi(), atof() below */
#include <math.h>
#include <string.h>
#include "ObjectTree.h"
#include "rix.tab.h" /* Generated by bison. */
#include "rixc.h"
#include "error.h"
#define INSERT_BUFF_LEN 1024
#define INDENT_STACK_MAX 512
int g_current_line_indent = 0;
int g_indent_levels[INDENT_STACK_MAX];
int g_indent_levels_index = -1;
int g_is_fake_outdent_symbol = 0;
int stack_top() { return g_indent_levels[g_indent_levels_index]; }
int stack_pop() { return g_indent_levels[g_indent_levels_index--]; }
void stack_push(int val) { g_indent_levels[++g_indent_levels_index] = val; }
extern int g_lineNum;
extern int g_lineCol;
#define YY_USER_INIT { \
stack_push(0); \
BEGIN(initial); \
}
//#define YY_DECL extern int yylex ( YYSTYPE * lvalp, YYLTYPE * llocp, yyscan_t scanner )
int yycolumn = 1;
void set_yycolumn(int val) {
yycolumn = val;
g_lineCol = yycolumn; //first column
//yylloc.last_column = yycolumn + yyleng - 1;
}
#define YY_USER_ACTION { \
g_lineNum = yylineno; \
set_yycolumn(yycolumn);\
yycolumn += yyleng; \
}
char* handleCodeInsert(void);
void* handleComma(void);
%}
%option yylineno
%option noyywrap
%x initial
%x indent
%s normal
%option bison-bridge bison-locations
alphabetic [a-zA-Z]
numeral [0-9]
alphanumeric {alphabetic}|{numeral}
integer {numeral}+
decimal "."{numeral}+|{numeral}+"."{numeral}+
float {decimal}|(({decimal}|{integer})[eE][-+]?{integer})
string \"(\\.|[^\\"])*\"
char \'(\\.|[^\\'])+\'
comment \/\/.*
colon ":"
paramComma ","
classDec "::"
classDecPrim ":::"
conditionLink "?"
selfIdent "$"
hash "#"
identifier ({selfIdent})*({alphabetic}|"_")({alphanumeric}|"_")*({selfIdent})*
unaryNegate "-"
mathOp "+"|"*"|"/"|"^^"|"%"
assignment "="
return "->"
lessThan "<"
greaterThan ">"
mathAssign "+="|"-="|"*="|"/="
comparison "<="|">="|"=="|"!="
ternary "<>"
bitwiseOp "&"|"^"|"|"|"~"|">>"|"<<"
booleanOp "||"|"&&"
condReturn "-->"
implies "==>"
accessor "."
dtvExternal "##external"
dtvAddsource "##addsource"
%%
int indent_caller = normal;
<*>\n { set_yycolumn(0); yylineno--; REJECT; }
<initial>. { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); }
<initial>\n { indent_caller = normal; yyless(0); BEGIN(indent); return ENDOFLINE; }
<indent>" " { g_current_line_indent++; }
<indent>\t { g_current_line_indent++; }
<indent>\n { g_current_line_indent = 0; /* ignoring blank line */ }
<indent><<EOF>> {
// When encountering the end of file, we want to emit an
// outdent for all indents currently left.
if(stack_top() != 0) {
stack_pop();
// See the same code below (<indent>.) for a rationale.
if(g_current_line_indent != stack_top()) {
size_t i;
unput('\n');
for(i = 0 ; i < stack_top() ; ++i) {
unput(' ');
}
} else {
BEGIN(indent_caller);
}
compilerDebugPrintf("lexer: EOF OUTDENT\n");
decPrev(); //for tracking last-statement.
return UNINDENT;
} else {
yyterminate();
}
}
<indent>. {
if(!g_is_fake_outdent_symbol) {
unput(*yytext);
}
g_is_fake_outdent_symbol = 0;
// -2: -1 for putting it back and -1 for ending at the last space.
set_yycolumn(yycolumn-1);
// Indentation level has increased. It can only ever
// increase by one level at a time. Remember how many
// spaces this level has and emit an indentation token.
if(g_current_line_indent > stack_top()) {
compilerDebugPrintf("lexer: INDENT\n");
stack_push(g_current_line_indent);
BEGIN(indent_caller);
incPrev(); //for tracking last-statement.
return INDENT;
} else if(g_current_line_indent < stack_top()) {
compilerDebugPrintf("lexer: OUTDENT\n");
// Outdenting is the most difficult, as we might need to
// outdent multiple times at once, but flex doesn't allow
// emitting multiple tokens at once! So we fake this by
// 'unput'ting fake lines which will give us the next
// outdent.
stack_pop();
if(g_current_line_indent != stack_top()) {
size_t i;
// Unput the rest of the current line, including the newline.
// We want to keep it untouched.
for(i = 0 ; i < g_current_line_indent ; ++i) {
unput(' ');
}
unput('\n');
// Now, insert a fake character indented just so
// that we get a correct outdent the next time.
unput('.');
// Though we need to remember that it's a fake one
// so we can ignore the symbol.
g_is_fake_outdent_symbol = 1;
for(i = 0 ; i < stack_top() ; ++i) {
unput(' ');
}
unput('\n');
} else {
BEGIN(indent_caller);
}
decPrev(); //for tracking last-statement.
return UNINDENT;
} else {
// No change in indentation, not much to do here...
BEGIN(indent_caller);
}
}
<normal>\n { compilerDebugPrintf("EOL\n");
g_current_line_indent = 0;
indent_caller = YY_START;
BEGIN(indent);
return ENDOFLINE; }
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] { /* DO NOTHING */ }
[/][*] { criticalError(ERROR_ParseError, "unterminated comment\n"); }
"`" {yylval -> sval = handleCodeInsert(); return CODE_INSERT; }
{comment} { compilerDebugPrintf("lexer: comment (ignored)\n"); /* do not forward comments at this time.*/ }
{string} { compilerDebugPrintf("lexer: string\n"); yylval->sval = strdup(yytext); return STRING; }
{integer} { compilerDebugPrintf("lexer: int\n"); yylval->ival = atoi(yytext); return INT; }
{char} { compilerDebugPrintf("lexer: char\n"); yylval->sval = strdup(yytext); return CHAR; }
{float} { compilerDebugPrintf("lexer: float\n"); yylval->fval = atof(yytext); return FLOAT; }
{mathOp} { compilerDebugPrintf("lexer: mathOp\n"); yylval->sval = strdup(yytext); return MATH_OP; }
{hash}{identifier} { char* first = strtok(yytext, "#");
Object* exists = findByName(first);
if (!exists) {
compilerDebugPrintf("lexer: new identifier %s\n",first);
yylval->sval = strdup(first);
return NEWIDENT;
} else {
char error[1024];
snprintf(error, 1024, "Trying to recreate identifier %s\n", first);
criticalError(ERROR_ParseError, error);
}
}
{identifier} { yylval->sval = strdup(yytext);
Object* exists = findByName(yytext);
if (!exists) {
compilerDebugPrintf("lexer: unidentified identifier\n");
return UNMARKEDNEWIDENT;
} else if (exists->category == Variable) {
compilerDebugPrintf("lexer: identifier\n");
return IDENT;
} else if (isVerb(exists)) {
compilerDebugPrintf("lexer: verb\n");
return VERB;
} else if (exists->category == Type) {
compilerDebugPrintf("lexer: type\n");
return TYPE;
} else {
criticalError(ERROR_ParseError, "lexer: unidentified identifier\n");
}
}
{assignment} { compilerDebugPrintf("lexer: assignment\n"); yylval->sval = strdup(yytext); return ASSIGNMENT; }
{conditionLink} { compilerDebugPrintf("lexer: CondLink\n"); yylval->sval = strdup(yytext); return CONDITIONLINK; }
{paramComma} { compilerDebugPrintf("lexer: ParamComma\n"); yylval->sval = strdup(yytext); /*handleComma();*/ return PARAMCOMMA; }
{colon} { compilerDebugPrintf("lexer: Colon\n"); yylval->sval = strdup(yytext); return COLON; }
{classDec} { compilerDebugPrintf("lexer: ClassDec\n"); yylval->sval = strdup(yytext); return CLASSDEC; }
{classDecPrim} { compilerDebugPrintf("lexer: CtorDecPrim\n"); yylval->sval = strdup(yytext); return CLASSDECPRIM; }
{return} { compilerDebugPrintf("lexer: Return\n"); yylval->sval = strdup(yytext); return RETURN; }
{selfIdent} { compilerDebugPrintf("lexer: self\n"); yylval->sval = strdup(yytext); return SELFIDENT; }
{unaryNegate} { compilerDebugPrintf("lexer: Unary Negate\n"); yylval->sval = strdup(yytext); return UNARYNEGATE; }
{booleanOp} { compilerDebugPrintf("lexer: bitwiseOp\n"); yylval->sval = strdup(yytext); return BOOLEANOP; }
{comparison} { compilerDebugPrintf("lexer: comparator\n"); yylval->sval = strdup(yytext); return COMPARISON; }
{lessThan} { compilerDebugPrintf("lexer: comparator<\n"); yylval->sval = strdup(yytext); return LESSTHAN; }
{greaterThan} { compilerDebugPrintf("lexer: comparator>\n"); yylval->sval = strdup(yytext); return GREATERTHAN; }
{ternary} { compilerDebugPrintf("lexer: ternary\n"); return TERNARY; }
{condReturn} { compilerDebugPrintf("lexer: condReturn\n"); return CONDRETURN; }
{mathAssign} { compilerDebugPrintf("lefloatxer: MathAssign\n"); yylval->sval = strdup(yytext); return MATHASSIGN; }
{accessor} { compilerDebugPrintf("lexer: accessor\n"); yylval->sval = strdup(yytext); return ACCESSOR; }
{hash} { compilerDebugPrintf("lexer: accessor\n"); yylval->sval = strdup(yytext); return DESTRUCTOR; }
{dtvExternal} { compilerDebugPrintf("lexer: directive\n"); yylval->sval = strdup(yytext); return DTV_EXTERNAL; }
{dtvAddsource} { compilerDebugPrintf("lexer: directive\n"); yylval->sval = strdup(yytext); return DTV_ADDSOURCE; }
<<EOF>> { return ENDOFFILE; }
[ \t]+ { g_lineCol += yyleng; } /* eat up whitespace */
"(" { compilerDebugPrintf("lexer: (\n"); return LPAREN; }
")" { compilerDebugPrintf("lexer: )\n"); return RPAREN; }
"[" { compilerDebugPrintf("lexer: (\n"); return LBRACKET; }
"]" { compilerDebugPrintf("lexer: )\n"); return RBRACKET; }
"]"[ \t]*{assignment} { compilerDebugPrintf("lexer: )\n"); return RBRACKETASSIGN; }
"{" { compilerDebugPrintf("lexer: (\n"); return LBRACE; }
"}" { compilerDebugPrintf("lexer: )\n"); return RBRACE; }
. { compilerDebugPrintf("Unknown symbol encountered: '%s'\n", yytext); }
%%
char* handleCodeInsert(void) {
compilerDebugPrintf("lexer: code insert\n");
int c;
int index = 0;
char * ci = (char*)malloc(INSERT_BUFF_LEN);
while ( c = input()) {
if ( c != '`' && index < INSERT_BUFF_LEN ) {
ci[index++] = c;
}else if ( c == '`' ) {
return ci;
}
if ( c == EOF ) {
criticalError(ERROR_ParseError, "unterminated code insert\n");
return 0;
}
}
}
void* handleComma(void) {
compilerDebugPrintf("lexer: eating spaces between commas\n");
int c;
while ( c = input()) {
if ( c != '\t' || c != ' ') {
return;
}
}
}