4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2004 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);
36 /* redefinition of input function for GNU Flex */
38 #define YY_INPUT(b,r,m) (r=yy_input_for_flex(b,m))
40 #define MAX_INPUTS 256
44 char * code; /* code string */
45 int offset; /* current offset */
46 int times; /* times to be fed to lexer/parser */
47 int dyn; /* if set, free() after usage */
50 static struct code_stack _code_stack[MAX_INPUTS];
51 static int _code_stack_i=-1;
53 /* dynamic string manipulation macros */
55 struct ds { char * d; int p; int s; };
56 #define ds_init(x) do { x.d=(char *)0; x.p=x.s=0; } while(0)
57 #define ds_rewind(x) x.p=0;
58 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
59 #define ds_redim(x) do { if(x.p >= x.s) x.d=realloc(x.d, x.s += 32); } while(0)
60 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++]=c; } while(0)
61 #define ds_pokes(x,t) do { char *p=t; while(*p) ds_poke(x, *p++); } while(0)
65 static struct ds _blk;
67 /* count of parentheses */
74 S_INTEGER [-+]{P_INTEGER}
75 P_REAL {DIGIT}*[\.]?{DIGIT}+
82 MARKNAME [-a-zA-Z0-9_]+
85 ASSERT_MARK !{MARKNAME}
87 BLOCKNAME [-a-zA-Z0-9_]+
96 /* integers without sign */
97 yylval.i=atoi(yytext);
101 /* signed integers */
102 yylval.i=atoi(yytext);
106 /* real numbers without sign */
107 yylval.d=atof(yytext);
111 /* signel real numbers */
112 yylval.d=atof(yytext);
118 char * _notes="ccddeffggaab";
120 yylval.i=strchr(_notes, *yytext) - _notes;
123 {NOTE_T3} { return(NOTE_T3); }
124 {NOTE_T5} { return(NOTE_T5); }
127 /* create new mark */
142 {WSPACE} { ; /* ignore blanks */ }
144 \/\* { BEGIN REM; /* C-like comments */ }
145 <REM>\*\/ { BEGIN 0; }
146 <REM>. { ; /* drop anything inside a comment */ }
148 \{ { BEGIN XC; /* start of extended commands */ }
150 /* double-quoted string */
151 yytext[yyleng - 1]='\0';
156 yylval.i=atoi(yytext);
160 yylval.i=atoi(yytext);
164 yylval.d=atof(yytext);
168 yylval.d=atof(yytext);
172 /* frame count, in seconds */
173 yytext[yyleng - 1]='\0';
174 yylval.i=atoi(yytext) * 1000;
178 /* frame count, in milliseconds */
179 yytext[yyleng - 2]='\0';
180 yylval.i=atoi(yytext);
183 <XC>wav { return(SS_WAV); }
184 <XC>pat { return(SS_PAT); }
185 <XC>sustain { return(SS_SUSTAIN); }
187 <XC>delay { return(SS_EFF_DELAY); }
188 <XC>echo { return(SS_EFF_ECHO); }
189 <XC>comb { return(SS_EFF_COMB); }
190 <XC>allpass { return(SS_EFF_ALLPASS); }
191 <XC>flanger { return(SS_EFF_FLANGER); }
192 <XC>wobble { return(SS_EFF_WOBBLE); }
193 <XC>square_wobble { return(SS_EFF_SQWOBBLE); }
194 <XC>fader { return(SS_EFF_FADER); }
197 <XC>{WSPACE} { ; /* ignore blanks */ }
198 <XC>. { return(*yytext); }
206 <BLK>\( { _blk_i++; ds_poke(_blk, *yytext); }
208 /* one parentheses less */
223 <BLK>. { ds_poke(_blk, *yytext); }
225 =[ \t\n]*{BLOCKNAME} {
226 /* block assignation */
229 /* skip (possible) spaces between
230 the = and the blockname; this lex
231 rule is written this way to avoid
232 having a global match for {BLOCKNAME},
233 that could swallow notes and such,
234 being mismatched as blocknames */
235 while(*yylval.p == ' ' ||
243 /* block insertion */
249 yytext[yyleng - 1]='\0';
254 . { return(*yytext); }
258 int yywrap(void) { return(1); }
260 char * ds_load(char * file)
266 if((f=fopen(file, "r")) == NULL)
271 while((c=fgetc(f)) != EOF)
281 int push_code(char * code, int times, int dyn)
283 struct code_stack * cs;
285 if(_code_stack_i == MAX_INPUTS)
289 cs=&_code_stack[_code_stack_i];
300 int push_code_from_file(char * file)
304 if((c=ds_load(file)) == NULL)
312 int code_getchar(void)
314 struct code_stack * cs;
317 while(c == '\0' && _code_stack_i > -1)
319 /* get current char */
320 cs=&_code_stack[_code_stack_i];
321 c=cs->code[cs->offset++];
338 /* move to previous */
342 /* in any case, return a separator */
351 int yy_input_for_flex(char * buf, int max)
353 buf[0]=code_getchar();
355 return(buf[0] == '\0' ? 0 : 1);