-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgherkin-perl.razor
163 lines (134 loc) · 4.15 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
@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;
$err = $self->_construct_parser_error(
$token,
["@Raw(string.Join("\", \"", expectedTokens))"],
"State: @state.Id - @Raw(state.Comment)",
);
$self->add_error( $context, $err );
return @state.Id;
</text>}
@helper MatchToken(TokenType tokenType)
{<text>$context->token_matcher->match_@(tokenType)($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 %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 );
}
sub _construct_parser_error {
my ($self, $token, @@args) = @@_;
my $error_class = "Gherkin::Exceptions::" . (
$token->is_eof ? 'UnexpectedEOF' : 'UnexpectedToken' );
return $error_class->new( $token, @@args );
}
@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 ) = @@_;
my ( $ok, $err );
@foreach(var transition in state.Transitions)
{
@:($ok, $err) = @MatchToken(transition.TokenType);
@:if ($ok) {
@:$self->add_error( $context, $err ) if $err;
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;
my $ok;
while (1) {
$token = $context->read_token();
$token->detach;
push( @@queue, $token );
@foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>
($match) = @MatchToken(tokenType);
last if $match;</text>}
@foreach(var tokenType in lookAheadHint.Skip) {<text>
($ok) = @MatchToken(tokenType);
next if $ok;</text>
}
last;
}
$context->add_tokens( @@queue );
return $match;
}</text>}
1;