1 #include "traversalcppgenvisitor.h"
2 #include "visitor.h"
3 #include "ast.h"
4 #include "strutils.h"
5
6 #include <assert.h>
7
8 using std::vector;
9 using std::string;
10
11
traversalcppgenvisitor(const char * filename,const char * traversalhppFilename,const char * astFilename)12 traversalcppgenvisitor::traversalcppgenvisitor(const char* filename, const char* traversalhppFilename, const char* astFilename)
13 : m_out(filename)
14 {
15 m_out << "#include \"" << traversalhppFilename << "\"\n";
16 m_out << "#include \"" << astFilename << "\"\n\n";
17 }
18
~traversalcppgenvisitor()19 traversalcppgenvisitor::~traversalcppgenvisitor()
20 {
21 }
22
visit_lhs_IDENT_SEPARATOR(const lhs_IDENT_SEPARATOR * plhs_IDENT_SEPARATOR)23 void traversalcppgenvisitor::visit_lhs_IDENT_SEPARATOR(const lhs_IDENT_SEPARATOR *plhs_IDENT_SEPARATOR)
24 {
25 m_rulename = *plhs_IDENT_SEPARATOR->m_IDENT;
26 }
27
visit_grammar_grammar_production(const grammar_grammar_production * pgrammar_grammar_production)28 void traversalcppgenvisitor::visit_grammar_grammar_production(const grammar_grammar_production *pgrammar_grammar_production)
29 {
30 if (pgrammar_grammar_production->m_grammar.get() != 0)
31 {
32 pgrammar_grammar_production->m_grammar->accept(this);
33 }
34 pgrammar_grammar_production->m_production->accept(this);
35
36 }
37
visit_grammar_grammar_COMMENT(const grammar_grammar_COMMENT * pgrammar_grammar_COMMENT)38 void traversalcppgenvisitor::visit_grammar_grammar_COMMENT(const grammar_grammar_COMMENT *pgrammar_grammar_COMMENT)
39 {
40 return;
41 }
42
visit_expression_base_OPT(const expression_base_OPT * pexpression_base_OPT)43 void traversalcppgenvisitor::visit_expression_base_OPT(const expression_base_OPT *pexpression_base_OPT)
44 {
45 // not yet implemented
46 assert(0);
47 pexpression_base_OPT->m_base->accept(this);
48 }
49
visit_production_lhs_expressionListList_TERMINATOR(const production_lhs_expressionListList_TERMINATOR * pproduction_lhs_expressionListList_TERMINATOR)50 void traversalcppgenvisitor::visit_production_lhs_expressionListList_TERMINATOR(const production_lhs_expressionListList_TERMINATOR *pproduction_lhs_expressionListList_TERMINATOR)
51 {
52 pproduction_lhs_expressionListList_TERMINATOR->m_lhs->accept(this);
53
54 // m_rulename has already been set
55 if (!isSpecialRule(m_rulename))
56 {
57 if (pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList.get() != 0)
58 {
59 if (pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->size() > 1)
60 {
61 for (vector<vector<expression*>*>::const_iterator i = pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->begin();
62 i != pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->end();
63 ++i)
64 {
65 if (*i)
66 {
67 write_traversal(i);
68 }
69 }
70 }
71 else
72 {
73 vector<vector<expression*>*>::const_iterator i = pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->begin();
74 write_traversal_simple(i);
75 /*
76 string classname = m_rulename;
77 m_out << "void traversalvisitor::visit_" << classname << "(\n";
78 m_out << "\tconst " << classname << "* p" << classname << "\n";
79 m_out << "\t)\n";
80 m_out << "{\n";
81 m_out << "}\n\n";
82 */
83 }
84 }
85 }
86 if (endsWithList(m_rulename))
87 {
88 if (pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList.get() != 0)
89 {
90 if (pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->size() > 1)
91 {
92 vector<vector<expression*>*>::const_iterator i = pproduction_lhs_expressionListList_TERMINATOR->m_expressionListList->begin();
93 if ((*i)->size() > 0 && *(*i)->begin())
94 {
95 (*(*i)->begin())->accept(this); // fills out m_ident
96 if (beginsWithStr(m_ident))
97 addListType(m_rulename, "std::string");
98 else
99 addListType(m_rulename, m_ident);
100 }
101 }
102 }
103 }
104
105 }
106
write_traversal(vector<vector<expression * > * >::const_iterator rule)107 void traversalcppgenvisitor::write_traversal(vector<vector<expression*>*>::const_iterator rule)
108 {
109 // first build up the classname and gather the idents
110 string classname = m_rulename;
111 vector<string> idents;
112 for (vector<expression*>::const_iterator j = (*rule)->begin();
113 j != (*rule)->end();
114 ++j)
115 {
116 m_literal.erase();
117 m_ident.erase();
118 (*j)->accept(this);
119 if (m_ident.length() > 0)
120 {
121 classname += "_" + m_ident;
122 idents.push_back(m_ident);
123 }
124 }
125 if ((*rule)->size() == 0)
126 {
127 classname += "_empty";
128 }
129
130 write_traversal_common(classname, idents);
131
132 }
133
write_traversal_simple(vector<vector<expression * > * >::const_iterator rule)134 void traversalcppgenvisitor::write_traversal_simple(vector<vector<expression*>*>::const_iterator rule)
135 {
136 // first build up the classname and gather the idents
137 string classname = m_rulename;
138 vector<string> idents;
139 for (vector<expression*>::const_iterator j = (*rule)->begin();
140 j != (*rule)->end();
141 ++j)
142 {
143 m_literal.erase();
144 m_ident.erase();
145 (*j)->accept(this);
146 if (m_ident.length() > 0)
147 {
148 idents.push_back(m_ident);
149 }
150 }
151
152 write_traversal_common(classname, idents);
153 }
154
write_traversal_common(const std::string & classname,const std::vector<std::string> & idents)155 void traversalcppgenvisitor::write_traversal_common(const std::string& classname, const std::vector<std::string>& idents)
156 {
157 m_out << "void Processor::visit_" << classname << "(\n";
158 m_out << "\tconst " << classname << "* p" << classname << "\n";
159 m_out << "\t)\n";
160 m_out << "{\n";
161 bool unusedParameter = true;
162 for (vector<string>::const_iterator i = idents.begin();
163 i != idents.end();
164 ++i)
165 {
166 if (beginsWithStr(*i))
167 {
168 }
169 else if (endsWithList(*i))
170 {
171 unusedParameter = false;
172 if (beginsWithOpt(*i))
173 {
174 m_out << "\tif (p" << classname << "->m_p" << *i <<
175 i - idents.begin() + 1 << ")\n";
176 m_out << "\t{\n";
177 m_out << "\t\tfor (std::list<" << getListType(*i)
178 << "*>::const_iterator i = p" << classname
179 << "->m_p" << *i << i - idents.begin() + 1 << "->begin();\n";
180 m_out << "\t\t\ti != p" << classname << "->m_p" <<
181 *i << i - idents.begin() + 1 << "->end();\n";
182 m_out << "\t\t\t++i )\n";
183
184 if (beginsWithOpt(getListType(*i)))
185 {
186 m_out << "\t\t{\n";
187 m_out << "\t\t\tif (*i)\n";
188 m_out << "\t\t\t\t(*i)->accept(this);\n";
189 m_out << "\t\t}\n";
190 }
191 else
192 {
193 m_out << "\t\t\t(*i)->accept(this);\n";
194 }
195 m_out << "\t}\n";
196 }
197 else
198 {
199 m_out << "\tfor (std::list<" << getListType(*i)
200 << "*>::const_iterator i = p" << classname
201 << "->m_p" << *i << i - idents.begin() + 1 << "->begin();\n";
202
203 bool isString = getListType(*i) == "std::string";
204
205 m_out << "\t\ti != p" << classname << "->m_p"
206 << *i << i - idents.begin() + 1 << "->end();\n";
207 m_out << "\t\t++i )\n";
208
209 m_out << "\t{\n";
210 if (beginsWithOpt(getListType(*i)))
211 {
212 m_out << "\t\tif (*i)\n";
213 m_out << "\t\t{\n";
214 if (!isString)
215 {
216 m_out << "\t\t\t(*i)->accept(this);\n";
217 }
218 m_out << "\t\t}\n";
219 }
220 else
221 {
222 if (!isString)
223 {
224 m_out << "\t\t(*i)->accept(this);\n";
225 }
226 }
227 m_out << "\t}\n";
228
229 }
230 }
231 else if (isAllCaps(*i))
232 {
233 }
234 else
235 {
236 unusedParameter = false;
237 if (beginsWithOpt(*i))
238 {
239 m_out << "\tif (p" << classname << "->m_p" << *i <<
240 i - idents.begin() + 1 << ")\n";
241
242 m_out << "\t\tp" << classname << "->m_p" << *i <<
243 i - idents.begin() + 1 << "->accept(this);\n";
244 }
245 else
246 {
247 m_out << "\tp" << classname << "->m_p" << *i <<
248 i - idents.begin() + 1 << "->accept(this);\n";
249 }
250 }
251
252 }
253 if (unusedParameter)
254 {
255 m_out << "\t(void)p" << classname << ";\n";
256 }
257
258 m_out << "}\n\n";
259
260 }
261
visit_expression_base_PLUS(const expression_base_PLUS * pexpression_base_PLUS)262 void traversalcppgenvisitor::visit_expression_base_PLUS(const expression_base_PLUS *pexpression_base_PLUS)
263 {
264 // not yet implemented
265 assert(0);
266 pexpression_base_PLUS->m_base->accept(this);
267 }
268
visit_expression_base(const expression_base * pexpression_base)269 void traversalcppgenvisitor::visit_expression_base(const expression_base *pexpression_base)
270 {
271 pexpression_base->m_base->accept(this);
272 }
273
visit_base_LITERAL(const base_LITERAL * pbase_LITERAL)274 void traversalcppgenvisitor::visit_base_LITERAL(const base_LITERAL *pbase_LITERAL)
275 {
276 m_literal = *pbase_LITERAL->m_LITERAL;
277 }
278
visit_expression_base_STAR(const expression_base_STAR * pexpression_base_STAR)279 void traversalcppgenvisitor::visit_expression_base_STAR(const expression_base_STAR *pexpression_base_STAR)
280 {
281 // not yet implemented
282 assert(0);
283 pexpression_base_STAR->m_base->accept(this);
284 }
285
visit_base_LPAREN_expressionList_RPAREN(const base_LPAREN_expressionList_RPAREN * pbase_LPAREN_expressionList_RPAREN)286 void traversalcppgenvisitor::visit_base_LPAREN_expressionList_RPAREN(const base_LPAREN_expressionList_RPAREN *pbase_LPAREN_expressionList_RPAREN)
287 {
288 // not yet implemented
289 assert(0);
290 if (pbase_LPAREN_expressionList_RPAREN->m_expressionList.get() != 0)
291 {
292 for (vector<expression*>::const_iterator i = pbase_LPAREN_expressionList_RPAREN->m_expressionList->begin();
293 i != pbase_LPAREN_expressionList_RPAREN->m_expressionList->end();
294 ++i)
295 {
296 (*i)->accept(this);
297 }
298 }
299 }
300
visit_expression_COMMENT(const expression_COMMENT * pexpression_COMMENT)301 void traversalcppgenvisitor::visit_expression_COMMENT(const expression_COMMENT *pexpression_COMMENT)
302 {
303 // not yet implemented
304 assert(0);
305 (void)pexpression_COMMENT->m_COMMENT;
306 }
307
visit_alternation_expression_OR_expression(const alternation_expression_OR_expression * palternation_expression_OR_expression)308 void traversalcppgenvisitor::visit_alternation_expression_OR_expression(const alternation_expression_OR_expression *palternation_expression_OR_expression)
309 {
310 // not yet implemented
311 assert(0);
312 palternation_expression_OR_expression->m_expression1->accept(this);
313 palternation_expression_OR_expression->m_expression2->accept(this);
314 }
315
visit_base_IDENT(const base_IDENT * pbase_IDENT)316 void traversalcppgenvisitor::visit_base_IDENT(const base_IDENT *pbase_IDENT)
317 {
318 m_ident = *pbase_IDENT->m_IDENT;
319 }
320
visit_base_LPAREN_alternation_RPAREN(const base_LPAREN_alternation_RPAREN * pbase_LPAREN_alternation_RPAREN)321 void traversalcppgenvisitor::visit_base_LPAREN_alternation_RPAREN(const base_LPAREN_alternation_RPAREN *pbase_LPAREN_alternation_RPAREN)
322 {
323 // not yet implemented
324 assert(0);
325 pbase_LPAREN_alternation_RPAREN->m_alternation->accept(this);
326 }
327
visit_alternation_alternation_OR_expression(const alternation_alternation_OR_expression * palternation_alternation_OR_expression)328 void traversalcppgenvisitor::visit_alternation_alternation_OR_expression(const alternation_alternation_OR_expression *palternation_alternation_OR_expression)
329 {
330 // not yet implemented
331 assert(0);
332 palternation_alternation_OR_expression->m_alternation->accept(this);
333 palternation_alternation_OR_expression->m_expression->accept(this);
334 }
335
336
337