Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaderOutput.cpp
More file actions
293 lines (272 loc) · 8.76 KB
/
Copy pathHeaderOutput.cpp
File metadata and controls
293 lines (272 loc) · 8.76 KB
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
#include "ParserOutputEgg.hpp"
#include "SymbolTable.hpp"
#define ALIASING
#include <sstream>
using namespace std;
extern std::unordered_map<std::string,void*> characterLiterals;
extern std::unordered_map<std::string,Token_Rule*> terminalTokens;
extern std::unordered_map<std::string,Symbol_Rule_List*> listRules;
extern std::unordered_map<std::string,Symbol_Rule_STRING*> stringTokens;
extern std::unordered_map<std::string,Token_Rule2*> stringAliases;
void outputListTypeDeclarations(ofstream* file)
{
auto iter = listRules.begin();
while (iter != listRules.end())
{
(*file) << "class " << iter->first << "_List;" << endl;
iter++;
}
}
void outputListTypes(ofstream* file)
{
auto iter = listRules.begin();
while (iter != listRules.end())
{
(*file) << "class " << iter->first << "_List: public ListRuleBaseClass" << endl;
(*file) << '{' << endl;
(*file) << "public:" << endl;
(*file) << '\t' << iter->first << "_List();" << endl;
(*file) << "\tRuleType type(){return e" << iter->first << "_List;}" << endl;
(*file) << "\tvoid add(" << iter->first << "_Type*);" << endl;
(*file) << "\tunsigned int size(){return list.size();}" << endl;
(*file) << '\t' << iter->first << "_Type* operator[](unsigned int index){return list[index];}" << endl;
(*file) << "private:" << endl;
(*file) << "\tstd::vector<" << iter->first << "_Type*> list;" << endl;
(*file) << "};" << endl;
iter++;
}
}
void outputFile(ofstream* outfile, const char* infileName);
void ParserOutput::outputHeader()
{
ofstream file(outputfile + ".h");
file << "#pragma once" << endl;
file << "/// Enums" << endl;
file << "enum RuleType" << endl;
file << '{' << endl;
rules->outputEnumDeclarations(&file);
auto iter = terminalTokens.begin();
while (iter != terminalTokens.end())
{
file << "\te" << iter->first << ',' << endl;
iter++;
}
auto stringIter = stringTokens.begin();
while (stringIter != stringTokens.end())
{
file << "\te" << stringIter->first << "_String," << endl;
stringIter++;
}
auto listIter = listRules.begin();
while (listIter != listRules.end())
{
file << "\te" << listIter->first << "_List," << endl;
listIter++;
}
file << "\teCharacterLiteralsBegin," << endl;
file << "\teTypesMax = eCharacterLiteralsBegin + 256" << endl;
file << "};" << endl;
{
std::string fullFilename(resourceDirectory);
fullFilename = fullFilename + "astres/HeaderPreamble.txt";
outputFile(&file,fullFilename.c_str());
}
file << "/// Forward declarations of all types" << endl;
rules->outputTypeDeclarations(&file);
file << "/// List rule types" << endl;
outputListTypeDeclarations(&file);
file << "/// Terminal token aliases" << endl;
iter = terminalTokens.begin();
while (iter != terminalTokens.end())
{
file << "class " << iter->first << "_Type;" << endl;
iter++;
}
stringIter = stringTokens.begin();
while (stringIter != stringTokens.end())
{
file << "class " << stringIter->first << "_String_Type;" << endl;
stringIter++;
}
file << "/// Character literal" << endl;
file << "class CharacterLiteral_Type;" << endl;
/// Type definitions
file << "/// Actual declarations" << endl;
file << "/// User defined rules" << endl;
rules->outputTypes(&file);
file << "/// List rules types" << endl;
outputListTypes(&file);
file << "/// Terminal tokens types" << endl;
iter = terminalTokens.begin();
while (iter != terminalTokens.end())
{
file << "class " << iter->first << "_Type: public TerminalTokenBaseClass" << endl;
file << '{' << endl;
file << "public:" << endl;
file << '\t' << iter->first << "_Type(char* _matchedText, char* _preceedingWhitespace);" << endl;
file << "\tRuleType type(){return e" << iter->first << ";}" << endl;
file << "};" << endl;
iter++;
}
stringIter = stringTokens.begin();
while (stringIter != stringTokens.end())
{
file << "class " << stringIter->first << "_String_Type: public TerminalTokenBaseClass" << endl;
file << '{' << endl;
file << "public:" << endl;
file << '\t' << stringIter->first << "_String_Type(char* _matchedText, char* _preceedingWhitespace);" << endl;
file << "\tRuleType type(){return e" << stringIter->first << "_String;}" << endl;
file << "};" << endl;
stringIter++;
}
file << "Script_Type* parse(const char* filename, bool outputMatchedTokens = false);" << endl;
}
void BisonRules_Rule::outputEnumDeclarations(ofstream* file)
{
for (unsigned int i = 0; i < rules.size(); i++)
{
rules[i]->outputEnumDeclaration(file);
}
}
void BisonRule_Rule::outputEnumDeclaration(ofstream* file)
{
(*file) << "\te" << name << ',' << endl;
}
void BisonRules_Rule::outputTypeDeclarations(ofstream* file)
{
for (unsigned int i = 0; i < rules.size(); i++)
{
rules[i]->outputTypeDeclaration(file);
}
}
void BisonRule_Rule::outputTypeDeclaration(ofstream* file)
{
(*file) << "class " << name << "_Type;" << endl;
if (rules->size() != 1)
{
for (unsigned int i = 0; i < rules->size(); i++)
{
(*file) << "class " << name << "_Type" << i+1 << ';' << endl;
}
}
}
void BisonRules_Rule::outputTypes(ofstream* file)
{
for (unsigned int i = 0; i < rules.size(); i++)
{
rules[i]->outputType(file);
}
}
void BisonRule_Rule::outputType(ofstream* file)
{
rules->outputType(file,name);
}
void DerivationRules_Rule::outputType(ofstream* file, char* type)
{
stringstream derivationName;
derivationName << type << "_Type";
std::string typeName = derivationName.str();
(*file) << "class " << typeName << ": public NonTerminalBaseClass" << endl;
(*file) << '{' << endl;
(*file) << "public:" << endl;
(*file) << '\t' << typeName << '(';
if (rules.size() == 1)
rules[0]->outputConstructorArguments(file);
else
(*file) << "const unsigned short _numargs, const unsigned short _derivationType";
(*file) << ");" << endl;
(*file) << "\tvirtual ~" << typeName << "();" << endl;
if (rules.size() == 1)
rules[0]->outputGetDeclarations(file);
else
(*file) << "\tunsigned short getType(){return derivationType;}" << endl;
(*file) << "\tRuleType type(){return e" << type << ";}" << endl;
(*file) << "private:" << endl;
if (rules.size() != 1)
(*file) << "\tunsigned short derivationType;" << endl;
(*file) << "};" << endl;
if (rules.size() != 1)
{
for (unsigned int i = 0; i < rules.size(); i++)
{
(*file) << "class " << typeName << i+1 << ": public " << typeName << endl;
(*file) << '{' << endl;
(*file) << "public:" << endl;
(*file) << "\t" << typeName << i+1 << '(';
rules[i]->outputConstructorArguments(file);
(*file) << ");" << endl;
(*file) << "\tvirtual ~" << typeName << i+1 << "();" << endl;
rules[i]->outputGetDeclarations(file);
(*file) << "private:" << endl;
(*file) << "};" << endl;
}
}
}
void Symbols_Rule::outputConstructorArguments(ofstream* file)
{
for (unsigned int i = 0; i < symbols.size(); i++)
{
symbols[i]->outputType(file);
(*file) << "*_arg" << i+1;
if (i < symbols.size()-1)
{
(*file) << ", ";
}
}
}
#include <cassert>
void Symbols_Rule::outputMemberDeclarations(ofstream* file)
{
assert(false); /// FIXME just remove all these functions
for (unsigned int i = 0; i < symbols.size(); i++)
{
(*file) << '\t';
symbols[i]->outputType(file);
(*file) << "*arg" << i+1 << ';' << endl;
}
}
void Symbols_Rule::outputGetDeclarations(ofstream* file)
{
for (unsigned int i = 0; i < symbols.size(); i++)
{
(*file) << '\t';
symbols[i]->outputType(file);
(*file) << "* getArg" << i+1 << "(){return (";
symbols[i]->outputType(file);
(*file) << "*)args[" << i << "];}" << endl;
}
}
void Symbol_Rule_ID::outputType(ofstream* file)
{
if (terminalTokens.find(name) == terminalTokens.end())
(*file) << name << "_Type ";
else
(*file) << name << "_Type ";
}
void Symbol_Rule_CHARACTER::outputType(ofstream* file)
{
/*
#ifndef ALIASING
(*file) << '"' << character << '"' << ' ';
#else
int literalInt = character;
(*file) << "LitChar" << literalInt << ' ';
#endif*/
(*file) << "CharacterLiteral_Type ";
}
void Symbol_Rule_STRING::outputType(ofstream* file)
{
(*file) << string << "_String_Type ";
// Token_Rule2* token = stringAliases.find(string)->second;
// (*file) << token->getId() << "_Type" << ' ';
}
void Symbol_Rule_List::outputType(ofstream* file)
{
(*file) << name << "_List ";
}
#include <cassert>
void Token_Rule::outputConstructorArguments(ofstream* file)
{
assert(false);
/// Will always be (char* matchedText, char* preceedingWhitespace)
}
You can’t perform that action at this time.
