-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_parse.y
180 lines (160 loc) · 5.65 KB
/
config_parse.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
/*
* Copyright (C) 2015 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "session.h"
#include "config.h"
#include "config_parse_util.h"
int yylex(Session *, const char *file_path);
%}
/* yyparse arguments */
%parse-param { Session *sess }
%parse-param { ConfigLevel config_level }
%parse-param { const char *file_path }
/* yylex arguments */
%lex-param { Session *sess }
%lex-param { const char *file_path }
/* Bison provides more verbose and specific error messages */
%error-verbose
/* Track locations */
%locations
/* Semantic value data types */
%union {
ASTNode *node;
char *string;
}
/* Free discarded tokens */
%destructor { free($$); } TKN_INTEGER TKN_STRING TKN_BOOLEAN TKN_REGEX
TKN_NAME TKN_UNQUOTED_STRING TKN_SHELL_COMMAND
%destructor { cp_free_ast($$); } value value_list identifier expression
statememt statememt_list statement_block
/* Terminal symbols */
%token<string> TKN_INTEGER "integer"
%token<string> TKN_STRING "string"
%token<string> TKN_BOOLEAN "boolean"
%token<string> TKN_REGEX "regex"
%token<string> TKN_NAME "identifier"
%token<string> TKN_SHELL_COMMAND "shell command"
%token<string> TKN_UNQUOTED_STRING "unquoted string"
%token TKN_ASSIGN "="
%token TKN_SEMI_COLON ";"
%token TKN_NEW_LINE "new line"
%token TKN_LEFT_BRACKET "{"
%token TKN_RIGHT_BRACKET "}"
/* All nonterminal symbols have type ASTNode * */
%type<node> value
%type<node> value_list
%type<node> identifier
%type<node> expression
%type<node> statememt
%type<node> statememt_list
%type<node> statement_block
/* Explicitly state start symbol */
%start program
%%
program:
| statememt_list {
cp_eval_ast(sess, config_level, $1); cp_free_ast($1);
}
;
statememt_list: statememt { $$ = $1; }
| statememt_list statememt {
if ($1 == NULL) {
$$ = $2;
} else {
cp_add_statement_to_list($1, $2);
$$ = $1;
}
}
;
statement_terminator: TKN_SEMI_COLON
| TKN_NEW_LINE
;
statememt: expression statement_terminator {
$$ = (ASTNode *)cp_new_statementnode(&@$, $1);
}
| statement_block { $$ = (ASTNode *)cp_new_statementnode(&@$, $1); }
| statement_terminator { $$ = NULL; }
| error statement_terminator { $$ = NULL; }
;
statement_block: TKN_NAME TKN_LEFT_BRACKET statememt_list TKN_RIGHT_BRACKET {
$$ = (ASTNode *)cp_new_statementblocknode(&@$, $1, $3);
free($1);
}
| error TKN_RIGHT_BRACKET { $$ = NULL; }
;
expression: identifier TKN_ASSIGN value {
$$ = (ASTNode *)cp_new_expressionnode(&@$, NT_ASSIGNMENT,
$1, $3);
}
| identifier value_list {
$$ = (ASTNode *)cp_new_expressionnode(&@$, NT_FUNCTION_CALL,
$1, $2);
}
| identifier {
ASTNode *vl_node = (ASTNode *)cp_new_valuelistnode(&@1, NULL);
$$ = (ASTNode *)cp_new_expressionnode(&@$, NT_FUNCTION_CALL,
$1, vl_node);
}
;
identifier: TKN_NAME {
$$ = (ASTNode *)cp_new_identifiernode(&@1, $1); free($1);
}
;
value_list: value { $$ = (ASTNode *)cp_new_valuelistnode(&@1, $1); }
| value_list value {
if ($1 == NULL) {
$$ = (ASTNode *)cp_new_valuelistnode(&@1, $2);
} else {
cp_add_value_to_list($1, $2);
$$ = $1;
}
}
;
value: TKN_INTEGER {
Value value; cp_convert_to_int_value($1, &value);
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_STRING {
Value value; cp_convert_to_string_value($1, &value);
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_BOOLEAN {
Value value; cp_convert_to_bool_value($1, &value);
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_REGEX {
Value value; cp_convert_to_regex_value($1, &value);
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_SHELL_COMMAND {
Value value; cp_convert_to_shell_command_value($1, &value);
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_UNQUOTED_STRING {
Value value = STR_VAL(strdup($1));
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
| TKN_NAME {
Value value = STR_VAL(strdup($1));
$$ = (ASTNode *)cp_new_valuenode(&@1, value); free($1);
}
;
%%