-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.y
232 lines (189 loc) · 8.14 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
%{
#include <string.h>
#include "templateasttree.h"
#include "parser.h"
#include "scanner.h"
%}
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { Node **node }
%parse-param { yyscan_t scanner }
%output "parser.c"
%defines "parser.h"
%union {
int integer;
char *string;
Node *node;
}
%token OPEN_BRACKET CLOSE_BRACKET OPEN_BRACE CLOSE_BRACE ANY_CHAR
%token IF FOR UNLESS ELSE ELSE_IF
%token VAR_TOKEN IN_TOKEN COMMA QUOTE_OPEN QUOTE_CLOSE DOT COLON
%token PLUS MINUS EQ NOT_EQ GREAT_OR_EQ GREAT LESS_OR_EQ LESS MULTIPLY DIVIDE
%token START_BRACKET QUESTION
%token <integer> INTEGER
%token <string> WORD VARIABLE
%type <node> template html_or_code if variable code html expression
%type <node> sub_expression text_variable text_variable_members
%type <node> statement for unless arguments argument_list
%type <node> else_if else_ifs string text_string call_helper
%type <node> object object_member object_members
%start start
%%
start: { *node = nodeAddHtmlText(""); }
| template { *node = $1; }
;
template: html_or_code { $$ = $1; }
| template html_or_code { $$ = nodeAddSibling($1, $2); }
;
html_or_code: code { $$ = $1; }
| html { $$ = $1; }
;
code: if { $$ = $1; }
| unless { $$ = $1; }
| for { $$ = $1; }
| variable { $$ = $1; }
;
html: ANY_CHAR {
char *ptr = yyval.string;
$$ = nodeAddHtmlText(yyval.string);
free(ptr);
}
;
text_string: WORD {
char *ptr = yyval.string;
$$ = nodeAddStringExpression(yyval.string);
free(ptr);
}
;
string: QUOTE_OPEN text_string QUOTE_CLOSE { $$ = $2; }
| QUOTE_OPEN QUOTE_CLOSE { $$ = nodeAddStringExpression(""); }
;
text_variable_members: text_variable { $$ = $1; }
| text_variable_members DOT text_variable { nodeAddVariableMember($1, $3); }
;
text_variable: WORD {
char *ptr = yyval.string;
$$ = nodeAddVariable(yyval.string);
free(ptr);
}
;
variable: START_BRACKET expression CLOSE_BRACE { $$ = $2; }
| VARIABLE {
char *ptr = yyval.string;
$$ = nodeAddVariable(yyval.string + 1);
free(ptr);
}
| VARIABLE arguments {
char *ptr = yyval.string;
$$ = nodeAddHelper(yyval.string + 1, $2);
free(ptr);
}
;
object: OPEN_BRACE CLOSE_BRACE { $$ = nodeAddObject(NULL); }
| OPEN_BRACE object_members CLOSE_BRACE { $$ = nodeAddObject($2); };
;
object_member: WORD COLON expression {
char *ptr = yyval.string;
$$ = nodeAddObjectMember(yyval.string, $3);
free(ptr);
}
;
object_members: object_member { $$ = $1; }
| object_members COMMA object_member { $$ = nodeAddSibling($1, $3); }
;
call_helper: WORD arguments {
char *ptr = yyval.string;
$$ = nodeAddHelper(yyval.string, $2);
free(ptr);
}
;
arguments: OPEN_BRACKET CLOSE_BRACKET { $$ = NULL; }
| OPEN_BRACKET argument_list CLOSE_BRACKET { $$ = $2; }
;
argument_list: expression { $$ = $1; }
| argument_list COMMA expression { $$ = nodeAddSibling($1, $3); }
;
if: IF OPEN_BRACKET expression CLOSE_BRACKET statement
{ $$ = nodeAddIfCondition($3, $5); }
| IF OPEN_BRACKET expression CLOSE_BRACKET statement else_ifs
{ $$ = nodeAddIfElseIfCondition($3, $5, $6); }
| IF OPEN_BRACKET expression CLOSE_BRACKET statement ELSE statement
{ $$ = nodeAddIfElseCondition($3, $5, $7); }
| IF OPEN_BRACKET expression CLOSE_BRACKET statement else_ifs ELSE statement
{ $$ = nodeAddIfElseIfElseCondition($3, $5, $6, $8); }
;
else_if: ELSE_IF OPEN_BRACKET expression CLOSE_BRACKET statement
{ $$ = nodeAddElseIfCondition($3, $5); }
;
else_ifs: else_if { $$ = $1; }
| else_ifs else_if { $$ = nodeAddSibling($1, $2); }
;
unless: UNLESS OPEN_BRACKET expression CLOSE_BRACKET statement
{ $$ = nodeAddUnlessCondition($3, $5); }
| UNLESS OPEN_BRACKET expression CLOSE_BRACKET statement ELSE statement
{ $$ = nodeAddUnlessElseCondition($3, $5, $7); }
;
for: FOR OPEN_BRACKET WORD IN_TOKEN text_variable CLOSE_BRACKET statement
{
$$ = nodeAddForLoop( nodeAddStringExpression($3), $5, $7);
free($3);
}
| FOR OPEN_BRACKET VAR_TOKEN WORD IN_TOKEN text_variable CLOSE_BRACKET statement
{
$$ = nodeAddForLoop( nodeAddStringExpression($4), $6, $8);
free($4);
}
;
sub_expression: INTEGER { $$ = nodeAddIntegerExpression(yyval.integer); }
| call_helper { $$ = $1; /* helper() */}
| call_helper DOT text_variable_members
{ $$ = $1; nodeAddHelperMembers($1, $3); /* helper().member.member */ }
| text_variable_members { $$ = $1; /* variable.member.member */ }
| string { $$ = $1; /* "string" */}
| object { $$ = $1; /* json object */ }
;
expression: sub_expression { $$ = $1; }
| sub_expression PLUS sub_expression { $$ = nodeAddPlus($1, $3); }
| sub_expression MINUS sub_expression { $$ = nodeAddMinus($1, $3); }
| sub_expression MULTIPLY sub_expression { $$ = nodeAddMutiply($1, $3); }
| sub_expression DIVIDE sub_expression { $$ = nodeAddDivide($1, $3); }
| sub_expression EQ sub_expression { $$ = nodeAddEq($1, $3); }
| sub_expression NOT_EQ sub_expression { $$ = nodeAddNotEq($1, $3); }
| sub_expression GREAT_OR_EQ sub_expression { $$ = nodeAddGreatOrEq($1, $3); }
| sub_expression GREAT sub_expression { $$ = nodeAddGreat($1, $3); }
| sub_expression LESS_OR_EQ sub_expression { $$ = nodeAddLessOrEq($1, $3); }
| sub_expression LESS sub_expression { $$ = nodeAddLess($1, $3); }
| sub_expression QUESTION sub_expression COLON sub_expression
{ $$ = nodeAddIfElseCondition($1, $3, $5); }
;
statement: OPEN_BRACE CLOSE_BRACE { $$ = nodeAddHtmlText(""); }
| OPEN_BRACE template CLOSE_BRACE { $$ = $2; }
;
%%
int yyerror(Node *node, yyscan_t *scanner, const char *msg)
{
fprintf(stderr,"Template compile error: %s\n", msg);
return 0;
}
Node *getAstTree(const char *templ)
{
Node *expression = NULL;
yyscan_t scanner;
YY_BUFFER_STATE state;
if (yylex_init(&scanner))
{
fprintf(stderr, "couldn't initialize\n");
return NULL;
}
yyset_debug(1, &scanner);
state = yy_scan_string(templ, scanner);
yyset_debug(1, &scanner);
if (yyparse(&expression, scanner))
{
fprintf(stderr, "error parsing\n");
return NULL;
}
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return expression;
}