blob: cf163c026591aeb1f6c5254ea767799fb6e71c3d [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
Doug Zongker8edb00c2009-06-11 17:21:44 -070028void yyerror(Expr** root, int* error_count, const char* s);
29int yyparse(Expr** root, int* error_count);
Doug Zongker37bee622009-06-08 17:35:39 -070030
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}
Doug Zongker8edb00c2009-06-11 17:21:44 -070048%parse-param {int* error_count}
Doug Zongker37bee622009-06-08 17:35:39 -070049%error-verbose
50
51/* declarations in increasing order of precedence */
52%left ';'
53%left ','
54%left OR
55%left AND
56%left EQ NE
57%left '+'
58%right '!'
59
60%%
61
62input: expr { *root = $1; }
63;
64
65expr: STRING {
66 $$ = malloc(sizeof(Expr));
67 $$->fn = Literal;
68 $$->name = $1;
69 $$->argc = 0;
70 $$->argv = NULL;
71}
72| '(' expr ')' { $$ = $2; }
73| expr ';' { $$ = $1; }
74| expr ';' expr { $$ = Build(SequenceFn, 2, $1, $3); }
75| error ';' expr { $$ = $3; }
76| expr '+' expr { $$ = Build(ConcatFn, 2, $1, $3); }
77| expr EQ expr { $$ = Build(EqualityFn, 2, $1, $3); }
78| expr NE expr { $$ = Build(InequalityFn, 2, $1, $3); }
79| expr AND expr { $$ = Build(LogicalAndFn, 2, $1, $3); }
80| expr OR expr { $$ = Build(LogicalOrFn, 2, $1, $3); }
81| '!' expr { $$ = Build(LogicalNotFn, 1, $2); }
82| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, 2, $2, $4); }
83| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, 3, $2, $4, $6); }
84| STRING '(' arglist ')' {
85 $$ = malloc(sizeof(Expr));
86 $$->fn = FindFunction($1);
87 if ($$->fn == NULL) {
88 char buffer[256];
89 snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
Doug Zongker8edb00c2009-06-11 17:21:44 -070090 yyerror(root, error_count, buffer);
Doug Zongker37bee622009-06-08 17:35:39 -070091 YYERROR;
92 }
93 $$->name = $1;
94 $$->argc = $3.argc;
95 $$->argv = $3.argv;
96}
97;
98
99arglist: /* empty */ {
100 $$.argc = 0;
101 $$.argv = NULL;
102}
103| expr {
104 $$.argc = 1;
105 $$.argv = malloc(sizeof(Expr*));
106 $$.argv[0] = $1;
107}
108| arglist ',' expr {
109 $$.argc = $1.argc + 1;
110 $$.argv = realloc($$.argv, $$.argc * sizeof(Expr*));
111 $$.argv[$$.argc-1] = $3;
112}
113;
114
115%%
116
Doug Zongker8edb00c2009-06-11 17:21:44 -0700117void yyerror(Expr** root, int* error_count, const char* s) {
Doug Zongker37bee622009-06-08 17:35:39 -0700118 if (strlen(s) == 0) {
119 s = "syntax error";
120 }
121 printf("line %d col %d: %s\n", gLine, gColumn, s);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700122 ++*error_count;
Doug Zongker37bee622009-06-08 17:35:39 -0700123}