Permalink
Browse files

Correctly handle the repeated string type in the ASDL pretty printer.

There was a case missing, so there were two unnecessary repr() calls.

Also:

- Research on serializing the parsed ASDL schema.  We need this for
  metaprogramming.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 16, 2017
1 parent 75cb52e commit 9d9f0770a4928ff99db58b83f9220f862390bb0e
Showing with 76 additions and 5 deletions.
  1. +3 −0 asdl/arith.asdl
  2. +18 −1 asdl/asdl_demo.py
  3. +15 −4 asdl/format.py
  4. +36 −0 asdl/format_test.py
  5. +4 −0 asdl/run.sh
View
@@ -17,6 +17,9 @@ module arith {
-- Optional int
token = (int id, string value, int? span_id)
-- Repeated string
assign = (string name, string* flags)
-- TODO:
-- - Add optional. For slicing maybe -- optional end.
-- - Add repeated. For function call maybe.
View
@@ -25,6 +25,13 @@ def main(argv):
with open(schema_path) as f:
module = asdl.parse(f)
# Note this is a big tree. But we really want a graph of pointers to
# instances.
# Type(name, Product(...))
# Type(name, Sum([Constructor(...), ...]))
print(module)
root = sys.modules[__name__]
# NOTE: We shouldn't pass in app_types for arith.asdl, but this is just a
# demo.
@@ -53,10 +60,20 @@ def main(argv):
#out = fmt.TextOutput(sys.stdout)
tree = fmt.MakeTree(obj)
#treee= ['hi', 'there', ['a', 'b'], 'c']
fmt.PrintTree(tree, sys.stdout)
f = fmt.DetectConsoleOutput(sys.stdout)
fmt.PrintTree(tree, f)
# Might need to print the output?
# out.WriteToFile?
elif action == 'repr':
# Hm this isn't valid Python code, but we can change it to be as a hack.
field1 = asdl.Field('str', 'name')
field2 = asdl.Field('int', 'age')
print(repr(field1))
s = asdl.Sum([field1, field2])
print(repr(s))
else:
raise RuntimeError('Invalid action %r' % action)
View
@@ -223,15 +223,21 @@ def __init__(self, node_type):
# problem: CompoundWord just has word_part though
# List of Obj or ColoredString
def __repr__(self):
return '<_Obj %s %s>' % (self.node_type, self.fields)
class _ColoredString:
"""Node for pretty-printing."""
def __init__(self, s, str_type):
self.s = s
self.str_type = str_type
def __repr__(self):
return '<_ColoredString %s %s>' % (self.s, self.str_type)
def FormatField(obj, field_name, abbrev_hook, omit_empty=True):
def MakeFieldSubtree(obj, field_name, abbrev_hook, omit_empty=True):
try:
field_val = getattr(obj, field_name)
except AttributeError:
@@ -293,8 +299,8 @@ def MakeTree(obj, abbrev_hook=None, omit_empty=True):
fields = out_node.fields
for field_name in obj.FIELDS:
out_val = FormatField(obj, field_name, abbrev_hook,
omit_empty=omit_empty)
out_val = MakeFieldSubtree(obj, field_name, abbrev_hook,
omit_empty=omit_empty)
if out_val is not None:
out_node.fields.append((field_name, out_val))
@@ -303,6 +309,9 @@ def MakeTree(obj, abbrev_hook=None, omit_empty=True):
if abbrev_hook:
abbrev_hook(obj, out_node)
elif isinstance(obj, str): # Could be an array of strings
return _ColoredString(obj, _STRING_LITERAL)
else:
# Id uses this now. TODO: Should we have plugins? Might need it for
# color.
@@ -542,7 +551,9 @@ def _TrySingleLine(node, f, max_chars):
elif isinstance(node, list): # Can we fit the WHOLE list on the line?
f.write('[')
for item in node:
for i, item in enumerate(node):
if i != 0:
f.write(' ')
if not _TrySingleLine(item, f, max_chars):
return False
f.write(']')
View
@@ -0,0 +1,36 @@
#!/usr/bin/python -S
"""
format_test.py: Tests for format.py
"""
import cStringIO
import sys
import unittest
from asdl import format as fmt
from asdl import asdl_
from asdl import arith_ast # module under test
class FormatTest(unittest.TestCase):
def testRepeatedString(self):
node = arith_ast.assign('declare', ['-r', '-x'])
f = cStringIO.StringIO()
ast_f = fmt.TextOutput(f)
tree = fmt.MakeTree(node)
#print(tree)
fmt.PrintTree(tree, ast_f)
pretty_str = f.getvalue()
print(pretty_str)
self.assertEqual('(assign name:declare flags:[-r -x])', pretty_str)
if __name__ == '__main__':
unittest.main()
View
@@ -33,6 +33,10 @@ asdl-py() {
asdl/asdl_demo.py py $schema
}
repr() {
asdl/asdl_demo.py repr
}
asdl-cpp() {
local schema=${1:-asdl/arith.asdl}
local src=${2:-_tmp/arith.asdl.h}

0 comments on commit 9d9f077

Please sign in to comment.