-
Notifications
You must be signed in to change notification settings - Fork 2
/
gsc_cod9.l
322 lines (281 loc) · 7.11 KB
/
gsc_cod9.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
D [0-9]
L [a-zA-Z_]
P [a-zA-Z_\\]
%{
#include <ctype.h>
#include <io.h>
#include "y.tab.h" // generated by yacc/bison
// windows fix
#define YY_NO_UNISTD_H
#define isatty _isatty // <io.h>
#define fileno _fileno // <stdio.h>
void comment(void);
void skipdevblock(void);
void ProcessEscapeSequences(char* stringValue);
int another_opening_square_bracket(void);
int next_closing_square_bracket(void);
void invalid_identifier(void);
void unexpected_character(void);
int lineCount = 1;
%}
%%
"/*" { comment(); }
"/#" { if (!compiler->devMode) skipdevblock(); }
"#/" { if (compiler->devMode) yyerror("unexpected devblock end"); }
"//"[^\n]* { /* consume //-comment */; }
"#include" { return(INCLUDE); }
"#using_animtree" { return(USING_ANIMTREE); }
"#animtree" {
yylval.intValue = -1;
return(INT_LITERAL);
}
"break" { return(BREAK); }
"case" { return(CASE); }
"continue" { return(CONTINUE); }
"default" { return(DEFAULT); }
"else" { return(ELSE); }
"false" {
yylval.intValue = 0;
return(INT_LITERAL);
}
"for" { return(FOR); }
"foreach" { return(FOREACH); }
"if" { return(IF); }
"in" { return(IN); }
"return" { return(RETURN); }
"switch" { return(SWITCH); }
"thread" { return(THREAD); }
"true" {
yylval.intValue = 1;
return(INT_LITERAL);
}
"undefined" { return(UNDEFINED); }
"wait" { return(WAIT); }
"waittillframeend" { return(WAITTILLFRAMEEND); }
"while" { return(WHILE); }
/* invalid identifier e.g 123myVar, we have to throw an error */
{D}+{L} {
invalid_identifier();
}
/* if PATH was defined before IDENTIFIER, then the lexer would return the PATH token always
when IDENTIFIER has the same size as PATH because its defined first */
/* identifier e.g. var name, function name... */
{L}({L}|{D})* {
char* c = 0;
yylval.stringValue = _strdup(yytext);
// convert the letters to lower case
for (c = yylval.stringValue; *c; c++)
{
*c = tolower(*c);
}
return(IDENTIFIER);
}
/* gsc file path e.g. maps\mp\_utility */
{P}({P}|{D})* {
char* c = 0;
yylval.stringValue = _strdup(yytext);
// convert backward slashes to forward slashes & the letters to lower case
for (c = yylval.stringValue; *c; c++)
{
if (*c == '\\')
*c = '/';
*c = tolower(*c);
}
return(PATH);
}
/* integer e.g. 10 */
{D}+ {
sscanf_s(yytext, "%d", &yylval.intValue);
return(INT_LITERAL);
}
/* float e.g. 1.5 or .5 */
{D}*"."{D}+ {
sscanf_s(yytext, "%f", &yylval.floatValue);
return(FLOAT_LITERAL);
}
/* string e.g. "myString" */
\"(\\.|[^\\"\n])*\" {
yylval.stringValue = _strdup(yytext);
// remove last " and skip "
yylval.stringValue[strlen(yylval.stringValue) - 1] = '\0';
memmove(yylval.stringValue, yylval.stringValue + 1, strlen(yylval.stringValue));
ProcessEscapeSequences(yylval.stringValue);
return(STRING_LITERAL);
}
/* localized string e.g. &"myString" */
&\"(\\.|[^\\"\n])*\" {
yylval.stringValue = _strdup(yytext);
// remove last " and skip &"
yylval.stringValue[strlen(yylval.stringValue) - 1] = '\0';
memmove(yylval.stringValue, yylval.stringValue + 2, strlen(yylval.stringValue));
ProcessEscapeSequences(yylval.stringValue); // not sure if its needed here
return(LOC_STRING_LITERAL);
}
/* hashed string e.g. #"myString" */
#\"(\\.|[^\\"\n])*\" {
yylval.stringValue = _strdup(yytext);
// remove last " and skip #"
yylval.stringValue[strlen(yylval.stringValue)] = '\0';
memmove(yylval.stringValue, yylval.stringValue + 2, strlen(yylval.stringValue));
return(HASH_STRING_LITERAL);
}
">>=" { return(SHIFT_RIGHT_ASSIGN); }
"<<=" { return(SHIFT_LEFT_ASSIGN); }
"+=" { return(PLUS_ASSIGN); }
"-=" { return(MINUS_ASSIGN); }
"*=" { return(MULTIPLY_ASSIGN); }
"/=" { return(DIVIDE_ASSIGN); }
"%=" { return(MOD_ASSIGN); }
"&=" { return(BIT_AND_ASSIGN); }
"^=" { return(BIT_EX_OR_ASSIGN); }
"|=" { return(BIT_OR_ASSIGN); }
">>" { return(SHIFT_RIGHT_OP); }
"<<" { return(SHIFT_LEFT_OP); }
"++" { return(INC_OP); }
"--" { return(DEC_OP); }
"&&" { return(LOGICAL_AND_OP); }
"||" { return(LOGICAL_OR_OP); }
"<=" { return(LESS_EQUAL_OP); }
">=" { return(GREATER_EQUAL_OP); }
"==" { return(EQUALITY_OP); }
"!=" { return(INEQUALITY_OP); }
"::" { return(REFERENCE_FUNC); }
";" { return(';'); }
"{" { return('{'); }
"}" { return('}'); }
"," { return(','); }
":" { return(':'); }
"=" { return('='); }
"(" { return('('); }
")" { return(')'); }
"[" { return(another_opening_square_bracket()?DEREFERENCE_FUNC_START:(next_closing_square_bracket()?EMPTY_ARRAY:'[')); }
"]" { return(']'); }
"." { return('.'); }
"&" { return('&'); }
"!" { return('!'); }
"~" { return('~'); }
"-" { return('-'); }
"+" { return('+'); }
"*" { return('*'); }
"/" { return('/'); }
"%" { return('%'); }
"<" { return('<'); }
">" { return('>'); }
"^" { return('^'); }
"|" { return('|'); }
"?" { return('?'); }
[ \t] { ; }
\n { lineCount++; }
. { unexpected_character(); }
%%
int yywrap(void)
{
return 1;
}
void comment(void)
{
char c, prev = 0;
#ifdef __cplusplus
while ((c = yyinput()) != 0) /* (EOF maps to 0) */
#else
while ((c = input()) != 0) /* (EOF maps to 0) */
#endif
{
if (c == '/' && prev == '*')
return;
else if (c == '\n')
lineCount++;
prev = c;
}
yyerror("unterminated comment");
}
void skipdevblock(void)
{
char c, prev = 0;
#ifdef __cplusplus
while ((c = yyinput()) != 0) /* (EOF maps to 0) */
#else
while ((c = input()) != 0) /* (EOF maps to 0) */
#endif
{
if (c == '/' && prev == '#')
return;
else if (c == '\n')
lineCount++;
prev = c;
}
yyerror("unterminated devblock");
}
void ProcessEscapeSequences(char* stringValue)
{
for (char* c = stringValue; *c; c++)
{
if (*c == '\\')
{
switch (*(c + 1))
{
case '\\':
*c = '\\';
break;
case 'n':
*c = '\n';
break;
case 't':
*c = '\t';
break;
default:
yyerror("unknown escape sequence in string");
break;
}
memmove(c + 1, c + 2, strlen(c + 2) + 1);
}
}
}
int another_opening_square_bracket(void)
{
char c = 0;
#ifdef __cplusplus
while ((c = yyinput()) != 0) /* (EOF maps to 0) */
#else
while ((c = input()) != 0) /* (EOF maps to 0) */
#endif
{
if (c == '[')
return 1;
else if (c != ' ' && c != '\t' && c != '\n')
{
unput(c);
return 0;
}
}
yyerror("EOF follows after opening square bracket");
return 0;
}
int next_closing_square_bracket(void)
{
char c = 0;
#ifdef __cplusplus
while ((c = yyinput()) != 0) /* (EOF maps to 0) */
#else
while ((c = input()) != 0) /* (EOF maps to 0) */
#endif
{
if (c == ']')
return 1;
else if (c != ' ' && c != '\t' && c != '\n')
{
unput(c);
return 0;
}
}
yyerror("EOF follows after opening square bracket");
return 0;
}
void invalid_identifier(void)
{
yyerror("invalid identifier");
}
void unexpected_character(void)
{
yyerror("unexpected character");
}