check_shapes.parser
¶
Module Contents¶
- check_shapes.parser._tree_children(tree)¶
Return all the children of tree that are trees themselves.
- Parameters:
tree (lark.tree.Tree[lark.lexer.Token]) –
- Return type:
Iterable[lark.tree.Tree[lark.lexer.Token]]
- check_shapes.parser._token_children(tree)¶
Return the values of all the children of tree that are tokens.
- Parameters:
tree (lark.tree.Tree[lark.lexer.Token]) –
- Return type:
Iterable[str]
- class check_shapes.parser._TreeVisitor¶
Bases:
abc.ABC
Functionality for visiting the nodes of parse-trees.
This differs from the classes built-in in Lark, in that it allows passing *args and **kwargs.
Subclasses should add methods with the same name as Lark rules. Those methods should take the parse tree of the rule, followed by any other *args and **kwargs you want. They may return anything.
- class check_shapes.parser._ParseSpec(source)¶
Bases:
_TreeVisitor
Functionality for visiting the nodes of parse-trees.
This differs from the classes built-in in Lark, in that it allows passing *args and **kwargs.
Subclasses should add methods with the same name as Lark rules. Those methods should take the parse tree of the rule, followed by any other *args and **kwargs you want. They may return anything.
- Parameters:
source (str) –
- class check_shapes.parser._RewritedocString(source, function_spec)¶
Bases:
_TreeVisitor
Functionality for visiting the nodes of parse-trees.
This differs from the classes built-in in Lark, in that it allows passing *args and **kwargs.
Subclasses should add methods with the same name as Lark rules. Those methods should take the parse tree of the rule, followed by any other *args and **kwargs you want. They may return anything.
- Parameters:
source (str) –
function_spec (check_shapes.specs.ParsedFunctionSpec) –
- _guess_indent(docstring)¶
Infer the level of indentation of a docstring.
Returns None if the indentation could not be inferred.
- Parameters:
docstring (str) –
- Return type:
Optional[int]
- class check_shapes.parser._CachedParser(grammar_filename, start_symbol, parser_name, re_terminal_descriptions, transformer_class, exception_class)¶
Small wrapper around Lark so that we can reuse as much code as possible between the different things we parse.
- Parameters:
grammar_filename (str) –
start_symbol (str) –
parser_name (str) –
re_terminal_descriptions (Mapping[str, str]) –
transformer_class (Type[_TreeVisitor]) –
exception_class (Type[check_shapes.exceptions.CheckShapesError]) –
- check_shapes.parser.parse_tensor_spec(tensor_spec, context)¶
Parse a check_shapes tensor specification.
- Parameters:
tensor_spec (str) –
context (check_shapes.error_contexts.ErrorContext) –
- Return type:
check_shapes.specs.ParsedTensorSpec
- check_shapes.parser.parse_function_spec(function_spec, context)¶
Parse all check_shapes argument or note specification for a single function.
- Parameters:
function_spec (Sequence[str]) –
context (check_shapes.error_contexts.ErrorContext) –
- Return type:
check_shapes.specs.ParsedFunctionSpec
- check_shapes.parser.parse_and_rewrite_docstring(docstring, function_spec, context)¶
Rewrite docstring to include the shapes specified by the argument_specs.
- Parameters:
docstring (Optional[str]) –
function_spec (check_shapes.specs.ParsedFunctionSpec) –
context (check_shapes.error_contexts.ErrorContext) –
- Return type:
Optional[str]