/[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 3008 by schoenebeck, Tue Oct 11 18:25:12 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 34  Line 36 
36  %defines  %defines
37  %error-verbose  %error-verbose
38    
39  %token <iValue> INTEGER  %token <iValue> INTEGER "integer literal"
40  %token <sValue> STRING  %token <sValue> STRING "string literal"
41  %token <sValue> IDENTIFIER  %token <sValue> IDENTIFIER "function name"
42  %token <sValue> VARIABLE  %token <sValue> VARIABLE "variable name"
43  %token ON END INIT NOTE DECLARE ASSIGNMENT WHILE IF OR RELEASE AND ELSE  %token ON "keyword 'on'"
44  %token BITWISE_OR BITWISE_AND BITWISE_NOT  %token END "keyword 'end'"
45  %token CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token INIT "keyword 'init'"
46  %token LE GE  %token NOTE "keyword 'note'"
47    %token RELEASE "keyword 'release'"
48  %type <nEventHandlers> script eventhandlers  %token CONTROLLER "keyword 'controller'"
49  %type <nEventHandler> eventhandler  %token DECLARE "keyword 'declare'"
50  %type <nStatements> statements body  %token ASSIGNMENT "operator ':='"
51    %token CONST_ "keyword 'const'"
52    %token POLYPHONIC "keyword 'polyphonic'"
53    %token WHILE "keyword 'while'"
54    %token IF "keyword 'if'"
55    %token ELSE "keyword 'else'"
56    %token SELECT "keyword 'select'"
57    %token CASE "keyword 'case'"
58    %token TO "keyword 'to'"
59    %token OR "operator 'or'"
60    %token AND "operator 'and'"
61    %token NOT "operator 'not'"
62    %token BITWISE_OR "bitwise operator '.or.'"
63    %token BITWISE_AND "bitwise operator '.and.'"
64    %token BITWISE_NOT "bitwise operator '.not.'"
65    %token FUNCTION "keyword 'function'"
66    %token CALL "keyword 'call'"
67    %token MOD "operator 'mod'"
68    %token LE "operator '<='"
69    %token GE "operator '>='"
70    %token END_OF_FILE 0 "end of file"
71    
72    %type <nEventHandlers> script sections
73    %type <nEventHandler> section eventhandler
74    %type <nStatements> statements opt_statements userfunctioncall
75  %type <nStatement> statement assignment  %type <nStatement> statement assignment
76  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
77  %type <nArgs> args  %type <nArgs> args
# Line 58  Line 84 
84  %%  %%
85    
86  script:  script:
87      eventhandlers  {      sections  {
88          $$ = context->handlers = $1;          $$ = context->handlers = $1;
89      }      }
90    
91  eventhandlers:  sections:
92      eventhandler  {      section  {
93          $$ = new EventHandlers();          $$ = new EventHandlers();
94          $$->add($1);          if ($1) $$->add($1);
95      }      }
96      | eventhandlers eventhandler  {      | sections section  {
97            $$ = $1;
98            if ($2) $$->add($2);
99        }
100    
101    section:
102        function_declaration  {
103            $$ = EventHandlerRef();
104        }
105        | eventhandler  {
106          $$ = $1;          $$ = $1;
         $$->add($2);  
107      }      }
108    
109  eventhandler:  eventhandler:
110      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
111          if (context->onNote)          if (context->onNote)
112              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
113          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
114          $$ = context->onNote;          $$ = context->onNote;
115      }      }
116      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
117          if (context->onInit)          if (context->onInit)
118              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
119          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
120          $$ = context->onInit;          $$ = context->onInit;
121      }      }
122      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
123          if (context->onRelease)          if (context->onRelease)
124              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
125          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
126          $$ = context->onRelease;          $$ = context->onRelease;
127      }      }
128      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
129          if (context->onController)          if (context->onController)
130              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
131          context->onController = new OnController($3);          context->onController = new OnController($3);
132          $$ = context->onController;          $$ = context->onController;
133      }      }
134    
135  body:  function_declaration:
136        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
137            const char* name = $2;
138            if (context->functionProvider->functionByName(name)) {
139                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
140            } else if (context->userFunctionByName(name)) {
141                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
142            } else {
143                context->userFnTable[name] = $3;
144            }
145        }
146    
147    opt_statements:
148      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
149          $$ = new Statements();          $$ = new Statements();
150      }      }
# Line 126  statement: Line 172  statement:
172      functioncall  {      functioncall  {
173          $$ = $1;          $$ = $1;
174      }      }
175        | userfunctioncall  {
176            $$ = $1;
177        }
178      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
179          const char* name = $2;          const char* name = $2;
180          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 289  statement: Line 338  statement:
338      | assignment  {      | assignment  {
339          $$ = $1;          $$ = $1;
340      }      }
341      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
342          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
343              $$ = new While($3, $5);              $$ = new While($3, $5);
344          } else {          } else {
# Line 297  statement: Line 346  statement:
346              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
347          }          }
348      }      }
349      | IF '(' expr ')' statements ELSE statements END IF  {      | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
350          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
351      }      }
352      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
353          $$ = new If($3, $5);          $$ = new If($3, $5);
354      }      }
355      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 323  caseclauses: Line 372  caseclauses:
372      }      }
373    
374  caseclause:  caseclause:
375      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
376          $$ = CaseBranch();          $$ = CaseBranch();
377          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
378          $$.statements = $3;          $$.statements = $3;
379      }      }
380      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
381          $$ = CaseBranch();          $$ = CaseBranch();
382          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
383          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
384          $$.statements = $5;          $$.statements = $5;
385      }      }
386    
387    userfunctioncall:
388        CALL IDENTIFIER  {
389            const char* name = $2;
390            StatementsRef fn = context->userFunctionByName(name);
391            if (context->functionProvider->functionByName(name)) {
392                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
393                $$ = StatementsRef();
394            } else if (!fn) {
395                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
396                $$ = StatementsRef();
397            } else {
398                $$ = fn;
399            }
400        }
401    
402  functioncall:  functioncall:
403      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
404          const char* name = $1;          const char* name = $1;
405          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
406          ArgsRef args = $3;          ArgsRef args = $3;
407          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
408          if (!fn) {          if (context->userFunctionByName(name)) {
409                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
410                $$ = new FunctionCall(name, args, NULL);
411            } else if (!fn) {
412              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());
413              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
414          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 371  functioncall: Line 438  functioncall:
438          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
439          ArgsRef args = new Args;          ArgsRef args = new Args;
440          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
441          if (!fn) {          if (context->userFunctionByName(name)) {
442                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
443                $$ = new FunctionCall(name, args, NULL);
444            } else if (!fn) {
445              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());
446              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
447          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 386  functioncall: Line 456  functioncall:
456          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
457          ArgsRef args = new Args;          ArgsRef args = new Args;
458          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
459          if (!fn) {          if (context->userFunctionByName(name)) {
460                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
461                $$ = new FunctionCall(name, args, NULL);
462            } else if (!fn) {
463              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());
464              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
465          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 727  void InstrScript_warning(YYLTYPE* locp, Line 800  void InstrScript_warning(YYLTYPE* locp,
800      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
801      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);
802  }  }
803    
804    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
805    int InstrScript_tnamerr(char* yyres, const char* yystr) {
806      if (*yystr == '"') {
807          int yyn = 0;
808          char const *yyp = yystr;
809          for (;;)
810            switch (*++yyp)
811              {
812    /*
813              case '\'':
814              case ',':
815                goto do_not_strip_quotes;
816    
817              case '\\':
818                if (*++yyp != '\\')
819                  goto do_not_strip_quotes;
820    */
821                /* Fall through.  */
822              default:
823                if (yyres)
824                  yyres[yyn] = *yyp;
825                yyn++;
826                break;
827    
828              case '"':
829                if (yyres)
830                  yyres[yyn] = '\0';
831                return yyn;
832              }
833        do_not_strip_quotes: ;
834        }
835    
836      if (! yyres)
837        return yystrlen (yystr);
838    
839      return yystpcpy (yyres, yystr) - yyres;
840    }

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

  ViewVC Help
Powered by ViewVC