Skip to content
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

Parsimonious stubs #7477

Merged
merged 18 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions stubs/parsimonious/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parsimonious.nodes.RuleDecoratorMeta.__new__
1 change: 1 addition & 0 deletions stubs/parsimonious/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "0.8.*"
8 changes: 8 additions & 0 deletions stubs/parsimonious/parsimonious/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from parsimonious.exceptions import (
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
BadGrammar as BadGrammar,
IncompleteParseError as IncompleteParseError,
ParseError as ParseError,
UndefinedLabel as UndefinedLabel,
)
from parsimonious.grammar import Grammar as Grammar, TokenGrammar as TokenGrammar
from parsimonious.nodes import NodeVisitor as NodeVisitor, rule as rule
24 changes: 24 additions & 0 deletions stubs/parsimonious/parsimonious/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from parsimonious.expressions import Expression as Expression
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
from parsimonious.grammar import LazyReference as LazyReference
from parsimonious.nodes import Node as Node
from parsimonious.utils import StrAndRepr as StrAndRepr

class ParseError(StrAndRepr, Exception):
text: str
pos: int
expr: Expression | None
def __init__(self, text: str, pos: int = ..., expr: Expression | None = ...) -> None: ...
def line(self) -> int: ...
def column(self) -> int: ...

class IncompleteParseError(ParseError): ...

class VisitationError(Exception):
original_class: type[BaseException]
def __init__(self, exc: BaseException, exc_class: type[BaseException], node: Node) -> None: ...

class BadGrammar(StrAndRepr, Exception): ...

class UndefinedLabel(BadGrammar):
label: LazyReference
def __init__(self, label: LazyReference) -> None: ...
68 changes: 68 additions & 0 deletions stubs/parsimonious/parsimonious/expressions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import typing
from typing import Any, Callable, Pattern

from parsimonious.exceptions import IncompleteParseError as IncompleteParseError, ParseError as ParseError
from parsimonious.grammar import Grammar as Grammar
from parsimonious.nodes import Node as Node, RegexNode as RegexNode
from parsimonious.utils import StrAndRepr as StrAndRepr

MARKER: Any

def is_callable(value: Any) -> bool: ...

_CALLABLE_RETURN_TYPE = int | tuple[int, list[Node]] | Node | None
_CALLABLE_TYPE = (
Callable[[str, int], _CALLABLE_RETURN_TYPE]
| Callable[[str, int, dict[tuple[int, int], Node], ParseError, Grammar], _CALLABLE_RETURN_TYPE]
| Callable[[Any, str, int], _CALLABLE_RETURN_TYPE]
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
| Callable[[Any, str, int, dict[tuple[int, int], Node], ParseError, Grammar], _CALLABLE_RETURN_TYPE]
)

def expression(callable: _CALLABLE_TYPE, rule_name: str, grammar: Grammar) -> Expression: ...

class Expression(StrAndRepr):
name: str
identity_tuple: tuple[str]
def __init__(self, name: str = ...) -> None: ...
def parse(self, text: str, pos: int = ...) -> Node: ...
def match(self, text: str, pos: int = ...) -> Node: ...
def match_core(self, text: str, pos: int, cache: dict[tuple[int, int], Node], error: ParseError) -> Node: ...
def as_rule(self) -> str: ...

class Literal(Expression):
literal: str
identity_tuple: tuple[str, str]
def __init__(self, literal: str, name: str = ...) -> None: ...

class TokenMatcher(Literal): ...

class Regex(Expression):
re: Pattern[str]
identity_tuple: tuple[str, Pattern[str]]
def __init__(
self,
pattern: str,
name: str = ...,
ignore_case: bool = ...,
locale: bool = ...,
multiline: bool = ...,
dot_all: bool = ...,
unicode: bool = ...,
verbose: bool = ...,
ascii: bool = ...,
) -> None: ...

class Compound(Expression):
members: typing.Sequence[Expression]
def __init__(self, *members: typing.Sequence[Expression], **kwargs: Any) -> None: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved

class Sequence(Compound): ...
class OneOf(Compound): ...
class Lookahead(Compound): ...
class Not(Compound): ...
class Optional(Compound): ...
class ZeroOrMore(Compound): ...

class OneOrMore(Compound):
min: int
def __init__(self, member: Expression, name: str = ..., min: int = ...) -> None: ...
69 changes: 69 additions & 0 deletions stubs/parsimonious/parsimonious/grammar.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from collections import OrderedDict
from typing import Any, Callable, Mapping, NoReturn
from typing_extensions import Self

from parsimonious.exceptions import BadGrammar as BadGrammar, UndefinedLabel as UndefinedLabel
from parsimonious.expressions import (
_CALLABLE_TYPE as _CALLABLE_TYPE,
Expression as Expression,
Literal as Literal,
Lookahead as Lookahead,
Not as Not,
OneOf as OneOf,
OneOrMore as OneOrMore,
Optional as Optional,
Regex as Regex,
Sequence as Sequence,
TokenMatcher as TokenMatcher,
ZeroOrMore as ZeroOrMore,
expression as expression,
is_callable as is_callable,
)
from parsimonious.nodes import Node as Node, NodeVisitor as NodeVisitor
from parsimonious.utils import evaluate_string as evaluate_string
from six import text_type
jpy-git marked this conversation as resolved.
Show resolved Hide resolved

