Skip to content

Format generated Python parser module #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: c53240a7f974b3707e13eac6710542cc96a2d61a # frozen: 24.10.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 89c421dff2e1026ba12cdb9ebd731f4a83aa8021 # frozen: v0.8.6
hooks:
- id: black
- id: ruff-format
files: "^python/"
- repo: https://github.com/asottile/pyupgrade
rev: 19364aa1b2ac289ce75e6bbe93994f7b4b9425f6 # frozen: v3.19.0
Expand Down
111 changes: 73 additions & 38 deletions python/gherkin-python.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
switch(production.Type)
{
case ProductionRuleType.Start:
@: self.start_rule(context, '@production.RuleName')
@: self.start_rule(context, "@production.RuleName")
break;
case ProductionRuleType.End:
@: self.end_rule(context, '@production.RuleName')
@: self.end_rule(context, "@production.RuleName")
break;
case ProductionRuleType.Process:
@: self.build(context, token)
@: self.build(context, token)
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text> state_comment = "State: @state.Id - @Raw(state.Comment)"
token.detach
expected_tokens = ["@Raw(string.Join("\", \"", expectedTokens))"]
error = UnexpectedEOFException(token, expected_tokens, state_comment) if token.eof() else UnexpectedTokenException(token, expected_tokens, state_comment)
{<text> state_comment = "State: @state.Id - @Raw(state.Comment)" # fmt: skip
token.detach()
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)
Expand All @@ -30,23 +40,28 @@ from __future__ import annotations

from collections import deque
from collections.abc import Callable
from typing import cast, TypeVar
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
from .parser_types import GherkinDocument
from .errors import UnexpectedEOFException, UnexpectedTokenException, ParserException, CompositeParserException

_T = TypeVar('_T')
_U = TypeVar('_U')
_V = TypeVar('_V')
_T = TypeVar("_T")
_U = TypeVar("_U")
_V = TypeVar("_V")

RULE_TYPE = [
'None',
"None",
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> '@rule.Name.Replace("#", "_")', # @rule.ToString(true)
{<text> "@rule.Name.Replace('#', '_')", # @rule.ToString(true)
</text>}
]

Expand Down Expand Up @@ -75,18 +90,18 @@ class @(Model.ParserClassName):
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
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(),
[])
context = ParserContext(token_scanner, token_matcher, deque(), [])

self.start_rule(context, '@Model.RuleSet.StartRule.Name')
self.start_rule(context, "@Model.RuleSet.StartRule.Name")
state = 0
token = None
while True:
Expand All @@ -95,7 +110,7 @@ class @(Model.ParserClassName):
if token.eof():
break

self.end_rule(context, '@Model.RuleSet.StartRule.Name')
self.end_rule(context, "@Model.RuleSet.StartRule.Name")

if context.errors:
raise CompositeParserException(context.errors)
Expand Down Expand Up @@ -123,8 +138,7 @@ class @(Model.ParserClassName):
def read_token(self, context: ParserContext) -> Token:
if context.token_queue:
return context.token_queue.popleft()
else:
return context.token_scanner.read()
return context.token_scanner.read()
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
def match_@(rule.Name.Replace("#", ""))(self, context: ParserContext, token: Token) -> bool:
Expand All @@ -133,20 +147,24 @@ class @(Model.ParserClassName):
@:if token.eof():
@: return False
}
return self.handle_external_error(context, False, token, context.token_matcher.match_@(rule.Name.Replace("#", "")))</text>
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]]= {
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 in state_map:
return state_map[state](token, context)
else:

if state not in state_map:
raise RuntimeError("Unknown state: " + str(state))

return state_map[state](token, context)
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{<text>
# @Raw(state.Comment)
Expand All @@ -157,39 +175,56 @@ class @(Model.ParserClassName):
if (transition.LookAheadHint != null)
{
@:if self.lookahead_@(transition.LookAheadHint.Id)(context, token):
}
foreach(var production in transition.Productions)
{
@CallProduction(production)
<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>
@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:
currentToken.detach
currentToken.detach()
token = None
queue = []
match = False
while True:
token = self.read_token(context)
token.detach
token.detach()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. we were not calling these methods, and still everything worked?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see now that the implemetation is a noop

queue.append(token)

if (@foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>self.@MatchToken(tokenType) or </text>}False):
if @foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>self.@MatchToken(tokenType)</text>}:
match = True
break

if not (@foreach(var tokenType in lookAheadHint.Skip) {<text>self.@MatchToken(tokenType) or </text>}False):
if not any(
(@foreach(var tokenType in lookAheadHint.Skip) {
<text>
self.@MatchToken(tokenType),</text>}

)
):
break

context.token_queue.extend(queue)

return match</text>
return match
</text>

}

# private
Expand Down
1 change: 0 additions & 1 deletion python/gherkin/ast_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class AstNode:

def __init__(self, rule_type: str) -> None:
self.rule_type = rule_type
self._sub_items: defaultdict[str, list[object]] = defaultdict(list)
Expand Down
1 change: 0 additions & 1 deletion python/gherkin/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@


class Dialect:

@classmethod
def for_name(cls, name: str) -> Self | None:
return cls(DIALECTS[name]) if name in DIALECTS else None
Expand Down
Loading
Loading