-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
gherkin-cpp-parser.razor
271 lines (239 loc) · 7.02 KB
/
gherkin-cpp-parser.razor
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
@using Berp;
@functions {
public static string ToSnakeCase(string str)
{
return
string.Concat(
str.Select(
(x, i) => i > 0 && char.IsUpper(x)
? "_" + x
: x.ToString()
)
).ToLower();
}
public static string NameOf(Rule rule)
{ return ToSnakeCase(rule.Name.Replace("#", "")); }
}
@helper CallProduction(ProductionRule production)
{
var rname = ToSnakeCase(production.RuleName);
switch(production.Type)
{
case ProductionRuleType.Start:
@: start_rule(context, rule_type::@rname);
break;
case ProductionRuleType.End:
@: end_rule(context, rule_type::@rname);
break;
case ProductionRuleType.Process:
@: build(context, token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
std::string state_comment = "State: @state.Id - @Raw(state.Comment)";
std::string expected_tokens = "@Raw(string.Join(", ", expectedTokens))";
auto ep =
token.is_eof()
? new_parser_error<unexpected_eof>(
token, expected_tokens, state_comment
)
: new_parser_error<unexpected_token>(
token, expected_tokens, state_comment
)
;
if (context.stop_at_first_error) {
throw *ep;
}
context.add_error(std::move(ep));
return @state.Id;
</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(ToSnakeCase(tokenType.Name))(context, token)</text>}
// This file is generated. Do not edit! Edit gherkin-cpp-parser.razor instead.
#include <cucumber/gherkin/parser_base.hpp>
#include <cucumber/gherkin/rule_type.hpp>
namespace cucumber::gherkin {
template <
typename Builder = ast_builder,
typename Scanner = token_scanner,
typename Matcher = token_matcher
>
class parser : public parser_base<Builder, Scanner, Matcher>
{
public:
using parent = parser_base<Builder, Scanner, Matcher>;
using parent::parent;
using parent::parse;
using context_type = typename parent::context_type;
protected:
void parse(context_type& context) override
{
start_rule(context, rule_type::@NameOf(Model.RuleSet.StartRule));
std::size_t state = 0;
while (true) {
auto token = context.read_token();
state = match_token(state, token, context);
if (token.is_eof()) {
break;
}
}
end_rule(context, rule_type::@NameOf(Model.RuleSet.StartRule));
if (context.has_errors()) {
throw composite_parser_error(context.eptrs);
}
}
void build(context_type& context, token& token)
{ context.builder.build(token); }
void start_rule(context_type& context, rule_type rule_type)
{
handle_ast_error(
context,
rule_type,
[&context](auto rtype) {
context.builder.start_rule(rtype);
}
);
}
void end_rule(context_type& context, rule_type rule_type)
{
handle_ast_error(
context,
rule_type,
[&context](auto rtype) {
context.builder.end_rule(rtype);
}
);
}
template <typename Argument, typename Action>
bool handle_external_error(
context_type& context,
bool default_value,
Argument&& argument,
Action&& action
)
{
using ret_type = decltype(action(argument));
try {
if constexpr (std::is_same_v<ret_type, void>) {
action(argument);
return default_value;
} else {
return action(argument);
}
} catch (const composite_parser_error& e) {
for (const auto& ep : e.errors()) {
context.add_error(ep);
}
} catch (const parser_error& e) {
auto ep = new_parser_error<parser_error>(e);
context.add_error(ep);
}
return default_value;
}
template <typename Argument, typename Action>
void handle_ast_error(
context_type& context,
Argument&& argument,
Action&& action
)
{ handle_external_error(context, true, argument, action); }
@foreach(var rule in Model.RuleSet.TokenRules)
{
<text>
bool match_@(NameOf(rule))(context_type& context, token& token)
{
@if (rule.Name != "#EOF")
{
@:if (token.is_eof()) {
@: return false;
@:}
@:
}
return
handle_external_error(
context,
false,
token,
[&context](auto& t) {
return context.matcher.match_@(NameOf(rule))(t);
}
);
}
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
bool lookahead_@(lookAheadHint.Id)(context_type& context, token& current_token)
{
current_token.detach();
token token;
token_queue queue;
bool match = false;
while (true) {
token = context.read_token();
token.detach();
queue.push_back(token);
if (@foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>@MatchToken(tokenType) || </text>}false) {
match = true;
break;
}
if (!(@foreach(var tokenType in lookAheadHint.Skip) {<text>@MatchToken(tokenType) || </text>}false)) {
break;
}
}
context.push_tokens(queue);
return match;
}
</text>
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{<text>
// @Raw(state.Comment)
std::size_t match_token_at_@(state.Id)(token& token, context_type& context)
{
@{var indent = " ";}
@foreach(var transition in state.Transitions)
{
@:if (@MatchToken(transition.TokenType)) {
if (transition.LookAheadHint != null)
{
@:if (lookahead_@(transition.LookAheadHint.Id)(context, token)) {
indent = " ";
}
foreach(var production in transition.Productions)
{
@indent@CallProduction(production)
}
@:@(indent)return @transition.TargetState;
if (transition.LookAheadHint != null)
{
indent = " ";
@:}
}
@:}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
}
</text>
}
std::size_t match_token(std::size_t state, token& token, context_type& context)
{
switch (state) {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:return match_token_at_@(state.Id)(token, context);
}
default:
throw
std::runtime_error(
"invalid operation: " + std::to_string(state)
);
return -1;
}
}
};
}