-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNFContext.cpp
605 lines (542 loc) · 17.5 KB
/
BNFContext.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "BNFContext.h"
//--------------------------------------------------------------------
#include "bux/FsUtil.h" // bux::search_dirs()
#include "bux/LogStream.h" // HRTN()
#include "bux/StrUtil.h" // bux::expand_env()
#include "bux/XException.h" // RUNTIME_ERROR()
#include <cstring> // memcmp()
#include <fstream> // std::ifstream
#include <limits> // std::numeric_limits<>
#include <print> // std::print()
namespace {
enum
{
BNM_KEYWORD =1<<0,
BNM_OPERATOR =1<<1
};
} // namespace
namespace ParserGen {
using namespace Main;
//
// Implement Classes
//
C_BNFContext::~C_BNFContext()
{
for (auto &i: m_PriorRaws)
delete i.m_pAttr;
}
bool C_BNFContext::activateBuiltinNonterminal(const std::string &id)
{
bool ret = false;
if (id == "keyword")
{
m_BuilinNonterminalMask |= BNM_KEYWORD;
ret = true;
}
else if (id == "operator")
{
m_BuilinNonterminalMask |= BNM_OPERATOR;
ret = true;
}
return ret;
}
bool C_BNFContext::addGenId(const std::string &id)
{
if (m_GeneTokens.find(id) == m_GeneTokens.end())
{
m_GeneTokens.insert(id);
return true;
}
return false;
}
bool C_BNFContext::addLexId(const std::string &id)
{
return m_Lex2ID.try_emplace(id).second;
}
void C_BNFContext::addIdFromToken(I_LexAttr *attr)
{
if (!attr)
return;
std::string id;
bool generated = false;
if (auto term =dynamic_cast<I_ProductionTerm*>(attr))
{
if (term->generateId(id))
{
generated = true;
addLiteral(term, id);
}
}
else if (auto i =dynamic_cast<C_IntegerLex*>(attr))
generated = getGeneratedID(i->str(),id);
if (generated)
addGenId(id);
}
void C_BNFContext::addLiteral(I_ProductionTerm *term, const std::string &id)
{
if (auto s =dynamic_cast<C_StrLiteral*>(term))
m_Literal2IdName[s->m_Str] = id;
}
void C_BNFContext::addOption(const std::string &key, C_Semantic *value)
{
auto &dst = m_OptMap[key];
if (value)
{
if (!dst.empty() && key == "EXTRA_TOKENS")
{
auto t = new C_BracketedLex("|");
dst.emplace_back(t);
}
dst.splice(dst.end(), *value);
}
}
bool C_BNFContext::addProduction(C_Production &prod, C_Semantic *semantic)
{
for (auto &i: m_Productions)
if (i == prod)
return false;
auto &key = m_Productions.emplace_back();
key.m_Lval.swap(prod.m_Lval);
key.m_Rval.swap(prod.m_Rval);
key.m_Index = m_OntoJumps.size();
auto &value = m_OntoPool.emplace_back();
value.m_Productions.emplace_back(&key);
m_OntoJumps.emplace_back(&value);
if (semantic)
value.semantic(*semantic);
else
m_Semanticless.emplace_back(&key);
return true;
}
void C_BNFContext::addPriority(E_Associativity assoc, bux::LR1::C_LexPtr &src)
{
C_PriorityRaw p;
p.m_Weight = m_PriorWeight;
p.m_Assoc = assoc;
p.m_pAttr = src.disown();
m_PriorRaws.emplace_back(p);
}
auto C_BNFContext::attr2id(I_LexAttr *attr) -> C_LexOrError
{
if (auto const term = dynamic_cast<I_ProductionTerm*>(attr))
return prodTerm2idIfAny(term);
if (auto const ilex = dynamic_cast<C_IntegerLex*>(attr))
{
std::string idstr;
if (getGeneratedID(ilex->str(), idstr))
{
auto found = m_GenLex2Id.find(idstr);
if (found != m_GenLex2Id.end() && m_IdSet)
return found->second;
return "User-defined id "+idstr+" not generated";
}
else
return T_LexID(ilex->str()[0]);
}
return "Unknown lex attr of type "+HRTN(attr);
}
bool C_BNFContext::checkSemanticlessProductions() const
{
if (!m_Semanticless.empty())
{
std::print("WARNING: The following productions are declared with no semantic!\n");
for (auto i: m_Semanticless)
std::print("\t{}\n", i->str());
return true;
}
return false;
}
bool C_BNFContext::checkUnusedOptions() const
{
bool found = false;
for (auto &i: m_OptMap)
if (!i.second.m_Used)
{
std::print("WARNING: Option %{} defined but not used !\n", i.first);
found = true;
}
return found;
}
bool C_BNFContext::equal(const C_Semantic &a, const C_Semantic &b) const
{
return ensureNoConcat(a) == ensureNoConcat(b);
/*
if (a.size() != b.size())
return false;
for (auto ia = a.begin(), ib = b.begin(); ia != a.end(); ++ia, ++ib)
{
if (auto sa =dynamic_cast<C_BracketedLex*>(*ia))
{
if (auto sb =dynamic_cast<C_BracketedLex*>(*ib))
{
if (sa->m_Str != sb->m_Str)
return false;
}
else
return false;
}
else
RUNTIME_ERROR("Unknown semantic lex type {}", HRTN(*ia));
}
return true;*/
}
std::string C_BNFContext::expand_include(const std::string &org_path) const
{
return bux::search_dirs(bux::expand_env(org_path.c_str()), m_IncDirs).string();
}
void C_BNFContext::log(bux::E_LogLevel level, const C_SourcePos &pos, std::string_view message)
{
static const char *const KIND[] ={
"FATAL",
"ERROR",
"WARNING",
"INFO",
"VERBOSE"
};
const auto kind = KIND[level];
std::print("{}({},{}) {}: {}\n", pos.m_Source, pos.m_Line, pos.m_Col, kind, message);
static const size_t ERR_LIMIT[] ={
0,
10,
100,
std::numeric_limits<size_t>::max(),
std::numeric_limits<size_t>::max()
};
if (++m_ErrorTotal[level] > ERR_LIMIT[level])
RUNTIME_ERROR("Too many ({}) {}s!", m_ErrorTotal[level] ,kind);
}
void C_BNFContext::normalize(const C_Semantic &src, C_Semantic &dst) const
{
std::string remains;
normalize(src, dst, remains);
if (!remains.empty())
{
auto *const lex = new C_BracketedLex(remains);
dst.emplace_back(lex);
}
}
void C_BNFContext::normalize(
const C_Semantic &src,
C_Semantic &dst,
std::string &remains ) const
{
for (auto &i: src)
{
if (!i)
;
else if (auto s =dynamic_cast<C_BracketedLex*>(i))
remains += s->m_Str;
else if (auto o =dynamic_cast<C_OptionLex*>(i))
{
auto found = m_OptMap.find(o->m_Var);
if (found == m_OptMap.end())
RUNTIME_ERROR("Option \"{}\" not found", o->m_Var);
found->second.m_Used = true;
normalize(found->second, dst, remains);
}
else if (auto l =dynamic_cast<C_LexSymbol*>(i))
{
if (!m_Lex2ID.contains(l->m_Var))
RUNTIME_ERROR("Lex \"{}\" not found", l->m_Var);
remains.append("TID_LEX_").append(l->m_Var);
}
else
RUNTIME_ERROR("Unknown semantic lex type {}", HRTN(i));
}
}
std::optional<std::string> C_BNFContext::setClassName(C_StringList &qualified, C_TemplateArgs &targs)
{
// Preconditions
if (!m_ClassName.empty())
return "Class name already defined";
#ifdef __TEMPLATE_OUTPUT
if (!m_TemplateArgs.empty())
return "Template args already defined";
#endif // __TEMPLATE_OUTPUT
if (qualified.empty())
return "Empty class name assigned";
// Do it
m_ClassName = qualified.back();
qualified.pop_back();
m_Namespace.swap(qualified);
#ifdef __TEMPLATE_OUTPUT
for (auto j = targs.begin(); j != targs.end(); ++j)
{
auto &t = m_TemplateArgs.emplace_back();
t.m_ArgDummy = j->back();
j->pop_back();
auto &s = t.m_TypeName;
for (auto k: *j)
{
if (!s.empty() && isalnum(s.back()) && isalnum(k.front()))
s += ' ';
s += k;
}
if (!s.empty() && isalnum(s.back()))
s += ' ';
}
#else
if (!targs.empty())
return "Template args not allowed";
#endif // __TEMPLATE_OUTPUT
return {};
}
size_t C_BNFContext::tempReductionIndex(const C_IndexedProd &prod) const
{
return m_OntoJumps[prod.m_Index]->m_Index;
}
void C_BNFContext::wrapup(const bux::C_SourcePos &pos)
{
C_ParserInfo::wrapup(); // MUST be called first
// Condition stack not emptied
for (auto i = m_CondStack.rbegin(); i != m_CondStack.rend(); ++i)
log(LL_ERROR, i->m_pos, "No matching #endif");
// Ensure every new token is added to m_GeneTokens
for (auto &i: m_Productions)
{
std::string id;
if (getNonterminalID(i.m_Lval, id))
addGenId(id);
for (auto &j: i.m_Rval)
if (j->generateId(id))
{
addGenId(id);
addLiteral(j, id); // terminal only
}
}
for (auto &i: m_PriorRaws)
addIdFromToken(i.m_pAttr);
if (auto found = getOption("IDDEF_SOURCE"))
// Parse the source
{
// Extract & assign ids for m_Lex2ID & m_GenLex2Id
std::ifstream in(found->expand());
unsigned long tid_max = 0;
for (std::string s; getline(in, s);)
{
const auto posName = s.find_first_not_of(" \t");
if (posName == std::string::npos ||
memcmp(s.data()+posName, "TID_", 4))
continue;
static const char PRENUM[] = "= bux::TOKENGEN_LB";
const auto posULong = s.find(PRENUM, posName);
if (posULong != std::string::npos)
// Found!
{
const auto t = strtoul(s.data()+posULong+sizeof PRENUM, nullptr, 10);
if (tid_max + 1 == t)
tid_max = t;
else if (!tid_max && !t)
; // First TID_xxxx
else
RUNTIME_ERROR("tid_max={} but next id is {}", tid_max, t);
const auto posEndName = s.find_first_of(" \t", posName);
if (posName == std::string::npos)
RUNTIME_ERROR("End of name not found \"{}\"", s.substr(posName));
if (!memcmp(s.data()+posName, "TID_LEX_", 8))
{
const auto off = posName + 8;
m_Lex2ID.insert_or_assign(s.substr(off, posEndName-off), T_LexID(bux::TOKENGEN_LB + t));
}
else
m_GenLex2Id[s.substr(posName, posEndName-posName)] = T_LexID(bux::TOKENGEN_LB + t);
}
else
{
s = s.substr(posName);
if (s != "TID_UB_")
RUNTIME_ERROR("Not expected final {}", s);
}
}
std::print("MAX(IdDef) = {}\n", tid_max);
// Build m_Nonterm2Id from m_GeneTokens
tid_max += bux::TOKENGEN_LB;
for (auto &i: m_GeneTokens)
if (memcmp(i.data(), "TID_", 4))
m_Nonterm2Id.insert_or_assign(i, T_LexID(++tid_max));
}
else
// Assign from scratch
{
// Assign id part of m_Lex2ID
bux::T_LexID count = bux::TOKENGEN_LB;
for (auto &i: m_Lex2ID)
i.second = count++;
// Build m_GenLexId & m_Nonterm2Id from m_GeneTokens
C_StringSet nonterms;
for (auto &i: m_GeneTokens)
{
if (!memcmp(i.data(), "TID_", 4))
m_GenLex2Id.insert_or_assign(i, count++);
else
nonterms.insert(i);
}
for (auto &i: nonterms)
m_Nonterm2Id.insert_or_assign(i, count++);
}
m_IdSet =true;
// Build m_PriorityMap from m_PriorRaws and m_Lex2ID
size_t count = 0;
for (auto i = m_PriorRaws.begin(); i != m_PriorRaws.end(); ++i, ++count)
{
const auto ret = attr2id(i->m_pAttr);
switch (ret.index())
{
case 0:
if (const auto id = std::get<0>(ret);
!m_PriorityMap.try_emplace(id, C_Priority{.m_Weight=i->m_Weight, .m_Assoc=i->m_Assoc}).second)
{
auto out = std::format("Duplicate priority assignments on id {}", id);
if (id < 127)
out += std::format("(\'{}\')", asciiLiteral(id));
log(LL_ERROR, pos, out);
}
break;
case 1:
log(LL_ERROR, pos, std::format("Fail to get id from the {}th priority record with token type {}: {}",
count, HRTN(*i->m_pAttr), std::get<1>(ret)));
break;
}
}
// Preconditions for building m_Productions and m_Reductions
if (m_Productions.size() != m_OntoPool.size() ||
m_Productions.size() != m_OntoJumps.size())
return log(LL_FATAL, pos, "Inconsistent data for production optimization!");
// Add the root production if not found
size_t rootCount{};
for (auto &i: m_Productions)
if (i.m_Lval == "@")
++rootCount;
if (!rootCount)
// Supply missing <@> production for user
{
bux::C_NewNode<C_ProductionLex> const t;
t->m_Lval = "@";
C_Semantic s;
if (!m_Productions.empty())
{
const auto pseudoRoot = m_Productions.front().m_Lval;
t->m_Rval.emplace_back(new C_Nonterminal{pseudoRoot});
for (auto &i: m_OntoPool)
{
for (auto j: i.m_Productions)
if (j->m_Lval == pseudoRoot)
{
if (i.m_Reduction.expand().find("$r") != std::string::npos)
{
s.emplace_back(new C_BracketedLex{"$r = $1;"});
goto AddProd;
}
break;
}
}
}
AddProd:
addProduction(*t, &s);
}
else if (rootCount > 1)
return log(LL_FATAL, pos, "<@> productions defined more than once!");
// Add productions of activated builtin nonterminals
if (m_BuilinNonterminalMask)
for (auto &i: m_Literal2IdName)
{
bux::C_NewNode<C_ProductionLex> const t;
if ((m_BuilinNonterminalMask &BNM_KEYWORD) &&
!i.second.compare(0, 12, "TID_KEYWORD_"))
{
t->m_Lval ="@keyword";
t->m_Rval.emplace_back(new C_Id(i.first));
}
else if ((m_BuilinNonterminalMask &BNM_OPERATOR) &&
!i.second.compare(0, 12, "TID_LITERAL_"))
{
t->m_Lval ="@operator";
t->m_Rval.emplace_back(new C_StrLiteral(i.first));
}
else
{
log(LL_FATAL, pos, std::format("Unexpected (literal,id) pair: ({},{})", i.first, i.second));
continue;
}
C_Semantic s;
s.emplace_back(new C_BracketedLex(std::format("$r = bux::createLex<std::string>(\"{}\");",asciiLiteral(i.first))));
addProduction(*t, &s);
} // for (auto &i: m_Literal2IdName)
// Build m_Productions and m_Reductions from
// m_OntoPool, m_OntoJumps, m_Lex2ID and m_PriorityMap
//
for (auto &i: m_OntoPool)
{
C_Semantic t;
normalize(i.m_Reduction, t);
t.swap(i.m_Reduction);
}
bool changed;
do
{
changed = false;
count = 0;
for (auto &i: m_OntoPool)
i.m_Index = count++;
for (auto i = m_OntoPool.begin(); i != m_OntoPool.end();)
{
for (auto j =m_OntoPool.begin(); j != i; ++j)
if (equal(i->m_Reduction,j->m_Reduction))
// Merge them
{
changed =true;
for (auto &k: i->m_Productions)
{
j->m_Productions.emplace_back(k);
m_OntoJumps[k->m_Index] = &*j;
}
i = m_OntoPool.erase(i);
goto iNext;
}
++i;
iNext:;
}
} while (changed);
for (auto &i: m_Productions)
i.m_Index = m_OntoJumps[i.m_Index]->m_Index;
for (auto &i: m_OntoPool)
m_Reductions.emplace_back(i.m_Reduction.expand());
// Normalize options
for (auto &i: m_OptMap)
{
C_Semantic t;
normalize(i.second, t);
t.swap(i.second);
}
}
void C_BNFContext::ifCond(bool y, const C_SourcePos &pos)
{
m_CondStack.emplace_back(y, pos);
}
void C_BNFContext::elseCond(const C_SourcePos &pos)
{
if (m_CondStack.empty() ||
pos.m_Source != m_CondStack.back().m_pos.m_Source)
log(LL_ERROR, pos, "No matching #if");
else
{
auto &dst = m_CondStack.back().m_yes;
dst = !dst;
}
}
void C_BNFContext::endifCond(const C_SourcePos &pos)
{
if (m_CondStack.empty() ||
pos.m_Source != m_CondStack.back().m_pos.m_Source)
log(LL_ERROR, pos, "No matching #if");
else
m_CondStack.pop_back();
}
bool C_BNFContext::testCond() const
{
for (auto &i: m_CondStack)
if (!i.m_yes)
return false;
return true;
}
} // namespace ParserGen