-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExprParser.h
executable file
·242 lines (206 loc) · 7.23 KB
/
ExprParser.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//#line 156 "dtParserTemplateD.g
//#line 162 "dtParserTemplateD.g
#include <iostream>
#include "ExprParserprs.h"
#include "lpg2/DeterministicParser.h"
#include "lpg2/diagnose.h"
#include "lpg2/ErrorToken.h"
#include "lpg2/Exception.h"
#include "lpg2/IAbstractArrayList.h"
#include "lpg2/IAst.h"
#include "lpg2/ILexStream.h"
#include "lpg2/ParseTable.h"
#include "lpg2/PrsStream.h"
#include "lpg2/RuleAction.h"
#include "lpg2/AstPoolHolder.h"
#include "ExprParser_top_level_ast.h"
using namespace ExprParser_top_level_ast;
struct ExprParser :public PrsStream, public RuleAction
{
bool unimplementedSymbolsWarning = false;
inline static ParseTable* prs = new ExprParserprs();
ParseTable* getParseTable() { return prs; }
DeterministicParser* dtParser = nullptr;
DeterministicParser* getParser() { return dtParser; }
void setResult(Object* object) { dtParser->setSym1(object); }
Object* getRhsSym(int i) { return dtParser->getSym(i); }
int getRhsTokenIndex(int i) { return dtParser->getToken(i); }
IToken* getRhsIToken(int i) { return PrsStream::getIToken(getRhsTokenIndex(i)); }
int getRhsFirstTokenIndex(int i) { return dtParser->getFirstToken(i); }
IToken* getRhsFirstIToken(int i) { return PrsStream::getIToken(getRhsFirstTokenIndex(i)); }
int getRhsLastTokenIndex(int i) { return dtParser->getLastToken(i); }
IToken* getRhsLastIToken(int i) { return PrsStream::getIToken(getRhsLastTokenIndex(i)); }
int getLeftSpan() { return dtParser->getFirstToken(); }
IToken* getLeftIToken() { return PrsStream::getIToken(getLeftSpan()); }
int getRightSpan() { return dtParser->getLastToken(); }
IToken* getRightIToken() { return PrsStream::getIToken(getRightSpan()); }
int getRhsErrorTokenIndex(int i)
{
int index = dtParser->getToken(i);
IToken* err = PrsStream::getIToken(index);
return (dynamic_cast<ErrorToken*>(err) ? index : 0);
}
ErrorToken* getRhsErrorIToken(int i)
{
int index = dtParser->getToken(i);
IToken* err = PrsStream::getIToken(index);
return (ErrorToken*)(dynamic_cast<ErrorToken*>(err) ? err : nullptr);
}
~ExprParser()
{
delete dtParser;
}
ExprParser(ILexStream* lexStream) :PrsStream(lexStream)
{
try
{
PrsStream::remapTerminalSymbols(orderedTerminalSymbols(), ExprParserprs::EOFT_SYMBOL);
}
catch (NullExportedSymbolsException& e) {
}
catch (NullTerminalSymbolsException& e) {
}
catch (UnimplementedTerminalsException& e)
{
if (unimplementedSymbolsWarning) {
auto unimplemented_symbols = e.getSymbols();
std::cout << ("The Lexer will not scan the following token(s):");
for (int i = 0; i < unimplemented_symbols.size(); i++)
{
auto id = unimplemented_symbols.at(i);
std::wcout << L" " << ExprParsersym::orderedTerminalSymbols[id];
}
std::cout << std::endl;
}
}
catch (UndefinedEofSymbolException& e)
{
//throw ( UndefinedEofSymbolException
// ("The Lexer does not implement the Eof symbol " +
// ExprParsersym::orderedTerminalSymbols[ExprParserprs::EOFT_SYMBOL]));
}
try
{
dtParser = new DeterministicParser(this, prs, this);
}
catch (NotDeterministicParseTableException& e)
{
throw (NotDeterministicParseTableException
("Regenerate ExprParserprs.java with -NOBACKTRACK option"));
}
catch (BadParseSymFileException& e)
{
throw (BadParseSymFileException("Bad Parser Symbol File -- ExprParsersym.java. Regenerate ExprParserprs.java"));
}
}
std::vector<std::wstring> orderedTerminalSymbols() { return ExprParsersym::orderedTerminalSymbols; }
std::wstring getTokenKindName(int kind) { return ExprParsersym::orderedTerminalSymbols[kind]; }
int getEOFTokenKind() { return ExprParserprs::EOFT_SYMBOL; }
PrsStream* getParseStream() { return (PrsStream*)this; }
Ast* parser()
{
return parser(nullptr, 0);
}
Ast* parser(Monitor* monitor)
{
return parser(monitor, 0);
}
Ast* parser(int error_repair_count)
{
return parser(nullptr, error_repair_count);
}
Ast* parser(Monitor* monitor, int error_repair_count)
{
dtParser->setMonitor(monitor);
try
{
return (Ast*)dtParser->parse();
}
catch (BadParseException e)
{
reset(e.error_token); // point to error token
DiagnoseParser diagnoseParser(this, prs);
diagnoseParser.diagnose(e.error_token);
}
return nullptr;
}
//
// Additional entry points, if any
//
//#line 318 "dtParserTemplateD.g
void ruleAction(int ruleNumber)
{
switch (ruleNumber)
{
using namespace ExprParser_top_level_ast;
//
// Rule 1: E ::= E + T
//
case 1: {
setResult(
//#line 13 ExprParser.g
_automatic_ast_pool << new E(getLeftIToken(), getRightIToken(),
//#line 13 ExprParser.g
(Ast*)getRhsSym(1),
//#line 13 ExprParser.g
(Ast*)getRhsSym(3))
//#line 13 ExprParser.g
);
break;
}
//
// Rule 2: E ::= T
//
case 2:
break;
//
// Rule 3: T ::= T * F
//
case 3: {
setResult(
//#line 15 ExprParser.g
_automatic_ast_pool << new T(getLeftIToken(), getRightIToken(),
//#line 15 ExprParser.g
(Ast*)getRhsSym(1),
//#line 15 ExprParser.g
(Ast*)getRhsSym(3))
//#line 15 ExprParser.g
);
break;
}
//
// Rule 4: T ::= F
//
case 4:
break;
//
// Rule 5: F ::= IntegerLiteral
//
case 5: {
setResult(
//#line 17 ExprParser.g
_automatic_ast_pool << new F(getRhsIToken(1))
//#line 17 ExprParser.g
);
break;
}
//
// Rule 6: F ::= ( E )
//
case 6: {
setResult(
//#line 18 ExprParser.g
_automatic_ast_pool << new ParenExpr(getLeftIToken(), getRightIToken(),
//#line 18 ExprParser.g
(Ast*)getRhsSym(2))
//#line 18 ExprParser.g
);
break;
}
//#line 322 "dtParserTemplateD.g
default:
break;
}
return;
}
};