/[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 2889 by schoenebeck, Mon Apr 25 17:28:23 2016 UTC revision 3054 by schoenebeck, Thu Dec 15 12:47:45 2016 UTC
# 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 CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token END "keyword 'end'"
46  %token LE GE  %token INIT "keyword 'init'"
47    %token NOTE "keyword 'note'"
48  %type <nEventHandlers> script eventhandlers  %token RELEASE "keyword 'release'"
49  %type <nEventHandler> eventhandler  %token CONTROLLER "keyword 'controller'"
50  %type <nStatements> statements  %token DECLARE "keyword 'declare'"
51    %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
79  %type <nExpression> arg expr or_expr and_expr rel_expr add_expr mul_expr unary_expr concat_expr  %type <nExpression> arg expr logical_or_expr logical_and_expr bitwise_or_expr bitwise_and_expr rel_expr add_expr mul_expr unary_expr concat_expr
80  %type <nCaseBranch> caseclause  %type <nCaseBranch> caseclause
81  %type <nCaseBranches> caseclauses  %type <nCaseBranches> caseclauses
82    
# Line 57  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 statements 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 statements 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 statements 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 statements 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    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) */  {
150            $$ = new Statements();
151        }
152        | statements  {
153            $$ = $1;
154        }
155    
156  statements:  statements:
157      statement  {      statement  {
158          $$ = new Statements();          $$ = new Statements();
# Line 117  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 240  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                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
307              }              }
308          }          }
# Line 280  statement: Line 340  statement:
340      | assignment  {      | assignment  {
341          $$ = $1;          $$ = $1;
342      }      }
343      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
344          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
345              $$ = new While($3, $5);              $$ = new While($3, $5);
346          } else {          } else {
# Line 288  statement: Line 348  statement:
348              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
349          }          }
350      }      }
351      | IF '(' expr ')' statements ELSE statements END IF  {      | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
352          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
353      }      }
354      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
355          $$ = new If($3, $5);          $$ = new If($3, $5);
356      }      }
357      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 314  caseclauses: Line 374  caseclauses:
374      }      }
375    
376  caseclause:  caseclause:
377      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
378          $$ = CaseBranch();          $$ = CaseBranch();
379          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
380          $$.statements = $3;          $$.statements = $3;
381      }      }
382      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
383          $$ = CaseBranch();          $$ = CaseBranch();
384          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
385          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
386          $$.statements = $5;          $$.statements = $5;
387      }      }
388    
389    userfunctioncall:
390        CALL IDENTIFIER  {
391            const char* name = $2;
392            StatementsRef fn = context->userFunctionByName(name);
393            if (context->functionProvider->functionByName(name)) {
394                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
395                $$ = StatementsRef();
396            } else if (!fn) {
397                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
398                $$ = StatementsRef();
399            } else {
400                $$ = fn;
401            }
402        }
403    
404  functioncall:  functioncall:
405      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
406          const char* name = $1;          const char* name = $1;
407          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
408          ArgsRef args = $3;          ArgsRef args = $3;
409          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
410          if (!fn) {          if (context->userFunctionByName(name)) {
411                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
412                $$ = new FunctionCall(name, args, NULL);
413            } else if (!fn) {
414              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());
415              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
416          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 348  functioncall: Line 426  functioncall:
426                      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());
427                      argsOK = false;                      argsOK = false;
428                      break;                      break;
429                    } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
430                        PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
431                        argsOK = false;
432                        break;
433                  }                  }
434              }              }
435              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
# Line 358  functioncall: Line 440  functioncall:
440          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
441          ArgsRef args = new Args;          ArgsRef args = new Args;
442          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
443          if (!fn) {          if (context->userFunctionByName(name)) {
444                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
445                $$ = new FunctionCall(name, args, NULL);
446            } else if (!fn) {
447              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());
448              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
449          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 373  functioncall: Line 458  functioncall:
458          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
459          ArgsRef args = new Args;          ArgsRef args = new Args;
460          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
461          if (!fn) {          if (context->userFunctionByName(name)) {
462                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
463                $$ = new FunctionCall(name, args, NULL);
464            } else if (!fn) {
465              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());
466              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
467          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 406  assignment: Line 494  assignment:
494              PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());
495          else if (var->isConstExpr())          else if (var->isConstExpr())
496              PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());
497            else if (!var->isAssignable())
498                PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
499          else if (var->exprType() != $3->exprType())          else if (var->exprType() != $3->exprType())
500              PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' is of type " + typeStr(var->exprType()) + ", assignment is of type " + typeStr($3->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' is of type " + typeStr(var->exprType()) + ", assignment is of type " + typeStr($3->exprType()) + " though.").c_str());
501          $$ = new Assignment(var, $3);          $$ = new Assignment(var, $3);
# Line 467  unary_expr: Line 557  unary_expr:
557      | '-' unary_expr  {      | '-' unary_expr  {
558          $$ = new Neg($2);          $$ = new Neg($2);
559      }      }
560        | BITWISE_NOT unary_expr  {
561            if ($2->exprType() != INT_EXPR) {
562                PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
563                $$ = new IntLiteral(0);
564            } else {
565                $$ = new BitwiseNot($2);
566            }
567        }
568      | NOT unary_expr  {      | NOT unary_expr  {
569          if ($2->exprType() != INT_EXPR) {          if ($2->exprType() != INT_EXPR) {
570              PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());              PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
# Line 480  expr: Line 578  expr:
578      concat_expr      concat_expr
579    
580  concat_expr:  concat_expr:
581      or_expr      logical_or_expr
582      | concat_expr '&' or_expr  {      | concat_expr '&' logical_or_expr  {
583          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
584          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
585          if (lhs->isConstExpr() && rhs->isConstExpr()) {          if (lhs->isConstExpr() && rhs->isConstExpr()) {
# Line 493  concat_expr: Line 591  concat_expr:
591          }          }
592      }      }
593    
594  or_expr:  logical_or_expr:
595      and_expr      logical_and_expr
596      | or_expr OR and_expr  {      | logical_or_expr OR logical_and_expr  {
597          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
598          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
599          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 509  or_expr: Line 607  or_expr:
607          }          }
608      }      }
609    
610  and_expr:  logical_and_expr:
611      rel_expr  {      bitwise_or_expr  {
612          $$ = $1;          $$ = $1;
613      }      }
614      | and_expr AND rel_expr  {      | logical_and_expr AND bitwise_or_expr  {
615          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
616          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
617          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 527  and_expr: Line 625  and_expr:
625          }          }
626      }      }
627    
628    bitwise_or_expr:
629        bitwise_and_expr
630        | bitwise_or_expr BITWISE_OR bitwise_and_expr  {
631            ExpressionRef lhs = $1;
632            ExpressionRef rhs = $3;
633            if (lhs->exprType() != INT_EXPR) {
634                PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
635                $$ = new IntLiteral(0);
636            } else if (rhs->exprType() != INT_EXPR) {
637                PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
638                $$ = new IntLiteral(0);
639            } else {
640                $$ = new BitwiseOr(lhs, rhs);
641            }
642        }
643    
644    bitwise_and_expr:
645        rel_expr  {
646            $$ = $1;
647        }
648        | bitwise_and_expr BITWISE_AND rel_expr  {
649            ExpressionRef lhs = $1;
650            ExpressionRef rhs = $3;
651            if (lhs->exprType() != INT_EXPR) {
652                PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
653                $$ = new IntLiteral(0);
654            } else if (rhs->exprType() != INT_EXPR) {
655                PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
656                $$ = new IntLiteral(0);
657            } else {
658                $$ = new BitwiseAnd(lhs, rhs);
659            }
660        }
661    
662  rel_expr:  rel_expr:
663        add_expr        add_expr
664      | rel_expr '<' add_expr  {      | rel_expr '<' add_expr  {
# Line 670  void InstrScript_warning(YYLTYPE* locp, Line 802  void InstrScript_warning(YYLTYPE* locp,
802      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
803      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);
804  }  }
805    
806    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
807    int InstrScript_tnamerr(char* yyres, const char* yystr) {
808      if (*yystr == '"') {
809          int yyn = 0;
810          char const *yyp = yystr;
811          for (;;)
812            switch (*++yyp)
813              {
814    /*
815              case '\'':
816              case ',':
817                goto do_not_strip_quotes;
818    
819              case '\\':
820                if (*++yyp != '\\')
821                  goto do_not_strip_quotes;
822    */
823                /* Fall through.  */
824              default:
825                if (yyres)
826                  yyres[yyn] = *yyp;
827                yyn++;
828                break;
829    
830              case '"':
831                if (yyres)
832                  yyres[yyn] = '\0';
833                return yyn;
834              }
835    /*
836        do_not_strip_quotes: ;
837    */
838        }
839    
840      if (! yyres)
841        return (int) yystrlen (yystr);
842    
843      return int( yystpcpy (yyres, yystr) - yyres );
844    }

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

  ViewVC Help
Powered by ViewVC