*** empty log message ***
[chuck-blob.git] / v1 / chuck.lex
blob7b8607556015a80e4f0da81a556db079f58704a1
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)*
9 %{
10 /*----------------------------------------------------------------------------
11     ChucK Concurrent, On-the-fly Audio Programming Language
12       Compiler and Virtual Machine
14     Copyright (c) 2004 Ge Wang and Perry R. Cook.  All rights reserved.
15       http://chuck.cs.princeton.edu/
16       http://soundlab.cs.princeton.edu/
18     This program is free software; you can redistribute it and/or modify
19     it under the terms of the GNU General Public License as published by
20     the Free Software Foundation; either version 2 of the License, or
21     (at your option) any later version.
23     This program is distributed in the hope that it will be useful,
24     but WITHOUT ANY WARRANTY; without even the implied warranty of
25     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26     GNU General Public License for more details.
28     You should have received a copy of the GNU General Public License
29     along with this program; if not, write to the Free Software
30     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31     U.S.A.
32 -----------------------------------------------------------------------------*/
34 //-----------------------------------------------------------------------------
35 // file: chuck.yy.c
36 // desc: chuck lexer
38 // author: Ge Wang (gewang.cs.princeton.edu) - generated by lex
39 //         Perry R. Cook (prc@cs.princeton.edu)
41 // based on the ansi C grammar by Jeff Lee, maintained by Jutta Degener 
42 // 
43 // date: Summer 2002
44 //-----------------------------------------------------------------------------
46 #include <stdlib.h>
47 #include <string.h>
48 #include "chuck_utils.h"
49 #include "chuck_absyn.h"
50 #include "chuck_errmsg.h"
52 #ifndef __PLATFORM_WIN32__
53   #include "chuck.tab.h"
54 #else
55   #include "chuck_win32.h"
56 #endif
60 // globals
61 extern YYSTYPE yylval;
62 int char_pos = 1;
64 // yywrap()
65 int yywrap( void )
67     char_pos = 1;
68     return 1;
71 // adjust()
72 void adjust()
74     EM_tokPos = char_pos;
75     char_pos += yyleng;
78 // strip
79 c_str strip_lit( c_str str )
81     str[strlen(str)-1] = '\0';
82     return str+1;
85 // alloc_str()
86 c_str alloc_str( c_str str )
88     c_str s = (c_str)malloc( strlen(str) + 1 );
89     strcpy( s, str );
91     return s;
94 // to long
95 long htol( c_str str )
97     char * c = str;
98     unsigned long n = 0;
100     // skip over 0x
101     c += 2;
102     while( *c )
103     {
104         n <<= 4; 
105         switch( *c )
106         {
107         case '1': case '2': case '3': case '4': case '5':
108         case '6': case '7': case '8': case '9': case '0':
109             n += *c - '0';
110             break;
112         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
113             n += *c - 'a' + 10;
114             break;
116         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
117             n += *c - 'A' + 10;
118             break;
119         }    
120         c++;
121     }
123     return n;
126 int comment();
132 "//"                    { adjust(); comment(); continue; }
133 " "                     { adjust(); continue; }
134 "\t"                    { adjust(); continue; }
135 "\r\n"                  { adjust(); EM_newline(); continue; }
136 "\n"                    { adjust(); EM_newline(); continue; }
138 "++"                    { adjust(); return PLUSPLUS; }
139 "--"                    { adjust(); return MINUSMINUS; }
141 ","                     { adjust(); return COMMA; }
142 ":"                     { adjust(); return COLON; }
143 "."                     { adjust(); return DOT; }
144 "+"                     { adjust(); return PLUS; }
145 "-"                     { adjust(); return MINUS; }
146 "*"                     { adjust(); return TIMES; }
147 "/"                     { adjust(); return DIVIDE; }
148 "%"                     { adjust(); return PERCENT; }
149 "#"                     { adjust(); return POUND; }
151 "(:"                    { adjust(); return L_NSPC; }
152 ":)"                    { adjust(); return R_NSPC; }
153 "::"                    { adjust(); return COLONCOLON; }
154 "=="                    { adjust(); return EQ; }
155 "!="                    { adjust(); return NEQ; }
156 "<"                     { adjust(); return LT; }
157 ">"                     { adjust(); return GT; }
158 "<="                    { adjust(); return LE; }
159 ">="                    { adjust(); return GE; }
160 "&&"                    { adjust(); return AND; }
161 "||"                    { adjust(); return OR; }
162 "&"                     { adjust(); return S_AND; }
163 "|"                     { adjust(); return S_OR; }
164 "^"                     { adjust(); return S_XOR; }
165 ">>"                    { adjust(); return SHIFT_RIGHT; }
166 "<<"                    { adjust(); return SHIFT_LEFT; }
167 "="                     { adjust(); return ASSIGN; }
168 "("                     { adjust(); return LPAREN; }
169 ")"                     { adjust(); return RPAREN; }
170 "["                     { adjust(); return LBRACK; }
171 "]"                     { adjust(); return RBRACK; }
172 "{"                     { adjust(); return LBRACE; }
173 "}"                     { adjust(); return RBRACE; }
174 ";"                     { adjust(); return SEMICOLON; }
175 "?"                     { adjust(); return QUESTION; }
176 "!"                     { adjust(); return EXCLAMATION; }
177 "~"                     { adjust(); return TILDA; }
178 for                     { adjust(); return FOR; }
179 while                   { adjust(); return WHILE; }
180 break                   { adjust(); return BREAK; }
181 if                      { adjust(); return IF; }
182 else                    { adjust(); return ELSE; }
183 then                    { adjust(); return THEN; }
185 NULL                    { adjust(); return NULL_TOK; }
186 null                    { adjust(); return NULL_TOK; }
187 return                  { adjust(); return RETURN; }
189 until                   { adjust(); return UNTIL; }
190 before                  { adjust(); return BEFORE; }
191 after                   { adjust(); return AFTER; }
192 at                      { adjust(); return AT; }
193 function                { adjust(); return FUNCTION; }
194 fun                     { adjust(); return FUNCTION; }
195 new                     { adjust(); return NEW; }
196 class                   { adjust(); return CLASS; }
197 extends                 { adjust(); return EXTENDS; }
198 implements              { adjust(); return IMPLEMENTS; }
199 public                  { adjust(); return PUBLIC; }
200 protected               { adjust(); return PROTECTED; }
201 private                 { adjust(); return PRIVATE; }
202 static                  { adjust(); return STATIC; }
203 const                   { adjust(); return CONST; }
204 spork                   { adjust(); return SPORK; }
206 "=>"                    { adjust(); return CHUCK; }
207 "=<"                    { adjust(); return UNCHUCK; }
208 "!=>"                   { adjust(); return UNCHUCK; }
209 "->"                    { adjust(); return S_CHUCK; }
210 "<-"                    { adjust(); return LEFT_S_CHUCK; }
211 "+->"                   { adjust(); return PLUS_CHUCK; }
212 "-->"                   { adjust(); return MINUS_CHUCK; }
213 "*->"                   { adjust(); return TIMES_CHUCK; }
214 "/->"                   { adjust(); return DIVIDE_CHUCK; }
215 "&->"                   { adjust(); return S_AND_CHUCK; }
216 "|->"                   { adjust(); return S_OR_CHUCK; }
217 "^->"                   { adjust(); return S_XOR_CHUCK; }
218 ">>->"                  { adjust(); return SHIFT_RIGHT_CHUCK; }
219 "<<->"                  { adjust(); return SHIFT_LEFT_CHUCK; }
220 "%->"                   { adjust(); return PERCENT_CHUCK; }
221 "@=>"                   { adjust(); return AT_CHUCK; }
222 "+=>"                   { adjust(); return PLUS_CHUCK; }
223 "-=>"                   { adjust(); return MINUS_CHUCK; }
224 "*=>"                   { adjust(); return TIMES_CHUCK; }
225 "/=>"                   { adjust(); return DIVIDE_CHUCK; }
226 "&=>"                   { adjust(); return S_AND_CHUCK; }
227 "|=>"                   { adjust(); return S_OR_CHUCK; }
228 "^=>"                   { adjust(); return S_XOR_CHUCK; }
229 ">>=>"                  { adjust(); return SHIFT_RIGHT_CHUCK; }
230 "<<=>"                  { adjust(); return SHIFT_LEFT_CHUCK; }
231 "%=>"                   { adjust(); return PERCENT_CHUCK; }
232 "@"                     { adjust(); return AT_SYM; }
233 "@@"                    { adjust(); return ATAT_SYM; }
235 0[xX][0-9a-fA-F]+{IS}?  { adjust(); yylval.ival=htol(yytext); return NUM; }
236 0[cC][0-7]+{IS}?        { adjust(); yylval.ival=atoi(yytext); return NUM; }
237 [0-9]+{IS}?             { adjust(); yylval.ival=atoi(yytext); return NUM; }
238 ([0-9]+"."[0-9]*)|([0-9]*"."[0-9]+)   { adjust(); yylval.fval=atof(yytext); return FLOAT; }
239 [A-Za-z_][A-Za-z0-9_]*  { adjust(); yylval.sval=alloc_str(yytext); return ID; }
240 \"(\\.|[^\\"])*\"       { adjust(); yylval.sval=alloc_str(strip_lit(yytext)); return STRING_LIT; }
242 .                       { adjust(); EM_error( EM_tokPos, "illegal token" ); }
246 // comment
247 int comment()
249     char c;
251     while ((c = input()) != '\n' && c != '\r' && c != 0);
253     if (c != 0) { adjust(); EM_newline(); }
254     
255     return 0;