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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
%{
/*****
* camp.l
* Andy Hammerlindl 2002/06/14
*
* The lexical analyzer of the Asymptote language.
*****/
#include <iostream>
#include <fstream>
#include <sstream>
#include "util.h"
#include "modifier.h"
#include "exp.h"
#include "stm.h"
#include "fundec.h"
#include "errormsg.h"
#include "interact.h"
#include "lexical.h"
using namespace absyntax;
using mem::string;
#include "camp.tab.h"
#define YY_NO_UNPUT
fileinfo* fi;
Int tokPos;
Int charPos;
//int commentDepth = 0;
bool eof;
string eofMessage;
extern errorstream em;
extern "C" int yywrap(void)
{
charPos=1;
return 1;
}
typedef size_t (*input_f) (char* bif, size_t max_size);
input_f yy_input = NULL;
void setlexer(input_f input, string filename)
{
YY_FLUSH_BUFFER;
yywrap();
fi = new fileinfo(filename);
yy_input = input;
tokPos = charPos = 1;
eof=false;
eofMessage="<no eof>";
}
#define YY_INPUT(buf,result,max_size) {result=yy_input(buf,max_size);}
position lexerPos()
{
position p;
p.init(fi, tokPos);
return p;
}
namespace {
position here()
{
return lexerPos();
}
void adjust()
{
tokPos = charPos;
charPos += yyleng;
yylval.pos = here();
}
void makesymbol(bool op=true, string text = yytext)
{
adjust();
yylval.ps.pos=here();
yylval.ps.sym=op ? symbol::opTrans(text) : symbol::literalTrans(text);
}
void makeselfsymbol(bool op=true, string text = yytext)
{
/* Copy all but the last digit, which should be an =. */
assert(text[text.length()-1] == '=');
text.erase(text.length()-1);
makesymbol(op,text);
}
void makemod(trans::modifier mod) {
yylval.mod.pos=here();
yylval.mod.val=mod;
}
void makeperm(trans::permission perm) {
yylval.perm.pos=here();
yylval.perm.val=perm;
}
void newline()
{
fi->newline();
charPos = tokPos = 1;
}
void error(void)
{
em.error(here());
}
}
// Used by the lexer rules to flag an unexpected end of input. The message is
// the error message that should be reported, and may differ if, say the input
// ends in the middle of a string or comment.
void setEOF(string message) {
eof=true;
eofMessage=message;
}
// Called by code outside of the lexer to see if a parse error was caused by
// running out of input.
bool lexerEOF()
{
return eof;
}
// Called by code outside of the lexer when it wants to report the unexpected
// eof as an error (instead of looking for more input).
void reportEOF() {
assert(eof);
error();
em << eofMessage;
em.sync();
}
string stringbuild;
namespace {
void append(char c)
{
stringbuild.push_back(c);
yylval.pos = here();
}
void getstring(void)
{
// NOTE: Replace here() with a position at the start of the string.
makesymbol(false,stringbuild);
string().swap(stringbuild);
}
}
%}
%x lexcomment
%x texstring
%x cstring
%x lexformat
%x opname
LETTER [_A-Za-z]
ESC \\
ENDL \\?(\r\n|\n|\r)
EXTRAOPS <<|>>|$|$$|@|@@|~
%%
<lexcomment>{
\/\* {adjust(); /*commentDepth++;*/}
\*\/ {adjust(); /*commentDepth--;*/
/*if (commentDepth == 0)*/ BEGIN INITIAL; }
\r\n|\n|\r {adjust(); newline(); continue; }
<<EOF>> {adjust();
setEOF("comment not terminated");
BEGIN INITIAL;
return GARBAGE;
}
. {adjust(); continue; }
}
<texstring>{
\"/([ \t]|{ENDL})*[\"\'] {adjust(); BEGIN INITIAL;}
\" {adjust();
BEGIN INITIAL;
getstring();
return STRING; }
<<EOF>> {adjust();
setEOF("string not terminated");
BEGIN INITIAL;
getstring();
return GARBAGE;
}
{ENDL} {adjust(); newline(); append('\n'); continue; }
{ESC}{ESC} {adjust(); append('\\'); append('\\'); continue; }
{ESC}\" {adjust(); append('\"'); continue; }
. {adjust(); append(*yytext); }
}
<cstring>{
\'/([ \t]|{ENDL})*[\"\'] {adjust(); BEGIN INITIAL;}
\' {adjust();
BEGIN INITIAL;
getstring();
return STRING; }
<<EOF>> {adjust();
setEOF("string not terminated");
BEGIN INITIAL;
getstring();
return GARBAGE;
}
{ENDL} {adjust(); newline(); append('\n'); continue; }
{ESC}(\'|\"|\?|\\) {adjust(); append(yytext[1]); continue; }
{ESC}a {adjust(); append('\a'); continue; }
{ESC}b {adjust(); append('\b'); continue; }
{ESC}f {adjust(); append('\f'); continue; }
{ESC}n {adjust(); append('\n'); continue; }
{ESC}r {adjust(); append('\r'); continue; }
{ESC}t {adjust(); append('\t'); continue; }
{ESC}v {adjust(); append('\v'); continue; }
{ESC}[0-7] {adjust();
char x=(char)(yytext[1]-'0');
append(x);
continue;
}
{ESC}[0-7][0-7] {adjust();
char x=(char)((yytext[1]-'0')*8+yytext[2]-'0');
append(x);
continue;
}
{ESC}[0-3][0-7][0-7] {adjust();
char x=(char)((yytext[1]-'0')*64+(yytext[2]-'0')*8
+yytext[3]-'0');
append(x);
continue;
}
{ESC}x[0-9,A-F] {adjust();
char x=(char) (yytext[2] <= '9' ? yytext[2]-'0' :
10+yytext[2]-'A');
append(x);
continue;
}
{ESC}x[0-9,A-F][0-9,A-F] {adjust();
char x=(char) ((yytext[2] <= '9' ? yytext[2]-'0' :
10+yytext[2]-'A')*16
+(yytext[3] <= '9' ? yytext[3]-'0' :
10+yytext[3]-'A'));
append(x);
continue;
}
. {adjust(); append(*yytext); }
}
[ \t] {adjust(); continue;}
{ENDL} {adjust(); newline(); continue;}
\/\/[^\n]* {adjust(); continue;}
"," {adjust(); return ','; }
":" {adjust(); return ':'; }
";" {adjust(); return ';'; }
"(" {adjust(); return '('; }
")" {adjust(); return ')'; }
"[" {adjust(); return '['; }
"]" {adjust(); return ']'; }
"{" {adjust(); return '{'; }
"}" {adjust(); return '}'; }
"." {adjust(); return '.'; }
"..." {adjust(); return ELLIPSIS; }
"+" {makesymbol(); return '+'; }
"-" {makesymbol(); return '-'; }
"*" {makesymbol(); return '*'; }
"/" {makesymbol(); return '/'; }
"%" {makesymbol(); return '%'; }
"^" {makesymbol(); return '^'; }
"**" {makesymbol(true,"^"); return '^'; }
"?" {adjust(); return '?'; }
"=" {adjust(); return ASSIGN; }
"==" {makesymbol(); return EQ; }
"!=" {makesymbol(); return NEQ; }
"<" {makesymbol(); return LT; }
"<=" {makesymbol(); return LE; }
">" {makesymbol(); return GT; }
">=" {makesymbol(); return GE; }
"&&" {makesymbol(); return CAND; }
"||" {makesymbol(); return COR; }
"!" {makesymbol(); return LOGNOT; }
"^^" {makesymbol(); return CARETS; }
"::" {makesymbol(); return COLONS; }
"++" {makesymbol(); return INCR; }
".." {makesymbol(); return DOTS; }
"--" {makesymbol(); return DASHES; }
"---" {makesymbol(); return LONGDASH; }
"&" {makesymbol(); return AMPERSAND; }
"|" {makesymbol(); return BAR; }
{EXTRAOPS} {makesymbol(); return OPERATOR; }
"+=" {makeselfsymbol(); return ADD; }
"-=" {makeselfsymbol(); return SUBTRACT; }
"*=" {makeselfsymbol(); return TIMES; }
"/=" {makeselfsymbol(); return DIVIDE; }
"%=" {makeselfsymbol(); return MOD; }
"^=" {makeselfsymbol(); return EXPONENT; }
and {adjust(); return AND; }
controls {makesymbol(); return CONTROLS; }
tension {makesymbol(); return TENSION; }
atleast {makesymbol(); return ATLEAST; }
curl {makesymbol(); return CURL; }
if {adjust(); return IF; }
else {adjust(); return ELSE; }
while {adjust(); return WHILE; }
for {adjust(); return FOR; }
do {adjust(); return DO; }
return {adjust(); return RETURN_; }
break {adjust(); return BREAK; }
continue {adjust(); return CONTINUE; }
struct {adjust(); return STRUCT; }
typedef {adjust(); return TYPEDEF; }
new {adjust(); return NEW; }
access {adjust(); return ACCESS; }
import {adjust(); return IMPORT; }
unravel {adjust(); return UNRAVEL; }
from {adjust(); return FROM; }
include {adjust(); return INCLUDE; }
quote {adjust(); return QUOTE; }
static {adjust(); makemod(trans::EXPLICIT_STATIC);
return MODIFIER; }
public {adjust(); makeperm(trans::PUBLIC); return PERM; }
private {adjust(); makeperm(trans::PRIVATE); return PERM; }
restricted {adjust(); makeperm(trans::RESTRICTED); return PERM; }
this {adjust(); return THIS; }
explicit {adjust(); return EXPLICIT; }
[0-9]+ try {
adjust(); yylval.e= new intExp(here(), lexical::cast<Int>(yytext));
} catch (lexical::bad_cast&) {
error();
em << "invalid integer";
yylval.e= new intExp(here(), 0);
} return LIT;
([0-9]*\.[0-9]+)|([0-9]+\.[0-9]*)|([0-9]*\.*[0-9]+e[-+]*[0-9]+)|([0-9]+\.[0-9]*e[-+]*[0-9]+) try {
adjust(); yylval.e= new realExp(here(), lexical::cast<double>(yytext));
} catch (lexical::bad_cast&) {
error();
em << "invalid real";
yylval.e= new realExp(here(), 0);
} return LIT;
true {
adjust(); yylval.e= new booleanExp(here(), true); return LIT; }
false {
adjust(); yylval.e= new booleanExp(here(), false); return LIT; }
null {
adjust(); yylval.e= new nullExp(here()); return LIT; }
cycle {
adjust(); yylval.e= new cycleExp(here()); return LIT; }
newframe {
adjust(); yylval.e= new newPictureExp(here()); return LIT; }
operator {adjust(); BEGIN opname; }
<opname>{
[ \t\r] {adjust(); continue;}
{ENDL} {adjust(); newline(); continue;}
<<EOF>> {adjust();
setEOF("missing operator name");
BEGIN INITIAL;
return GARBAGE;
}
"**" { makesymbol(true, "^");
BEGIN INITIAL;
return ID;
}
[-+*/%^!<>]|==|!=|<=|>=|&|\||\^\^|\.\.|::|--|---|\+\+|{EXTRAOPS} {
makesymbol();
BEGIN INITIAL;
return ID;}
{LETTER}({LETTER}|[0-9])* {
makesymbol(true);
BEGIN INITIAL;
return ID; }
. {}
}
{LETTER}({LETTER}|[0-9])* {
makesymbol(false);
return ID; }
\/\* {adjust(); /*commentDepth = 1;*/ BEGIN lexcomment; }
\" {adjust(); BEGIN texstring; }
\' {adjust(); BEGIN cstring; }
<<EOF>> { setEOF("unexpected end of input"); yyterminate(); }
. {
adjust(); error(); em <<"invalid token '" << yytext << "'"; }
|