-
-
Notifications
You must be signed in to change notification settings - Fork 414
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
Fix for PR #1149 #1150
Merged
Merged
Fix for PR #1149 #1150
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,18 @@ | |
from lark import Tree, Transformer | ||
from lark.exceptions import MissingVariableError | ||
|
||
Branch = Union[Tree[str], str] | ||
TreeOrCode = Union[Tree[str], str] | ||
MatchResult = Dict[str, Tree] | ||
_TEMPLATE_MARKER = '$' | ||
|
||
|
||
class TemplateConf: | ||
"""Template Configuration | ||
|
||
Allows customization for different uses of Template | ||
|
||
parse() must return a Tree instance. | ||
""" | ||
|
||
def __init__(self, parse=None): | ||
|
@@ -49,15 +53,21 @@ def _get_tree(self, template: TreeOrCode) -> Tree[str]: | |
assert self._parse | ||
template = self._parse(template) | ||
|
||
assert isinstance(template, Tree) | ||
if not isinstance(template, Tree): | ||
raise TypeError("template parser must return a Tree instance") | ||
|
||
return template | ||
|
||
def __call__(self, template: Tree[str]) -> 'Template': | ||
return Template(template, conf=self) | ||
|
||
def _match_tree_template(self, template: TreeOrCode, tree: TreeOrCode) -> Optional[Dict[str, TreeOrCode]]: | ||
def _match_tree_template(self, template: TreeOrCode, tree: Branch) -> Optional[MatchResult]: | ||
"""Returns dict of {var: match} if found a match, else None | ||
""" | ||
template_var = self.test_var(template) | ||
if template_var: | ||
if not isinstance(tree, Tree): | ||
raise TypeError(f"Template variables can only match Tree instances. Not {tree}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return {template_var: tree} | ||
|
||
if isinstance(template, str): | ||
|
@@ -100,7 +110,7 @@ def __default__(self, data, children, meta) -> Tree[str]: | |
|
||
|
||
class Template: | ||
"""Represents a tree templates, tied to a specific configuration | ||
"""Represents a tree template, tied to a specific configuration | ||
|
||
A tree template is a tree that contains nodes that are template variables. | ||
Those variables will match any tree. | ||
|
@@ -111,7 +121,7 @@ def __init__(self, tree: Tree[str], conf: TemplateConf = TemplateConf()): | |
self.conf = conf | ||
self.tree = conf._get_tree(tree) | ||
|
||
def match(self, tree: TreeOrCode) -> Optional[Dict[str, TreeOrCode]]: | ||
def match(self, tree: TreeOrCode) -> Optional[MatchResult]: | ||
"""Match a tree template to a tree. | ||
|
||
A tree template without variables will only match ``tree`` if it is equal to the template. | ||
|
@@ -127,7 +137,7 @@ def match(self, tree: TreeOrCode) -> Optional[Dict[str, TreeOrCode]]: | |
tree = self.conf._get_tree(tree) | ||
return self.conf._match_tree_template(self.tree, tree) | ||
|
||
def search(self, tree: TreeOrCode) -> Iterator[Tuple[Tree[str], Dict[str, TreeOrCode]]]: | ||
def search(self, tree: TreeOrCode) -> Iterator[Tuple[Tree[str], MatchResult]]: | ||
"""Search for all occurances of the tree template inside ``tree``. | ||
""" | ||
tree = self.conf._get_tree(tree) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[build-system] | ||
requires = ["setuptools>=42"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
|
||
[tool.mypy] | ||
files = "lark" | ||
python_version = "3.6" | ||
show_error_codes = true | ||
enable_error_code = ["ignore-without-code"] | ||
exclude = [ | ||
"^lark/__pyinstaller", | ||
] | ||
|
||
# You can disable imports or control per-module/file settings here | ||
[[tool.mypy.overrides]] | ||
module = [ "js2py" ] | ||
ignore_missing_imports = true |
Submodule nearley
updated
from a46b37 to 326831
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
[tox] | ||
envlist = py36, py37, py38, py39, pypy, pypy3 | ||
skip_missing_interpreters=true | ||
envlist = py36, py37, py38, py39, py310, pypy3, type | ||
skip_missing_interpreters = true | ||
|
||
[testenv] | ||
whitelist_externals = git | ||
deps = | ||
-rtest-requirements.txt | ||
passenv = | ||
TERM | ||
|
||
# to always force recreation and avoid unexpected side effects | ||
recreate=True | ||
recreate = True | ||
|
||
commands= | ||
commands = | ||
git submodule sync -q | ||
git submodule update --init | ||
python -m tests {posargs} | ||
|
||
[testenv:type] | ||
description = run type check on code base | ||
skip_install = true | ||
recreate = false | ||
deps = | ||
mypy==0.950 | ||
types-atomicwrites | ||
types-regex | ||
rich | ||
commands = | ||
mypy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be annotated with
Callable[[str], Tree]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but there's a lot of "could be" to add there. We can do that in a different PR.