diff --git a/asdl/gen_python.py b/asdl/gen_python.py index e3a80e30cf..5aa3f50cde 100755 --- a/asdl/gen_python.py +++ b/asdl/gen_python.py @@ -121,15 +121,10 @@ class GenMyPyVisitor(visitor.AsdlVisitor): TODO: Remove the code above. This should substitute for it. """ - def __init__(self, f, type_lookup, abbrev_mod=None): + def __init__(self, f, type_lookup, abbrev_mod_entries=None): visitor.AsdlVisitor.__init__(self, f) self.type_lookup = type_lookup - if abbrev_mod: - self.abbrev_mod_name = abbrev_mod.__name__.split('.')[-1] - self.abbrev_mod_entries = dir(abbrev_mod) - else: - self.abbrev_mod_name = '' - self.abbrev_mod_entries = [] + self.abbrev_mod_entries = abbrev_mod_entries or [] def VisitSimpleSum(self, sum, name, depth): # First emit a type @@ -336,8 +331,9 @@ def _GenClass(self, desc, class_name, super_name, depth, tag_num=None): self.Emit(' def AbbreviatedTree(self):') self.Emit(' # type: () -> PrettyNode') - if class_name in self.abbrev_mod_entries: - self.Emit(' p = %s.%s(self)' % (self.abbrev_mod_name, class_name)) + abbrev_name = '_%s' % class_name + if abbrev_name in self.abbrev_mod_entries: + self.Emit(' p = %s(self)' % abbrev_name) # If the user function didn't return anything, fall back. self.Emit(' return p if p else self._AbbreviatedTree()') else: diff --git a/asdl/typed.sh b/asdl/typed.sh index 2d68be1e72..a8c9f45a92 100755 --- a/asdl/typed.sh +++ b/asdl/typed.sh @@ -33,7 +33,6 @@ typecheck() { check-arith() { local strict='--strict' - #strict='' MYPYPATH=. PYTHONPATH=. typecheck $strict \ asdl/typed_arith_parse.py asdl/typed_arith_parse_test.py asdl/tdop.py } diff --git a/asdl/typed_arith_abbrev.py b/asdl/typed_arith_abbrev.py index 9f762e3196..f87a0dcd32 100644 --- a/asdl/typed_arith_abbrev.py +++ b/asdl/typed_arith_abbrev.py @@ -1,19 +1,11 @@ -#!/usr/bin/python """ -typed_arith_abbrev.py +typed_arith_abbrev.py - Abbreviations for pretty-printing typed_arith.asdl. """ -from __future__ import print_function - -# Causes a circular import! Gah. -#from _devbuild.gen import typed_arith_asdl as A -#from typing import Optional from asdl import runtime -_PrettyBase = runtime._PrettyBase - -def arith_expr__Unary(obj): - # X type: (A.arith_expr__Unary) -> Optional[_PrettyBase] +def _arith_expr__Unary(obj): + # type: (arith_expr__Unary) -> Optional[runtime.PrettyNode] p_node = runtime.PrettyNode() p_node.abbrev = True @@ -24,8 +16,8 @@ def arith_expr__Unary(obj): return p_node -def arith_expr__Binary(obj): - # X type: (A.arith_expr__Binary) -> Optional[_PrettyBase] +def _arith_expr__Binary(obj): + # type: (arith_expr__Binary) -> Optional[runtime.PrettyNode] if obj.op == '=': # test for fallback return None @@ -40,16 +32,18 @@ def arith_expr__Binary(obj): return p_node -def arith_expr__Const(obj): +def _arith_expr__Const(obj): + # type: (arith_expr__Const) -> Optional[runtime.PrettyNode] p_node = runtime.PrettyNode() p_node.abbrev = True - p_node.node_type = None # omit it + p_node.node_type = '' # omit it n = runtime.PrettyLeaf(str(obj.i), runtime.Color_OtherConst) p_node.unnamed_fields.append(n) return p_node -def arith_expr__Var(obj): +def _arith_expr__Var(obj): + # type: (arith_expr__Var) -> Optional[runtime.PrettyNode] p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = '$' diff --git a/core/asdl_gen.py b/core/asdl_gen.py index dfcce442f1..460e026a32 100755 --- a/core/asdl_gen.py +++ b/core/asdl_gen.py @@ -103,14 +103,21 @@ def main(argv): """) - # TODO: inline the module at the END + abbrev_mod_entries = dir(abbrev_mod) if abbrev_mod else [] + v = gen_python.GenMyPyVisitor(f, type_lookup, abbrev_mod_entries) + v.VisitModule(schema_ast) + if abbrev_mod: - package, module = abbrev_module_name.split('.') - f.write('from %s import %s\n' % (package, module)) - f.write('\n') + f.write("""\ +# +# CONCATENATED FILE +# - v = gen_python.GenMyPyVisitor(f, type_lookup, abbrev_mod) - v.VisitModule(schema_ast) +""") + package, module = abbrev_module_name.split('.') + path = os.path.join(package, module + '.py') + with open(path) as in_f: + f.write(in_f.read()) else: raise RuntimeError('Invalid action %r' % action) diff --git a/frontend/syntax_abbrev.py b/frontend/syntax_abbrev.py index f5118280db..c1acd57e7a 100644 --- a/frontend/syntax_abbrev.py +++ b/frontend/syntax_abbrev.py @@ -1,10 +1,7 @@ -#!/usr/bin/python """ syntax_abbrev.py - Abbreviations for pretty-printing syntax.asdl. """ -import sys - from asdl import runtime from core.meta import Id @@ -18,7 +15,7 @@ def _AbbreviateToken(token, out): out.append(n2) -def token(obj): +def _token(obj): p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = '' # don't show @@ -29,7 +26,7 @@ def token(obj): return p_node -def word_part__LiteralPart(obj): +def _word_part__LiteralPart(obj): p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = '' # don't show @@ -38,7 +35,7 @@ def word_part__LiteralPart(obj): return p_node -def word_part__SimpleVarSub(obj): +def _word_part__SimpleVarSub(obj): p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = '$' @@ -46,7 +43,7 @@ def word_part__SimpleVarSub(obj): return p_node -def word_part__BracedVarSub(obj): +def _word_part__BracedVarSub(obj): p_node = runtime.PrettyNode() if obj.prefix_op or obj.bracket_op or obj.suffix_op: return None # we have other fields to display; don't abbreviate @@ -57,7 +54,7 @@ def word_part__BracedVarSub(obj): return p_node -def word_part__DoubleQuotedPart(obj): +def _word_part__DoubleQuotedPart(obj): p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = 'DQ' @@ -67,7 +64,7 @@ def word_part__DoubleQuotedPart(obj): return p_node -def word_part__SingleQuotedPart(obj): +def _word_part__SingleQuotedPart(obj): # Only abbreviate 'foo', not $'foo\n' if obj.left.id != Id.Left_SingleQuote: return None # Fall back on obj._AbbreviatedTree() @@ -81,7 +78,7 @@ def word_part__SingleQuotedPart(obj): return p_node -def word__CompoundWord(obj): +def _word__CompoundWord(obj): p_node = runtime.PrettyNode() p_node.abbrev = True p_node.node_type = '' # don't show @@ -93,7 +90,7 @@ def word__CompoundWord(obj): return p_node -def command__SimpleCommand(obj): +def _command__SimpleCommand(obj): p_node = runtime.PrettyNode() if obj.redirects or obj.more_env: return None # we have other fields to display; don't abbreviate diff --git a/test/lint.sh b/test/lint.sh index e6e8a7a7e7..ac291d484b 100755 --- a/test/lint.sh +++ b/test/lint.sh @@ -106,8 +106,9 @@ flake8-all() { # astgen.py has a PROLOGUE which must have unused imports! # opcode.py triggers a flake8 bug? Complains about def_op() when it is # defined. + # _abbrev.py modules are concatenated, and don't need to check on their own. local -a exclude=( - --exclude 'opy/_regtest,opy/byterun,opy/tools/astgen.py,opy/lib/opcode.py') + --exclude 'opy/_regtest,opy/byterun,opy/tools/astgen.py,opy/lib/opcode.py,*/*_abbrev.py') # Step 1: Stop the build if there are Python syntax errors, undefined names, # unused imports