class Grammar(OrderedDict[str, Expression]):
default_rule: Expression | Any
def __init__(self, rules: str = ..., **more_rules: Expression | _CALLABLE_TYPE) -> None: ...
def default(self, rule_name: str) -> Grammar: ...
def parse(self, text: str, pos: int = ...) -> Node: ...
def match(self, text: str, pos: int = ...) -> Node: ...

class TokenGrammar(Grammar): ...
class BootstrappingGrammar(Grammar): ...

rule_syntax: str

class LazyReference(text_type):
name: str

class RuleVisitor(NodeVisitor):
quantifier_classes: dict[str, Expression]
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
visit_expression: Callable[[Self, Node, list[Any]], Any]
visit_term: Callable[[Self, Node, list[Any]], Any]
visit_atom: Callable[[Self, Node, list[Any]], Any]
custom_rules: dict[str, Expression]
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, custom_rules: Mapping[str, Expression] | None = ...) -> None: ...
def visit_rules(self, node: Node, rules_list: list[Any]) -> tuple[OrderedDict[str, Expression], Expression | None]: ...
def visit_rule(self, node: Node, rule: list[Any]) -> Expression: ...
def visit_label(self, node: Node, label: list[Any]) -> str: ...
def visit_ored(self, node: Node, ored: list[Any]) -> OneOf: ...
def visit_or_term(self, node: Node, or_term: list[Any]) -> Expression: ...
def visit_sequence(self, node: Node, sequence: list[Any]) -> Sequence: ...
def visit_not_term(self, node: Node, not_term: list[Any]) -> Not: ...
def visit_lookahead_term(self, node: Node, lookahead_term: list[Any]) -> Lookahead: ...
def visit_quantified(self, node: Node, quantified: list[Any]) -> Expression: ...
def visit_quantifier(self, node: Node, quantifier: list[Any]) -> Node: ...
def visit_reference(self, node: Node, reference: list[Any]) -> LazyReference: ...
def visit_literal(self, node: Node, literal: list[Any]) -> Literal: ...
def visit_spaceless_literal(self, spaceless_literal: Node, visited_children: list[Any]) -> Literal: ...
def visit_regex(self, node: Node, regex: list[Any]) -> Regex: ...
def visit_parenthesized(self, node: Node, parenthesized: list[Any]) -> Expression: ...
def generic_visit(self, node: Node, visited_children: list[Any]) -> list[Any] | Node: ...

class TokenRuleVisitor(RuleVisitor):
def visit_spaceless_literal(self, spaceless_literal: Node, visited_children: list[Any]) -> TokenMatcher: ...
def visit_regex(self, node: Node, regex: list[Any]) -> NoReturn: ...

rule_grammar: Grammar
40 changes: 40 additions & 0 deletions stubs/parsimonious/parsimonious/nodes.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from re import Match
from typing import Any, Callable, Iterator, NoReturn, TypeVar
from typing_extensions import Self
jpy-git marked this conversation as resolved.
Show resolved Hide resolved

from parsimonious.exceptions import UndefinedLabel as UndefinedLabel, VisitationError as VisitationError
from parsimonious.expressions import Expression as Expression
from parsimonious.grammar import Grammar as Grammar

class Node:
expr: Expression
full_text: str
start: int
end: int
children: list[Node]
def __init__(self, expr: Expression, full_text: str, start: int, end: int, children: list[Node] | None = ...) -> None: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
@property
def expr_name(self) -> str: ...
def __iter__(self) -> Iterator[Node]: ...
@property
def text(self) -> str: ...
def prettily(self, error: Node | None = ...) -> str: ...

class RegexNode(Node):
match: Match[str]

class RuleDecoratorMeta(type):
def __new__(metaclass: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Self: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
jpy-git marked this conversation as resolved.
Show resolved Hide resolved

class NodeVisitor(type):
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
grammar: Grammar | Any
unwrapped_exceptions: tuple[Exception]
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
def visit(self, node: Node) -> Any: ...
def generic_visit(self, node: Node, visited_children: list[Any]) -> NoReturn: ...
def parse(self, text: str, pos: int = ...) -> Node: ...
def match(self, text: str, pos: int = ...) -> Node: ...
def lift_child(self, node: Node, children: list[Any]) -> Any: ...

_T = TypeVar("_T")

def rule(rule_string: str) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions stubs/parsimonious/parsimonious/utils.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ast
from typing import Any

class StrAndRepr: ...

def evaluate_string(string: str | ast.AST) -> Any: ...

class Token(StrAndRepr):
type: str
def __init__(self, type: str) -> None: ...