-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathgherkin-python.razor
More file actions
262 lines (228 loc) · 8.02 KB
/
Copy pathgherkin-python.razor
File metadata and controls
262 lines (228 loc) · 8.02 KB
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
@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> state_comment = "State: @state.Id - @Raw(state.Comment)" # fmt: skip
expected_tokens = [
</text>
@foreach (var token in expectedTokens)
{<text> "@token",
</text>
}
<text> ]
error = (
UnexpectedEOFException(token, expected_tokens, state_comment)
if token.eof()
else UnexpectedTokenException(token, expected_tokens, state_comment)
)
if self.stop_at_first_error:
raise error
self.add_error(context, error)
return @state.Id</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(tokenType)(context, token)</text>}
# This file is generated. Do not edit! Edit gherkin-python.razor instead.
from __future__ import annotations
from collections import deque
from collections.abc import Callable
from typing import TypeVar, cast
from .ast_builder import AstBuilder
from .errors import (
CompositeParserException,
ParserException,
UnexpectedEOFException,
UnexpectedTokenException,
)
from .parser_types import GherkinDocument
from .token import Token
from .token_matcher import TokenMatcher
from .token_scanner import TokenScanner
_T = TypeVar("_T")
_U = TypeVar("_U")
_V = TypeVar("_V")
RULE_TYPE = [
"None",
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> "@rule.Name.Replace('#', '_')", # @rule.ToString(true)
</text>}
]
class ParserContext:
def __init__(
self,
token_scanner: TokenScanner,
token_matcher: TokenMatcher,
token_queue: deque[Token],
errors: list[ParserException],
) -> None:
self.token_scanner = token_scanner
self.token_matcher = token_matcher
self.token_queue = token_queue
self.errors = errors
class @(Model.ParserClassName):
def __init__(self, ast_builder: AstBuilder | None = None) -> None:
self.ast_builder = ast_builder if ast_builder is not None else AstBuilder()
self.stop_at_first_error = False
def parse(
self,
token_scanner_or_str: TokenScanner | str,
token_matcher: TokenMatcher | None = None,
) -> GherkinDocument:
token_scanner = (
TokenScanner(token_scanner_or_str)
if isinstance(token_scanner_or_str, str)
else token_scanner_or_str
)
self.ast_builder.reset()
if token_matcher is None:
token_matcher = TokenMatcher()
token_matcher.reset()
context = ParserContext(token_scanner, token_matcher, deque(), [])
self.start_rule(context, "@Model.RuleSet.StartRule.Name")
state = 0
token = None
while True:
token = self.read_token(context)
state = self.match_token(state, token, context)
if token.eof():
break
self.end_rule(context, "@Model.RuleSet.StartRule.Name")
if context.errors:
raise CompositeParserException(context.errors)
return cast(GherkinDocument, self.get_result())
def build(self, context: ParserContext, token: Token) -> None:
self.handle_ast_error(context, token, self.ast_builder.build)
def add_error(self, context: ParserContext, error: ParserException) -> None:
if str(error) not in (str(e) for e in context.errors):
context.errors.append(error)
if len(context.errors) > 10:
raise CompositeParserException(context.errors)
def start_rule(self, context: ParserContext, rule_type: str) -> None:
self.handle_ast_error(context, rule_type, self.ast_builder.start_rule)
def end_rule(self, context: ParserContext, rule_type: str) -> None:
self.handle_ast_error(context, rule_type, self.ast_builder.end_rule)
def get_result(self) -> object:
return self.ast_builder.get_result()
def read_token(self, context: ParserContext) -> Token:
if context.token_queue:
return context.token_queue.popleft()
return context.token_scanner.read()
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
def match_@(rule.Name.Replace("#", ""))(self, context: ParserContext, token: Token) -> bool:
@if (rule.Name != "#EOF")
{
@:if token.eof():
@: return False
}
return self.handle_external_error(
context,
False,
token,
context.token_matcher.match_@(rule.Name.Replace("#", "")),
)
</text>
}
def match_token(self, state: int, token: Token, context: ParserContext) -> int:
state_map: dict[int, Callable[[Token, ParserContext], int]] = {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@: @state.Id: self.match_token_at_@(state.Id),
}
}
if state not in state_map:
raise RuntimeError(f"Unknown state: {state}")
return state_map[state](token, context)
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{<text>
# @Raw(state.Comment)
def match_token_at_@(state.Id)(self, token: Token, context: ParserContext) -> int:
@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)
{
<text> </text>@CallProduction(production)
}
@:return @transition.TargetState
} else
{
foreach(var production in transition.Productions)
{
<text> </text>@CallProduction(production)
}
@:return @transition.TargetState
}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
def lookahead_@(lookAheadHint.Id)(self, context: ParserContext, currentToken: Token) -> bool:
token = None
queue = []
match = False
# Effectively do-while
continue_lookahead = True
while continue_lookahead:
token = self.read_token(context)
queue.append(token)
@foreach(var tokenType in lookAheadHint.ExpectedTokens) {
<text>
if self.@MatchToken(tokenType):
match = True
break</text>
}
continue_lookahead = False
@foreach(var tokenType in lookAheadHint.Skip) {
<text>
if self.@MatchToken(tokenType):
continue_lookahead = True
continue</text>
}
context.token_queue.extend(queue)
return match
</text>
}
# private
def handle_ast_error(
self,
context: ParserContext,
argument: _T,
action: Callable[[_T], object],
) -> None:
self.handle_external_error(context, True, argument, action)
def handle_external_error(
self,
context: ParserContext,
default_value: _U,
argument: _T,
action: Callable[[_T], _V],
) -> _V | _U:
if self.stop_at_first_error:
return action(argument)
try:
return action(argument)
except CompositeParserException as e:
for error in e.errors:
self.add_error(context, error)
except ParserException as e:
self.add_error(context, e)
return default_value