blob: 8fa7a97c01f32b2f32ee4fbd06bf79e81d58a440 (
plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
%{
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <FreezeScript/GrammarUtil.h> // Before Grammar.h, so that YYSTYPE is defined
#include <FreezeScript/Grammar.h>
#include <IceUtil/InputUtil.h>
#include <stdlib.h>
#include <math.h>
#include <map>
#if defined(_MSC_VER) && defined(ICE_64)
//
// 'initializing' : conversion from '__int64' to 'int', possible loss of data
// Puts a pointer-difference into an int
//
# pragma warning( 4 : 4244 )
#endif
using namespace std;
using namespace FreezeScript;
namespace FreezeScript
{
typedef map<string, int> KeywordMap;
static KeywordMap keywordMap;
void initScanner();
int checkKeyword(const string&);
StringTokPtr parseString(char);
}
#define YY_USER_INIT initScanner();
#define YY_INPUT(buf, result, max_size) { result = getInput(buf, max_size); }
%}
%option noyywrap
%option never-interactive
%option prefix="freeze_script_"
%option outfile="lex.yy.c"
identifier [[:alpha:]_][[:alnum:]_]*
integer_constant (\+|-)?((0[0-7]+)|(0x[[:xdigit:]]+)|([[:digit:]]+))
fractional_constant (\+|-)?(([[:digit:]]*\.[[:digit:]]+)|([[:digit:]]+\.))
exponent_part (e|E)(\+|-)?[[:digit:]]+
floating_literal (({fractional_constant}{exponent_part}?)|((\+|-)?[[:digit:]]+{exponent_part}))[fF]?
%%
"//" {
// C++-style comment
int c;
do
{
c = yyinput();
if(c == '\n')
{
parseLine++;
}
}
while(c != '\n' && c != EOF);
}
"/*" {
// C-style comment
while(true)
{
int c = yyinput();
if(c == '\n')
{
parseLine++;
}
else if(c == '*')
{
int next = yyinput();
if(next == '/')
{
break;
}
else
{
unput(next);
}
}
else if(c == EOF)
{
parseErrorReporter->expressionSyntaxError("EOF in comment");
break;
}
}
}
{identifier} {
StringTokPtr ident = new StringTok;
ident->v = yytext;
*yylvalp = ident;
return checkKeyword(ident->v);
}
\" {
StringTokPtr str = parseString('"');
*yylvalp = str;
return TOK_STRING_LITERAL;
}
\' {
StringTokPtr str = parseString('\'');
*yylvalp = str;
return TOK_STRING_LITERAL;
}
{integer_constant} {
IntegerTokPtr itp = new IntegerTok;
*yylvalp = itp;
if(!IceUtil::stringToInt64(string(yytext), itp->v))
{
assert(itp->v != 0);
string msg = "integer constant `";
msg += yytext;
msg += "' out of range";
parseErrorReporter->expressionSyntaxError(msg);
}
return TOK_INTEGER_LITERAL;
}
{floating_literal} {
errno = 0;
FloatingTokPtr ftp = new FloatingTok;
*yylvalp = ftp;
string literal(yytext);
char lastChar = literal[literal.size() - 1];
if(lastChar == 'f' || lastChar == 'F')
{
literal = literal.substr(0, literal.size() - 1); // Clobber trailing 'f' or 'F' suffix
}
ftp->v = strtod(literal.c_str(), 0);
if((ftp->v == HUGE_VAL || ftp->v == -HUGE_VAL) && errno == ERANGE)
{
string msg = "floating-point constant `";
msg += yytext;
msg += "' too large (overflow)";
parseErrorReporter->expressionSyntaxError(msg);
}
else if(ftp->v == 0 && errno == ERANGE)
{
string msg = "floating-point constant `";
msg += yytext;
msg += "' too small (underflow)";
parseErrorReporter->expressionSyntaxError(msg);
}
return TOK_FLOATING_POINT_LITERAL;
}
[[:space:]] {
// Igore white-space
if(yytext[0] == '\n')
{
parseLine++;
}
}
"<" return TOK_LESS_THAN;
">" return TOK_GREATER_THAN;
"<=" return TOK_LESS_EQUAL;
">=" return TOK_GREATER_EQUAL;
"==" return TOK_EQUAL;
"!=" return TOK_NEQ;
"+" return TOK_ADD;
"-" return TOK_SUB;
"*" return TOK_MUL;
"/" return TOK_DIV;
"%" return TOK_MOD;
"(" return TOK_LPAREN;
")" return TOK_RPAREN;
"[" return TOK_LBRACKET;
"]" return TOK_RBRACKET;
"::" return TOK_SCOPE_DELIMITER;
. {
return yytext[0];
}
%%
namespace FreezeScript
{
void
initScanner()
{
keywordMap["true"] = TOK_TRUE;
keywordMap["false"] = TOK_FALSE;
keywordMap["and"] = TOK_AND;
keywordMap["or"] = TOK_OR;
keywordMap["not"] = TOK_NOT;
keywordMap["nil"] = TOK_NIL;
}
int
checkKeyword(const string& id)
{
KeywordMap::const_iterator pos = keywordMap.find(id);
if(pos != keywordMap.end())
{
return pos->second;
}
return TOK_IDENTIFIER;
}
StringTokPtr
parseString(char start)
{
StringTokPtr str = new StringTok;
while(true)
{
char c = static_cast<char>(yyinput());
if(c == start)
{
break;
}
else if(c == EOF)
{
parseErrorReporter->expressionSyntaxError("EOF in string");
break;
}
else if(c == '\n')
{
parseErrorReporter->expressionSyntaxError("newline in string");
}
else if(c == '\\')
{
char next = static_cast<char>(yyinput());
switch(next)
{
case '\\':
case '"':
case '\'':
{
str->v += next;
break;
}
case 'n':
{
str->v += '\n';
break;
}
case 'r':
{
str->v += '\r';
break;
}
case 't':
{
str->v += '\t';
break;
}
case 'v':
{
str->v += '\v';
break;
}
case 'f':
{
str->v += '\f';
break;
}
case 'a':
{
str->v += '\a';
break;
}
case 'b':
{
str->v += '\b';
break;
}
case '?':
{
str->v += '\?';
break;
}
case '0':
case '1':
case '2':
case '3':
{
static string octalDigits = "01234567";
unsigned short us = next - '0';
if(octalDigits.find_first_of(next = static_cast<char>(yyinput())) != string::npos)
{
us = us * 8 + next - '0';
if(octalDigits.find_first_of(next = static_cast<char>(yyinput())) != string::npos)
{
us = us * 8 + next - '0';
}
else
{
unput(next);
}
}
else
{
unput(next);
}
str->v += static_cast<char>(us);
break;
}
case 'x':
{
IceUtil::Int64 ull = 0;
while(isxdigit(next = static_cast<char>(yyinput())))
{
ull *= 16;
if(isdigit(next))
{
ull += next - '0';
}
else if(islower(next))
{
ull += next - 'a' + 10;
}
else
{
ull += next - 'A' + 10;
}
}
unput(next);
str->v += static_cast<char>(ull);
break;
}
// TODO: add universal character names
default:
{
str->v += c;
unput(next);
}
}
}
else
{
str->v += c;
}
}
return str;
}
} // End of namespace FreezeScript
|