-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathConfig.py
199 lines (142 loc) · 7.78 KB
/
Config.py
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
def standard_header(a): return "<%s>" % a
def normal_header(a):
return "\"%s\"" % a
def include_all(include_list):
res = ""
for i in include_list:
res += "#include %s\n" %i
return res
class Config:
def __init__(self):
self.output_path = ""
self.include_path = "include/"
self.src_path = "src/"
self.template_path = "template/"
self.parser_path = "parser/"
self.flex_headerfile = "%sflex_lexer.h" % self.output_path
self.flex_cpp_file = "%sflex_lexer.cpp" % self.output_path
self.api_prefix = "ff_"
self.token_prefix = "SQL_"
self.bison_output_path = "%sbison.y" % self.parser_path
self.flex_output_path = "%sflex.l" % self.parser_path
self.bison_cpp_file = "%sbison_parser.cpp" % self.output_path
self.bison_headerfile = "%sbison_parser.h" % self.output_path
self.bison_parser_typedef_template_path = "%sparser_typedef_template.h" % self.template_path
self.bison_parser_typedef_output_path = "%sparser_typedef.h" % self.parser_path
# Flex
self.flex_include_files = [standard_header("stdio.h"), standard_header("sstream"), standard_header("string"), standard_header("cstring"), normal_header(self.bison_headerfile), normal_header("parser_utils.h")]
self.flex_declarations = []
self.flex_declarations.append("static thread_local std::stringstream strbuf;")
#self.flex_declarations.append("char* substr(const char* source, int from, int to);")
self.flex_option_list = ["reentrant", "bison-bridge", "never-interactive", "batch", "noyywrap", "warn", "bison-locations"]
#self.flex_option_list.append("case-insensitive")
self.flex_option_list.append("header-file=\"%s\"" % self.flex_headerfile)
self.flex_option_list.append("outfile=\"%s\"" % self.flex_cpp_file)
self.flex_option_list.append("prefix=\"%s\"" % self.api_prefix)
self.flex_extra_option = []
self.flex_extra_option.append("%s COMMENT")
self.flex_extra_option.append("%x singlequotedstring")
# Bison
self.bison_top_input_type = "Program"
#self.ast_definition_path = "ast.h"
self.bison_include_files = [standard_header("stdio.h"), standard_header("string.h"), normal_header(self.bison_headerfile), normal_header(self.flex_headerfile)]
self.bison_declarations = []
self.bison_declarations.append("int yyerror(YYLTYPE* llocp, %s * result, yyscan_t scanner, const char *msg) { return 0; }" % self.bison_top_input_type)
self.bison_code_require_include_files = [normal_header("../include/ast.h"), normal_header("parser_typedef.h")]
self.bison_code_require_defines = dict()
self.bison_code_require_defines["api.pure"] = "full"
self.bison_code_require_defines["api.prefix"] = "{%s}" % self.api_prefix
self.bison_code_require_defines["api.token.prefix"] = "{%s}" % self.token_prefix
self.bison_code_require_defines["parse.error"] = "verbose"
self.bison_code_require_extra_defines = []
self.bison_code_require_extra_defines.append('%locations')
self.bison_code_require_extra_defines.append("""
%initial-action {
// Initialize
@$.first_column = 0;
@$.last_column = 0;
@$.first_line = 0;
@$.last_line = 0;
@$.total_column = 0;
@$.string_length = 0;
};""")
self.bison_code_require_lex_params = dict()
self.bison_code_require_lex_params["scanner"] = "yyscan_t"
self.bison_code_require_parse_params = dict()
self.bison_code_require_parse_params["result"] = "%s*" % self.bison_top_input_type
self.bison_code_require_parse_params["scanner"] = "yyscan_t"
self.bison_data_type_prefix = "FF"
## header
self.define_header_template_path = "%sdefine_header_template.h" % (self.template_path)
self.define_header_output_path = "%sdefine.h" % self.include_path
self.ast_header_template_path = "%sast_header_template.h" % (self.template_path)
self.ast_src_template_path = "%sast_src_template.cpp" % (self.template_path)
self.ast_src_output_path = "%s/ast.cpp" % (self.src_path)
self.ast_header_output_path = "%s/ast.h" % (self.include_path)
self.mutate_header_template_path = "%smutate_header_template.h" % (self.template_path)
self.mutate_src_template_path = "%smutate_src_template.cpp" % (self.template_path)
self.mutate_src_output_path = "%s/mutate.cpp" % (self.src_path)
self.mutate_header_output_path = "%s/mutate.h" % (self.include_path)
## Others
self.parser_utils_header_template_path = "%sparser_utils_header_template.h" % (self.template_path)
self.parser_utils_header_output_path = "%sparser_utils.h" % (self.parser_path)
self.utils_header_template_path = "%sutils_header_template.h" % (self.template_path)
self.utils_src_template_path = "%sutils_src_template.cpp" % (self.template_path)
self.utils_src_output_path = "%s/utils.cpp" % (self.src_path)
self.utils_header_output_path = "%s/utils.h" % (self.include_path)
# Variable Definition
self.vardef_header_template_path = "%svar_definition_header_template.h" % (self.template_path)
self.vardef_src_template_path = "%svar_definition_src_template.cpp" % (self.template_path)
self.vardef_src_output_path = "%s/var_definition.cpp" % (self.src_path)
self.vardef_header_output_path = "%s/var_definition.h" % (self.include_path)
# Type system
self.ts_header_template_path = "%stypesystem_header_template.h" % (self.template_path)
self.ts_src_template_path = "%stypesystem_src_template.cpp" % (self.template_path)
self.ts_src_output_path = "%s/typesystem.cpp" % (self.src_path)
self.ts_header_output_path = "%s/typesystem.h" % (self.include_path)
self.case_num = 400
def gen_flex_definition(self):
res = ""
res += include_all(self.flex_include_files)
#res += "#define TOKEN(name) { cout <<\"get token:\" <<#name << endl; return %s##name; }\n" % self.token_prefix
res += "#define TOKEN(name) { return %s##name; }\n" % self.token_prefix
for i in self.flex_declarations:
res += i + "\n"
return "%%{\n%s%%}\n" % res
def gen_flex_options(self):
res = ""
for i in self.flex_option_list:
res += "%%option %s\n" % i
for i in self.flex_extra_option:
res += "%s\n" % i
return res
def gen_bison_definition(self):
res = ""
res += include_all(self.bison_include_files)
for i in self.bison_declarations:
res += i + "\n"
return "%%{\n%s%%}\n" % res
def gen_bison_code_require(self):
res = ""
for i in self.bison_code_require_include_files:
res += "#include %s\n" % i
res += "#define YYDEBUG 1"
return "%%code requires {\n%s}\n" % res
def gen_bison_params(self):
res = ""
for (key,value) in self.bison_code_require_defines.items():
res += "%%define %s\t%s\n" % (key,value)
for i in self.bison_code_require_extra_defines:
res += "%s\n" % i
for (key,value) in self.bison_code_require_lex_params.items():
res += "%%lex-param { %s %s }\n" % (value, key)
for (key,value) in self.bison_code_require_parse_params.items():
res += "%%parse-param { %s %s }\n" % (value, key)
return res
def gen_parser_typedef(self):
with open(self.bison_parser_typedef_template_path, "rb") as f:
contents = f.read()
contents = contents.replace("REPLACEME", self.bison_data_type_prefix)
with open(self.bison_parser_typedef_output_path, 'wb') as ff:
ff.write(contents)
configuration = Config()