blob: 67a210fcae77ead8732db3504f3820a299ed49fe [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
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "expr.h"
23#include "parser.h"
24
25extern int gLine;
26extern int gColumn;
27
28void yyerror(Expr** root, const char* s);
29int yyparse(Expr** root);
30
31%}
32
33%union {
34 char* str;
35 Expr* expr;
36 struct {
37 int argc;
38 Expr** argv;
39 } args;
40}
41
42%token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF
43%token <str> STRING BAD
44%type <expr> expr
45%type <args> arglist
46
47%parse-param {Expr** root}
48%error-verbose
49
50/* declarations in increasing order of precedence */
51%left ';'
52%left ','
53%left OR
54%left AND
55%left EQ NE
56%left '+'
57%right '!'
58
59%%
60
61input: expr { *root = $1; }
62;
63
64expr: STRING {
65 $$ = malloc(sizeof(Expr));
66 $$->fn = Literal;
67 $$->name = $1;
68 $$->argc = 0;
69 $$->argv = NULL;
70}
71| '(' expr ')' { $$ = $2; }
72| expr ';' { $$ = $1; }
73| expr ';' expr { $$ = Build(SequenceFn, 2, $1, $3); }
74| error ';' expr { $$ = $3; }
75| expr '+' expr { $$ = Build(ConcatFn, 2, $1, $3); }
76| expr EQ expr { $$ = Build(EqualityFn, 2, $1, $3); }
77| expr NE expr { $$ = Build(InequalityFn, 2, $1, $3); }
78| expr AND expr { $$ = Build(LogicalAndFn, 2, $1, $3); }
79| expr OR expr { $$ = Build(LogicalOrFn, 2, $1, $3); }
80| '!' expr { $$ = Build(LogicalNotFn, 1, $2); }
81| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, 2, $2, $4); }
82| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, 3, $2, $4, $6); }
83| STRING '(' arglist ')' {
84 $$ = malloc(sizeof(Expr));
85 $$->fn = FindFunction($1);
86 if ($$->fn == NULL) {
87 char buffer[256];
88 snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
89 yyerror(root, buffer);
90 YYERROR;
91 }
92 $$->name = $1;
93 $$->argc = $3.argc;
94 $$->argv = $3.argv;
95}
96;
97
98arglist: /* empty */ {
99 $$.argc = 0;
100 $$.argv = NULL;
101}
102| expr {
103 $$.argc = 1;
104 $$.argv = malloc(sizeof(Expr*));
105 $$.argv[0] = $1;
106}
107| arglist ',' expr {
108 $$.argc = $1.argc + 1;
109 $$.argv = realloc($$.argv, $$.argc * sizeof(Expr*));
110 $$.argv[$$.argc-1] = $3;
111}
112;
113
114%%
115
116void yyerror(Expr** root, const char* s) {
117 if (strlen(s) == 0) {
118 s = "syntax error";
119 }
120 printf("line %d col %d: %s\n", gLine, gColumn, s);
121}