Debian Codegraph @

src/core/chuck.lex

raw
1
2 D [0-9]
3 L [a-zA-Z_]
4 H [a-fA-F0-9]
5 E [Ee][+-]?{D}+
6 FS (f|F|l|L)
7 IS (u|U|l|L)*
8
9 %{
10
11 /*----------------------------------------------------------------------------
12 ChucK Strongly-timed Audio Programming Language
13 Compiler and Virtual Machine
14
15 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
16 http://chuck.cs.princeton.edu/
17 http://soundlab.cs.princeton.edu/
18
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 U.S.A.
33 -----------------------------------------------------------------------------*/
34
35 //-----------------------------------------------------------------------------
36 // file: chuck.yy.c
37 // desc: chuck lexer
38 //
39 // author: Ge Wang ([email protected]) - generated by lex
40 // Perry R. Cook ([email protected])
41 //
42 // initial version created by Ge Wang;
43 // based on ansi C grammar by Jeff Lee, maintained by Jutta Degener
44 //
45 // date: Summer 2002
46 //-----------------------------------------------------------------------------
47 #include "chuck_absyn.h"
48 #include "chuck_errmsg.h"
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 // check platforms
54 #if !defined(__PLATFORM_WINDOWS__) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__USE_CHUCK_YACC__)
55 #include "chuck.tab.h" // use fresh bison/flex-generated header
56 #else
57 #include "chuck_yacc.h" // use pre-generated header (no bison/flex needed)
58 // created by `make chuck_yacc.h` and `make chuck_yacc.c` in core/
59 #endif
60
61 // globals
62 extern YYSTYPE yylval;
63 // 1.5.0.5 (ge) added for precise location tracking
64 extern YYLTYPE yylloc;
65 // 1.5.0.5 (ge) added
66 t_CKINT yycolumn = 1;
67
68 // define error handling
69 #define YY_FATAL_ERROR(msg) EM_error2( 0, msg )
70
71 #if defined(_cplusplus) || defined(__cplusplus)
72 extern "C" {
73 #endif
74
75 // function prototypes
76 int yywrap(void);
77 void adjust();
78 c_str strip_lit( c_str str );
79 c_str alloc_str( c_str str );
80 long htol( c_str str );
81
82 // 1.5.0.5 (ge) added
83 void a_newline( void );
84 void yycleanup( void );
85 void yyinitial( void );
86 void yyflush( void );
87
88 #if defined(_cplusplus) || defined(__cplusplus)
89 }
90 #endif
91
92 // yycleanup | 1.5.0.5 (ge)
93 void yycleanup( void )
94 {
95 // clean up the FILE
96 if( yyin )
97 {
98 fclose( yyin );
99 yyin = NULL;
100 }
101 }
102
103 // yyflush | 1.5.0.5 (ge)
104 void yyflush( void )
105 {
106 // flush the buffer
107 YY_FLUSH_BUFFER;
108 }
109
110 // yyinitial | 1.5.0.5 (ge)
111 void yyinitial( void )
112 {
113 // set to initial
114 BEGIN(YY_START);
115 // reset
116 yycolumn = 1;
117 EM_tokPos = yycolumn;
118 yylloc.first_line = yylloc.last_line = 1;
119 yylloc.first_column = yylloc.last_column = 0;
120 // 1.5.2.4 (ge) added
121 yylineno = 1;
122 }
123
124 // yywrap()
125 int yywrap( void )
126 {
127 yycolumn = 1;
128 return 1;
129 }
130
131 // new line
132 void a_newline()
133 {
134 EM_newline( yylloc.last_column, yylloc.last_line );
135 }
136
137 // manually advance
138 void advance_m()
139 {
140 yycolumn++;
141 yylloc.first_column++;
142 yylloc.last_column++;
143 }
144
145 // adjust()
146 void adjust()
147 {
148 // update tok pos
149 EM_tokPos = yylloc.first_column;
150
151 // handy debug print for precise position and bounds of each token
152 // fprintf( stderr, "yylloc: %d %ld %d %d ------ %s\n", yylloc.first_line, yycolumn, yylloc.last_line, yylloc.last_column, yytext );
153 }
154
155 // strip
156 c_str strip_lit( c_str str )
157 {
158 str[strlen(str)-1] = '\0';
159 return str+1;
160 }
161
162 // alloc_str()
163 c_str alloc_str( c_str str )
164 {
165 c_str s = (c_str)malloc( strlen(str) + 1 );
166 strcpy( s, str );
167
168 return s;
169 }
170
171 // to long
172 long htol( c_str str )
173 {
174 char * c = str;
175 unsigned long n = 0;
176
177 // skip over 0x
178 c += 2;
179 while( *c )
180 {
181 n <<= 4;
182 switch( *c )
183 {
184 case '1': case '2': case '3': case '4': case '5':
185 case '6': case '7': case '8': case '9': case '0':
186 n += *c - '0';
187 break;
188
189 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
190 n += *c - 'a' + 10;
191 break;
192
193 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
194 n += *c - 'A' + 10;
195 break;
196 }
197 c++;
198 }
199
200 return n;
201 }
202
203 // for debugging (string literals, especially multi-line strings)
204 void testLineNumPrint()
205 {
206 fprintf( stderr, "TEST: %ld %i\n", EM_lineNum, yylloc.last_line );
207 }
208
209
210 // block comment hack
211 #define block_comment_hack loop: \
212 while ((c = input()) != '*' && c != 0 && c != EOF ) { \
213 if( c == '\n' ) { a_newline(); } \
214 else { advance_m(); adjust(); } \
215 } \
216 if( c == EOF || c == 0 ) /* EOF; return 0 */ { adjust(); return 0; } \
217 if( (c1 = input()) != '/' && c != 0 && c != EOF ) { \
218 advance_m(); \
219 adjust(); \
220 unput(c1); \
221 goto loop; \
222 } \
223 if( c1 == EOF || c1 == 0 ) /* EOF; return 0 */ { adjust(); return 0; } \
224 else { advance_m(); advance_m(); adjust(); };
225
226
227 // comment hack
228 #define comment_hack \
229 while( (c = input()) != '\n' && c != '\r' && c != 0 && c != EOF ) ; \
230 if( c == EOF || c == 0 ) /* EOF; return 0 */ { adjust(); return 0; } \
231 if( c == '\n' ) { a_newline(); }
232
233
234 // 1.5.0.5 added for tracking | (thanks ekeyser + Becca Royal-Gordon)
235 // https://stackoverflow.com/questions/656703/how-does-flex-support-bison-location-exactly
236 // https://www.gnu.org/software/bison/manual/html_node/Tracking-Locations.html
237 #define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
238 yylloc.first_column = yycolumn; yylloc.last_column = yycolumn + yyleng - 1; \
239 yycolumn += yyleng;
240
241
242 %}
243
244 /* generate line number */
245 %option yylineno
246
247 /* 1.5.0.7 (ge) added to remove dependecy on isatty() and unistd.h
248 without the above option, flex/bison will check isatty() to determine
249 whether it's getting its input from a TTY terminal input OR file/pipe,
250 and chooses a corresponding caching scheme. since chuck is passing
251 data to the parser either as a file or through a string buffer using
252 yy_scan_string(), chuck is never using using the parser for direct
253 TTY input. (FYI the opposite of this option is `%option interactive`
254 if neither `never-interactive` nor `interactive` is specified, bison
255 will test using isatty()) */
256 %option never-interactive
257
258 /* 1.5.1.5 (ge) reentrant lexer */
259 /* %option reentrant */
260
261 /* float exponent | 1.5.0.5 (ge) */
262 EXP ([Ee][-+]?[0-9]+)
263 /* universal character name */
264 UCN (\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})
265 /* thanks O'Reilly book for the above recipes for STRING_LIT / CHAR_LIT / FLOAT_VAL
266 https://web.iitd.ac.in/~sumeet/flex__bison.pdf */
267
268 %%
269 /* --------------------- RULES SECTION --------------------------
270 NOTE in this section, begin comments on a new line AND
271 with one or more whitespaces before comment
272 --------------------------------------------------------------
273 NOTE since . matches anything except a newline,
274 .* will gobble up the rest of the line
275 (from /Flex & Bison/ by John Levin, published O'Reilly 2009)
276 ALTERNATIVE "//".* { char c; adjust(); continue; }
277 ---------------------------------------------------------------*/
278 /* "<--" { char c; adjust(); comment_hack; continue; } */
279 /* ------------------------------------------------------------ */
280 "//" { char c; adjust(); comment_hack; continue; }
281 "/*" { char c, c1; adjust(); block_comment_hack; continue; }
282 " " { adjust(); continue; }
283 "\t" { adjust(); continue; }
284 "\r\n" { adjust(); a_newline(); continue; }
285 "\n" { adjust(); a_newline(); continue; }
286
287 "++" { adjust(); return PLUSPLUS; }
288 "--" { adjust(); return MINUSMINUS; }
289 "#(" { adjust(); return POUNDPAREN; }
290 "%(" { adjust(); return PERCENTPAREN; }
291 "@(" { adjust(); return ATPAREN; }
292
293 "," { adjust(); return COMMA; }
294 ":" { adjust(); return COLON; }
295 "." { adjust(); return DOT; }
296 "+" { adjust(); return PLUS; }
297 "-" { adjust(); return MINUS; }
298 "*" { adjust(); return TIMES; }
299 "/" { adjust(); return DIVIDE; }
300 "%" { adjust(); return PERCENT; }
301 "#" { adjust(); return POUND; }
302 "$" { adjust(); return DOLLAR; }
303
304 "::" { adjust(); return COLONCOLON; }
305 "==" { adjust(); return EQ; }
306 "!=" { adjust(); return NEQ; }
307 "<" { adjust(); return LT; }
308 ">" { adjust(); return GT; }
309 "<=" { adjust(); return LE; }
310 ">=" { adjust(); return GE; }
311 "&&" { adjust(); return AND; }
312 "||" { adjust(); return OR; }
313 "&" { adjust(); return S_AND; }
314 "|" { adjust(); return S_OR; }
315 "^" { adjust(); return S_XOR; }
316 ">>" { adjust(); return SHIFT_RIGHT; }
317 "<<" { adjust(); return SHIFT_LEFT; }
318 "=" { adjust(); return ASSIGN; }
319 "(" { adjust(); return LPAREN; }
320 ")" { adjust(); return RPAREN; }
321 "[" { adjust(); return LBRACK; }
322 "]" { adjust(); return RBRACK; }
323 "{" { adjust(); return LBRACE; }
324 "}" { adjust(); return RBRACE; }
325 ";" { adjust(); return SEMICOLON; }
326 "?" { adjust(); return QUESTION; }
327 "!" { adjust(); return EXCLAMATION; }
328 "~" { adjust(); return TILDA; }
329 for { adjust(); return FOR; }
330 while { adjust(); return WHILE; }
331 until { adjust(); return UNTIL; }
332 repeat { adjust(); return LOOP; }
333 continue { adjust(); return CONTINUE; }
334 break { adjust(); return BREAK; }
335 if { adjust(); return IF; }
336 else { adjust(); return ELSE; }
337 do { adjust(); return DO; }
338 "<<<" { adjust(); return L_HACK; }
339 ">>>" { adjust(); return R_HACK; }
340
341 return { adjust(); return RETURN; }
342
343 function { adjust(); return FUNCTION; }
344 fun { adjust(); return FUNCTION; }
345 new { adjust(); return NEW; }
346 class { adjust(); return CLASS; }
347 interface { adjust(); return INTERFACE; }
348 extends { adjust(); return EXTENDS; }
349 implements { adjust(); return IMPLEMENTS; }
350 public { adjust(); return PUBLIC; }
351 protected { adjust(); return PROTECTED; }
352 private { adjust(); return PRIVATE; }
353 static { adjust(); return STATIC; }
354 pure { adjust(); return ABSTRACT; }
355 const { adjust(); return CONST; }
356 spork { adjust(); return SPORK; }
357 typeof { adjust(); return TYPEOF; }
358 external { adjust(); return EXTERNAL; }
359 global { adjust(); return GLOBAL; }
360
361 "=>" { adjust(); return CHUCK; }
362 "=<" { adjust(); return UNCHUCK; }
363 "!=>" { adjust(); return UNCHUCK; }
364 "=^" { adjust(); return UPCHUCK; }
365 "=v" { adjust(); return DOWNCHUCK; }
366 "@=>" { adjust(); return AT_CHUCK; }
367 "+=>" { adjust(); return PLUS_CHUCK; }
368 "-=>" { adjust(); return MINUS_CHUCK; }
369 "*=>" { adjust(); return TIMES_CHUCK; }
370 "/=>" { adjust(); return DIVIDE_CHUCK; }
371 "&=>" { adjust(); return S_AND_CHUCK; }
372 "|=>" { adjust(); return S_OR_CHUCK; }
373 "^=>" { adjust(); return S_XOR_CHUCK; }
374 ">>=>" { adjust(); return SHIFT_RIGHT_CHUCK; }
375 "<<=>" { adjust(); return SHIFT_LEFT_CHUCK; }
376 "%=>" { adjust(); return PERCENT_CHUCK; }
377 "@" { adjust(); return AT_SYM; }
378 "@@" { adjust(); return ATAT_SYM; }
379 "@operator" { adjust(); return AT_OP; }
380 "@construct" { adjust(); return AT_CTOR; }
381 "@destruct" { adjust(); return AT_DTOR; }
382 "@import" { adjust(); return AT_IMPORT; }
383 "@doc" { adjust(); return AT_DOC; }
384 "@example" { adjust(); return AT_EXAMPLE; }
385 "->" { adjust(); return ARROW_RIGHT; }
386 "<-" { adjust(); return ARROW_LEFT; }
387 "-->" { adjust(); return GRUCK_RIGHT; }
388 "<--" { adjust(); return GRUCK_LEFT; }
389 "--<" { adjust(); return UNGRUCK_RIGHT; }
390 ">--" { adjust(); return UNGRUCK_LEFT; }
391
392 [A-Za-z_][A-Za-z0-9_]* { adjust(); yylval.sval=alloc_str(yytext); return ID; }
393
394 [0-9]+{IS}? { adjust(); yylval.ival=atoi(yytext); return INT_VAL; }
395 0[cC][0-7]+{IS}? { adjust(); yylval.ival=atoi(yytext); return INT_VAL; }
396 0[xX][0-9a-fA-F]+{IS}? { adjust(); yylval.ival=htol(yytext); return INT_VAL; }
397
398 [0-9]+{EXP}[flFL]? { adjust(); yylval.fval=atof(yytext); return FLOAT_VAL; }
399 ([0-9]*\.[0-9]+|[0-9]+\.){EXP}?[flFL]? { adjust(); yylval.fval=atof(yytext); return FLOAT_VAL; }
400 0[Xx]([0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.?)[Pp][-+]?[0-9]+[flFL]? { adjust(); yylval.fval=atof(yytext); return FLOAT_VAL; }
401
402 \"([^"\\]|\\['"?\\abfnrtv]|\\[0-7]{1,3}|\\[Xx][0-9a-fA-F]+|{UCN})*\" { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
403 \`([^`\\]|\\['"?\\abfnrtv]|\\[0-7]{1,3}|\\[Xx][0-9a-fA-F]+|{UCN})*\` { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
404 \'([^'\\]|\\['"?\\abfnrtv]|\\[0-7]{1,3}|\\[Xx][0-9a-fA-F]+|{UCN})+\' { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return CHAR_LIT; }
405
406 . { adjust(); EM_error( EM_tokPos, "illegal token" ); }
407
408 %%
409
410 // older
411 // ([0-9]+"."[0-9]*)|([0-9]*"."[0-9]+) { adjust(); yylval.fval=atof(yytext); return FLOAT_VAL; }
412 // \"(\\.|[^\\"])*\" { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
413 // `(\\.|[^\\`])*` { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
414 // '(\\.|[^\\'])' { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return CHAR_LIT; }