blob: 3f9ade1449b0aa876deb3b6b428a72916c3ffe32 [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"
Doug Zongkerd9c9d102009-06-12 12:24:39 -070023#include "yydefs.h"
Doug Zongker37bee622009-06-08 17:35:39 -070024#include "parser.h"
25
26extern int gLine;
27extern int gColumn;
28
Doug Zongker8edb00c2009-06-11 17:21:44 -070029void yyerror(Expr** root, int* error_count, const char* s);
30int yyparse(Expr** root, int* error_count);
Doug Zongker37bee622009-06-08 17:35:39 -070031
32%}
33
Doug Zongkerd9c9d102009-06-12 12:24:39 -070034%locations
35
Doug Zongker37bee622009-06-08 17:35:39 -070036%union {
37 char* str;
38 Expr* expr;
39 struct {
40 int argc;
41 Expr** argv;
42 } args;
43}
44
45%token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF
46%token <str> STRING BAD
47%type <expr> expr
48%type <args> arglist
49
50%parse-param {Expr** root}
Doug Zongker8edb00c2009-06-11 17:21:44 -070051%parse-param {int* error_count}
Doug Zongker37bee622009-06-08 17:35:39 -070052%error-verbose
53
54/* declarations in increasing order of precedence */
55%left ';'
56%left ','
57%left OR
58%left AND
59%left EQ NE
60%left '+'
61%right '!'
62
63%%
64
65input: expr { *root = $1; }
66;
67
68expr: STRING {
69 $$ = malloc(sizeof(Expr));
70 $$->fn = Literal;
71 $$->name = $1;
72 $$->argc = 0;
73 $$->argv = NULL;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074 $$->start = @$.start;
75 $$->end = @$.end;
Doug Zongker37bee622009-06-08 17:35:39 -070076}
Doug Zongkerd9c9d102009-06-12 12:24:39 -070077| '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; }
78| expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; }
79| expr ';' expr { $$ = Build(SequenceFn, @$, 2, $1, $3); }
80| error ';' expr { $$ = $3; $$->start=@$.start; $$->end=@$.end; }
81| expr '+' expr { $$ = Build(ConcatFn, @$, 2, $1, $3); }
82| expr EQ expr { $$ = Build(EqualityFn, @$, 2, $1, $3); }
83| expr NE expr { $$ = Build(InequalityFn, @$, 2, $1, $3); }
84| expr AND expr { $$ = Build(LogicalAndFn, @$, 2, $1, $3); }
85| expr OR expr { $$ = Build(LogicalOrFn, @$, 2, $1, $3); }
86| '!' expr { $$ = Build(LogicalNotFn, @$, 1, $2); }
87| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); }
88| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
Doug Zongker37bee622009-06-08 17:35:39 -070089| STRING '(' arglist ')' {
90 $$ = malloc(sizeof(Expr));
91 $$->fn = FindFunction($1);
92 if ($$->fn == NULL) {
93 char buffer[256];
94 snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
Doug Zongker8edb00c2009-06-11 17:21:44 -070095 yyerror(root, error_count, buffer);
Doug Zongker37bee622009-06-08 17:35:39 -070096 YYERROR;
97 }
98 $$->name = $1;
99 $$->argc = $3.argc;
100 $$->argv = $3.argv;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700101 $$->start = @$.start;
102 $$->end = @$.end;
Doug Zongker37bee622009-06-08 17:35:39 -0700103}
104;
105
106arglist: /* empty */ {
107 $$.argc = 0;
108 $$.argv = NULL;
109}
110| expr {
111 $$.argc = 1;
112 $$.argv = malloc(sizeof(Expr*));
113 $$.argv[0] = $1;
114}
115| arglist ',' expr {
116 $$.argc = $1.argc + 1;
117 $$.argv = realloc($$.argv, $$.argc * sizeof(Expr*));
118 $$.argv[$$.argc-1] = $3;
119}
120;
121
122%%
123
Doug Zongker8edb00c2009-06-11 17:21:44 -0700124void yyerror(Expr** root, int* error_count, const char* s) {
Doug Zongker37bee622009-06-08 17:35:39 -0700125 if (strlen(s) == 0) {
126 s = "syntax error";
127 }
128 printf("line %d col %d: %s\n", gLine, gColumn, s);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700129 ++*error_count;
Doug Zongker37bee622009-06-08 17:35:39 -0700130}