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