No Description

parser.yy 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %{
  2. /*
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include <android-base/macros.h>
  24. #include "edify/expr.h"
  25. #include "yydefs.h"
  26. #include "parser.h"
  27. extern int gLine;
  28. extern int gColumn;
  29. void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s);
  30. int yyparse(std::unique_ptr<Expr>* root, int* error_count);
  31. struct yy_buffer_state;
  32. void yy_switch_to_buffer(struct yy_buffer_state* new_buffer);
  33. struct yy_buffer_state* yy_scan_string(const char* yystr);
  34. // Convenience function for building expressions with a fixed number
  35. // of arguments.
  36. static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
  37. va_list v;
  38. va_start(v, count);
  39. Expr* e = new Expr(fn, "(operator)", loc.start, loc.end);
  40. for (size_t i = 0; i < count; ++i) {
  41. e->argv.emplace_back(va_arg(v, Expr*));
  42. }
  43. va_end(v);
  44. return e;
  45. }
  46. %}
  47. %locations
  48. %union {
  49. char* str;
  50. Expr* expr;
  51. std::vector<std::unique_ptr<Expr>>* args;
  52. }
  53. %token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF
  54. %token <str> STRING BAD
  55. %type <expr> expr
  56. %type <args> arglist
  57. %destructor { delete $$; } expr
  58. %destructor { delete $$; } arglist
  59. %parse-param {std::unique_ptr<Expr>* root}
  60. %parse-param {int* error_count}
  61. %error-verbose
  62. /* declarations in increasing order of precedence */
  63. %left ';'
  64. %left ','
  65. %left OR
  66. %left AND
  67. %left EQ NE
  68. %left '+'
  69. %right '!'
  70. %%
  71. input: expr { root->reset($1); }
  72. ;
  73. expr: STRING {
  74. $$ = new Expr(Literal, $1, @$.start, @$.end);
  75. }
  76. | '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; }
  77. | expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; }
  78. | expr ';' expr { $$ = Build(SequenceFn, @$, 2, $1, $3); }
  79. | error ';' expr { $$ = $3; $$->start=@$.start; $$->end=@$.end; }
  80. | expr '+' expr { $$ = Build(ConcatFn, @$, 2, $1, $3); }
  81. | expr EQ expr { $$ = Build(EqualityFn, @$, 2, $1, $3); }
  82. | expr NE expr { $$ = Build(InequalityFn, @$, 2, $1, $3); }
  83. | expr AND expr { $$ = Build(LogicalAndFn, @$, 2, $1, $3); }
  84. | expr OR expr { $$ = Build(LogicalOrFn, @$, 2, $1, $3); }
  85. | '!' expr { $$ = Build(LogicalNotFn, @$, 1, $2); }
  86. | IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); }
  87. | IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
  88. | STRING '(' arglist ')' {
  89. Function fn = FindFunction($1);
  90. if (fn == nullptr) {
  91. std::string msg = "unknown function \"" + std::string($1) + "\"";
  92. yyerror(root, error_count, msg.c_str());
  93. YYERROR;
  94. }
  95. $$ = new Expr(fn, $1, @$.start, @$.end);
  96. $$->argv = std::move(*$3);
  97. }
  98. ;
  99. arglist: /* empty */ {
  100. $$ = new std::vector<std::unique_ptr<Expr>>;
  101. }
  102. | expr {
  103. $$ = new std::vector<std::unique_ptr<Expr>>;
  104. $$->emplace_back($1);
  105. }
  106. | arglist ',' expr {
  107. UNUSED($1);
  108. $$->push_back(std::unique_ptr<Expr>($3));
  109. }
  110. ;
  111. %%
  112. void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s) {
  113. if (strlen(s) == 0) {
  114. s = "syntax error";
  115. }
  116. printf("line %d col %d: %s\n", gLine, gColumn, s);
  117. ++*error_count;
  118. }
  119. int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count) {
  120. yy_switch_to_buffer(yy_scan_string(str));
  121. return yyparse(root, error_count);
  122. }