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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3054 by schoenebeck, Thu Dec 15 12:47:45 2016 UTC revision 3729 by schoenebeck, Fri Jan 31 10:57:53 2020 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014-2016 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 21  Line 21 
21      yylloc->first_line = yylineno;      \      yylloc->first_line = yylineno;      \
22      yylloc->last_line  = yylineno;      \      yylloc->last_line  = yylineno;      \
23      yylloc->first_column = yycolumn;    \      yylloc->first_column = yycolumn;    \
24        yylloc->first_byte = yyextra->nbytes; \
25        yylloc->length_bytes = (int) yyleng;  \
26      yycolumn += yyleng;                 \      yycolumn += yyleng;                 \
27        yyextra->nbytes += (int) yyleng;    \
28      yylloc->last_column = yycolumn - 1; \      yylloc->last_column = yycolumn - 1; \
29      /*printf("lex: line '%s'\n", yytext);*/  \      /*printf("lex: line '%s'\n", yytext);*/  \
30  }  }
# Line 38  Line 41 
41  }  }
42    
43  static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {  static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {
44      context->addErr(locp->first_line, locp->last_line, locp->first_column, locp->last_column, err);      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) {  static void scanner_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {
50      context->addWrn(locp->first_line, locp->last_line, locp->first_column, locp->last_column, txt);      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)  #define SCANNER_ERR(txt)  scanner_error(yylloc, yyextra, txt)
# Line 76  static int countCharsPastLastNewLine(con Line 83  static int countCharsPastLastNewLine(con
83      if (nl) yycolumn = countCharsPastLastNewLine(yytext); \      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  /* use Flex's built-in support for line numbers
# Line 101  static int countCharsPastLastNewLine(con Line 115  static int countCharsPastLastNewLine(con
115  %x PREPROC_SET_COND PREPROC_RESET_COND PREPROC_IF PREPROC_IF_NOT PREPROC_BODY_EAT PREPROC_PRE_BODY_USE PREPROC_PRE_BODY_EAT  %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]  DIGIT    [0-9]
118  ID       [a-zA-Z0-9_]+  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    
# Line 112  ID       [a-zA-Z0-9_]+ Line 128  ID       [a-zA-Z0-9_]+
128  }  }
129    
130  {DIGIT}+ {  {DIGIT}+ {
131      yylval->iValue = atoi(yytext);      if (sizeof(vmint) < 8)
132            yylval->iValue = atoi(yytext);
133        else
134            yylval->iValue = atoll(yytext);
135      return INTEGER;      return INTEGER;
136  }  }
137    
138   /* there is currently no support for floating point numbers in NKSP yet */  {DIGIT}+"."{DIGIT}+ {
139   /*{DIGIT}+"."{DIGIT}* {      yylval->fValue = atof(yytext);
140      printf("A float: %s (%g)\n", yytext, atof(yytext));      return REAL;
141   }*/  }
142    
143    {DIGIT}+({METRIC}{1,2}|({METRIC}{0,2}{UNIT}?)) {
144        int pos = 0;
145    
146        // parse number portion
147        vmint value = 0;
148        for (; yytext[pos] >= '0' && yytext[pos] <= '9'; ++pos) {
149            value *= 10;
150            value += yytext[pos] - '0';
151        }
152        yylval->iUnitValue.iValue = value;
153    
154        // parse metric prefix portion
155        for (int i = 0; i < 2; ++i, ++pos) {
156            switch (yytext[pos]) {
157                case 'k': yylval->iUnitValue.prefix[i] = VM_KILO;  continue;
158                case 'h': yylval->iUnitValue.prefix[i] = VM_HECTO; continue;
159                case 'c': yylval->iUnitValue.prefix[i] = VM_CENTI; continue;
160                case 'm': yylval->iUnitValue.prefix[i] = VM_MILLI; continue;
161                case 'u': yylval->iUnitValue.prefix[i] = VM_MICRO; continue;
162                case 'd':
163                    if (yytext[pos+1] == 'a') {
164                        yylval->iUnitValue.prefix[i] = VM_DECA;
165                        ++pos;
166                    } else {
167                        yylval->iUnitValue.prefix[i] = VM_DECI;
168                    }
169                    continue;
170                default:
171                    yylval->iUnitValue.prefix[i] = VM_NO_PREFIX;
172                    goto parseIntStdUnit;
173            }
174        }
175    
176        parseIntStdUnit:
177    
178        // parse standard measurement unit
179        switch (yytext[pos]) {
180            case 's': yylval->iUnitValue.unit = VM_SECOND;  break;
181            case 'H': yylval->iUnitValue.unit = VM_HERTZ;   break;
182            case 'B': yylval->iUnitValue.unit = VM_BEL;     break;
183            default:  yylval->iUnitValue.unit = VM_NO_UNIT; break;
184        }
185    
186        return INTEGER_UNIT;
187    }
188    
189    {DIGIT}+"."{DIGIT}+({METRIC}{1,2}|({METRIC}{0,2}{UNIT}?)) {
190        int pos = 0;
191    
192        // parse number portion
193        for (; (yytext[pos] >= '0' && yytext[pos] <= '9') || yytext[pos] == '.'; ++pos) {
194        }
195        {
196            const char tmp = yytext[pos];
197            yytext[pos] = 0; // mark temporary end of string
198            yylval->fUnitValue.fValue = atof(yytext);
199            yytext[pos] = tmp; // restore
200        }
201    
202        // parse metric prefix portion
203        for (int i = 0; i < 2; ++i, ++pos) {
204            switch (yytext[pos]) {
205                case 'k': yylval->fUnitValue.prefix[i] = VM_KILO;  continue;
206                case 'h': yylval->fUnitValue.prefix[i] = VM_HECTO; continue;
207                case 'c': yylval->fUnitValue.prefix[i] = VM_CENTI; continue;
208                case 'm': yylval->fUnitValue.prefix[i] = VM_MILLI; continue;
209                case 'u': yylval->fUnitValue.prefix[i] = VM_MICRO; continue;
210                case 'd':
211                    if (yytext[pos+1] == 'a') {
212                        yylval->fUnitValue.prefix[i] = VM_DECA;
213                        ++pos;
214                    } else {
215                        yylval->fUnitValue.prefix[i] = VM_DECI;
216                    }
217                    continue;
218                default:
219                    yylval->fUnitValue.prefix[i] = VM_NO_PREFIX;
220                    goto parseRealStdUnit;
221            }
222        }
223    
224        parseRealStdUnit:
225    
226        // parse standard measurement unit
227        switch (yytext[pos]) {
228            case 's': yylval->fUnitValue.unit = VM_SECOND;  break;
229            case 'H': yylval->fUnitValue.unit = VM_HERTZ;   break;
230            case 'B': yylval->fUnitValue.unit = VM_BEL;     break;
231            default:  yylval->fUnitValue.unit = VM_NO_UNIT; break;
232        }
233    
234        return REAL_UNIT;
235    }
236    
237    
238   /* Preprocessor statement:  SET_CONDITION(name) */   /* Preprocessor statement:  SET_CONDITION(name) */
# Line 176  ID       [a-zA-Z0-9_]+ Line 289  ID       [a-zA-Z0-9_]+
289   */   */
290    
291  <*>"USE_CODE_IF"[ \t]*"(" {  <*>"USE_CODE_IF"[ \t]*"(" {
292      //printf("USE_CODE_IF\n");      //printf("{%s}\n", yytext);
293      yy_push_state(PREPROC_IF, yyscanner);      yy_push_state(PREPROC_IF, yyscanner);
294  }  }
295    <PREPROC_BODY_EAT>"USE_CODE_IF"[ \t]*"("{ID}")" {
296        //printf("[EAT{%s}\n", yytext);
297        yy_push_state(PREPROC_BODY_EAT, yyscanner);
298    }
299  <*>"USE_CODE_IF_NOT"[ \t]*"(" {  <*>"USE_CODE_IF_NOT"[ \t]*"(" {
300      //printf("USE_CODE_IF_NOT\n");      //printf("USE_CODE_IF_NOT\n");
301      yy_push_state(PREPROC_IF_NOT, yyscanner);      yy_push_state(PREPROC_IF_NOT, yyscanner);
302  }  }
303    <PREPROC_BODY_EAT>"USE_CODE_IF_NOT"[ \t]*"("{ID}")" {
304        //printf("[EAT{%s}\n", yytext);
305        yy_push_state(PREPROC_BODY_EAT, yyscanner);
306    }
307  <PREPROC_IF>{ID} {  <PREPROC_IF>{ID} {
308      //printf("preproc use code if '%s'\n", yytext);      //printf("preproc use code if '%s'\n", yytext);
309      yy_pop_state(yyscanner);      yy_pop_state(yyscanner);
# Line 204  ID       [a-zA-Z0-9_]+ Line 325  ID       [a-zA-Z0-9_]+
325      yy_push_state(PREPROC_BODY_USE, yyscanner);      yy_push_state(PREPROC_BODY_USE, yyscanner);
326  }  }
327  <PREPROC_PRE_BODY_EAT>[ \t]*")" {  <PREPROC_PRE_BODY_EAT>[ \t]*")" {
328      //printf("PREPROCESSOR EAT : \n");      //printf("PREPROCESSOR EAT : {%s}\n", yytext);
329      yy_pop_state(yyscanner);      yy_pop_state(yyscanner);
330      yy_push_state(PREPROC_BODY_EAT, yyscanner);      yy_push_state(PREPROC_BODY_EAT, yyscanner);
331  }  }
332  <*>.*"END_USE_CODE" {  <PREPROC_BODY_EAT,PREPROC_BODY_USE>"END_USE_CODE" {
333      //printf("-->END_USE_CODE\n");      //printf("-->END_USE_CODE\n");
334      yy_pop_state(yyscanner);      yy_pop_state(yyscanner);
335  }  }
336  <PREPROC_BODY_EAT>[ \t\r\n]* { /* eat up code block filtered out by preprocessor */  <PREPROC_BODY_EAT>[ \t\r\n]* { /* eat up code block filtered out by preprocessor */
337        //printf("PREPROCESSOR EAT2 : {%s}\n", yytext);
338      processLocation();      processLocation();
339  }  }
340  <PREPROC_BODY_EAT>.* { /* eat up code block filtered out by preprocessor */  <PREPROC_BODY_EAT>.* { /* eat up code block filtered out by preprocessor */
341      processLocation();      //printf("PREPROCESSOR EAT3 : {%s}\n", yytext);
342        yyextra->addPreprocessorComment(yylloc->first_line, yylloc->last_line,
343                                        yylloc->first_column+1, yylloc->last_column+1,
344                                        yylloc->first_byte, yylloc->length_bytes);
345  }  }
346    
347    
# Line 238  ID       [a-zA-Z0-9_]+ Line 363  ID       [a-zA-Z0-9_]+
363  "not" return NOT;  "not" return NOT;
364  "else" return ELSE;  "else" return ELSE;
365  "controller" return CONTROLLER;  "controller" return CONTROLLER;
366    "rpn" return RPN;
367    "nrpn" return NRPN;
368  "case" return CASE;  "case" return CASE;
369  "select" return SELECT;  "select" return SELECT;
370  "to" return TO;  "to" return TO;
# Line 248  ID       [a-zA-Z0-9_]+ Line 375  ID       [a-zA-Z0-9_]+
375  "mod" return MOD;  "mod" return MOD;
376  "function" return FUNCTION;  "function" return FUNCTION;
377  "call" return CALL;  "call" return CALL;
378    "synchronized" return SYNCHRONIZED;
379    
380  [&,()[\]<>=*+#/-] {  [&,!()[\]<>=*+#\/-] {
381      return *yytext;      return *yytext;
382  }  }
383    
384  ("$"|"@"|"%"){ID} {  ("$"|"@"|"%"|"~"|"?"){ID} {
385      yylval->sValue = strdup(yytext);      yylval->sValue = strdup(yytext);
386      return VARIABLE;      return VARIABLE;
387  }  }
# Line 278  ID       [a-zA-Z0-9_]+ Line 406  ID       [a-zA-Z0-9_]+
406    
407  [ \t\r]+ /* eat up whitespace */  [ \t\r]+ /* eat up whitespace */
408    
409  . printf( "Unrecognized character: '%s' (line %d, column %d)\n", yytext, yylineno, yycolumn);  . {
410        printf( "Unrecognized character: '%s' (line %d, column %d)\n", yytext, yylineno, yycolumn);
411        return UNKNOWN_CHAR;
412    }
413    
414  %%  %%
415    

Legend:
Removed from v.3054  
changed lines
  Added in v.3729

  ViewVC Help
Powered by ViewVC