ast_utils, grammar, and indentergm/2021-09-23T00Z/github.com--lark-parser-lark/1.0b
| @@ -1,17 +0,0 @@ | |||
| import types | |||
| from typing import Optional | |||
| from .visitors import Transformer | |||
| class Ast(object): | |||
| pass | |||
| class AsList(object): | |||
| pass | |||
| def create_transformer( | |||
| ast_module: types.ModuleType, | |||
| transformer: Optional[Transformer]=None | |||
| ) -> Transformer: | |||
| ... | |||
| @@ -1,14 +0,0 @@ | |||
| from typing import Optional, Tuple | |||
| class RuleOptions: | |||
| keep_all_tokens: bool | |||
| expand1: bool | |||
| priority: int | |||
| template_source: Optional[str] | |||
| empty_indices: Tuple[bool, ...] | |||
| class Symbol: | |||
| name: str | |||
| is_term: bool | |||
| @@ -1,47 +0,0 @@ | |||
| # -*- coding: utf-8 -*- | |||
| from typing import Tuple, List, Iterator, Optional | |||
| from abc import ABC, abstractmethod | |||
| from .lexer import Token | |||
| from .lark import PostLex | |||
| class Indenter(PostLex, ABC): | |||
| paren_level: Optional[int] | |||
| indent_level: Optional[List[int]] | |||
| def __init__(self) -> None: | |||
| ... | |||
| def handle_NL(self, token: Token) -> Iterator[Token]: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def NL_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def OPEN_PAREN_types(self) -> List[str]: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def CLOSE_PAREN_types(self) -> List[str]: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def INDENT_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def DEDENT_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def tab_len(self) -> int: | |||
| ... | |||
| @@ -3,6 +3,8 @@ | |||
| """ | |||
| import inspect, re | |||
| import types | |||
| from typing import Optional | |||
| from lark import Transformer, v_args | |||
| @@ -27,7 +29,7 @@ def _call(func, _data, children, _meta): | |||
| inline = v_args(wrapper=_call) | |||
| def create_transformer(ast_module, transformer=None): | |||
| def create_transformer(ast_module: types.ModuleType, transformer: Optional[Transformer]=None) -> Transformer: | |||
| """Collects `Ast` subclasses from the given module, and creates a Lark transformer that builds the AST. | |||
| For each class, we create a corresponding rule in the transformer, with a matching name. | |||
| @@ -49,4 +51,4 @@ def create_transformer(ast_module, transformer=None): | |||
| setattr(t, camel_to_snake(name), obj) | |||
| return t | |||
| return t | |||
| @@ -1,3 +1,5 @@ | |||
| from typing import Optional, Tuple | |||
| from .utils import Serialize | |||
| ###{standalone | |||
| @@ -5,10 +7,10 @@ from .utils import Serialize | |||
| class Symbol(Serialize): | |||
| __slots__ = ('name',) | |||
| is_term = NotImplemented | |||
| is_term: bool = NotImplemented | |||
| def __init__(self, name): | |||
| self.name = name | |||
| self.name: str = name | |||
| def __eq__(self, other): | |||
| assert isinstance(other, Symbol), other | |||
| @@ -50,11 +52,11 @@ class RuleOptions(Serialize): | |||
| __serialize_fields__ = 'keep_all_tokens', 'expand1', 'priority', 'template_source', 'empty_indices' | |||
| def __init__(self, keep_all_tokens=False, expand1=False, priority=None, template_source=None, empty_indices=()): | |||
| self.keep_all_tokens = keep_all_tokens | |||
| self.expand1 = expand1 | |||
| self.priority = priority | |||
| self.template_source = template_source | |||
| self.empty_indices = empty_indices | |||
| self.keep_all_tokens: bool = keep_all_tokens | |||
| self.expand1: bool = expand1 | |||
| self.priority: int = priority | |||
| self.template_source: Optional[str] = template_source | |||
| self.empty_indices: Tuple[bool, ...] = empty_indices | |||
| def __repr__(self): | |||
| return 'RuleOptions(%r, %r, %r, %r)' % ( | |||
| @@ -1,5 +1,8 @@ | |||
| "Provides Indentation services for languages with indentation similar to Python" | |||
| from abc import ABC, abstractmethod | |||
| from typing import Tuple, List, Iterator, Optional | |||
| from .exceptions import LarkError | |||
| from .lark import PostLex | |||
| from .lexer import Token | |||
| @@ -8,13 +11,13 @@ from .lexer import Token | |||
| class DedentError(LarkError): | |||
| pass | |||
| class Indenter(PostLex): | |||
| def __init__(self): | |||
| self.paren_level = None | |||
| self.indent_level = None | |||
| class Indenter(PostLex, ABC): | |||
| def __init__(self) -> None: | |||
| self.paren_level: Optional[int] = None | |||
| self.indent_level: Optional[List[int]] = None | |||
| assert self.tab_len > 0 | |||
| def handle_NL(self, token): | |||
| def handle_NL(self, token: Token) -> Iterator[Token]: | |||
| if self.paren_level > 0: | |||
| return | |||
| @@ -64,4 +67,34 @@ class Indenter(PostLex): | |||
| def always_accept(self): | |||
| return (self.NL_type,) | |||
| @property | |||
| @abstractmethod | |||
| def NL_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def OPEN_PAREN_types(self) -> List[str]: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def CLOSE_PAREN_types(self) -> List[str]: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def INDENT_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def DEDENT_type(self) -> str: | |||
| ... | |||
| @property | |||
| @abstractmethod | |||
| def tab_len(self) -> int: | |||
| ... | |||
| ###} | |||