Skip to content

Commit

Permalink
[ASDL] Concatenate abbreviations module to avoid circular dependency.
Browse files Browse the repository at this point in the history
typed_arith.* now type checks in strict mode!
  • Loading branch information
Andy Chu committed Feb 14, 2019
1 parent 61c9f56 commit 06e0587
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
14 changes: 5 additions & 9 deletions asdl/gen_python.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion asdl/typed.sh
Expand Up @@ -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
}
Expand Down
26 changes: 10 additions & 16 deletions 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
Expand All @@ -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
Expand All @@ -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 = '$'
Expand Down
19 changes: 13 additions & 6 deletions core/asdl_gen.py
Expand Up @@ -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)
Expand Down
19 changes: 8 additions & 11 deletions 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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -38,15 +35,15 @@ 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 = '$'
_AbbreviateToken(obj.token, p_node.unnamed_fields)
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
Expand All @@ -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'
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/lint.sh
Expand Up @@ -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
Expand Down

0 comments on commit 06e0587

Please sign in to comment.