-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeGenerator.cpp
277 lines (248 loc) · 8.89 KB
/
CodeGenerator.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "CodeGenerator.h"
#include <utility>
namespace CocoAST {
CodeGenerator::CodeGenerator(std::wostream& output_stream,
coco_to_antlr::Buffer* scanner_buffer) :
CocoAstVisitor(),
output(output_stream),
scanner_buffer(scanner_buffer)
{
assert(scanner_buffer);
}
void CodeGenerator::visit(Production &ast) {
output << ast.antlr_name;
current_scope_attr_regex = std::wregex();
if(ast.attr_decl) {
ast.attr_decl->visit(*this);
build_scope_attr_regex(*ast.attr_decl);
}
output << ":\n";
indent(+1);
if(ast.sem_text) {
output << indent();
ast.sem_text->visit(*this);
output << "\n";
}
ast.expression.visit(*this);
output << "\n" << indent(-1) << ";\n";
output << "\n";
current_scope_attr_regex = std::wregex();
}
void CodeGenerator::visit(AttrDecl &ast) {
if(! convert_instrumentation)
return;
if(ast.attributes.empty())
return;
for(bool check_output : {false, true}) {
bool first = true;
for (auto &attr : ast.attributes) {
auto& type = std::get<0>(attr);
auto& name = std::get<1>(attr);
auto& is_output_parameter = std::get<bool>(attr);
if (check_output == is_output_parameter) {
if (first) {
first = false;
if(check_output)
output << " returns";
output << "[";
} else {
output << ", ";
}
output << type << " " << name;
}
}
if(!first)
output << "]";
}
}
void CodeGenerator::visit(Expression &ast) {
output << indent();
bool first = true;
for(auto& term : ast.terms) {
if(first) {
first = false;
} else {
output << "\n" << indent(-1) << "|\t";
indent(+1);
}
term.visit(*this);
}
}
void CodeGenerator::visit(Term &ast) {
if(ast.resolver)
ast.resolver->visit(*this);
bool first = true;
for(auto& factor : ast.factors) {
assert(factor);
if(first) {
first = false;
} else {
output << "\n" << indent();
}
factor->visit(*this);
}
}
void CodeGenerator::visit(Factor_Sym& ast) {
ast.sym.visit(*this);
if(! ast.attribs)
return;
if(! convert_instrumentation)
return;
auto& attribs = *ast.attribs;
if(attribs.attributes.empty())
return;
assert(ast.referenced_rule);
// first, add non-output parameters in []
bool first = true;
size_t i = 0;
for (auto &attr : attribs.attributes) {
auto& expr = std::get<0>(attr);
auto& is_output_parameter = std::get<bool>(attr);
if (! is_output_parameter) {
if (first) {
first = false;
output << "[";
} else {
output << ", ";
}
output << expr;
}
++i;
}
if(!first)
output << "]";
// second, assign output parameters, which are fields of the returned context in ANTLR
first = true;
bool second = false; // false iff we output exactly one parameter, so we can put } on the same line
i = 0;
for (auto &attr : attribs.attributes) {
auto& expr = std::get<0>(attr);
auto& is_output_parameter = std::get<1>(attr);
if (is_output_parameter) {
if(first) {
first = false;
output << "\n" << indent() << "{ ";
indent(+1);
} else {
second = true;
output << "\n" << indent();
}
auto& attr_decl = ast.referenced_rule->attr_decl->attributes.at(i);
auto& decl_name = std::get<1>(attr_decl);
output << replace_current_scope_attr(expr)
<< " = $" << ast.sym.antlr_name << "." << decl_name << ";";
}
++i;
}
if(!first) {
indent(-1);
if(second)
output << "\n" << indent();
else
output << " ";
output << "}";
}
}
void CodeGenerator::visit(Factor_Braced &ast) {
output << "(\n";
indent(+1);
ast.expression.visit(*this);
output << '\n' << indent(-1) << ')';
}
void CodeGenerator::visit(Factor_Optional &ast) {
output << "(\n"; indent(+1);
ast.expression.visit(*this);
output << '\n' << indent(-1) << ")?";
}
void CodeGenerator::visit(Factor_Iterate &ast) {
output << "(\n"; indent(+1);
ast.expression.visit(*this);
output << '\n' << indent(-1) << ")*";
}
void CodeGenerator::visit(Factor_ANY &ast) {
output << '.';
output << " /*" C2A_TODO "implement Coco ANY in ANTLR lexer, not possible in parser */\n";
}
void CodeGenerator::visit(Resolver &ast) {
if(! convert_instrumentation)
return;
output << "/* " C2A_TODO "IF() conflict resolver from Coco, check ANTLR code:\n";
copy_verbatim(output, ast.pos_start, ast.pos_end, copy_mode::replace);
output << "\n*/\n" << indent();
}
void CodeGenerator::visit(Sym &ast) {
if (ast.literal) {
output << "'" << ast.name << "'";
} else {
output << ast.antlr_name;
}
}
void CodeGenerator::visit(SemText &ast) {
if(! convert_instrumentation)
return;
output << "{ ";
std::wostringstream stream;
copy_verbatim(stream, ast.pos_start, ast.pos_end, copy_mode::replace);
output << replace_current_scope_attr(stream.str());
output << " }";
}
std::wstring CodeGenerator::indent(int increment) {
indent_level += increment;
assert(indent_level >= 0);
indent_level = std::max(0, indent_level);
return std::wstring(indent_level, '\t');
}
void CodeGenerator::copy_verbatim(std::wostream& output, int pos_start, int pos_end, copy_mode mode) {
const wchar_t* attr = scanner_buffer->GetString(pos_start, pos_end);
switch(mode) {
case copy_mode::copy:
output << attr;
break;
case copy_mode::warn:
if (std::regex_search(attr, token_object_regex))
output << L"/*" C2A_TODO "references to current Coco Token in this code block, adjust for ANTLR: */\n";
output << attr;
break;
case copy_mode::replace: {
std::wcmatch match;
// cannot use regex_iterator because it doesn't give us access to rest behind last match (here: attr after loop)
// cannot use regex_token_iterator because it doesn't tell us index of matched subgroup
while (std::regex_search(attr, match, token_object_regex)) {
output << match.prefix();
for (size_t i = 2; i < match.size(); ++i) { // all relevant groups
auto sub_match = match[i];
if (sub_match.matched) {
output << token_object_replace_map.at(i);
attr = sub_match.second; // rest of the input
break;
}
}
}
output << attr;
break;
}
}
}
std::wstring CodeGenerator::replace_current_scope_attr(const wstring &expr) {
return std::regex_replace(expr.c_str(),
current_scope_attr_regex,
// $` means characters before the match
L"$$$&" // $& means the matched characters
); // $' means characters following the match);
}
void CodeGenerator::build_scope_attr_regex(const AttrDecl &attr_decl) {
std::wostringstream builder(L"\\b(", std::ios::ate);
bool first = true;
for(const auto& attr : attr_decl.attributes) {
auto& name = std::get<1>(attr);
if(first) {
first = false;
} else {
builder << L"|";
}
builder << name;
}
builder << L")\\b";
current_scope_attr_regex = builder.str();
}
}