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
35 void yyerror(char * s);
36 int yy_input_for_flex(char * buf, int max);
39 static char * ascii_notes="ccddeffggaab";
41 /* redefinition of input function for GNU Flex */
43 #define YY_INPUT(b,r,m) (r=yy_input_for_flex(b,m))
45 #define MAX_INPUTS 256
49 char * code; /* code string */
50 int offset; /* current offset */
51 int times; /* times to be fed to lexer/parser */
52 int dyn; /* if set, free() after usage */
55 static struct code_stack code_stack[MAX_INPUTS];
56 static int code_stack_i=-1;
58 /* dynamic string manipulation macros */
60 struct ds { char * d; int p; int s; };
61 #define ds_init(x) do { x.d=(char *)0; x.p=x.s=0; } while(0)
62 #define ds_rewind(x) x.p=0;
63 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
64 #define ds_redim(x) do { if(x.p >= x.s) x.d=realloc(x.d, x.s += 32); } while(0)
65 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++]=c; } while(0)
66 #define ds_pokes(x,t) do { char *p=t; while(*p) ds_poke(x, *p++); } while(0)
70 static struct ds ds_blk;
72 /* count of parentheses */
82 S_INTEGER [-+]{P_INTEGER}
83 P_REAL {DIGIT}*[\.]?{DIGIT}+
90 MARKNAME [-a-zA-Z0-9_]+
93 ASSERT_MARK !{MARKNAME}
95 BLOCKNAME [-a-zA-Z0-9_]+
104 /* integers without sign */
105 yylval.i=atoi(yytext);
109 /* signed integers */
110 yylval.i=atoi(yytext);
114 /* real numbers without sign */
115 yylval.d=atof(yytext);
119 /* signel real numbers */
120 yylval.d=atof(yytext);
126 yylval.i=strchr(ascii_notes, *yytext) - ascii_notes;
129 {NOTE_T3} { return(NOTE_T3); }
130 {NOTE_T5} { return(NOTE_T5); }
133 /* create new mark */
148 {WSPACE} { ; /* ignore blanks */ }
152 \/\* { BEGIN REM; /* C-like comments */ }
153 <REM>\*\/ { BEGIN 0; }
154 <REM>\n { yyline++; }
155 <REM>. { ; /* drop anything inside a comment */ }
157 \{ { BEGIN XC; /* start of extended commands */ }
159 /* double-quoted string */
160 yytext[yyleng - 1]='\0';
161 yylval.p=strdup(yytext + 1);
165 /* single-quoted string */
166 yytext[yyleng - 1]='\0';
167 yylval.p=strdup(yytext + 1);
171 yylval.i=atoi(yytext);
175 yylval.i=atoi(yytext);
179 yylval.d=atof(yytext);
183 yylval.d=atof(yytext);
187 /* frame count, in seconds */
188 yytext[yyleng - 1]='\0';
189 yylval.d=atof(yytext) * 1000;
193 /* frame count, in milliseconds */
194 yytext[yyleng - 2]='\0';
195 yylval.d=atof(yytext);
198 <XC>{NOTE_P}[\#\&]?{P_INTEGER} {
202 yylval.i=strchr(ascii_notes, *ptr) - ascii_notes;
205 /* process optional sharps or flats */
219 yylval.i += atoi(ptr) * 12;
224 <XC>wav { return(SS_WAV); }
225 <XC>pat { return(SS_PAT); }
226 <XC>sustain { return(SS_SUSTAIN); }
227 <XC>vibrato { return(SS_VIBRATO); }
228 <XC>channel { return(SS_CHANNEL); }
229 <XC>vol { return(SS_VOL); }
231 <XC>delay { return(SS_EFF_DELAY); }
232 <XC>echo { return(SS_EFF_ECHO); }
233 <XC>comb { return(SS_EFF_COMB); }
234 <XC>allpass { return(SS_EFF_ALLPASS); }
235 <XC>flanger { return(SS_EFF_FLANGER); }
236 <XC>wobble { return(SS_EFF_WOBBLE); }
237 <XC>square_wobble { return(SS_EFF_SQWOBBLE); }
238 <XC>fader { return(SS_EFF_FADER); }
239 <XC>reverb { return(SS_EFF_REVERB); }
240 <XC>off { return(SS_EFF_OFF); }
242 <XC>pitch_stretch { return(SS_PITCH_STRETCH); }
243 <XC>time_stretch { return(SS_TIME_STRETCH); }
244 <XC>print_wave_tempo { return(SS_PRINT_WAVE_TEMPO); }
246 <XC>midi_channel { return(MIDI_CHANNEL); }
247 <XC>midi_program { return(MIDI_PROGRAM); }
248 <XC>midi_generic { return(MIDI_GENERIC); }
250 <XC>\/\* { BEGIN REMXC; /* C-like comments inside XC */ }
251 <REMXC>\*\/ { BEGIN XC; /* jump back to XC processing */ }
252 <REMXC>\n { yyline++; }
256 <XC>{WSPACE} { ; /* ignore blanks */ }
258 <XC>. { return(*yytext); }
266 <BLK>\( { ds_blk_i++; ds_poke(ds_blk, *yytext); }
268 /* one parentheses less */
274 ds_poke(ds_blk, '\0');
281 ds_poke(ds_blk, ')');
283 <BLK>\n { ds_poke(ds_blk, ' '); yyline++; }
284 <BLK>. { ds_poke(ds_blk, *yytext); }
286 =[ \t\n]*{BLOCKNAME} {
287 /* block assignation */
290 /* skip (possible) spaces between
291 the = and the blockname; this lex
292 rule is written this way to avoid
293 having a global match for {BLOCKNAME},
294 that could swallow notes and such,
295 being mismatched as blocknames */
296 while(*yylval.p == ' ' ||
304 /* block insertion */
310 yytext[yyleng - 1]='\0';
315 . { return(*yytext); }
319 int yywrap(void) { return(1); }
321 char * ds_load(char * file)
327 if((f=path_fopen(file, "r")) == NULL)
332 while((c=fgetc(f)) != EOF)
342 int push_code(char * code, int times, int dyn)
344 struct code_stack * cs;
346 if(code_stack_i == MAX_INPUTS)
350 cs=&code_stack[code_stack_i];
361 int push_code_from_file(char * file)
365 if((c=ds_load(file)) == NULL)
373 int code_getchar(void)
375 struct code_stack * cs;
378 while(c == '\0' && code_stack_i > -1)
380 /* get current char */
381 cs=&code_stack[code_stack_i];
382 c=cs->code[cs->offset++];
399 /* move to previous */
403 /* in any case, return a separator */
412 int yy_input_for_flex(char * buf, int max)
414 buf[0]=code_getchar();
416 return(buf[0] == '\0' ? 0 : 1);