This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlslmini.l
168 lines (144 loc) · 4.63 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
%option bison-locations
%option bison-bridge
%option reentrant
%option noyywrap
%option nounput
%{
#include <stdlib.h>
#include "lslmini.hh"
#include "lslmini.tab.h"
//#include "llconstants.hh"
#ifdef WIN32
#include <io.h>
#define isatty _isatty
#endif /* WIN32 */
#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);
%}
D [0-9]
BD [-]?[0-9]
N [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F)
/* exclusive state to eat comments of any length without overflowing any buffers */
%x COMMENT
%x C_COMMENT
%%
%{
/* HACK */
yylloc = yylloc_param;
LLOC_STEP();
%}
"//" { BEGIN COMMENT; }
<COMMENT>"$[E"{D}{5}"]" {
ErrorCode e = (ErrorCode) strtoul( yytext+3, NULL, 10 );
LOG( LOG_INFO, yylloc, "Adding assertion for E%d.", (int)e );
Logger::get()->add_assertion( yylloc->first_line, 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); }
"." { return(PERIOD); }
0[xX]{H}+ { yylval->ival = strtoul(yytext, NULL, 0); return(INTEGER_CONSTANT); }
[-]?{D}+ { yylval->ival = strtoul(yytext, NULL, 10); return(INTEGER_CONSTANT); }
{BD}+ {
yylval->ival = strtoul(yytext, NULL, 10);
ERROR( yylloc, W_BAD_DECIMAL_LEX, yytext, yylval->ival);
return(INTEGER_CONSTANT);
}
"TRUE" { yylval->ival = 1; return(INTEGER_TRUE); }
"FALSE" { yylval->ival = 0; return(INTEGER_FALSE); }
{L}({L}|{N})* { yylval->sval = new char[strlen(yytext) + 1]; strcpy(yylval->sval, yytext); return(IDENTIFIER); }
[-]?{D}+{E} { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
[-]?{D}*"."{D}+({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
[-]?{D}+"."{D}*({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
L?\"(\\.|[^\\"])*\" { yylval->sval = parse_string(yytext); 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); }
\n { LLOC_LINES(1); LLOC_STEP(); }
. { LLOC_STEP(); /* ignore bad characters */ }
%%
char *parse_string(char *input) {
char *str = new char[strlen(input) + 1 - 2];
char *yp = input + 1;
char *sp = str;
while ( *yp ) {
if ( *yp == '\\' ) {
switch ( *++yp ) {
case 'n': *sp++ = '\n'; break;
case 't': *sp++ = '\t'; break; // TODO: should this be \t or 4 spaces?
case '\\': *sp++ = '\\'; break;
case '"': *sp++ = '"'; break;
default: *sp++ = '\\'; *sp++ = *yp; break;
}
yp++;
} else {
*sp++ = *yp++;
}
}
*--sp = 0;
return str;
}