/[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 2935 by schoenebeck, Sun Jul 10 14:24:13 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 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      | eventhandlers eventhandler  {      | sections section  {
98            $$ = $1;
99            if ($2) $$->add($2);
100        }
101    
102    section:
103        function_declaration  {
104            $$ = EventHandlerRef();
105        }
106        | 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 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                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
307              }              }
308          }          }
# Line 289  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 297  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 323  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 357  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 367  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 382  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 415  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 721  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.2935  
changed lines
  Added in v.3054

  ViewVC Help
Powered by ViewVC