-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutput.h
228 lines (195 loc) · 6.16 KB
/
Output.h
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
#pragma once
#include "ActionMap.h" // C_ActionMap
#include "ParserGenBase.h" // C_ParserInfo
//---------------------------------------------------------------------------
#include "bux/XException.h" // RUNTIME_ERROR()
#include <optional> // std::optional<>
//
// Major.Minor.Release
//
constinit const int VERSION_MAJOR = 1;
constinit const int VERSION_MINOR = 7;
constinit const int VERSION_RELEASE = 5;
enum
{
//
// Commmand-Line Flags
//
ALWAYS_OVERWRITE = 1<<0,
WITH_BOM = 1<<1,
};
namespace bux { // reusability to be assessed
template<class T>
class C_PrimeBelts
{
public:
// Types
typedef std::vector<T> C_Belt;
// Nonvirtuals
C_PrimeBelts(): m_wrappedUp(false) {}
void add(const C_Belt &belt);
bool empty() const { return m_primes.empty(); }
std::optional<std::pair<size_t,size_t>> find(const C_Belt &belt);
void visit(std::function<void(const C_Belt &belt, size_t ind)> apply);
private:
// Types
typedef std::map<C_Belt,size_t> C_Belt2Ind;
// Data
C_Belt2Ind m_primes;
bool m_wrappedUp;
// Nonvirtuals
void wrapup();
};
//
// Function Templates
//
template<class T>
int findSubseq(const std::vector<T> &super, const std::vector<T> &sub)
{
int ret = -1;
if (!super.empty())
{
for (auto i = super.begin(), end = super.end() - ssize(sub); i <= end; ++i)
{
bool match = true;
auto j = i;
for (auto m: sub)
if (m != *j++)
{
match = false;
break;
}
if (match)
{
ret = int(i - super.begin());
break;
}
}
}
return ret;
}
//
// Implement Class Templates
//
template<class T>
void C_PrimeBelts<T>::add(const C_Belt &belt)
{
if (m_wrappedUp)
RUNTIME_ERROR("Add after wrapup");
// Find if any existing alphabet contains the new one
bool found = false;
for (auto &j: m_primes)
if (findSubseq(j.first, belt) >= 0)
{
found = true;
break;
}
if (!found)
{
// Eliminate properly contained alphaets
for (auto j = m_primes.begin(); j != m_primes.end();)
if (findSubseq(belt, j->first) >= 0)
m_primes.erase(j++);
else
++j;
// Add new prime
m_primes[belt];
}
}
template<class T>
std::optional<std::pair<size_t,size_t>> C_PrimeBelts<T>::find(const C_Belt &belt)
{
wrapup();
for (auto &i: m_primes)
{
auto ind = findSubseq(i.first, belt);
if (ind >= 0)
return {{i.second, ind}};
}
return {};
}
template<class T>
void C_PrimeBelts<T>::visit(std::function<void(const C_Belt &belt, size_t ind)> apply)
{
wrapup();
for (const auto &i: m_primes)
apply(i.first, i.second);
}
template<class T>
void C_PrimeBelts<T>::wrapup()
{
if (!m_wrappedUp && !m_primes.empty())
{
size_t count = 0;
for (auto &i: m_primes)
i.second = count++;
m_wrappedUp = true;
}
}
} // namespace bux
struct C_GotoMap;
class FC_Output
{
public:
// Nonvirtuals
FC_Output(const C_ParserInfo &parsed, const C_GotoMap &stateMap,
const C_ActionMap &actionMap, const std::string &grammarPath);
bool operator()(const char *outputPath, const char *tokenPath, int flags) const;
private:
// Types
typedef std::pair<std::string,std::string> C_StrPair;
struct C_IATableOutput
{
std::map<bux::T_LexID,C_Strings> m_outMapping;
int m_iaTableInd; // >=0 as table index; < 0 to compress
};
typedef std::map<C_Lex2Action,C_IATableOutput> C_ActionTblRendering;
typedef std::map<std::string,size_t> C_StringToId;
typedef std::map<const C_Production*,size_t> C_ProductionSerials;
typedef bux::C_PrimeBelts<bux::T_LexID> C_AlphaPrimes;
typedef C_AlphaPrimes::C_Belt C_Alphabet;
typedef std::vector<std::pair<bux::T_LexID,size_t>> C_Lex2State;
struct C_ISTableOutput
{
int m_tableInd; // >=0 as table index; < 0 to compress
};
typedef std::map<C_Lex2State,C_ISTableOutput> C_StateTblRendering;
class C_RenderReduction;
// Data
const C_ParserInfo &m_Parsed;
const std::string m_ContextType;
const std::string m_Banner;
// Deduced from actionMap
std::map<bux::T_LexID,std::string> m_lex2str;
C_ActionTblRendering m_iatbl2out;
C_StateTblRendering m_istbl2out;
std::vector<C_StrPair> m_state2iatbl;
std::map<size_t,C_StrPair> m_state2next;
mutable C_AlphaPrimes m_alphaPrimes;
C_StringMap m_findPrimes;
std::string m_InputType;
std::string m_StateType;
std::string m_iaTblSzType, m_isTblSzType;
std::string m_ActionType;
std::string m_PoplenType;
bux::T_LexID m_MinTokenId;
// Deduced from m_Parsed.productions()
C_ProductionSerials m_ProdNums;
// Deduced from m_Parsed.templateArgs()
#ifdef __TEMPLATE_OUTPUT
std::string m_TemplateParams;
std::string m_ClassSuffix;
#endif // __TEMPLATE_OUTPUT
// Flags
bool m_readySoFar{true};
bool m_needGLR{};
mutable bool m_isKeyCalled{};
// Nonvirtuals
void addParserMap(C_RenderReduction &rr, const std::string &className, size_t argInd) const;
bool hasContext() const { return !m_ContextType.empty(); }
std::string outputFindKey(const C_Alphabet &alphabet);
std::string outputIsKey(bux::T_LexID key) const;
void outputTokens(std::ostream &out, const std::string &headerBase) const;
size_t serial(const C_Production*) const;
void writeUserSection(std::ostream &out, const char *optionKey) const;
};