4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2006 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))
47 char * code; /* code string */
48 int offset; /* current offset */
51 static struct code_stack * code_stack = NULL;
52 static int code_stack_i = -1;
54 /* dynamic string manipulation macros */
56 struct ds { char * d; int p; int s; };
57 #define ds_init(x) do { x.d = (char *)0; x.p = x.s = 0; } while(0)
58 #define ds_rewind(x) x.p = 0;
59 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
60 #define ds_redim(x) do { if(x.p >= x.s) x.d = realloc(x.d, x.s += 32); } while(0)
61 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++] = c; } while(0)
62 #define ds_pokes(x,t) do { char *p = t; while(*p) ds_poke(x, *p++); } while(0)
66 static struct ds ds_blk;
68 /* count of parentheses */
78 S_INTEGER [-+]{P_INTEGER}
79 P_REAL {DIGIT}*[\.]?{DIGIT}+
86 MARKNAME [-a-zA-Z0-9_]+
89 ASSERT_MARK !{MARKNAME}
91 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 */
132 yylval.p = yytext + 1;
137 yylval.p = yytext + 1;
142 yylval.p = yytext + 1;
147 /* alteration string */
148 yylval.p = yytext + 1;
152 {WSPACE} { ; /* ignore blanks */ }
156 \/\* { BEGIN REM; /* C-like comments */ }
157 <REM>\*\/ { BEGIN 0; }
158 <REM>\n { yyline++; }
159 <REM>. { ; /* drop anything inside a comment */ }
161 \{ { BEGIN XC; /* start of extended commands */ }
163 /* double-quoted string */
164 yytext[yyleng - 1] = '\0';
165 yylval.p = strdup(yytext + 1);
169 /* single-quoted string */
170 yytext[yyleng - 1] = '\0';
171 yylval.p = strdup(yytext + 1);
175 yylval.i = atoi(yytext);
179 yylval.i = atoi(yytext);
183 yylval.d = atof(yytext);
187 yylval.d = atof(yytext);
191 /* frame count, in seconds */
192 yytext[yyleng - 1] = '\0';
193 yylval.d = atof(yytext) * 1000;
197 /* frame count, in milliseconds */
198 yytext[yyleng - 2] = '\0';
199 yylval.d = atof(yytext);
202 <XC>{NOTE_P}[\#\&]?{P_INTEGER} {
206 yylval.i = strchr(ascii_notes, *ptr) - ascii_notes;
209 /* process optional sharps or flats */
223 yylval.i += atoi(ptr) * 12;
228 <XC>wav { return(SS_WAV); }
229 <XC>pat { return(SS_PAT); }
230 <XC>sustain { return(SS_SUSTAIN); }
231 <XC>vibrato { return(SS_VIBRATO); }
232 <XC>portamento { return(SS_PORTAMENTO); }
233 <XC>channel { return(SS_CHANNEL); }
234 <XC>vol { return(SS_VOL); }
236 <XC>delay { return(SS_EFF_DELAY); }
237 <XC>echo { return(SS_EFF_ECHO); }
238 <XC>comb { return(SS_EFF_COMB); }
239 <XC>allpass { return(SS_EFF_ALLPASS); }
240 <XC>flanger { return(SS_EFF_FLANGER); }
241 <XC>wobble { return(SS_EFF_WOBBLE); }
242 <XC>square_wobble { return(SS_EFF_SQWOBBLE); }
243 <XC>half_wobble { return(SS_EFF_HFWOBBLE); }
244 <XC>fader { return(SS_EFF_FADER); }
245 <XC>reverb { return(SS_EFF_REVERB); }
246 <XC>foldback { return(SS_EFF_FOLDBACK); }
247 <XC>off { return(SS_EFF_OFF); }
249 <XC>pitch_stretch { return(SS_PITCH_STRETCH); }
250 <XC>time_stretch { return(SS_TIME_STRETCH); }
251 <XC>print_wave_tempo { return(SS_PRINT_WAVE_TEMPO); }
253 <XC>song_info { return(SONG_INFO); }
255 <XC>midi_channel { return(MIDI_CHANNEL); }
256 <XC>midi_program { return(MIDI_PROGRAM); }
257 <XC>midi_generic { return(MIDI_GENERIC); }
259 <XC>\/\* { BEGIN REMXC; /* C-like comments inside XC */ }
260 <REMXC>\*\/ { BEGIN XC; /* jump back to XC processing */ }
261 <REMXC>\n { yyline++; }
265 <XC>{WSPACE} { ; /* ignore blanks */ }
267 <XC>. { return(*yytext); }
275 <BLK>\( { ds_blk_i++; ds_poke(ds_blk, *yytext); }
277 /* one parentheses less */
283 ds_poke(ds_blk, '\0');
290 ds_poke(ds_blk, ')');
292 <BLK>\n { ds_poke(ds_blk, ' '); yyline++; }
293 <BLK>. { ds_poke(ds_blk, *yytext); }
295 =[ \t\n]*{BLOCKNAME} {
296 /* block assignation */
297 yylval.p = yytext + 1;
299 /* skip (possible) spaces between
300 the = and the blockname; this lex
301 rule is written this way to avoid
302 having a global match for {BLOCKNAME},
303 that could swallow notes and such,
304 being mismatched as blocknames */
305 while(*yylval.p == ' ' ||
313 /* block insertion */
314 yylval.p = yytext + 1;
319 yytext[yyleng - 1] = '\0';
320 yylval.p = yytext + 1;
324 . { return(*yytext); }
328 int yywrap(void) { return(1); }
330 char * ds_load(char * file)
336 if((f = libpath_fopen(file, "r")) == NULL)
341 while((c = fgetc(f)) != EOF)
351 int push_code(char * code)
353 struct code_stack * cs;
356 GROW(code_stack, code_stack_i, struct code_stack);
358 cs = &code_stack[code_stack_i];
367 int push_code_from_file(char * file)
371 if((c = ds_load(file)) == NULL)
379 int code_getchar(void)
381 struct code_stack * cs;
384 while(c == '\0' && code_stack_i > -1)
386 /* get current char */
387 cs = &code_stack[code_stack_i];
388 c = cs->code[cs->offset++];
396 /* move to previous */
399 /* in any case, return a separator */
408 int yy_input_for_flex(char * buf, int max)
410 buf[0] = code_getchar();
412 return(buf[0] == '\0' ? 0 : 1);