/[svn]/linuxsampler/trunk/src/scriptvm/scanner.l
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/scanner.l

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3836 - (show annotations) (download)
Sat Nov 28 14:47:14 2020 UTC (3 years, 4 months ago) by schoenebeck
File size: 12715 byte(s)
* NKSP parser: fixed memory leak; string tokens were allocated as C
  strings and never freed.

* Bumped version (2.1.1.svn66).

1 /*
2 * Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 /* Token scanner for NKSP real-time instrument script language. */
11
12 %{
13
14 #include "parser_shared.h"
15 #include <math.h>
16 // reentrant scanner data context
17 #define YY_EXTRA_TYPE ParserContext*
18 // set line number each time a token is recognized
19 #define YY_USER_ACTION \
20 { \
21 yylloc->first_line = yylineno; \
22 yylloc->last_line = yylineno; \
23 yylloc->first_column = yycolumn; \
24 yylloc->first_byte = yyextra->nbytes; \
25 yylloc->length_bytes = (int) yyleng; \
26 yycolumn += yyleng; \
27 yyextra->nbytes += (int) yyleng; \
28 yylloc->last_column = yycolumn - 1; \
29 /*printf("lex: line '%s'\n", yytext);*/ \
30 }
31 // custom (f)lex input for reading from std::istream object
32 #define YY_INPUT(buf,result,max_size) \
33 { \
34 char c = yyextra->is->get(); \
35 if (yyextra->is->eof()) \
36 result = YY_NULL; \
37 else { \
38 buf[0] = c; \
39 result = 1; \
40 } \
41 }
42
43 static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {
44 context->addErr(locp->first_line, locp->last_line, locp->first_column,
45 locp->last_column, locp->first_byte, locp->length_bytes,
46 err);
47 }
48
49 static void scanner_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {
50 context->addWrn(locp->first_line, locp->last_line, locp->first_column,
51 locp->last_column, locp->first_byte, locp->length_bytes,
52 txt);
53 }
54
55 #define SCANNER_ERR(txt) scanner_error(yylloc, yyextra, txt)
56 #define SCANNER_WRN(txt) scanner_warning(yylloc, yyextra, txt)
57
58 // shut up warning that 'register' keyword is deprecated as of C++11
59 #if defined(__cplusplus) && __cplusplus >= 201103L
60 # define register
61 #endif
62
63 using namespace LinuxSampler;
64
65 static int countNewLineChars(const char* txt) {
66 int n = 0;
67 for (int i = 0; txt[i]; ++i)
68 if (txt[i] == '\n') ++n;
69 return n;
70 }
71
72 static int countCharsPastLastNewLine(const char* txt) {
73 const int n = (int)strlen(txt);
74 for (int i = n - 1; i >= 0; --i)
75 if (txt[i] == '\n')
76 return n - i - 1;
77 return n;
78 }
79
80 #define processLocation() { \
81 const int nl = countNewLineChars(yytext); \
82 yylineno += nl; \
83 if (nl) yycolumn = countCharsPastLastNewLine(yytext); \
84 }
85
86 // if compiled for debugging, throw an exception instead of exiting on fatal
87 // lexer errors (so the debugger may pause with the appropriate back trace)
88 #if DEBUG
89 # include <stdexcept>
90 # define YY_FATAL_ERROR(msg) throw std::runtime_error(msg)
91 #endif
92
93 %}
94
95 /* use Flex's built-in support for line numbers
96 (disabled, because it seems to be unreliable, so we are using our own
97 tracking code in the respective scanner rules below) */
98 /*%option yylineno*/
99 /* generate a reentrant safe scanner */
100 %option reentrant
101 /* avoid symbol collision with other (i.e. future) scanner symbols */
102 %option prefix="InstrScript_"
103 /* bison-bridge adds an argument yylval to yylex, and bison-locations adds an
104 argument code yylloc for location tracking. */
105 %option bison-bridge
106 %option bison-locations
107 /* yywrap() would be called at EOF, we don't need it */
108 %option noyywrap
109 /* enable functions yy_push_state(), yy_pop_state(), yy_top_state() */
110 %option stack
111
112 /* inclusive scanner conditions */
113 %s PREPROC_BODY_USE
114 /* exclusive scanner conditions */
115 %x PREPROC_SET_COND PREPROC_RESET_COND PREPROC_IF PREPROC_IF_NOT PREPROC_BODY_EAT PREPROC_PRE_BODY_USE PREPROC_PRE_BODY_EAT
116
117 DIGIT [0-9]
118 ID [a-zA-Z][a-zA-Z0-9_]*
119 METRIC (k|h|(da)|d|c|m|u)
120 UNIT (s|(Hz)|B)
121
122 %%
123
124 \"[^"]*\" {
125 char* s = strdup(yytext + 1);
126 s[strlen(s) - 1] = '\0';
127 yylval->sValue = s;
128 yyextra->autoFreeAfterParse(s);
129 return STRING;
130 }
131
132 {DIGIT}+ {
133 if (sizeof(vmint) < 8)
134 yylval->iValue = atoi(yytext);
135 else
136 yylval->iValue = atoll(yytext);
137 return INTEGER;
138 }
139
140 {DIGIT}+"."{DIGIT}+ {
141 yylval->fValue = atof(yytext);
142 return REAL;
143 }
144
145 {DIGIT}+({METRIC}{1,2}|({METRIC}{0,2}{UNIT}?)) {
146 int pos = 0;
147
148 // parse number portion
149 vmint value = 0;
150 for (; yytext[pos] >= '0' && yytext[pos] <= '9'; ++pos) {
151 value *= 10;
152 value += yytext[pos] - '0';
153 }
154 yylval->iUnitValue.iValue = value;
155
156 // parse metric prefix portion
157 for (int i = 0; i < 2; ++i, ++pos) {
158 switch (yytext[pos]) {
159 case 'k': yylval->iUnitValue.prefix[i] = VM_KILO; continue;
160 case 'h': yylval->iUnitValue.prefix[i] = VM_HECTO; continue;
161 case 'c': yylval->iUnitValue.prefix[i] = VM_CENTI; continue;
162 case 'm': yylval->iUnitValue.prefix[i] = VM_MILLI; continue;
163 case 'u': yylval->iUnitValue.prefix[i] = VM_MICRO; continue;
164 case 'd':
165 if (yytext[pos+1] == 'a') {
166 yylval->iUnitValue.prefix[i] = VM_DECA;
167 ++pos;
168 } else {
169 yylval->iUnitValue.prefix[i] = VM_DECI;
170 }
171 continue;
172 default:
173 yylval->iUnitValue.prefix[i] = VM_NO_PREFIX;
174 goto parseIntStdUnit;
175 }
176 }
177
178 parseIntStdUnit:
179
180 // parse standard measurement unit
181 switch (yytext[pos]) {
182 case 's': yylval->iUnitValue.unit = VM_SECOND; break;
183 case 'H': yylval->iUnitValue.unit = VM_HERTZ; break;
184 case 'B': yylval->iUnitValue.unit = VM_BEL; break;
185 default: yylval->iUnitValue.unit = VM_NO_UNIT; break;
186 }
187
188 return INTEGER_UNIT;
189 }
190
191 {DIGIT}+"."{DIGIT}+({METRIC}{1,2}|({METRIC}{0,2}{UNIT}?)) {
192 int pos = 0;
193
194 // parse number portion
195 for (; (yytext[pos] >= '0' && yytext[pos] <= '9') || yytext[pos] == '.'; ++pos) {
196 }
197 {
198 const char tmp = yytext[pos];
199 yytext[pos] = 0; // mark temporary end of string
200 yylval->fUnitValue.fValue = atof(yytext);
201 yytext[pos] = tmp; // restore
202 }
203
204 // parse metric prefix portion
205 for (int i = 0; i < 2; ++i, ++pos) {
206 switch (yytext[pos]) {
207 case 'k': yylval->fUnitValue.prefix[i] = VM_KILO; continue;
208 case 'h': yylval->fUnitValue.prefix[i] = VM_HECTO; continue;
209 case 'c': yylval->fUnitValue.prefix[i] = VM_CENTI; continue;
210 case 'm': yylval->fUnitValue.prefix[i] = VM_MILLI; continue;
211 case 'u': yylval->fUnitValue.prefix[i] = VM_MICRO; continue;
212 case 'd':
213 if (yytext[pos+1] == 'a') {
214 yylval->fUnitValue.prefix[i] = VM_DECA;
215 ++pos;
216 } else {
217 yylval->fUnitValue.prefix[i] = VM_DECI;
218 }
219 continue;
220 default:
221 yylval->fUnitValue.prefix[i] = VM_NO_PREFIX;
222 goto parseRealStdUnit;
223 }
224 }
225
226 parseRealStdUnit:
227
228 // parse standard measurement unit
229 switch (yytext[pos]) {
230 case 's': yylval->fUnitValue.unit = VM_SECOND; break;
231 case 'H': yylval->fUnitValue.unit = VM_HERTZ; break;
232 case 'B': yylval->fUnitValue.unit = VM_BEL; break;
233 default: yylval->fUnitValue.unit = VM_NO_UNIT; break;
234 }
235
236 return REAL_UNIT;
237 }
238
239
240 /* Preprocessor statement: SET_CONDITION(name) */
241
242 <*>"SET_CONDITION"[ \t]*"(" {
243 //printf("SET_CONDITION\n");
244 yy_push_state(PREPROC_SET_COND, yyscanner);
245 }
246 <PREPROC_SET_COND>{ID} {
247 //printf("preproc set condition '%s'\n", yytext);
248 bool success = yyextra->setPreprocessorCondition(yytext);
249 if (!success) {
250 SCANNER_WRN((String("Preprocessor: Condition '") +
251 yytext + "' is already set.").c_str());
252 }
253 }
254 <PREPROC_SET_COND>[ \t]*")" {
255 //printf("End of PREPROC_SET_COND\n");
256 yy_pop_state(yyscanner);
257 }
258
259
260 /* Preprocessor statement: RESET_CONDITION(name) */
261
262 <*>"RESET_CONDITION"[ \t]*"(" {
263 //printf("RESET_CONDITION\n");
264 yy_push_state(PREPROC_RESET_COND, yyscanner);
265 }
266 <PREPROC_RESET_COND>{ID} {
267 //printf("preproc reset condition '%s'\n", yytext);
268 bool success = yyextra->resetPreprocessorCondition(yytext);
269 if (!success) {
270 SCANNER_ERR((String("Preprocessor: could not reset condition '") +
271 yytext + "' (either not set or a built-in condition).").c_str());
272 }
273 }
274 <PREPROC_RESET_COND>[ \t]*")" {
275 //printf("End of RESET_CONDITION\n");
276 yy_pop_state(yyscanner);
277 }
278
279
280 /* Preprocessor conditional statements:
281
282 USE_CODE_IF(name)
283 ...
284 END_USE_CODE
285
286 and:
287
288 USE_CODE_IF_NOT(name)
289 ...
290 END_USE_CODE
291 */
292
293 <*>"USE_CODE_IF"[ \t]*"(" {
294 //printf("{%s}\n", yytext);
295 yy_push_state(PREPROC_IF, yyscanner);
296 }
297 <PREPROC_BODY_EAT>"USE_CODE_IF"[ \t]*"("{ID}")" {
298 //printf("[EAT{%s}\n", yytext);
299 yy_push_state(PREPROC_BODY_EAT, yyscanner);
300 }
301 <*>"USE_CODE_IF_NOT"[ \t]*"(" {
302 //printf("USE_CODE_IF_NOT\n");
303 yy_push_state(PREPROC_IF_NOT, yyscanner);
304 }
305 <PREPROC_BODY_EAT>"USE_CODE_IF_NOT"[ \t]*"("{ID}")" {
306 //printf("[EAT{%s}\n", yytext);
307 yy_push_state(PREPROC_BODY_EAT, yyscanner);
308 }
309 <PREPROC_IF>{ID} {
310 //printf("preproc use code if '%s'\n", yytext);
311 yy_pop_state(yyscanner);
312 if (yyextra->isPreprocessorConditionSet(yytext))
313 yy_push_state(PREPROC_PRE_BODY_USE, yyscanner);
314 else
315 yy_push_state(PREPROC_PRE_BODY_EAT, yyscanner);
316 }
317 <PREPROC_IF_NOT>{ID} {
318 //printf("preproc use code if not '%s'\n", yytext);
319 yy_pop_state(yyscanner);
320 if (!yyextra->isPreprocessorConditionSet(yytext))
321 yy_push_state(PREPROC_PRE_BODY_USE, yyscanner);
322 else
323 yy_push_state(PREPROC_PRE_BODY_EAT, yyscanner);
324 }
325 <PREPROC_PRE_BODY_USE>[ \t]*")" {
326 yy_pop_state(yyscanner);
327 yy_push_state(PREPROC_BODY_USE, yyscanner);
328 }
329 <PREPROC_PRE_BODY_EAT>[ \t]*")" {
330 //printf("PREPROCESSOR EAT : {%s}\n", yytext);
331 yy_pop_state(yyscanner);
332 yy_push_state(PREPROC_BODY_EAT, yyscanner);
333 }
334 <PREPROC_BODY_EAT,PREPROC_BODY_USE>"END_USE_CODE" {
335 //printf("-->END_USE_CODE\n");
336 yy_pop_state(yyscanner);
337 }
338 <PREPROC_BODY_EAT>[ \t\r\n]* { /* eat up code block filtered out by preprocessor */
339 //printf("PREPROCESSOR EAT2 : {%s}\n", yytext);
340 processLocation();
341 }
342 <PREPROC_BODY_EAT>.* { /* eat up code block filtered out by preprocessor */
343 //printf("PREPROCESSOR EAT3 : {%s}\n", yytext);
344 yyextra->addPreprocessorComment(yylloc->first_line, yylloc->last_line,
345 yylloc->first_column+1, yylloc->last_column+1,
346 yylloc->first_byte, yylloc->length_bytes);
347 }
348
349
350 /* Language keywords */
351
352 "on" return ON;
353 "end" return END;
354 "note" return NOTE;
355 "init" return INIT;
356 "declare" return DECLARE;
357 "while" return WHILE;
358 "if" return IF;
359 ".or." return BITWISE_OR;
360 "or" return OR;
361 "release" return RELEASE;
362 ".and." return BITWISE_AND;
363 "and" return AND;
364 ".not." return BITWISE_NOT;
365 "not" return NOT;
366 "else" return ELSE;
367 "controller" return CONTROLLER;
368 "rpn" return RPN;
369 "nrpn" return NRPN;
370 "case" return CASE;
371 "select" return SELECT;
372 "to" return TO;
373 "<=" return LE;
374 ">=" return GE;
375 "const" return CONST_; // note: "CONST" is already defined for C/C++ compilers on Windows by default
376 "polyphonic" return POLYPHONIC;
377 "patch" return PATCH;
378 "mod" return MOD;
379 "function" return FUNCTION;
380 "call" return CALL;
381 "synchronized" return SYNCHRONIZED;
382
383 [&,!()[\]<>=*+#\/-] {
384 return *yytext;
385 }
386
387 ("$"|"@"|"%"|"~"|"?"){ID} {
388 yylval->sValue = strdup(yytext);
389 yyextra->autoFreeAfterParse(yylval->sValue);
390 return VARIABLE;
391 }
392
393 {ID} {
394 yylval->sValue = strdup(yytext);
395 yyextra->autoFreeAfterParse(yylval->sValue);
396 return IDENTIFIER;
397 }
398
399 ":=" return ASSIGNMENT;
400
401 \n+ {
402 yylineno += countNewLineChars(yytext);
403 yycolumn = 0;
404 //printf("lex: new line %d\n", yylineno, yytext);
405 //return LF;
406 }
407
408 "{"[^}]*"}" { /* eat up comments */
409 processLocation();
410 }
411
412 [ \t\r]+ /* eat up whitespace */
413
414 . {
415 printf( "Unrecognized character: '%s' (line %d, column %d)\n", yytext, yylineno, yycolumn);
416 return UNKNOWN_CHAR;
417 }
418
419 %%
420
421 namespace LinuxSampler {
422
423 void ParserContext::createScanner(std::istream* is) {
424 if (scanner) destroyScanner();
425 this->is = is;
426 yylex_init(&scanner);
427 yyset_extra(this, scanner);
428 }
429
430 void ParserContext::destroyScanner() {
431 if (!scanner) return;
432 yylex_destroy(scanner);
433 scanner = NULL;
434 }
435
436 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC