forked from pclewis/lslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lslmini.l
226 lines (201 loc) · 6.17 KB
/
lslmini.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
%option bison-locations
%option bison-bridge
%option reentrant
%option noyywrap
%option nounput
%{
#include <stdlib.h>
#include "lslmini.hh"
#include "lslmini.tab.h"
#ifdef _MSC_VER
#include <io.h>
#define isatty _isatty
#endif /* _MSC_VER */
#define LLOC_RESET() yylloc->first_column = yylloc->first_line = yylloc->last_column = yylloc->last_line = 1;
#define LLOC_LINES(num) yylloc->last_column = 1; yylloc->last_line += (num);
#define LLOC_STEP() yylloc->first_column = yylloc->last_column; yylloc->first_line = yylloc->last_line;
#define YY_USER_ACTION yylloc->last_column += yyleng;
#define YY_USER_INIT LLOC_RESET()
char *parse_string(char *input, int *last_line, int *last_column);
%}
N [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{N}+
FS (f|F)
/* exclusive state to eat comments of any length without overflowing any buffers */
%x COMMENT
%x C_COMMENT
/* exclusive state to skip preprocessor commands */
%x PREPROC
%%
%{
/* HACK */
yylloc = yylloc_param;
LLOC_STEP();
%}
"//" { BEGIN COMMENT; }
<COMMENT,C_COMMENT>"$["[EML]{N}{5}"]" {
ErrorCode e = (ErrorCode) strtoul( yytext+3, NULL, 10 );
if ((mono_mode && yytext[2] != 'L') || (!mono_mode && yytext[2] != 'M')) {
LOG( LOG_INFO, yylloc, "Adding%s assertion for %d.",
yytext[2] == 'L' ? " LSO-only" :
yytext[2] == 'M' ? " Mono-only" : "", (int)e );
Logger::get()->add_assertion( yylloc->first_line, e );
} else {
LOG( LOG_INFO, yylloc, "Ignoring%s assertion for %d.",
yytext[2] == 'L' ? " LSO-only" :
yytext[2] == 'M' ? " Mono-only" : "", (int)e );
}
}
<COMMENT>. { /* eat comments */ }
<COMMENT>\n { BEGIN 0; LLOC_LINES(1); LLOC_STEP(); }
"/*" { BEGIN C_COMMENT; }
<C_COMMENT>"*/" { BEGIN 0; LLOC_STEP(); }
<C_COMMENT>\n { LLOC_LINES(1); LLOC_STEP(); }
<C_COMMENT>. { LLOC_STEP(); }
"integer" { return(INTEGER); }
"float" { return(FLOAT_TYPE); }
"string" { return(STRING); }
"key" { return(LLKEY); }
"vector" { return(VECTOR); }
"quaternion" { return(QUATERNION); }
"rotation" { return(QUATERNION); }
"list" { return(LIST); }
"default" { yylval->sval = new char[strlen(yytext) + 1]; strcpy(yylval->sval, yytext); return(STATE_DEFAULT); }
"state" { return(STATE); }
"event" { return(EVENT); }
"jump" { return(JUMP); }
"return" { return(RETURN); }
"if" { return(IF); }
"else" { return(ELSE); }
"for" { return(FOR); }
"do" { return(DO); }
"while" { return(WHILE); }
"print" { return(PRINT); }
"." { return(PERIOD); }
0[xX]{H}+ { yylval->ival = strtoul(yytext, NULL, 0); return(INTEGER_CONSTANT); }
{N}+ { yylval->ival = strtoul(yytext, NULL, 10); return(INTEGER_CONSTANT); }
"TRUE" { yylval->ival = 1; return(INTEGER_TRUE); }
"FALSE" { yylval->ival = 0; return(INTEGER_FALSE); }
{L}({L}|{N})* {
if (switch_stmt) {
if (!strcmp(yytext, "switch")) return(SWITCH);
if (!strcmp(yytext, "break")) return(BREAK);
if (!strcmp(yytext, "case")) return(CASE);
}
yylval->sval = new char[strlen(yytext) + 1]; strcpy(yylval->sval, yytext);
return(IDENTIFIER);
}
{N}+{E}{FS} {
yylval->fval = (F32)atof(yytext);
ERROR( yylloc, E_SYNTAX_ERROR, "F can't go after a float number without a decimal point." );
return(FP_CONSTANT);
}
{N}+{E} { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
{N}*"."{N}+({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
{N}+"."{N}*({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
L?\"(\\.|[^\\"])*\" {
if (yytext[0] == 'L') {
ERROR( yylloc, W_L_STRING );
}
int last_line = yylloc->last_line;
int last_column = yylloc->last_column;
yylval->sval = parse_string(yytext, &last_line, &last_column);
yylloc->last_line = last_line;
yylloc->last_column = last_column;
return(STRING_CONSTANT);
}
"++" { return(INC_OP); }
"--" { return(DEC_OP); }
"+=" { return(ADD_ASSIGN); }
"-=" { return(SUB_ASSIGN); }
"*=" { return(MUL_ASSIGN); }
"/=" { return(DIV_ASSIGN); }
"%=" { return(MOD_ASSIGN); }
";" { return(';'); }
"{" { return('{'); }
"}" { return('}'); }
"," { return(','); }
"=" { return('='); }
"(" { return('('); }
")" { return(')'); }
"-" { return('-'); }
"+" { return('+'); }
"*" { return('*'); }
"/" { return('/'); }
"%" { return('%'); }
"@" { return('@'); }
":" { return(':'); }
">" { return('>'); }
"<" { return('<'); }
"]" { return(']'); }
"[" { return('['); }
"==" { return(EQ); }
"!=" { return(NEQ); }
">=" { return(GEQ); }
"<=" { return(LEQ); }
"&" { return('&'); }
"|" { return('|'); }
"^" { return('^'); }
"~" { return('~'); }
"!" { return('!'); }
"&&" { return(BOOLEAN_AND); }
"||" { return(BOOLEAN_OR); }
"<<" { return(SHIFT_LEFT); }
">>" { return(SHIFT_RIGHT); }
^[ \t\f\v\r]*"#" { LLOC_STEP(); if (skip_preproc) { BEGIN PREPROC; } }
<PREPROC>[^\n]*\\\r?\n { LLOC_LINES(1); LLOC_STEP(); }
<PREPROC>[^\n]*\n { BEGIN 0; LLOC_LINES(1); LLOC_STEP(); }
\n { LLOC_LINES(1); LLOC_STEP(); }
[\x80-\xBF] { yylloc->last_column--; }
. { LLOC_STEP(); /* ignore bad characters */ }
%%
char *parse_string(char *input, int *last_line, int *last_column) {
char *str = new char[(strlen(input) - 2) * 2 + 1];
char *yp = input + 1;
char *sp = str;
if (*input == 'L') {
*sp++ = *yp++;
}
while ( *yp ) {
if ( *yp == '\\' ) {
++*last_column;
switch ( *++yp ) {
case 'n': *sp++ = '\n'; break;
case 't':
*sp++ = ' ';
*sp++ = ' ';
*sp++ = ' ';
*sp++ = ' ';
break;
case '\\': *sp++ = '\\'; break;
case '"': *sp++ = '"'; break;
case '\n':
/* thorny! OK if preprocessor in use;
causes a syntax disaster if not */
/* TODO: Emit warning, but how? */
*sp++ = '\n';
break;
default:
/* TODO: Emit warning, but how? */
*sp++ = *yp;
break;
}
++*last_column;
yp++;
} else if ( *yp == '\n') {
++*last_line;
*last_column = 1;
*sp++ = *yp++;
} else {
if ( (unsigned char)*yp < 0x80 || (unsigned char)*yp >= 0xC0) {
// count only the first byte of UTF-8 sequences
++*last_column;
}
*sp++ = *yp++;
}
}
*--sp = 0;
return str;
}