/[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 2942 by schoenebeck, Wed Jul 13 15:51:06 2016 UTC revision 3260 by schoenebeck, Wed May 31 21:07:44 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 SYNCHRONIZED "keyword 'synchronized'"
56    %token IF "keyword 'if'"
57    %token ELSE "keyword 'else'"
58    %token SELECT "keyword 'select'"
59    %token CASE "keyword 'case'"
60    %token TO "keyword 'to'"
61    %token OR "operator 'or'"
62    %token AND "operator 'and'"
63    %token NOT "operator 'not'"
64    %token BITWISE_OR "bitwise operator '.or.'"
65    %token BITWISE_AND "bitwise operator '.and.'"
66    %token BITWISE_NOT "bitwise operator '.not.'"
67    %token FUNCTION "keyword 'function'"
68    %token CALL "keyword 'call'"
69    %token MOD "operator 'mod'"
70    %token LE "operator '<='"
71    %token GE "operator '>='"
72    %token END_OF_FILE 0 "end of file"
73    %token UNKNOWN_CHAR "unknown character"
74    
75    %type <nEventHandlers> script sections
76    %type <nEventHandler> section eventhandler
77    %type <nStatements> statements opt_statements userfunctioncall
78  %type <nStatement> statement assignment  %type <nStatement> statement assignment
79  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
80  %type <nArgs> args  %type <nArgs> args
# Line 58  Line 87 
87  %%  %%
88    
89  script:  script:
90      eventhandlers  {      sections  {
91          $$ = context->handlers = $1;          $$ = context->handlers = $1;
92      }      }
93    
94  eventhandlers:  sections:
95      eventhandler  {      section  {
96          $$ = new EventHandlers();          $$ = new EventHandlers();
97          $$->add($1);          if ($1) $$->add($1);
98        }
99        | sections section  {
100            $$ = $1;
101            if ($2) $$->add($2);
102        }
103    
104    section:
105        function_declaration  {
106            $$ = EventHandlerRef();
107      }      }
108      | eventhandlers eventhandler  {      | eventhandler  {
109          $$ = $1;          $$ = $1;
         $$->add($2);  
110      }      }
111    
112  eventhandler:  eventhandler:
113      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
114          if (context->onNote)          if (context->onNote)
115              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
116          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
117          $$ = context->onNote;          $$ = context->onNote;
118      }      }
119      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
120          if (context->onInit)          if (context->onInit)
121              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
122          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
123          $$ = context->onInit;          $$ = context->onInit;
124      }      }
125      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
126          if (context->onRelease)          if (context->onRelease)
127              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
128          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
129          $$ = context->onRelease;          $$ = context->onRelease;
130      }      }
131      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
132          if (context->onController)          if (context->onController)
133              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
134          context->onController = new OnController($3);          context->onController = new OnController($3);
135          $$ = context->onController;          $$ = context->onController;
136      }      }
137    
138  body:  function_declaration:
139        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
140            const char* name = $2;
141            if (context->functionProvider->functionByName(name)) {
142                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
143            } else if (context->userFunctionByName(name)) {
144                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
145            } else {
146                context->userFnTable[name] = $3;
147            }
148        }
149    
150    opt_statements:
151      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
152          $$ = new Statements();          $$ = new Statements();
153      }      }
# Line 126  statement: Line 175  statement:
175      functioncall  {      functioncall  {
176          $$ = $1;          $$ = $1;
177      }      }
178        | userfunctioncall  {
179            $$ = $1;
180        }
181      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
182          const char* name = $2;          const char* name = $2;
183          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 230  statement: Line 282  statement:
282                  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());
283                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
284              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
285                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
286                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
287                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
288                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
# Line 249  statement: Line 301  statement:
301                          break;                          break;
302                      }                      }
303                  }                  }
304                  if (argsOK)                  if (argsOK) {
305                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
306                  else                      $$ = new NoOperation;
307                    } else
308                        $$ = new FunctionCall("nothing", new Args, NULL); // whatever
309                }
310            }
311        }
312        | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
313            const char* name = $3;
314            if (!$5->isConstExpr()) {
315                PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
316                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
317            } else if ($5->exprType() != INT_EXPR) {
318                PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
319                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
320            } else if (context->variableByName(name)) {
321                PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
322                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
323            } else {
324                IntExprRef sizeExpr = $5;
325                ArgsRef args = $9;
326                int size = sizeExpr->evalInt();
327                if (size <= 0) {
328                    PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
329                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever
330                } else if (args->argsCount() > size) {
331                    PARSE_ERR(@9, (String("Array variable '") + name +
332                              "' was declared with size " + ToString(size) +
333                              " but " + ToString(args->argsCount()) +
334                              " values were assigned." ).c_str());
335                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever          
336                } else {
337                    bool argsOK = true;
338                    for (int i = 0; i < args->argsCount(); ++i) {
339                        if (args->arg(i)->exprType() != INT_EXPR) {
340                            PARSE_ERR(
341                                @9,
342                                (String("Array variable '") + name +
343                                "' declared with invalid assignment values. Assigned element " +
344                                ToString(i+1) + " is not an integer expression.").c_str()
345                            );
346                            argsOK = false;
347                            break;
348                        }
349                        if (!args->arg(i)->isConstExpr()) {
350                            PARSE_ERR(
351                                @9,
352                                (String("const array variable '") + name +
353                                "' must be defined with const values. Assigned element " +
354                                ToString(i+1) + " is not a const expression though.").c_str()
355                            );
356                            argsOK = false;
357                            break;
358                        }
359                    }
360                    if (argsOK) {
361                        context->vartable[name] = new IntArrayVariable(context, size, args, true);
362                        $$ = new NoOperation;
363                    } else
364                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
365              }              }
366          }          }
# Line 289  statement: Line 398  statement:
398      | assignment  {      | assignment  {
399          $$ = $1;          $$ = $1;
400      }      }
401      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
402          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
403              $$ = new While($3, $5);              $$ = new While($3, $5);
404          } else {          } else {
# Line 297  statement: Line 406  statement:
406              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
407          }          }
408      }      }
409      | IF '(' expr ')' statements ELSE statements END IF  {      | SYNCHRONIZED opt_statements END SYNCHRONIZED  {
410            $$ = new SyncBlock($2);
411        }
412        | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
413          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
414      }      }
415      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
416          $$ = new If($3, $5);          $$ = new If($3, $5);
417      }      }
418      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 323  caseclauses: Line 435  caseclauses:
435      }      }
436    
437  caseclause:  caseclause:
438      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
439          $$ = CaseBranch();          $$ = CaseBranch();
440          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
441          $$.statements = $3;          $$.statements = $3;
442      }      }
443      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
444          $$ = CaseBranch();          $$ = CaseBranch();
445          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
446          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
447          $$.statements = $5;          $$.statements = $5;
448      }      }
449    
450    userfunctioncall:
451        CALL IDENTIFIER  {
452            const char* name = $2;
453            StatementsRef fn = context->userFunctionByName(name);
454            if (context->functionProvider->functionByName(name)) {
455                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
456                $$ = StatementsRef();
457            } else if (!fn) {
458                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
459                $$ = StatementsRef();
460            } else {
461                $$ = fn;
462            }
463        }
464    
465  functioncall:  functioncall:
466      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
467          const char* name = $1;          const char* name = $1;
468          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
469          ArgsRef args = $3;          ArgsRef args = $3;
470          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
471          if (!fn) {          if (context->userFunctionByName(name)) {
472                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
473                $$ = new FunctionCall(name, args, NULL);
474            } else if (!fn) {
475              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());
476              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
477          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 357  functioncall: Line 487  functioncall:
487                      PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects " + typeStr(fn->argType(i)) + " type, but type " + typeStr(args->arg(i)->exprType()) + " was given instead.").c_str());                      PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects " + typeStr(fn->argType(i)) + " type, but type " + typeStr(args->arg(i)->exprType()) + " was given instead.").c_str());
488                      argsOK = false;                      argsOK = false;
489                      break;                      break;
490                    } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
491                        PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
492                        argsOK = false;
493                        break;
494                  }                  }
495              }              }
496              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
# Line 367  functioncall: Line 501  functioncall:
501          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
502          ArgsRef args = new Args;          ArgsRef args = new Args;
503          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
504          if (!fn) {          if (context->userFunctionByName(name)) {
505                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
506                $$ = new FunctionCall(name, args, NULL);
507            } else if (!fn) {
508              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());
509              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
510          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 382  functioncall: Line 519  functioncall:
519          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
520          ArgsRef args = new Args;          ArgsRef args = new Args;
521          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
522          if (!fn) {          if (context->userFunctionByName(name)) {
523                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
524                $$ = new FunctionCall(name, args, NULL);
525            } else if (!fn) {
526              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());
527              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
528          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 428  assignment: Line 568  assignment:
568              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
569          else if (var->exprType() != INT_ARR_EXPR)          else if (var->exprType() != INT_ARR_EXPR)
570              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());
571            else if (var->isConstExpr())
572                PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
573            else if (!var->isAssignable())
574                PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
575          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
576              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());
577          else if ($6->exprType() != INT_EXPR)          else if ($6->exprType() != INT_EXPR)
578              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());
579            else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
580                PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
581                              " exceeds size of array variable '" + name +
582                              "' which was declared with size " +
583                              ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
584          IntArrayElementRef element = new IntArrayElement(var, $3);          IntArrayElementRef element = new IntArrayElement(var, $3);
585          $$ = new Assignment(element, $6);          $$ = new Assignment(element, $6);
586      }      }
# Line 466  unary_expr: Line 615  unary_expr:
615              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());
616              $$ = new IntLiteral(0);              $$ = new IntLiteral(0);
617          } else {          } else {
618                if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
619                    PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
620                                   " exceeds size of array variable '" + name +
621                                   "' which was declared with size " +
622                                   ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
623              $$ = new IntArrayElement(var, $3);              $$ = new IntArrayElement(var, $3);
624          }          }
625      }      }
# Line 723  void InstrScript_warning(YYLTYPE* locp, Line 877  void InstrScript_warning(YYLTYPE* locp,
877      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
878      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);
879  }  }
880    
881    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
882    int InstrScript_tnamerr(char* yyres, const char* yystr) {
883      if (*yystr == '"') {
884          int yyn = 0;
885          char const *yyp = yystr;
886          for (;;)
887            switch (*++yyp)
888              {
889    /*
890              case '\'':
891              case ',':
892                goto do_not_strip_quotes;
893    
894              case '\\':
895                if (*++yyp != '\\')
896                  goto do_not_strip_quotes;
897    */
898                /* Fall through.  */
899              default:
900                if (yyres)
901                  yyres[yyn] = *yyp;
902                yyn++;
903                break;
904    
905              case '"':
906                if (yyres)
907                  yyres[yyn] = '\0';
908                return yyn;
909              }
910    /*
911        do_not_strip_quotes: ;
912    */
913        }
914    
915      if (! yyres)
916        return (int) yystrlen (yystr);
917    
918      return int( yystpcpy (yyres, yystr) - yyres );
919    }

Legend:
Removed from v.2942  
changed lines
  Added in v.3260

  ViewVC Help
Powered by ViewVC