/[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 2947 by schoenebeck, Thu Jul 14 10:37:28 2016 UTC revision 3034 by schoenebeck, Mon Oct 31 00:05:00 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 opt_statements  %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:
# Line 98  eventhandler: Line 132  eventhandler:
132          $$ = context->onController;          $$ = context->onController;
133      }      }
134    
135    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:  opt_statements:
148      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
149          $$ = new Statements();          $$ = new Statements();
# 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 249  statement: Line 298  statement:
298                          break;                          break;
299                      }                      }
300                  }                  }
301                  if (argsOK)                  if (argsOK) {
302                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
303                  else                      $$ = new NoOperation;
304                    } else
305                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
306              }              }
307          }          }
# Line 335  caseclause: Line 385  caseclause:
385          $$.statements = $5;          $$.statements = $5;
386      }      }
387    
388    userfunctioncall:
389        CALL IDENTIFIER  {
390            const char* name = $2;
391            StatementsRef fn = context->userFunctionByName(name);
392            if (context->functionProvider->functionByName(name)) {
393                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
394                $$ = StatementsRef();
395            } else if (!fn) {
396                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
397                $$ = StatementsRef();
398            } else {
399                $$ = fn;
400            }
401        }
402    
403  functioncall:  functioncall:
404      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
405          const char* name = $1;          const char* name = $1;
406          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
407          ArgsRef args = $3;          ArgsRef args = $3;
408          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
409          if (!fn) {          if (context->userFunctionByName(name)) {
410                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
411                $$ = new FunctionCall(name, args, NULL);
412            } else if (!fn) {
413              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());
414              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
415          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 371  functioncall: Line 439  functioncall:
439          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
440          ArgsRef args = new Args;          ArgsRef args = new Args;
441          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
442          if (!fn) {          if (context->userFunctionByName(name)) {
443                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
444                $$ = new FunctionCall(name, args, NULL);
445            } else if (!fn) {
446              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());
447              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
448          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 386  functioncall: Line 457  functioncall:
457          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
458          ArgsRef args = new Args;          ArgsRef args = new Args;
459          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
460          if (!fn) {          if (context->userFunctionByName(name)) {
461                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
462                $$ = new FunctionCall(name, args, NULL);
463            } else if (!fn) {
464              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());
465              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
466          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 727  void InstrScript_warning(YYLTYPE* locp, Line 801  void InstrScript_warning(YYLTYPE* locp,
801      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
802      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);
803  }  }
804    
805    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
806    int InstrScript_tnamerr(char* yyres, const char* yystr) {
807      if (*yystr == '"') {
808          int yyn = 0;
809          char const *yyp = yystr;
810          for (;;)
811            switch (*++yyp)
812              {
813    /*
814              case '\'':
815              case ',':
816                goto do_not_strip_quotes;
817    
818              case '\\':
819                if (*++yyp != '\\')
820                  goto do_not_strip_quotes;
821    */
822                /* Fall through.  */
823              default:
824                if (yyres)
825                  yyres[yyn] = *yyp;
826                yyn++;
827                break;
828    
829              case '"':
830                if (yyres)
831                  yyres[yyn] = '\0';
832                return yyn;
833              }
834    /*
835        do_not_strip_quotes: ;
836    */
837        }
838    
839      if (! yyres)
840        return yystrlen (yystr);
841    
842      return yystpcpy (yyres, yystr) - yyres;
843    }

Legend:
Removed from v.2947  
changed lines
  Added in v.3034

  ViewVC Help
Powered by ViewVC