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 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include <android-base/macros.h> 27 28 #include "edify/expr.h" 29 #include "yydefs.h" 30 #include "parser.h" 31 32 extern int gLine; 33 extern int gColumn; 34 35 void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s); 36 int yyparse(std::unique_ptr<Expr>* root, int* error_count); 37 38 struct yy_buffer_state; 39 void yy_switch_to_buffer(struct yy_buffer_state* new_buffer); 40 struct yy_buffer_state* yy_scan_string(const char* yystr); 41 42 // Convenience function for building expressions with a fixed number 43 // of arguments. 44 static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { 45 va_list v; 46 va_start(v, count); 47 Expr* e = new Expr(fn, "(operator)", loc.start, loc.end); 48 for (size_t i = 0; i < count; ++i) { 49 e->argv.emplace_back(va_arg(v, Expr*)); 50 } 51 va_end(v); 52 return e; 53 } 54 55 %} 56 57 %locations 58 59 %union { 60 char* str; 61 Expr* expr; 62 std::vector<std::unique_ptr<Expr>>* args; 63 } 64 65 %token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF 66 %token <str> STRING BAD 67 %type <expr> expr 68 %type <args> arglist 69 70 %destructor { delete $$; } expr 71 %destructor { delete $$; } arglist 72 73 %parse-param {std::unique_ptr<Expr>* root} 74 %parse-param {int* error_count} 75 %error-verbose 76 77 /* declarations in increasing order of precedence */ 78 %left ';' 79 %left ',' 80 %left OR 81 %left AND 82 %left EQ NE 83 %left '+' 84 %right '!' 85 86 %% 87 88 input: expr { root->reset($1); } 89 ; 90 91 expr: STRING { 92 $$ = new Expr(Literal, $1, @$.start, @$.end); 93 } 94 | '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; } 95 | expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; } 96 | expr ';' expr { $$ = Build(SequenceFn, @$, 2, $1, $3); } 97 | error ';' expr { $$ = $3; $$->start=@$.start; $$->end=@$.end; } 98 | expr '+' expr { $$ = Build(ConcatFn, @$, 2, $1, $3); } 99 | expr EQ expr { $$ = Build(EqualityFn, @$, 2, $1, $3); } 100 | expr NE expr { $$ = Build(InequalityFn, @$, 2, $1, $3); } 101 | expr AND expr { $$ = Build(LogicalAndFn, @$, 2, $1, $3); } 102 | expr OR expr { $$ = Build(LogicalOrFn, @$, 2, $1, $3); } 103 | '!' expr { $$ = Build(LogicalNotFn, @$, 1, $2); } 104 | IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); } 105 | IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); } 106 | STRING '(' arglist ')' { 107 Function fn = FindFunction($1); 108 if (fn == nullptr) { 109 std::string msg = "unknown function \"" + std::string($1) + "\""; 110 yyerror(root, error_count, msg.c_str()); 111 YYERROR; 112 } 113 $$ = new Expr(fn, $1, @$.start, @$.end); 114 $$->argv = std::move(*$3); 115 } 116 ; 117 118 arglist: /* empty */ { 119 $$ = new std::vector<std::unique_ptr<Expr>>; 120 } 121 | expr { 122 $$ = new std::vector<std::unique_ptr<Expr>>; 123 $$->emplace_back($1); 124 } 125 | arglist ',' expr { 126 UNUSED($1); 127 $$->push_back(std::unique_ptr<Expr>($3)); 128 } 129 ; 130 131 %% 132 133 void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s) { 134 if (strlen(s) == 0) { 135 s = "syntax error"; 136 } 137 printf("line %d col %d: %s\n", gLine, gColumn, s); 138 ++*error_count; 139 } 140 141 int ParseString(const std::string& str, std::unique_ptr<Expr>* root, int* error_count) { 142 yy_switch_to_buffer(yy_scan_string(str.c_str())); 143 return yyparse(root, error_count); 144 } 145