blob: 5e1e847a6f6b1023d688e9e620d291f2c74faf1a [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001%{
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
Colin Crossff48d3d2021-12-20 15:41:06 -080018#include <stdarg.h>
Doug Zongker37bee622009-06-08 17:35:39 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
Tianjie Xuc4447322017-03-06 14:44:59 -080023#include <memory>
24#include <string>
25#include <vector>
26
Tianjie Xuc3c34962017-03-28 11:49:15 -070027#include <android-base/macros.h>
28
Tao Baoe6f7f952017-10-04 09:33:01 -070029#include "edify/expr.h"
Doug Zongkerd9c9d102009-06-12 12:24:39 -070030#include "yydefs.h"
Doug Zongker37bee622009-06-08 17:35:39 -070031#include "parser.h"
32
33extern int gLine;
34extern int gColumn;
35
Tianjie Xuc4447322017-03-06 14:44:59 -080036void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s);
37int yyparse(std::unique_ptr<Expr>* root, int* error_count);
Doug Zongker37bee622009-06-08 17:35:39 -070038
Doug Zongker0d32f252014-02-13 15:07:56 -080039struct yy_buffer_state;
40void yy_switch_to_buffer(struct yy_buffer_state* new_buffer);
41struct yy_buffer_state* yy_scan_string(const char* yystr);
42
Tao Bao39119ad2016-10-10 22:52:18 -070043// Convenience function for building expressions with a fixed number
44// of arguments.
45static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
46 va_list v;
47 va_start(v, count);
Tianjie Xuc4447322017-03-06 14:44:59 -080048 Expr* e = new Expr(fn, "(operator)", loc.start, loc.end);
Tao Bao39119ad2016-10-10 22:52:18 -070049 for (size_t i = 0; i < count; ++i) {
Tianjie Xuc4447322017-03-06 14:44:59 -080050 e->argv.emplace_back(va_arg(v, Expr*));
Tao Bao39119ad2016-10-10 22:52:18 -070051 }
52 va_end(v);
Tao Bao39119ad2016-10-10 22:52:18 -070053 return e;
54}
55
Doug Zongker37bee622009-06-08 17:35:39 -070056%}
57
Doug Zongkerd9c9d102009-06-12 12:24:39 -070058%locations
59
Doug Zongker37bee622009-06-08 17:35:39 -070060%union {
61 char* str;
62 Expr* expr;
Tianjie Xuc4447322017-03-06 14:44:59 -080063 std::vector<std::unique_ptr<Expr>>* args;
Doug Zongker37bee622009-06-08 17:35:39 -070064}
65
66%token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF
67%token <str> STRING BAD
68%type <expr> expr
69%type <args> arglist
70
Tianjie Xuc4447322017-03-06 14:44:59 -080071%destructor { delete $$; } expr
72%destructor { delete $$; } arglist
73
74%parse-param {std::unique_ptr<Expr>* root}
Doug Zongker8edb00c2009-06-11 17:21:44 -070075%parse-param {int* error_count}
Steven Morelandee3dd452020-01-14 12:54:14 -080076%define parse.error verbose
Doug Zongker37bee622009-06-08 17:35:39 -070077
78/* declarations in increasing order of precedence */
79%left ';'
80%left ','
81%left OR
82%left AND
83%left EQ NE
84%left '+'
85%right '!'
86
87%%
88
Tianjie Xuc4447322017-03-06 14:44:59 -080089input: expr { root->reset($1); }
Doug Zongker37bee622009-06-08 17:35:39 -070090;
91
92expr: STRING {
Tianjie Xuc4447322017-03-06 14:44:59 -080093 $$ = new Expr(Literal, $1, @$.start, @$.end);
Doug Zongker37bee622009-06-08 17:35:39 -070094}
Doug Zongkerd9c9d102009-06-12 12:24:39 -070095| '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; }
96| expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; }
97| expr ';' expr { $$ = Build(SequenceFn, @$, 2, $1, $3); }
98| error ';' expr { $$ = $3; $$->start=@$.start; $$->end=@$.end; }
99| expr '+' expr { $$ = Build(ConcatFn, @$, 2, $1, $3); }
100| expr EQ expr { $$ = Build(EqualityFn, @$, 2, $1, $3); }
101| expr NE expr { $$ = Build(InequalityFn, @$, 2, $1, $3); }
102| expr AND expr { $$ = Build(LogicalAndFn, @$, 2, $1, $3); }
103| expr OR expr { $$ = Build(LogicalOrFn, @$, 2, $1, $3); }
104| '!' expr { $$ = Build(LogicalNotFn, @$, 1, $2); }
105| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); }
106| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
Doug Zongker37bee622009-06-08 17:35:39 -0700107| STRING '(' arglist ')' {
Tianjie Xuc4447322017-03-06 14:44:59 -0800108 Function fn = FindFunction($1);
109 if (fn == nullptr) {
110 std::string msg = "unknown function \"" + std::string($1) + "\"";
111 yyerror(root, error_count, msg.c_str());
Doug Zongker37bee622009-06-08 17:35:39 -0700112 YYERROR;
113 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800114 $$ = new Expr(fn, $1, @$.start, @$.end);
115 $$->argv = std::move(*$3);
Doug Zongker37bee622009-06-08 17:35:39 -0700116}
117;
118
119arglist: /* empty */ {
Tianjie Xuc4447322017-03-06 14:44:59 -0800120 $$ = new std::vector<std::unique_ptr<Expr>>;
Doug Zongker37bee622009-06-08 17:35:39 -0700121}
122| expr {
Tianjie Xuc4447322017-03-06 14:44:59 -0800123 $$ = new std::vector<std::unique_ptr<Expr>>;
124 $$->emplace_back($1);
Doug Zongker37bee622009-06-08 17:35:39 -0700125}
126| arglist ',' expr {
Tianjie Xuc3c34962017-03-28 11:49:15 -0700127 UNUSED($1);
Tianjie Xuc4447322017-03-06 14:44:59 -0800128 $$->push_back(std::unique_ptr<Expr>($3));
Doug Zongker37bee622009-06-08 17:35:39 -0700129}
130;
131
132%%
133
Tianjie Xuc4447322017-03-06 14:44:59 -0800134void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s) {
Doug Zongker37bee622009-06-08 17:35:39 -0700135 if (strlen(s) == 0) {
136 s = "syntax error";
137 }
138 printf("line %d col %d: %s\n", gLine, gColumn, s);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700139 ++*error_count;
Doug Zongker37bee622009-06-08 17:35:39 -0700140}
Doug Zongker0d32f252014-02-13 15:07:56 -0800141
Tao Baod8d514f2018-07-09 13:32:28 -0700142int ParseString(const std::string& str, std::unique_ptr<Expr>* root, int* error_count) {
143 yy_switch_to_buffer(yy_scan_string(str.c_str()));
144 return yyparse(root, error_count);
Doug Zongker0d32f252014-02-13 15:07:56 -0800145}