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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
%{
/*****
* camp.l
* Andy Hammerlindl 2002/06/14
*
* The lexical analyzer of the Asymptote language.
*****/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#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"
#include "opsymbols.h"
static void yyunput(int, char *);
void (*unused)(int,char *) = yyunput;
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 savesymbol(symbol name)
{
adjust();
yylval.ps.pos=yylval.pos; // avoid invoking here() twice
yylval.ps.sym=name;
}
/* For optimization reasons, the operator names are translated into symbols
* just once, and can be accessed throughout the code as SYM_PLUS, SYM_DASHES,
* etc. Following the Don't Repeat Yourself principle, the mapping from
* strings to names is defined only here in camp.l (because we can't produce
* lex rules from a C style macro).
* The script opsymbols.pl reads this file scanning for rules using DEFSYMBOL
* and creates opsymbols.h which defines the names for use in C++ code.
*/
#define DEFSYMBOL(name) \
savesymbol(name)
void makesymbol()
{
assert(strlen(yytext) == (size_t)yyleng);
savesymbol(symbol::rawTrans(yytext, yyleng+1));
}
void makeopsymbol()
{
savesymbol(symbol::opTrans(yytext));
}
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();
}
position stringpos; // The position of the start of the string.
string stringbuild; // Stores the string literal as it is read.
namespace {
void startstring()
{
adjust();
stringpos = here();
}
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.
yylval.stre = new stringExp(stringpos, 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; }
"+" {DEFSYMBOL(SYM_PLUS); return '+'; }
"-" {DEFSYMBOL(SYM_MINUS); return '-'; }
"*" {DEFSYMBOL(SYM_TIMES); return '*'; }
"/" {DEFSYMBOL(SYM_DIVIDE); return '/'; }
"%" {DEFSYMBOL(SYM_MOD); return '%'; }
"^" {DEFSYMBOL(SYM_CARET); return '^'; }
"**" {savesymbol(SYM_CARET); return '^'; }
"?" {adjust(); return '?'; }
"=" {adjust(); return ASSIGN; }
"==" {DEFSYMBOL(SYM_EQ); return EQ; }
"!=" {DEFSYMBOL(SYM_NEQ); return NEQ; }
"<" {DEFSYMBOL(SYM_LT); return LT; }
"<=" {DEFSYMBOL(SYM_LE); return LE; }
">" {DEFSYMBOL(SYM_GT); return GT; }
">=" {DEFSYMBOL(SYM_GE); return GE; }
"&&" {DEFSYMBOL(SYM_CAND); return CAND; }
"||" {DEFSYMBOL(SYM_COR); return COR; }
"!" {DEFSYMBOL(SYM_LOGNOT); return LOGNOT; }
"^^" {DEFSYMBOL(SYM_CARETS); return CARETS; }
"::" {DEFSYMBOL(SYM_COLONS); return COLONS; }
"++" {DEFSYMBOL(SYM_INCR); return INCR; }
".." {DEFSYMBOL(SYM_DOTS); return DOTS; }
"--" {DEFSYMBOL(SYM_DASHES); return DASHES; }
"---" {DEFSYMBOL(SYM_LONGDASH); return LONGDASH; }
"&" {DEFSYMBOL(SYM_AMPERSAND); return AMPERSAND; }
"|" {DEFSYMBOL(SYM_BAR); return BAR; }
{EXTRAOPS} {makeopsymbol(); return OPERATOR; }
"+=" {savesymbol(SYM_PLUS); return SELFOP; }
"-=" {savesymbol(SYM_MINUS); return SELFOP; }
"*=" {savesymbol(SYM_TIMES); return SELFOP; }
"/=" {savesymbol(SYM_DIVIDE); return SELFOP; }
"%=" {savesymbol(SYM_MOD); return SELFOP; }
"^=" {savesymbol(SYM_CARET); return SELFOP; }
and {adjust(); return AND; }
controls {DEFSYMBOL(SYM_CONTROLS); return CONTROLS; }
tension {DEFSYMBOL(SYM_TENSION); return TENSION; }
atleast {DEFSYMBOL(SYM_ATLEAST); return ATLEAST; }
curl {DEFSYMBOL(SYM_CURL); 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;
}
"**" { savesymbol(SYM_CARET);
BEGIN INITIAL;
return ID;
}
[-+*/%^!<>]|==|!=|<=|>=|&|\||\^\^|\.\.|::|--|---|\+\+|{EXTRAOPS} {
makeopsymbol();
BEGIN INITIAL;
return ID;}
{LETTER}({LETTER}|[0-9])* {
makeopsymbol();
BEGIN INITIAL;
return ID; }
. {}
}
{LETTER}({LETTER}|[0-9])* {
makesymbol();
return ID; }
\/\* {adjust(); /*commentDepth = 1;*/ BEGIN lexcomment; }
\" {startstring(); BEGIN texstring; }
\' {startstring(); BEGIN cstring; }
<<EOF>> { setEOF("unexpected end of input"); yyterminate(); }
. {adjust();
error();
em << "invalid token";
if (isgraph(yytext[0]))
em << " '" << yytext[0] << "'";
}
|