-
-
Notifications
You must be signed in to change notification settings - Fork 432
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
Tree Templates (first draft) + py3to2 example #1006
Merged
Merged
Changes from all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Python 3 to Python 2 converter (tree templates) | ||
=============================================== | ||
|
||
This example demonstrates how to translate between two trees using tree templates. | ||
It parses Python 3, translates it to a Python 2 AST, and then outputs the result as Python 2 code. | ||
|
||
Uses reconstruct_python.py for generating the final Python 2 code. | ||
""" | ||
|
||
|
||
from lark import Lark | ||
from lark.tree_templates import TemplateConf, TemplateTranslator | ||
|
||
from lark.indenter import PythonIndenter | ||
from reconstruct_python import PythonReconstructor | ||
|
||
|
||
# | ||
# 1. Define a Python parser that also accepts template vars in the code (in the form of $var) | ||
# | ||
TEMPLATED_PYTHON = r""" | ||
%import python (single_input, file_input, eval_input, atom, var, stmt, expr, testlist_star_expr, _NEWLINE, _INDENT, _DEDENT, COMMENT, NAME) | ||
|
||
%extend atom: TEMPLATE_NAME -> var | ||
|
||
TEMPLATE_NAME: "$" NAME | ||
|
||
?template_start: (stmt | testlist_star_expr _NEWLINE) | ||
|
||
%ignore /[\t \f]+/ // WS | ||
%ignore /\\[\t \f]*\r?\n/ // LINE_CONT | ||
%ignore COMMENT | ||
""" | ||
|
||
parser = Lark(TEMPLATED_PYTHON, parser='lalr', start=['single_input', 'file_input', 'eval_input', 'template_start'], postlex=PythonIndenter(), maybe_placeholders=False) | ||
|
||
|
||
def parse_template(s): | ||
return parser.parse(s + '\n', start='template_start') | ||
|
||
def parse_code(s): | ||
return parser.parse(s + '\n', start='file_input') | ||
|
||
|
||
# | ||
# 2. Define translations using templates (each template code is parsed to a template tree) | ||
# | ||
|
||
pytemplate = TemplateConf(parse=parse_template) | ||
|
||
translations_3to2 = { | ||
'yield from $a': | ||
'for _tmp in $a: yield _tmp', | ||
|
||
'raise $e from $x': | ||
'raise $e', | ||
|
||
'$a / $b': | ||
'float($a) / $b', | ||
} | ||
translations_3to2 = {pytemplate(k): pytemplate(v) for k, v in translations_3to2.items()} | ||
|
||
# | ||
# 3. Translate and reconstruct Python 3 code into valid Python 2 code | ||
# | ||
|
||
python_reconstruct = PythonReconstructor(parser) | ||
|
||
def translate_py3to2(code): | ||
tree = parse_code(code) | ||
tree = TemplateTranslator(translations_3to2).translate(tree) | ||
return python_reconstruct.reconstruct(tree) | ||
|
||
|
||
# | ||
# Test Code | ||
# | ||
|
||
_TEST_CODE = ''' | ||
if a / 2 > 1: | ||
yield from [1,2,3] | ||
else: | ||
raise ValueError(a) from e | ||
|
||
''' | ||
|
||
def test(): | ||
print(_TEST_CODE) | ||
print(' -----> ') | ||
print(translate_py3to2(_TEST_CODE)) | ||
|
||
if __name__ == '__main__': | ||
test() |
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 |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"""This module defines utilities for matching and translation tree templates. | ||
|
||
A tree templates is a tree that contains nodes that are template variables. | ||
|
||
""" | ||
|
||
from typing import Union, Optional, Mapping | ||
|
||
from lark import Tree, Transformer | ||
|
||
TreeOrCode = Union[Tree, str] | ||
|
||
class TemplateConf: | ||
"""Template Configuration | ||
|
||
Allows customization for different uses of Template | ||
""" | ||
|
||
def __init__(self, parse=None): | ||
self._parse = parse | ||
|
||
|
||
def test_var(self, var: Union[Tree, str]) -> Optional[str]: | ||
"""Given a tree node, if it is a template variable return its name. Otherwise, return None. | ||
|
||
This method may be overridden for customization | ||
|
||
Parameters: | ||
var: Tree | str - The tree node to test | ||
|
||
""" | ||
if isinstance(var, str) and var.startswith('$'): | ||
return var.lstrip('$') | ||
|
||
if isinstance(var, Tree) and var.data == 'var' and var.children[0].startswith('$'): | ||
return var.children[0].lstrip('$') | ||
|
||
|
||
def _get_tree(self, template: TreeOrCode): | ||
if isinstance(template, str): | ||
assert self._parse | ||
template = self._parse(template) | ||
|
||
assert isinstance(template, Tree) | ||
return template | ||
|
||
def __call__(self, template): | ||
return Template(template, conf=self) | ||
|
||
def _match_tree_template(self, template, tree): | ||
template_var = self.test_var(template) | ||
if template_var: | ||
return {template_var: tree} | ||
|
||
if isinstance(template, str): | ||
if template == tree: | ||
return {} | ||
return | ||
|
||
assert isinstance(template, Tree), template | ||
|
||
if template.data == tree.data and len(template.children) == len(tree.children): | ||
res = {} | ||
for t1, t2 in zip(template.children, tree.children): | ||
matches = self._match_tree_template(t1, t2) | ||
if matches is None: | ||
return | ||
|
||
res.update(matches) | ||
|
||
return res | ||
|
||
|
||
|
||
class _ReplaceVars(Transformer): | ||
def __init__(self, conf, vars): | ||
self._conf = conf | ||
self._vars = vars | ||
|
||
def __default__(self, data, children, meta): | ||
tree = super().__default__(data, children, meta) | ||
|
||
var = self._conf.test_var(tree) | ||
if var: | ||
return self._vars[var] | ||
return tree | ||
|
||
|
||
class Template: | ||
"""Represents a tree templates, tied to a specific configuration | ||
|
||
A tree template is a tree that contains nodes that are template variables. | ||
Those variables will match any tree. | ||
(future versions may support annotations on the variables, to allow more complex templates) | ||
""" | ||
|
||
def __init__(self, tree: Tree, conf = TemplateConf()): | ||
self.conf = conf | ||
self.tree = conf._get_tree(tree) | ||
|
||
def match(self, tree: TreeOrCode): | ||
"""Match a tree template to a tree. | ||
|
||
A tree template without variables will only match ``tree`` if it is equal to the template. | ||
|
||
Parameters: | ||
tree (Tree): The tree to match to the template | ||
|
||
Returns: | ||
Optional[Dict[str, Tree]]: If match is found, returns a dictionary mapping | ||
template variable names to their matching tree nodes. | ||
If no match was found, returns None. | ||
""" | ||
tree = self.conf._get_tree(tree) | ||
return self.conf._match_tree_template(self.tree, tree) | ||
|
||
def search(self, tree: TreeOrCode): | ||
"""Search for all occurances of the tree template inside ``tree``. | ||
""" | ||
tree = self.conf._get_tree(tree) | ||
for subtree in tree.iter_subtrees(): | ||
res = self.match(subtree) | ||
if res: | ||
yield subtree, res | ||
|
||
def apply_vars(self, vars: Mapping[str, Tree]): | ||
"""Apply vars to the template tree | ||
""" | ||
return _ReplaceVars(self.conf, vars).transform(self.tree) | ||
|
||
|
||
def translate(t1: Template, t2: Template, tree: TreeOrCode): | ||
"""Search tree and translate each occurrance of t1 into t2. | ||
""" | ||
tree = t1.conf._get_tree(tree) # ensure it's a tree, parse if necessary and possible | ||
for subtree, vars in t1.search(tree): | ||
res = t2.apply_vars(vars) | ||
subtree.set(res.data, res.children) | ||
return tree | ||
|
||
|
||
|
||
class TemplateTranslator: | ||
"""Utility class for translating a collection of patterns | ||
""" | ||
|
||
def __init__(self, translations: Mapping[TreeOrCode, TreeOrCode]): | ||
assert all( isinstance(k, Template) and isinstance(v, Template) for k, v in translations.items() ) | ||
self.translations = translations | ||
|
||
def translate(self, tree: Tree): | ||
for k, v in self.translations.items(): | ||
tree = translate(k, v, tree) | ||
return tree |
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.
Seems like a perfect usecase for an
%include
statement.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 was thinking that too
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.
You didn't submit a PR for this, right?
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.
It is part of #998, but that is not a finished implementation.
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.
Hmm okay. Anyway, we can change it to
%include
when it's ready.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.
Yes.