4
4
switch (production .Type )
5
5
{
6
6
case ProductionRuleType .Start :
7
- @: self .start_rule (context , ' @production.RuleName' )
7
+ @: self .start_rule (context , " @production.RuleName" )
8
8
break ;
9
9
case ProductionRuleType .End :
10
- @: self .end_rule (context , ' @production.RuleName' )
10
+ @: self .end_rule (context , " @production.RuleName" )
11
11
break ;
12
12
case ProductionRuleType .Process :
13
- @: self .build (context , token )
13
+ @: self .build (context , token )
14
14
break ;
15
15
}
16
16
}
17
17
@helper HandleParserError(IEnumerable<string > expectedTokens, State state)
18
- { <text > state_comment = " State: @state.Id - @Raw(state.Comment)"
18
+ { <text > state_comment = " State: @state.Id - @Raw(state.Comment)" # fmt : skip
19
19
token .detach
20
- expected_tokens = [" @Raw(string.Join(" \" , \" " , expectedTokens ))" ]
21
- error = UnexpectedEOFException (token , expected_tokens , state_comment ) if token .eof () else UnexpectedTokenException (token , expected_tokens , state_comment )
20
+ expected_tokens = [
21
+ < / text >
22
+ @foreach (var token in expectedTokens )
23
+ {< text > " @token" ,
24
+ < / text >
25
+ }
26
+ < text > ]
27
+ error = (
28
+ UnexpectedEOFException (token , expected_tokens , state_comment )
29
+ if token .eof ()
30
+ else UnexpectedTokenException (token , expected_tokens , state_comment )
31
+ )
22
32
if self .stop_at_first_error :
23
33
raise error
24
34
self .add_error (context , error )
@@ -30,23 +40,28 @@ from __future__ import annotations
30
40
31
41
from collections import deque
32
42
from collections.abc import Callable
33
- from typing import cast , TypeVar
43
+ from typing import TypeVar, cast
34
44
35
45
from .ast_builder import AstBuilder
46
+ from .errors import (
47
+ CompositeParserException,
48
+ ParserException,
49
+ UnexpectedEOFException,
50
+ UnexpectedTokenException,
51
+ )
52
+ from .parser_types import GherkinDocument
36
53
from .token import Token
37
54
from .token_matcher import TokenMatcher
38
55
from .token_scanner import TokenScanner
39
- from .parser_types import GherkinDocument
40
- from .errors import UnexpectedEOFException , UnexpectedTokenException , ParserException , CompositeParserException
41
56
42
- _T = TypeVar ('_T' )
43
- _U = TypeVar ('_U' )
44
- _V = TypeVar ('_V' )
57
+ _T = TypeVar("_T" )
58
+ _U = TypeVar("_U" )
59
+ _V = TypeVar("_V" )
45
60
46
61
RULE_TYPE = [
47
- ' None' ,
62
+ " None" ,
48
63
@foreach( var rule in Model .RuleSet .Where (r => ! r .TempRule ))
49
- {< text > ' @rule.Name.Replace("#", "_")' , # @rule .ToString (true )
64
+ { <text > " @rule.Name.Replace('#', '_') " , # @rule.ToString(true)
50
65
</text >}
51
66
]
52
67
@@ -75,18 +90,18 @@ class @(Model.ParserClassName):
75
90
token_scanner_or_str: TokenScanner | str,
76
91
token_matcher: TokenMatcher | None = None,
77
92
) -> GherkinDocument:
78
- token_scanner = TokenScanner (token_scanner_or_str ) if isinstance (token_scanner_or_str , str ) else token_scanner_or_str
93
+ token_scanner = (
94
+ TokenScanner(token_scanner_or_str)
95
+ if isinstance(token_scanner_or_str, str)
96
+ else token_scanner_or_str
97
+ )
79
98
self.ast_builder.reset()
80
99
if token_matcher is None:
81
100
token_matcher = TokenMatcher()
82
101
token_matcher.reset()
83
- context = ParserContext (
84
- token_scanner ,
85
- token_matcher ,
86
- deque (),
87
- [])
102
+ context = ParserContext(token_scanner, token_matcher, deque(), [])
88
103
89
- self .start_rule (context , ' @Model.RuleSet.StartRule.Name' )
104
+ self.start_rule(context, " @Model.RuleSet.StartRule.Name " )
90
105
state = 0
91
106
token = None
92
107
while True:
@@ -95,7 +110,7 @@ class @(Model.ParserClassName):
95
110
if token.eof():
96
111
break
97
112
98
- self .end_rule (context , ' @Model.RuleSet.StartRule.Name' )
113
+ self.end_rule(context, " @Model.RuleSet.StartRule.Name " )
99
114
100
115
if context.errors:
101
116
raise CompositeParserException(context.errors)
@@ -123,8 +138,7 @@ class @(Model.ParserClassName):
123
138
def read_token(self, context: ParserContext) -> Token:
124
139
if context.token_queue:
125
140
return context.token_queue.popleft()
126
- else :
127
- return context .token_scanner .read ()
141
+ return context.token_scanner.read()
128
142
@foreach( var rule in Model .RuleSet .TokenRules )
129
143
{ <text >
130
144
def match_ @( rule .Name .Replace (" #" , " " )) (self , context : ParserContext , token : Token ) -> bool :
@@ -133,20 +147,24 @@ class @(Model.ParserClassName):
133
147
@: if token .eof ():
134
148
@: return False
135
149
}
136
- return self .handle_external_error (context , False , token , context .token_matcher .match_ @(rule .Name .Replace (" #" , " " )))< / text >
150
+ return self .handle_external_error (
151
+ context , False , token , context .token_matcher .match_ @(rule .Name .Replace (" #" , " " ))
152
+ )
153
+ < / text >
137
154
}
138
155
139
156
def match_token(self, state: int, token: Token, context: ParserContext) -> int:
140
- state_map : dict [int , Callable [[Token , ParserContext ], int ]]= {
157
+ state_map: dict[int, Callable[[Token, ParserContext], int]] = {
141
158
@foreach( var state in Model .States .Values .Where (s => ! s .IsEndState ))
142
159
{
143
160
@: @state.Id : self .match_token_at_ @( state .Id ) ,
144
161
}
145
162
}
146
- if state in state_map :
147
- return state_map [state ](token , context )
148
- else :
163
+
164
+ if state not in state_map:
149
165
raise RuntimeError("Unknown state: " + str(state))
166
+
167
+ return state_map[state](token, context)
150
168
@foreach( var state in Model .States .Values .Where (s => ! s .IsEndState ))
151
169
{ <text >
152
170
# @Raw(state.Comment)
@@ -157,15 +175,24 @@ class @(Model.ParserClassName):
157
175
if (transition .LookAheadHint != null )
158
176
{
159
177
@: if self .lookahead_ @(transition .LookAheadHint .Id )(context , token ):
160
- }
161
178
foreach (var production in transition .Productions )
162
179
{
163
- @CallProduction (production )
180
+ < text > < / text > @CallProduction (production )
164
181
}
165
182
@: return @transition .TargetState
183
+ } else
184
+ {
185
+ foreach (var production in transition .Productions )
186
+ {
187
+ <text > </text >@CallProduction(production)
188
+ }
189
+ @: return @transition .TargetState
190
+ }
166
191
}
167
192
168
- @HandleParserError (state .Transitions .Select (t => " #" + t .TokenType .ToString ()).Distinct (), state )< / text >
193
+ @HandleParserError(state.Transitions.Select(t => " #" + t .TokenType .ToString ()).Distinct (), state )
194
+
195
+ </text >
169
196
}
170
197
@foreach( var lookAheadHint in Model .RuleSet .LookAheadHints )
171
198
{
@@ -180,16 +207,24 @@ class @(Model.ParserClassName):
180
207
token .detach
181
208
queue .append (token )
182
209
183
- if ( @foreach (var tokenType in lookAheadHint .ExpectedTokens ) {< text > self .@MatchToken (tokenType ) or < / text > }False ) :
210
+ if @foreach (var tokenType in lookAheadHint .ExpectedTokens ) {< text > self .@MatchToken (tokenType )< / text > }:
184
211
match = True
185
212
break
186
213
187
- if not (@foreach (var tokenType in lookAheadHint .Skip ) {< text > self .@MatchToken (tokenType ) or < / text > }False ):
214
+ if not any (
215
+ (@foreach (var tokenType in lookAheadHint .Skip ) {
216
+ < text >
217
+ self .@MatchToken (tokenType ),< / text > }
218
+
219
+ )
220
+ ):
188
221
break
189
222
190
223
context .token_queue .extend (queue )
191
224
192
- return match < / text >
225
+ return match
226
+ < / text >
227
+
193
228
}
194
229
195
230
# private
0 commit comments