/[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 3257 by schoenebeck, Tue May 30 17:20:02 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    
73    %type <nEventHandlers> script sections
74    %type <nEventHandler> section eventhandler
75    %type <nStatements> statements opt_statements userfunctioncall
76  %type <nStatement> statement assignment  %type <nStatement> statement assignment
77  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
78  %type <nArgs> args  %type <nArgs> args
# Line 58  Line 85 
85  %%  %%
86    
87  script:  script:
88      eventhandlers  {      sections  {
89          $$ = context->handlers = $1;          $$ = context->handlers = $1;
90      }      }
91    
92  eventhandlers:  sections:
93      eventhandler  {      section  {
94          $$ = new EventHandlers();          $$ = new EventHandlers();
95          $$->add($1);          if ($1) $$->add($1);
96        }
97        | sections section  {
98            $$ = $1;
99            if ($2) $$->add($2);
100        }
101    
102    section:
103        function_declaration  {
104            $$ = EventHandlerRef();
105      }      }
106      | eventhandlers eventhandler  {      | eventhandler  {
107          $$ = $1;          $$ = $1;
         $$->add($2);  
108      }      }
109    
110  eventhandler:  eventhandler:
111      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
112          if (context->onNote)          if (context->onNote)
113              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
114          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
115          $$ = context->onNote;          $$ = context->onNote;
116      }      }
117      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
118          if (context->onInit)          if (context->onInit)
119              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
120          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
121          $$ = context->onInit;          $$ = context->onInit;
122      }      }
123      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
124          if (context->onRelease)          if (context->onRelease)
125              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
126          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
127          $$ = context->onRelease;          $$ = context->onRelease;
128      }      }
129      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
130          if (context->onController)          if (context->onController)
131              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
132          context->onController = new OnController($3);          context->onController = new OnController($3);
133          $$ = context->onController;          $$ = context->onController;
134      }      }
135    
136  body:  function_declaration:
137        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
138            const char* name = $2;
139            if (context->functionProvider->functionByName(name)) {
140                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
141            } else if (context->userFunctionByName(name)) {
142                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
143            } else {
144                context->userFnTable[name] = $3;
145            }
146        }
147    
148    opt_statements:
149      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
150          $$ = new Statements();          $$ = new Statements();
151      }      }
# Line 126  statement: Line 173  statement:
173      functioncall  {      functioncall  {
174          $$ = $1;          $$ = $1;
175      }      }
176        | userfunctioncall  {
177            $$ = $1;
178        }
179      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
180          const char* name = $2;          const char* name = $2;
181          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 230  statement: Line 280  statement:
280                  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());
281                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
282              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
283                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
284                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
285                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
286                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
# Line 249  statement: Line 299  statement:
299                          break;                          break;
300                      }                      }
301                  }                  }
302                  if (argsOK)                  if (argsOK) {
303                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
304                  else                      $$ = new NoOperation;
305                    } else
306                        $$ = new FunctionCall("nothing", new Args, NULL); // whatever
307                }
308            }
309        }
310        | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
311            const char* name = $3;
312            if (!$5->isConstExpr()) {
313                PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
314                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
315            } else if ($5->exprType() != INT_EXPR) {
316                PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
317                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
318            } else if (context->variableByName(name)) {
319                PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
320                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
321            } else {
322                IntExprRef sizeExpr = $5;
323                ArgsRef args = $9;
324                int size = sizeExpr->evalInt();
325                if (size <= 0) {
326                    PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
327                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever
328                } else if (args->argsCount() > size) {
329                    PARSE_ERR(@9, (String("Array variable '") + name +
330                              "' was declared with size " + ToString(size) +
331                              " but " + ToString(args->argsCount()) +
332                              " values were assigned." ).c_str());
333                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever          
334                } else {
335                    bool argsOK = true;
336                    for (int i = 0; i < args->argsCount(); ++i) {
337                        if (args->arg(i)->exprType() != INT_EXPR) {
338                            PARSE_ERR(
339                                @9,
340                                (String("Array variable '") + name +
341                                "' declared with invalid assignment values. Assigned element " +
342                                ToString(i+1) + " is not an integer expression.").c_str()
343                            );
344                            argsOK = false;
345                            break;
346                        }
347                        if (!args->arg(i)->isConstExpr()) {
348                            PARSE_ERR(
349                                @9,
350                                (String("const array variable '") + name +
351                                "' must be defined with const values. Assigned element " +
352                                ToString(i+1) + " is not a const expression though.").c_str()
353                            );
354                            argsOK = false;
355                            break;
356                        }
357                    }
358                    if (argsOK) {
359                        context->vartable[name] = new IntArrayVariable(context, size, args, true);
360                        $$ = new NoOperation;
361                    } else
362                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
363              }              }
364          }          }
# Line 289  statement: Line 396  statement:
396      | assignment  {      | assignment  {
397          $$ = $1;          $$ = $1;
398      }      }
399      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
400          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
401              $$ = new While($3, $5);              $$ = new While($3, $5);
402          } else {          } else {
# Line 297  statement: Line 404  statement:
404              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
405          }          }
406      }      }
407      | IF '(' expr ')' statements ELSE statements END IF  {      | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
408          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
409      }      }
410      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
411          $$ = new If($3, $5);          $$ = new If($3, $5);
412      }      }
413      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 323  caseclauses: Line 430  caseclauses:
430      }      }
431    
432  caseclause:  caseclause:
433      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
434          $$ = CaseBranch();          $$ = CaseBranch();
435          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
436          $$.statements = $3;          $$.statements = $3;
437      }      }
438      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
439          $$ = CaseBranch();          $$ = CaseBranch();
440          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
441          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
442          $$.statements = $5;          $$.statements = $5;
443      }      }
444    
445    userfunctioncall:
446        CALL IDENTIFIER  {
447            const char* name = $2;
448            StatementsRef fn = context->userFunctionByName(name);
449            if (context->functionProvider->functionByName(name)) {
450                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
451                $$ = StatementsRef();
452            } else if (!fn) {
453                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
454                $$ = StatementsRef();
455            } else {
456                $$ = fn;
457            }
458        }
459    
460  functioncall:  functioncall:
461      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
462          const char* name = $1;          const char* name = $1;
463          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
464          ArgsRef args = $3;          ArgsRef args = $3;
465          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
466          if (!fn) {          if (context->userFunctionByName(name)) {
467                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
468                $$ = new FunctionCall(name, args, NULL);
469            } else if (!fn) {
470              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());
471              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
472          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 371  functioncall: Line 496  functioncall:
496          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
497          ArgsRef args = new Args;          ArgsRef args = new Args;
498          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
499          if (!fn) {          if (context->userFunctionByName(name)) {
500                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
501                $$ = new FunctionCall(name, args, NULL);
502            } else if (!fn) {
503              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());
504              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
505          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 386  functioncall: Line 514  functioncall:
514          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
515          ArgsRef args = new Args;          ArgsRef args = new Args;
516          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
517          if (!fn) {          if (context->userFunctionByName(name)) {
518                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
519                $$ = new FunctionCall(name, args, NULL);
520            } else if (!fn) {
521              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());
522              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
523          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 432  assignment: Line 563  assignment:
563              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
564          else if (var->exprType() != INT_ARR_EXPR)          else if (var->exprType() != INT_ARR_EXPR)
565              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());
566            else if (var->isConstExpr())
567                PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
568            else if (!var->isAssignable())
569                PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
570          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
571              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());
572          else if ($6->exprType() != INT_EXPR)          else if ($6->exprType() != INT_EXPR)
573              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());
574            else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
575                PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
576                              " exceeds size of array variable '" + name +
577                              "' which was declared with size " +
578                              ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
579          IntArrayElementRef element = new IntArrayElement(var, $3);          IntArrayElementRef element = new IntArrayElement(var, $3);
580          $$ = new Assignment(element, $6);          $$ = new Assignment(element, $6);
581      }      }
# Line 470  unary_expr: Line 610  unary_expr:
610              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());
611              $$ = new IntLiteral(0);              $$ = new IntLiteral(0);
612          } else {          } else {
613                if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
614                    PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
615                                   " exceeds size of array variable '" + name +
616                                   "' which was declared with size " +
617                                   ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
618              $$ = new IntArrayElement(var, $3);              $$ = new IntArrayElement(var, $3);
619          }          }
620      }      }
# Line 727  void InstrScript_warning(YYLTYPE* locp, Line 872  void InstrScript_warning(YYLTYPE* locp,
872      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
873      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);
874  }  }
875    
876    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
877    int InstrScript_tnamerr(char* yyres, const char* yystr) {
878      if (*yystr == '"') {
879          int yyn = 0;
880          char const *yyp = yystr;
881          for (;;)
882            switch (*++yyp)
883              {
884    /*
885              case '\'':
886              case ',':
887                goto do_not_strip_quotes;
888    
889              case '\\':
890                if (*++yyp != '\\')
891                  goto do_not_strip_quotes;
892    */
893                /* Fall through.  */
894              default:
895                if (yyres)
896                  yyres[yyn] = *yyp;
897                yyn++;
898                break;
899    
900              case '"':
901                if (yyres)
902                  yyres[yyn] = '\0';
903                return yyn;
904              }
905    /*
906        do_not_strip_quotes: ;
907    */
908        }
909    
910      if (! yyres)
911        return (int) yystrlen (yystr);
912    
913      return int( yystpcpy (yyres, yystr) - yyres );
914    }

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

  ViewVC Help
Powered by ViewVC