/[svn]/linuxsampler/trunk/src/scriptvm/parser.y
ViewVC logotype

Diff of /linuxsampler/trunk/src/scriptvm/parser.y

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

revision 2945 by schoenebeck, Thu Jul 14 00:22:26 2016 UTC revision 3259 by schoenebeck, Wed May 31 14:41:04 2017 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 18  Line 18 
18    
19      void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err);      void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err);
20      void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt);      void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt);
21        int InstrScript_tnamerr(char* yyres, const char* yystr);
22      int InstrScript_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);      int InstrScript_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
23      #define scanner context->scanner      #define scanner context->scanner
24      #define PARSE_ERR(loc,txt)  yyerror(&loc, context, txt)      #define PARSE_ERR(loc,txt)  yyerror(&loc, context, txt)
25      #define PARSE_WRN(loc,txt)  InstrScript_warning(&loc, context, txt)      #define PARSE_WRN(loc,txt)  InstrScript_warning(&loc, context, txt)
26        #define yytnamerr(res,str)  InstrScript_tnamerr(res, str)
27  %}  %}
28    
29  // generate reentrant safe parser  // generate reentrant safe parser
# Line 29  Line 31 
31  %parse-param { LinuxSampler::ParserContext* context }  %parse-param { LinuxSampler::ParserContext* context }
32  %lex-param { void* scanner }  %lex-param { void* scanner }
33  // avoid symbol collision with other (i.e. future) auto generated (f)lex scanners  // avoid symbol collision with other (i.e. future) auto generated (f)lex scanners
34  %name-prefix "InstrScript_"  // (NOTE: "=" is deprecated here with Bison 3.x, however removing it would cause an error with Bison 2.x)
35    %name-prefix="InstrScript_"
36  %locations  %locations
37  %defines  %defines
38  %error-verbose  %error-verbose
39    
40  %token <iValue> INTEGER  %token <iValue> INTEGER "integer literal"
41  %token <sValue> STRING  %token <sValue> STRING "string literal"
42  %token <sValue> IDENTIFIER  %token <sValue> IDENTIFIER "function name"
43  %token <sValue> VARIABLE  %token <sValue> VARIABLE "variable name"
44  %token ON END INIT NOTE DECLARE ASSIGNMENT WHILE IF OR RELEASE AND ELSE  %token ON "keyword 'on'"
45  %token BITWISE_OR BITWISE_AND BITWISE_NOT  %token END "keyword 'end'"
46  %token CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token INIT "keyword 'init'"
47  %token LE GE  %token NOTE "keyword 'note'"
48    %token RELEASE "keyword 'release'"
49  %type <nEventHandlers> script eventhandlers  %token CONTROLLER "keyword 'controller'"
50  %type <nEventHandler> eventhandler  %token DECLARE "keyword 'declare'"
51  %type <nStatements> statements body  %token ASSIGNMENT "operator ':='"
52    %token CONST_ "keyword 'const'"
53    %token POLYPHONIC "keyword 'polyphonic'"
54    %token WHILE "keyword 'while'"
55    %token IF "keyword 'if'"
56    %token ELSE "keyword 'else'"
57    %token SELECT "keyword 'select'"
58    %token CASE "keyword 'case'"
59    %token TO "keyword 'to'"
60    %token OR "operator 'or'"
61    %token AND "operator 'and'"
62    %token NOT "operator 'not'"
63    %token BITWISE_OR "bitwise operator '.or.'"
64    %token BITWISE_AND "bitwise operator '.and.'"
65    %token BITWISE_NOT "bitwise operator '.not.'"
66    %token FUNCTION "keyword 'function'"
67    %token CALL "keyword 'call'"
68    %token MOD "operator 'mod'"
69    %token LE "operator '<='"
70    %token GE "operator '>='"
71    %token END_OF_FILE 0 "end of file"
72    %token UNKNOWN_CHAR "unknown character"
73    
74    %type <nEventHandlers> script sections
75    %type <nEventHandler> section eventhandler
76    %type <nStatements> statements opt_statements userfunctioncall
77  %type <nStatement> statement assignment  %type <nStatement> statement assignment
78  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
79  %type <nArgs> args  %type <nArgs> args
# Line 58  Line 86 
86  %%  %%
87    
88  script:  script:
89      eventhandlers  {      sections  {
90          $$ = context->handlers = $1;          $$ = context->handlers = $1;
91      }      }
92    
93  eventhandlers:  sections:
94      eventhandler  {      section  {
95          $$ = new EventHandlers();          $$ = new EventHandlers();
96          $$->add($1);          if ($1) $$->add($1);
97        }
98        | sections section  {
99            $$ = $1;
100            if ($2) $$->add($2);
101        }
102    
103    section:
104        function_declaration  {
105            $$ = EventHandlerRef();
106      }      }
107      | eventhandlers eventhandler  {      | eventhandler  {
108          $$ = $1;          $$ = $1;
         $$->add($2);  
109      }      }
110    
111  eventhandler:  eventhandler:
112      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
113          if (context->onNote)          if (context->onNote)
114              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
115          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
116          $$ = context->onNote;          $$ = context->onNote;
117      }      }
118      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
119          if (context->onInit)          if (context->onInit)
120              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
121          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
122          $$ = context->onInit;          $$ = context->onInit;
123      }      }
124      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
125          if (context->onRelease)          if (context->onRelease)
126              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
127          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
128          $$ = context->onRelease;          $$ = context->onRelease;
129      }      }
130      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
131          if (context->onController)          if (context->onController)
132              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
133          context->onController = new OnController($3);          context->onController = new OnController($3);
134          $$ = context->onController;          $$ = context->onController;
135      }      }
136    
137  body:  function_declaration:
138        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
139            const char* name = $2;
140            if (context->functionProvider->functionByName(name)) {
141                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
142            } else if (context->userFunctionByName(name)) {
143                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
144            } else {
145                context->userFnTable[name] = $3;
146            }
147        }
148    
149    opt_statements:
150      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
151          $$ = new Statements();          $$ = new Statements();
152      }      }
# Line 126  statement: Line 174  statement:
174      functioncall  {      functioncall  {
175          $$ = $1;          $$ = $1;
176      }      }
177        | userfunctioncall  {
178            $$ = $1;
179        }
180      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
181          const char* name = $2;          const char* name = $2;
182          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 230  statement: Line 281  statement:
281                  PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());                  PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
282                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
283              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
284                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
285                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
286                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
287                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
# Line 249  statement: Line 300  statement:
300                          break;                          break;
301                      }                      }
302                  }                  }
303                  if (argsOK)                  if (argsOK) {
304                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
305                  else                      $$ = new NoOperation;
306                    } else
307                        $$ = new FunctionCall("nothing", new Args, NULL); // whatever
308                }
309            }
310        }
311        | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
312            const char* name = $3;
313            if (!$5->isConstExpr()) {
314                PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
315                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
316            } else if ($5->exprType() != INT_EXPR) {
317                PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
318                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
319            } else if (context->variableByName(name)) {
320                PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
321                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
322            } else {
323                IntExprRef sizeExpr = $5;
324                ArgsRef args = $9;
325                int size = sizeExpr->evalInt();
326                if (size <= 0) {
327                    PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
328                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever
329                } else if (args->argsCount() > size) {
330                    PARSE_ERR(@9, (String("Array variable '") + name +
331                              "' was declared with size " + ToString(size) +
332                              " but " + ToString(args->argsCount()) +
333                              " values were assigned." ).c_str());
334                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever          
335                } else {
336                    bool argsOK = true;
337                    for (int i = 0; i < args->argsCount(); ++i) {
338                        if (args->arg(i)->exprType() != INT_EXPR) {
339                            PARSE_ERR(
340                                @9,
341                                (String("Array variable '") + name +
342                                "' declared with invalid assignment values. Assigned element " +
343                                ToString(i+1) + " is not an integer expression.").c_str()
344                            );
345                            argsOK = false;
346                            break;
347                        }
348                        if (!args->arg(i)->isConstExpr()) {
349                            PARSE_ERR(
350                                @9,
351                                (String("const array variable '") + name +
352                                "' must be defined with const values. Assigned element " +
353                                ToString(i+1) + " is not a const expression though.").c_str()
354                            );
355                            argsOK = false;
356                            break;
357                        }
358                    }
359                    if (argsOK) {
360                        context->vartable[name] = new IntArrayVariable(context, size, args, true);
361                        $$ = new NoOperation;
362                    } else
363                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
364              }              }
365          }          }
# Line 289  statement: Line 397  statement:
397      | assignment  {      | assignment  {
398          $$ = $1;          $$ = $1;
399      }      }
400      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
401          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
402              $$ = new While($3, $5);              $$ = new While($3, $5);
403          } else {          } else {
# Line 297  statement: Line 405  statement:
405              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
406          }          }
407      }      }
408      | IF '(' expr ')' statements ELSE statements END IF  {      | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
409          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
410      }      }
411      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
412          $$ = new If($3, $5);          $$ = new If($3, $5);
413      }      }
414      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 323  caseclauses: Line 431  caseclauses:
431      }      }
432    
433  caseclause:  caseclause:
434      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
435          $$ = CaseBranch();          $$ = CaseBranch();
436          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
437          $$.statements = $3;          $$.statements = $3;
438      }      }
439      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
440          $$ = CaseBranch();          $$ = CaseBranch();
441          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
442          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
443          $$.statements = $5;          $$.statements = $5;
444      }      }
445    
446    userfunctioncall:
447        CALL IDENTIFIER  {
448            const char* name = $2;
449            StatementsRef fn = context->userFunctionByName(name);
450            if (context->functionProvider->functionByName(name)) {
451                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
452                $$ = StatementsRef();
453            } else if (!fn) {
454                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
455                $$ = StatementsRef();
456            } else {
457                $$ = fn;
458            }
459        }
460    
461  functioncall:  functioncall:
462      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
463          const char* name = $1;          const char* name = $1;
464          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
465          ArgsRef args = $3;          ArgsRef args = $3;
466          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
467          if (!fn) {          if (context->userFunctionByName(name)) {
468                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
469                $$ = new FunctionCall(name, args, NULL);
470            } else if (!fn) {
471              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
472              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
473          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 371  functioncall: Line 497  functioncall:
497          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
498          ArgsRef args = new Args;          ArgsRef args = new Args;
499          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
500          if (!fn) {          if (context->userFunctionByName(name)) {
501                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
502                $$ = new FunctionCall(name, args, NULL);
503            } else if (!fn) {
504              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
505              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
506          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 386  functioncall: Line 515  functioncall:
515          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
516          ArgsRef args = new Args;          ArgsRef args = new Args;
517          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
518          if (!fn) {          if (context->userFunctionByName(name)) {
519                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
520                $$ = new FunctionCall(name, args, NULL);
521            } else if (!fn) {
522              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
523              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
524          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 432  assignment: Line 564  assignment:
564              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
565          else if (var->exprType() != INT_ARR_EXPR)          else if (var->exprType() != INT_ARR_EXPR)
566              PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());              PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
567            else if (var->isConstExpr())
568                PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
569            else if (!var->isAssignable())
570                PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
571          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
572              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
573          else if ($6->exprType() != INT_EXPR)          else if ($6->exprType() != INT_EXPR)
574              PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());              PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());
575            else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
576                PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
577                              " exceeds size of array variable '" + name +
578                              "' which was declared with size " +
579                              ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
580          IntArrayElementRef element = new IntArrayElement(var, $3);          IntArrayElementRef element = new IntArrayElement(var, $3);
581          $$ = new Assignment(element, $6);          $$ = new Assignment(element, $6);
582      }      }
# Line 470  unary_expr: Line 611  unary_expr:
611              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
612              $$ = new IntLiteral(0);              $$ = new IntLiteral(0);
613          } else {          } else {
614                if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
615                    PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
616                                   " exceeds size of array variable '" + name +
617                                   "' which was declared with size " +
618                                   ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
619              $$ = new IntArrayElement(var, $3);              $$ = new IntArrayElement(var, $3);
620          }          }
621      }      }
# Line 727  void InstrScript_warning(YYLTYPE* locp, Line 873  void InstrScript_warning(YYLTYPE* locp,
873      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
874      context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);      context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);
875  }  }
876    
877    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
878    int InstrScript_tnamerr(char* yyres, const char* yystr) {
879      if (*yystr == '"') {
880          int yyn = 0;
881          char const *yyp = yystr;
882          for (;;)
883            switch (*++yyp)
884              {
885    /*
886              case '\'':
887              case ',':
888                goto do_not_strip_quotes;
889    
890              case '\\':
891                if (*++yyp != '\\')
892                  goto do_not_strip_quotes;
893    */
894                /* Fall through.  */
895              default:
896                if (yyres)
897                  yyres[yyn] = *yyp;
898                yyn++;
899                break;
900    
901              case '"':
902                if (yyres)
903                  yyres[yyn] = '\0';
904                return yyn;
905              }
906    /*
907        do_not_strip_quotes: ;
908    */
909        }
910    
911      if (! yyres)
912        return (int) yystrlen (yystr);
913    
914      return int( yystpcpy (yyres, yystr) - yyres );
915    }

Legend:
Removed from v.2945  
changed lines
  Added in v.3259

  ViewVC Help
Powered by ViewVC