forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgherkin-perl.razor
183 lines (150 loc) · 4.63 KB
/
gherkin-perl.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
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type)
{
case ProductionRuleType.Start:
@: $self->_start_rule($context, '@production.RuleName');
break;
case ProductionRuleType.End:
@: $self->_end_rule($context, '@production.RuleName');
break;
case ProductionRuleType.Process:
@: $self->_build($context, $token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text> $token->detach;
# Create the appropriate error
my $error_class = "Gherkin::Exceptions::" . (
$token->is_eof ? 'UnexpectedEOF' : 'UnexpectedToken' );
my @@args = (
$token,
["@Raw(string.Join("\", \"", expectedTokens))"], #"
"State: @state.Id - @Raw(state.Comment)",
);
$error_class->throw( @@args ) if $self->stop_at_first_error;
eval {$error_class->throw( @@args )};
$self->add_error( $context, $@@ );
return @state.Id;
</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(tokenType)($context, $token)</text>}
package Gherkin::Generated::@(Model.ParserClassName);
# This file is generated. Do not edit! Edit gherkin-perl.razor instead.
use strict;
use warnings;
use base 'Gherkin::ParserBase';
our @@RULE_TYPES = [
'None',
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> '@rule.Name.Replace("#", "_")', # @rule.ToString(true)
</text>}
];
our %states_to_match_names = (
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState)) //..
{
@: @state.Id => "match_token_at_@(state.Id)",
}
);
sub parse {
my ( $self, $token_scanner, $uri ) = @@_;
$token_scanner = Gherkin::TokenScanner->new($token_scanner)
unless ref $token_scanner && (ref $token_scanner ne 'SCALAR');
$self->ast_builder->reset($uri);
$self->token_matcher->reset();
my $context = Gherkin::ParserContext->new(
{
token_scanner => $token_scanner,
token_matcher => $self->token_matcher,
}
);
$self->_start_rule( $context, '@Model.RuleSet.StartRule.Name' );
my $state = 0;
my $token;
while (1) {
$token = $context->read_token($context);
$state = $self->match_token( $state, $token, $context );
last if $token->is_eof();
}
$self->_end_rule( $context, '@Model.RuleSet.StartRule.Name' );
if ( my @@errors = $context->errors ) {
Gherkin::Exceptions::CompositeParser->throw(@@errors);
}
return $self->get_result();
}
sub match_token {
my ( $self, $state, $token, $context ) = @@_;
my $method_name = $states_to_match_names{ $state } ||
die "Unknown state: $state";
$self->$method_name( $token, $context );
}
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
sub match_@(rule.Name.Replace("#", "")) {
my ($self, $context, $token) = @@_;
@if (rule.Name != "#EOF")
{
@: return if $token->is_eof;
}
return $self->handle_external_error(
$context,
0, # Default return value
sub { $context->token_matcher->[email protected]("#", "")
( $token ) }
);
}
</text>}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState)) //..
{<text>
# @Raw(state.Comment)
sub match_token_at_@(state.Id) {
my ( $self, $token, $context ) = @@_;
@foreach(var transition in state.Transitions)
{
@:if ($self->@MatchToken(transition.TokenType)) {
if (transition.LookAheadHint != null)
{
@:if ($self->lookahead_@(transition.LookAheadHint.Id)($context, $token)) {
}
foreach(var production in transition.Productions)
{
@CallProduction(production)
}
@:return @transition.TargetState;
if (transition.LookAheadHint != null)
{
@:}
}
@:}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
}
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
sub lookahead_@(lookAheadHint.Id) {
my ($self, $context, $current_token) = @@_;
$current_token->detach();
my $token;
my @@queue;
my $match = 0;
while (1) {
$token = $context->read_token();
$token->detach;
push( @@queue, $token );
if (@foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>$self->@MatchToken(tokenType) || </text>}0) {
$match = 1;
last;
}
if (! (@foreach(var tokenType in lookAheadHint.Skip) {<text>$self->@MatchToken(tokenType) || </text>}0)) {
last;
}
}
$context->add_tokens( @@queue );
return $match;
}</text>}
1;