-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalcParser.trison
279 lines (244 loc) · 8.33 KB
/
CalcParser.trison
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
// 2017.08.17 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
// ///////////////////////////////////////////////////////////////////////////
// cpp parser header-file-related directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.header_filename "CalcParser.hpp"
%target.cpp.implementation_filename "CalcParser.cpp"
%target.cpp.top_of_header_file %{
#include <iostream>
class Scanner;
namespace Ast {
struct Base;
} // end of namespace Ast
%}
%target.cpp.class_name CalcParser
%target.cpp.parse_method_access "public"
%target.cpp.bottom_of_class {
bool ScannerDebugSpew () const;
void ScannerDebugSpew (bool debug_spew);
void AttachIstream (std::istream &in);
private:
Scanner *m_scanner;
}
// ///////////////////////////////////////////////////////////////////////////
// cpp parser implementation-file-related directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.top_of_implementation_file %{
#include "ast.hpp"
#include <cmath>
#include <sstream>
#include "Scanner.hpp"
typedef Ast::Operator<Ast::Base> BaseOperator;
template <typename... Args_>
std::shared_ptr<BaseOperator> base_operator (Args_&&... args)
{
return std::make_shared<BaseOperator>(std::forward<Args_>(args)...);
}
%}
%target.cpp.constructor_actions {
m_scanner = new Scanner();
m_scanner->DebugSpew(true);
}
%target.cpp.destructor_actions {
delete m_scanner;
m_scanner = NULL;
}
// %target.cpp.top_of_parse_method_actions %{
// EmitExecutionMessage("starting parser");
// %}
// %target.cpp.bottom_of_parse_method_actions %{
// if (parse_return_code == PRC_SUCCESS)
// EmitExecutionMessage("parse was successful");
// %}
%target.cpp.bottom_of_implementation_file %{
void CalcParser::AttachIstream (std::istream &in)
{
assert(m_scanner != NULL);
m_scanner->AttachIstream(in);
}
// FiLoc const &CalcParser::GetFiLoc () const
// {
// assert(m_scanner != nullptr);
// return m_scanner->GetFiLoc();
// }
bool CalcParser::ScannerDebugSpew () const
{
return m_scanner->DebugSpew();
}
void CalcParser::ScannerDebugSpew (bool debug_spew)
{
m_scanner->DebugSpew(debug_spew);
}
%}
// ///////////////////////////////////////////////////////////////////////////
// cpp parser token-specific directives
// ///////////////////////////////////////////////////////////////////////////
%target.cpp.token_data_type "std::shared_ptr<Ast::Base>"
%target.cpp.token_data_default "std::shared_ptr<Ast::Base>()"
// none are necessary because std::shared_ptr is reference counted
%target.cpp.throw_away_token_actions { }
%target.cpp.scan_actions {
assert(m_scanner != NULL);
std::shared_ptr<Ast::Base> token;
TokenId scanner_token_id = m_scanner->Scan(token);
switch (scanner_token_id)
{
case BAD_TOKEN:
case CHAR_LITERAL:
case IDENTIFIER:
case STRING_LITERAL:
return Token(Terminal::BAD_TOKEN, token);
case END_OF_FILE:
return Token(Terminal::END_, token);
case INTEGER_LITERAL:
return Token(Terminal::NUM, Ast::numeric_literal(double(token->as<Ast::IntegerLiteral>().m_value)));
case NUMERIC_LITERAL:
return Token(Terminal::NUM, token);
default:
assert(std::uint32_t(scanner_token_id) < 256 && "You probably forgot a TokenId");
return Token(Terminal::Name(scanner_token_id), nullptr);
}
}
%target.cpp.reset_for_new_input_actions {
// m_recoverable_error_encountered = false;
}
// ///////////////////////////////////////////////////////////////////////////
// cpp parser misc directives
// ///////////////////////////////////////////////////////////////////////////
// this adds a lot of string content to parser source
%target.cpp.generate_debug_spew_code
// ///////////////////////////////////////////////////////////////////////////
// cpp parser terminal directives
// ///////////////////////////////////////////////////////////////////////////
%terminal '+' '-' '*' '/' '^' '?'
%terminal '(' ')' ';'
%terminal NUM BAD_TOKEN
// ///////////////////////////////////////////////////////////////////////////
// cpp parser precedence directives
// ///////////////////////////////////////////////////////////////////////////
// Lowest precedence is first, highest is last.
%prec.left %default
%prec.left LOWEST
%prec.left LOW
%prec.left ADDITIVE
%prec.left MULTIPLICATIVE
%prec.nonassoc QUESTION
%prec.right UNARY
%prec.right EXPONENTIATION
%prec.left HIGHEST
// ///////////////////////////////////////////////////////////////////////////
// cpp parser grammar specification
// ///////////////////////////////////////////////////////////////////////////
%default_parse_nonterminal stmt_then_end
%%
%nonterminal stmt_then_end
:
stmt:st %end
%target.cpp {
std::cout << "stmt_then_end <- stmt %end\n";
return st;
}
|
%error[!%end] %end
%target.cpp {
std::cout << "stmt_then_end <- %error[!%end] %end\n";
return Ast::error_dummy("stmt_then_end <- %error[!%end] %end");
}
;
%nonterminal stmt
:
expr:ex ';'
%target.cpp {
// std::cout << "stmt <- expr ';'\n";
return ex;
}
|
%error[!%end] ';'
%target.cpp {
// std::cout << "stmt <- %error ';'\n";
return Ast::error_dummy("stmt <- %error ';'");
}
;
%nonterminal expr
:
'(' expr:e ')'
%target.cpp {
// std::cout << "expr <- '(' expr ')'\n";
return e;
}
|
'(' %error[!%end] ')'
%target.cpp {
// std::cout << "expr <- '(' %error[!%end] ')'\n";
return Ast::error_dummy("expr <- '(' %error[!%end] ')'");
}
|
// NOTE: This will match everything until %end.
// This rule prevents ';' from being the error-stopper in statements, since
// the %error token matches everything including ';'.
// If %error_until(%end, ';') were added, this situation could be handled better.
'(' %error[![%end|';']]
%target.cpp {
// std::cout << "expr <- '(' %error[![%end|';']]\n";
return Ast::error_dummy("expr <- '(' %error[![%end|';']]");
}
|
NUM:num
%target.cpp {
// std::cout << "expr <- NUM(" << num << ")\n";
return num;
}
|
expr:lhs '+' expr:rhs %prec ADDITIVE
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '+' expr(" << rhs << ")\n";
return base_operator("+", BaseOperator::ChildNodes{lhs, rhs});
}
|
expr:lhs '+' '+' '+' '+' expr:rhs %prec LOWEST
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '+' '+' '+' '+' expr(" << rhs << ")\n";
return base_operator("++++", BaseOperator::ChildNodes{lhs, rhs});
}
|
expr:lhs '+' '+' '+' expr:rhs %prec LOW
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '+' '+' '+' expr(" << rhs << ")\n";
return base_operator("+++", BaseOperator::ChildNodes{lhs, rhs});
}
|
expr:lhs '+' '+' expr:rhs %prec HIGHEST
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '+' '+' expr(" << rhs << ")\n";
return base_operator("++", BaseOperator::ChildNodes{lhs, rhs});
}
|
expr:lhs '*' expr:rhs %prec MULTIPLICATIVE
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '*' expr(" << rhs << ")\n";
return base_operator("*", BaseOperator::ChildNodes{lhs, rhs});
}
|
expr:lhs '?' expr:rhs %prec QUESTION
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '?' expr(" << rhs << ")\n";
return base_operator("?", BaseOperator::ChildNodes{lhs, rhs});
}
|
'-' expr:op %prec UNARY
%target.cpp {
// std::cout << "expr <- '-' expr(" << op << ")\n";
return base_operator("-", BaseOperator::ChildNodes{op});
}
|
expr:lhs '^' expr:rhs %prec EXPONENTIATION
%target.cpp {
// std::cout << "expr <- expr(" << lhs << ") '^' expr(" << rhs << ")\n";
return base_operator("^", BaseOperator::ChildNodes{lhs, rhs});
}
// |
// // This just screws everything up. It seems like there should just never be
// // a lone %error in a rule. Though %error[![%end|';']] could solve this.
// %error[![%end|';']] %target.cpp { std::cout << "expr <- %error[![%end|';']]\n"; return 0.0; }
;