*** empty log message ***
[chuck-blob.git] / v2 / chuck.lex
blob48582c15a268982a39d3d6de34fe4f9ee7176a4b
3 D           [0-9]
4 L           [a-zA-Z_]
5 H           [a-fA-F0-9]
6 E           [Ee][+-]?{D}+
7 FS          (f|F|l|L)
8 IS          (u|U|l|L)*
11 /*----------------------------------------------------------------------------
12     ChucK Concurrent, On-the-fly Audio Programming Language
13       Compiler and Virtual Machine
15     Copyright (c) 2004 Ge Wang and Perry R. Cook.  All rights reserved.
16       http://chuck.cs.princeton.edu/
17       http://soundlab.cs.princeton.edu/
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.
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.
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 -----------------------------------------------------------------------------*/
35 //-----------------------------------------------------------------------------
36 // file: chuck.yy.c
37 // desc: chuck lexer
39 // author: Ge Wang (gewang.cs.princeton.edu) - generated by lex
40 //         Perry R. Cook (prc@cs.princeton.edu)
42 // based on the ansi C grammar by Jeff Lee, maintained by Jutta Degener 
43 // 
44 // date: Summer 2002
45 //-----------------------------------------------------------------------------
47 #include <stdlib.h>
48 #include <string.h>
49 #include "chuck_utils.h"
50 #include "chuck_absyn.h"
51 #include "chuck_errmsg.h"
53 #ifndef __PLATFORM_WIN32__
54   #include "chuck.tab.h"
55 #else
56   #include "chuck_win32.h"
57 #endif
61 // globals
62 extern YYSTYPE yylval;
63 int char_pos = 1;
65 // define error handling
66 #define YY_FATAL_ERROR(msg) EM_error2( 0, msg )
68 #if defined(_cplusplus) || defined(__cplusplus)
69 extern "C" {
70 #endif
72   int yywrap(void);
73   void adjust();
74   c_str strip_lit( c_str str );
75   c_str alloc_str( c_str str );
76   long htol( c_str str );
77   int comment();
78   int block_comment();
80 #if defined(_cplusplus) || defined(__cplusplus)
82 #endif
84 // yywrap()
85 int yywrap( void )
87     char_pos = 1;
88     return 1;
91 // adjust()
92 void adjust()
94     EM_tokPos = char_pos;
95     char_pos += yyleng;
98 // strip
99 c_str strip_lit( c_str str )
101     str[strlen(str)-1] = '\0';
102     return str+1;
105 // alloc_str()
106 c_str alloc_str( c_str str )
108     c_str s = (c_str)malloc( strlen(str) + 1 );
109     strcpy( s, str );
111     return s;
114 // to long
115 long htol( c_str str )
117     char * c = str;
118     unsigned long n = 0;
120     // skip over 0x
121     c += 2;
122     while( *c )
123     {
124         n <<= 4; 
125         switch( *c )
126         {
127         case '1': case '2': case '3': case '4': case '5':
128         case '6': case '7': case '8': case '9': case '0':
129             n += *c - '0';
130             break;
132         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
133             n += *c - 'a' + 10;
134             break;
136         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
137             n += *c - 'A' + 10;
138             break;
139         }    
140         c++;
141     }
143     return n;
146 // block comment hack (thanks to unput/yytext_ptr inconsistency)
147 #define block_comment_hack loop: \
148     while ((c = input()) != '*' && c != 0 && c != EOF ) \
149         if( c == '\n' ) EM_newline(); \
150     if( c == EOF ) adjust(); \
151     else if( (c1 = input()) != '/' && c != 0 ) \
152     { \
153         unput(c1); \
154         goto loop; \
155     } \
156     if( c != 0 ) adjust();
158 // comment hack
159 #define comment_hack \
160     while ((c = input()) != '\n' && c != '\r' && c != 0 && c != EOF ); \
161     if (c != 0) { \
162        adjust(); \
163        if (c == '\n') EM_newline(); \
164     }
166 // .*\-\->                 { adjust(); continue; }
172 "//"                    { char c; adjust(); comment_hack; continue; }
173 "<--"                   { char c; adjust(); comment_hack; continue; }
174 "/*"                    { char c, c1; adjust(); block_comment_hack; continue; }
175 " "                     { adjust(); continue; }
176 "\t"                    { adjust(); continue; }
177 "\r\n"                  { adjust(); EM_newline(); continue; }
178 "\n"                    { adjust(); EM_newline(); continue; }
180 "++"                    { adjust(); return PLUSPLUS; }
181 "--"                    { adjust(); return MINUSMINUS; }
182 "#("                    { adjust(); return POUNDPAREN; }
183 "%("                    { adjust(); return PERCENTPAREN; }
185 ","                     { adjust(); return COMMA; }
186 ":"                     { adjust(); return COLON; }
187 "."                     { adjust(); return DOT; }
188 "+"                     { adjust(); return PLUS; }
189 "-"                     { adjust(); return MINUS; }
190 "*"                     { adjust(); return TIMES; }
191 "/"                     { adjust(); return DIVIDE; }
192 "%"                     { adjust(); return PERCENT; }
193 "#"                     { adjust(); return POUND; }
194 "$"                     { adjust(); return DOLLAR; }
196 "::"                    { adjust(); return COLONCOLON; }
197 "=="                    { adjust(); return EQ; }
198 "!="                    { adjust(); return NEQ; }
199 "<"                     { adjust(); return LT; }
200 ">"                     { adjust(); return GT; }
201 "<="                    { adjust(); return LE; }
202 ">="                    { adjust(); return GE; }
203 "&&"                    { adjust(); return AND; }
204 "||"                    { adjust(); return OR; }
205 "&"                     { adjust(); return S_AND; }
206 "|"                     { adjust(); return S_OR; }
207 "^"                     { adjust(); return S_XOR; }
208 ">>"                    { adjust(); return SHIFT_RIGHT; }
209 "<<"                    { adjust(); return SHIFT_LEFT; }
210 "="                     { adjust(); return ASSIGN; }
211 "("                     { adjust(); return LPAREN; }
212 ")"                     { adjust(); return RPAREN; }
213 "["                     { adjust(); return LBRACK; }
214 "]"                     { adjust(); return RBRACK; }
215 "{"                     { adjust(); return LBRACE; }
216 "}"                     { adjust(); return RBRACE; }
217 ";"                     { adjust(); return SEMICOLON; }
218 "?"                     { adjust(); return QUESTION; }
219 "!"                     { adjust(); return EXCLAMATION; }
220 "~"                     { adjust(); return TILDA; }
221 for                     { adjust(); return FOR; }
222 while                   { adjust(); return WHILE; }
223 until                   { adjust(); return UNTIL; }
224 repeat                  { adjust(); return LOOP; }
225 continue                { adjust(); return CONTINUE; }
226 break                   { adjust(); return BREAK; }
227 if                      { adjust(); return IF; }
228 else                    { adjust(); return ELSE; }
229 do                      { adjust(); return DO; }
230 "<<<"                   { adjust(); return L_HACK; }
231 ">>>"                   { adjust(); return R_HACK; }
233 return                  { adjust(); return RETURN; }
235 function                { adjust(); return FUNCTION; }
236 fun                     { adjust(); return FUNCTION; }
237 new                     { adjust(); return NEW; }
238 class                   { adjust(); return CLASS; }
239 interface               { adjust(); return INTERFACE; }
240 extends                 { adjust(); return EXTENDS; }
241 implements              { adjust(); return IMPLEMENTS; }
242 public                  { adjust(); return PUBLIC; }
243 protected               { adjust(); return PROTECTED; }
244 private                 { adjust(); return PRIVATE; }
245 static                  { adjust(); return STATIC; }
246 pure                    { adjust(); return ABSTRACT; }
247 const                   { adjust(); return CONST; }
248 spork                   { adjust(); return SPORK; }
249 typeof                  { adjust(); return TYPEOF; }
251 "=>"                    { adjust(); return CHUCK; }
252 "=<"                    { adjust(); return UNCHUCK; }
253 "!=>"                   { adjust(); return UNCHUCK; }
254 "=^"                    { adjust(); return UPCHUCK; }
255 "@=>"                   { adjust(); return AT_CHUCK; }
256 "+=>"                   { adjust(); return PLUS_CHUCK; }
257 "-=>"                   { adjust(); return MINUS_CHUCK; }
258 "*=>"                   { adjust(); return TIMES_CHUCK; }
259 "/=>"                   { adjust(); return DIVIDE_CHUCK; }
260 "&=>"                   { adjust(); return S_AND_CHUCK; }
261 "|=>"                   { adjust(); return S_OR_CHUCK; }
262 "^=>"                   { adjust(); return S_XOR_CHUCK; }
263 ">>=>"                  { adjust(); return SHIFT_RIGHT_CHUCK; }
264 "<<=>"                  { adjust(); return SHIFT_LEFT_CHUCK; }
265 "%=>"                   { adjust(); return PERCENT_CHUCK; }
266 "@"                     { adjust(); return AT_SYM; }
267 "@@"                    { adjust(); return ATAT_SYM; }
268 "->"                    { adjust(); return ARROW_RIGHT; }
269 "<-"                    { adjust(); return ARROW_LEFT; }
271 0[xX][0-9a-fA-F]+{IS}?  { adjust(); yylval.ival=htol(yytext); return NUM; }
272 0[cC][0-7]+{IS}?        { adjust(); yylval.ival=atoi(yytext); return NUM; }
273 [0-9]+{IS}?             { adjust(); yylval.ival=atoi(yytext); return NUM; }
274 ([0-9]+"."[0-9]*)|([0-9]*"."[0-9]+)   { adjust(); yylval.fval=atof(yytext); return FLOAT; }
275 [A-Za-z_][A-Za-z0-9_]*  { adjust(); yylval.sval=alloc_str(yytext); return ID; }
276 \"(\\.|[^\\"])*\"       { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
278 .                       { adjust(); EM_error( EM_tokPos, "illegal token" ); }
283 // comment
284 int comment()
286     char c;
288     while ((c = yyinput()) != '\n' && c != '\r' && c != 0 && c != EOF );
290     if (c != 0) { 
291        adjust(); 
292        if (c == '\n') EM_newline();
293     }
294     
295     return 0;
298 // block comment
299 int block_comment()
301     char c, c1;
303 loop:
304     while ((c = yyinput()) != '*' && c != 0 && c != EOF )
305         if( c == '\n' ) EM_newline();
307     if( c == EOF )
308     {
309         adjust();
310         return 1;
311     }
312     
313     if( (c1 = yyinput()) != '/' && c != 0 )
314     {
315         unput(c1);
316         goto loop;
317     }
318     
319     if( c != 0 ) adjust();
321     return 0;