4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
7 compiler.l - Scripting language [F]lex lexer
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
33 void yyerror(char * s);
34 int yy_input_for_flex(char * buf, int max);
37 static char * ascii_notes="ccddeffggaab";
39 /* redefinition of input function for GNU Flex */
41 #define YY_INPUT(b,r,m) (r=yy_input_for_flex(b,m))
43 #define MAX_INPUTS 256
47 char * code; /* code string */
48 int offset; /* current offset */
49 int times; /* times to be fed to lexer/parser */
50 int dyn; /* if set, free() after usage */
53 static struct code_stack _code_stack[MAX_INPUTS];
54 static int _code_stack_i=-1;
56 /* dynamic string manipulation macros */
58 struct ds { char * d; int p; int s; };
59 #define ds_init(x) do { x.d=(char *)0; x.p=x.s=0; } while(0)
60 #define ds_rewind(x) x.p=0;
61 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
62 #define ds_redim(x) do { if(x.p >= x.s) x.d=realloc(x.d, x.s += 32); } while(0)
63 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++]=c; } while(0)
64 #define ds_pokes(x,t) do { char *p=t; while(*p) ds_poke(x, *p++); } while(0)
68 static struct ds _blk;
70 /* count of parentheses */
80 S_INTEGER [-+]{P_INTEGER}
81 P_REAL {DIGIT}*[\.]?{DIGIT}+
88 MARKNAME [-a-zA-Z0-9_]+
91 ASSERT_MARK !{MARKNAME}
93 BLOCKNAME [-a-zA-Z0-9_]+
102 /* integers without sign */
103 yylval.i=atoi(yytext);
107 /* signed integers */
108 yylval.i=atoi(yytext);
112 /* real numbers without sign */
113 yylval.d=atof(yytext);
117 /* signel real numbers */
118 yylval.d=atof(yytext);
124 yylval.i=strchr(ascii_notes, *yytext) - ascii_notes;
127 {NOTE_T3} { return(NOTE_T3); }
128 {NOTE_T5} { return(NOTE_T5); }
131 /* create new mark */
146 {WSPACE} { ; /* ignore blanks */ }
150 \/\* { BEGIN REM; /* C-like comments */ }
151 <REM>\*\/ { BEGIN 0; }
152 <REM>\n { yyline++; }
153 <REM>. { ; /* drop anything inside a comment */ }
155 \{ { BEGIN XC; /* start of extended commands */ }
157 /* double-quoted string */
158 yytext[yyleng - 1]='\0';
159 yylval.p=strdup(yytext + 1);
163 /* single-quoted string */
164 yytext[yyleng - 1]='\0';
165 yylval.p=strdup(yytext + 1);
169 yylval.i=atoi(yytext);
173 yylval.i=atoi(yytext);
177 yylval.d=atof(yytext);
181 yylval.d=atof(yytext);
185 /* frame count, in seconds */
186 yytext[yyleng - 1]='\0';
187 yylval.i=atof(yytext) * 1000;
191 /* frame count, in milliseconds */
192 yytext[yyleng - 2]='\0';
193 yylval.i=atoi(yytext);
196 <XC>{NOTE_P}[\#\&]?{P_INTEGER} {
200 yylval.i=strchr(ascii_notes, *ptr) - ascii_notes;
203 /* process optional sharps or flats */
217 yylval.i += atoi(ptr) * 12;
222 <XC>wav { return(SS_WAV); }
223 <XC>pat { return(SS_PAT); }
224 <XC>sustain { return(SS_SUSTAIN); }
226 <XC>delay { return(SS_EFF_DELAY); }
227 <XC>echo { return(SS_EFF_ECHO); }
228 <XC>comb { return(SS_EFF_COMB); }
229 <XC>allpass { return(SS_EFF_ALLPASS); }
230 <XC>flanger { return(SS_EFF_FLANGER); }
231 <XC>wobble { return(SS_EFF_WOBBLE); }
232 <XC>square_wobble { return(SS_EFF_SQWOBBLE); }
233 <XC>fader { return(SS_EFF_FADER); }
234 <XC>reverb { return(SS_EFF_REVERB); }
236 <XC>\/\* { BEGIN REMXC; /* C-like comments inside XC */ }
237 <REMXC>\*\/ { BEGIN XC; /* jump back to XC processing */ }
238 <REMXC>\n { yyline++; }
242 <XC>{WSPACE} { ; /* ignore blanks */ }
244 <XC>. { return(*yytext); }
252 <BLK>\( { _blk_i++; ds_poke(_blk, *yytext); }
254 /* one parentheses less */
269 <BLK>\n { yyline++; }
270 <BLK>. { ds_poke(_blk, *yytext); }
272 =[ \t\n]*{BLOCKNAME} {
273 /* block assignation */
276 /* skip (possible) spaces between
277 the = and the blockname; this lex
278 rule is written this way to avoid
279 having a global match for {BLOCKNAME},
280 that could swallow notes and such,
281 being mismatched as blocknames */
282 while(*yylval.p == ' ' ||
290 /* block insertion */
296 yytext[yyleng - 1]='\0';
301 . { return(*yytext); }
305 int yywrap(void) { return(1); }
307 char * ds_load(char * file)
313 if((f=fopen(file, "r")) == NULL)
318 while((c=fgetc(f)) != EOF)
328 int push_code(char * code, int times, int dyn)
330 struct code_stack * cs;
332 if(_code_stack_i == MAX_INPUTS)
336 cs=&_code_stack[_code_stack_i];
347 int push_code_from_file(char * file)
351 if((c=ds_load(file)) == NULL)
359 int code_getchar(void)
361 struct code_stack * cs;
364 while(c == '\0' && _code_stack_i > -1)
366 /* get current char */
367 cs=&_code_stack[_code_stack_i];
368 c=cs->code[cs->offset++];
385 /* move to previous */
389 /* in any case, return a separator */
398 int yy_input_for_flex(char * buf, int max)
400 buf[0]=code_getchar();
402 return(buf[0] == '\0' ? 0 : 1);