-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserGenBase.cpp
398 lines (338 loc) · 9.32 KB
/
ParserGenBase.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "ParserGenBase.h"
#include "ParserIdDef.h" // namespace Main::LexId
//---------------------------------------------------------------------------
#include "bux/StrUtil.h" // HRTN()
#include "bux/XException.h" // RUNTIME_ERROR()
#include <ctype.h> // isascii(), isalnum()
#include <ostream> // std::ostream
namespace ParserGen {
using namespace Main;
//
// Functions
//
std::function<bool(const std::string &str, std::string &dst)> getGeneratedID;
bool getNonterminalID(const std::string &str, std::string &dst)
{
if (str == "@")
return false;
if (str.at(0) == '@')
dst ="NIDa_"+str.substr(1);
else
dst ="NID_"+str;
return true;
}
bool isKeyword(const std::string &str)
{
for (auto c: str)
if (!isascii(c) || (c != '_' && !isalnum(c)))
return false;
return true;
}
std::string literalSuffix(const std::string &s)
{
std::string ret;
for (auto c: s)
{
if (isascii(c) && isalnum(c))
{
ret += c;
ret += '_';
}
else
bux::addAsHex(ret, (uint8_t)c);
}
return ret;
}
std::string ensureNoConcat(std::string s)
{
auto stripPos = s.find_last_not_of(" \t\r");
if (stripPos != std::string::npos)
// Trim trailing spaces
s.erase(stripPos+1);
else
// Empty (Or only spaces)
return {};
// Special treatment to one-liner
if (s.find('\n',0) == std::string::npos)
{
stripPos = s.find_first_not_of(" \t\r");
if (stripPos != std::string::npos && stripPos)
s.replace(0, stripPos, " ");
else
s = " " + s;
s += '\n';
}
return s;
}
std::string ensureNoConcat(const C_Semantic &src)
{
return ensureNoConcat(src.expand());
}
//
// Class Implementations
//
I_SemanticChunk::~I_SemanticChunk()
{
}
C_Semantic::~C_Semantic()
{
for (auto i =begin(); i != end(); ++i)
delete *i;
}
std::string C_Semantic::expand() const
{
std::string ret;
for (auto i: *this)
if (auto blex = dynamic_cast<C_BracketedLex*>(i))
{
for (auto j: blex->m_Str)
if (j != '\r' && (j != '\n' || !ret.empty()))
ret += j;
}
else
RUNTIME_ERROR("Unknown semantic lex type ", HRTN(i));
return ret;
}
C_Production::~C_Production()
{
for (auto i =m_Rval.begin(); i != m_Rval.end(); ++i)
delete *i;
}
bool C_Production::operator==(const C_Production &a) const
{
if (m_Lval == a.m_Lval && m_Rval.size() == a.m_Rval.size())
{
for (auto i =m_Rval.begin(), j =a.m_Rval.begin(); i != m_Rval.end(); ++i, ++j)
if (**i != **j)
return false;
return true;
}
return false;
}
std::string C_Production::inputName(size_t pos, const std::string &mark) const
{
auto ret ="<"+m_Lval+"> ::=";
size_t ipos =0;
for (auto i: m_Rval)
{
if (ipos++ == pos)
ret += mark;
ret += ' ';
ret += i->displayStr();
}
if (ipos <= pos)
ret += mark;
return ret;
}
std::string C_ParserInfo::contextType() const
{
std::string s;
if (auto i = getOption("CONTEXT"))
s = i->expand();
return s;
}
void C_ParserInfo::enterNamespaces(std::ostream &out) const
{
if (!m_Namespace.empty())
{
out <<'\n';
for (auto &i: m_Namespace)
out <<"namespace " <<i <<" {\n";
}
}
std::string C_ParserInfo::fullNamespace() const
{
std::string t;
bool first =true;
for (auto &i: m_Namespace)
{
if (first)
first =false;
else
t.append("::");
t.append(i);
}
return t;
}
const C_Semantic *C_ParserInfo::getOption(const std::string &name) const
{
const C_Semantic *ret{};
auto i = m_OptMap.find(name);
if (i != m_OptMap.end())
{
i->second.m_Used = true;
ret = &i->second;
}
return ret;
}
const std::string *C_ParserInfo::getReduction(size_t index) const
{
if (index >= m_Reductions.size())
return 0;
return &m_Reductions[index];
}
std::string C_ParserInfo::getReduction(const C_IndexedProd &prod) const
{
if (prod.m_Index >= m_Reductions.size())
RUNTIME_ERROR("Production index {} > production count {}", prod.m_Index, m_Reductions.size());
return m_Reductions[prod.m_Index];
}
bool C_ParserInfo::hasLexSymbol(const std::string &id) const
{
return m_Lex2ID.contains(id);
}
void C_ParserInfo::leaveNamespaces(std::ostream &out) const
{
if (!m_Namespace.empty())
{
out <<'\n';
for (auto &i: m_Namespace)
out <<"} // namespace " <<i <<"\n";
}
}
std::string C_ParserInfo::outputId(const I_ProductionTerm *term) const
{
std::string idstr;
if (!term)
idstr ="bux::TID_EOF";
else if (term->generateId(idstr))
;
else if (auto const lex =dynamic_cast<const C_LexSymbol*>(term))
{
if (!m_Lex2ID.contains(lex->m_Var))
RUNTIME_ERROR("Lex '{}' not found", lex->m_Var);
idstr.assign("TID_LEX_").append(lex->m_Var);
}
else if (auto const slex =dynamic_cast<const C_StrLiteral*>(term))
return std::string(1u,'\'').append(asciiLiteral(uint8_t(slex->m_Str[0]))).append(1u,'\'');
else
RUNTIME_ERROR("{}Fail to produce output id for {}", idstr, HRTN(*term));
return std::string("ZIP_TOKEN(").append(idstr).append(1,')');
}
T_LexID C_ParserInfo::prodTerm2id(const I_ProductionTerm *attr) const
{
auto ret = prodTerm2idIfAny(attr);
if (ret.index())
RUNTIME_ERROR("{}", std::get<1>(ret));
return std::get<0>(ret);
}
auto C_ParserInfo::prodTerm2idIfAny(const I_ProductionTerm *term) const -> C_LexOrError
{
if (!term)
return TID_EOF;
std::string idstr;
if (term->generateId(idstr))
{
if (m_IdSet)
{
auto found = m_GenLex2Id.find(idstr);
if (found != m_GenLex2Id.end())
return found->second;
found = m_Nonterm2Id.find(idstr);
if (found != m_Nonterm2Id.end())
return found->second;
}
return "User-defined id "+idstr+" not generated";
}
if (auto const lex = dynamic_cast<const C_LexSymbol*>(term))
{
if (m_IdSet)
{
auto found = m_Lex2ID.find(lex->m_Var);
if (found != m_Lex2ID.end())
return found->second;
}
return "Number id of $"+lex->m_Var+" not found";
}
if (auto const slex = dynamic_cast<const C_StrLiteral*>(term))
return T_LexID(slex->m_Str[0]);
return "Unexpected "+HRTN(term);
}
void C_ParserInfo::wrapup()
{
if (getGeneratedID)
LOGIC_ERROR("getGeneratedID already assigned");
getGeneratedID = [ignoreCases = ignoreKeywordCase()](const std::string &str, std::string &dst) {
if (str.size() == 1)
return false;
if (!isKeyword(str))
dst = "TID_LITERAL_" + literalSuffix(str);
else
{
dst = "TID_KEYWORD_";
if (!ignoreCases)
dst += str;
else
for (auto c: str)
dst += char(std::toupper(c));
}
return true;
};
}
C_StrLiteral::C_StrLiteral(std::string_view s, char quoteMark):
m_Str(s),
m_QuoteMark(quoteMark)
{
}
C_StrLiteral::C_StrLiteral(size_t n, char c, char quoteMark):
m_Str(n, c),
m_QuoteMark(quoteMark)
{
}
bool C_StrLiteral::operator==(const I_ProductionTerm &a) const
{
if (auto const t =dynamic_cast<const C_StrLiteral*>(&a))
return m_Str == t->m_Str;
return false;
}
std::string C_StrLiteral::displayStr() const
{
std::string ret(asciiLiteral(m_Str));
if (ret.size() != m_Str.size() || ret == "[[")
ret.insert(0,"\"") += '\"';
return ret;
}
bool C_StrLiteral::generateId(std::string &id) const
{
return getGeneratedID(m_Str, id);
}
bool C_LexSymbol::operator==(const I_ProductionTerm &a) const
{
if (auto const t =dynamic_cast<const C_LexSymbol*>(&a))
return m_Var == t->m_Var;
return false;
}
std::string C_LexSymbol::displayStr() const
{
return "$" +m_Var;
}
bool C_LexSymbol::generateId(std::string &) const
{
return false;
}
bool C_Nonterminal::operator==(const I_ProductionTerm &a) const
{
if (auto const t =dynamic_cast<const C_Nonterminal*>(&a))
return m_id == t->m_id;
return false;
}
std::string C_Nonterminal::displayStr() const
{
return "<"+m_id+">";
}
bool C_Nonterminal::generateId(std::string &id) const
{
return getNonterminalID(m_id, id);
}
bool FC_LessLex::operator()(const I_ProductionTerm *a, const I_ProductionTerm *b) const
{
return id(a) < id(b);
}
T_LexID FC_LessLex::id(const I_ProductionTerm *term) const
{
const auto ret = m_Parsed.prodTerm2idIfAny(term);
if (ret.index())
RUNTIME_ERROR("No id for {}\t({})", term->displayStr(), std::get<1>(ret));
return std::get<0>(ret);
}
} // namespace ParserGen