Permalink
Browse files

Generated ASDL types are now pretty-printed with repr().

Also:

- Remove duplicate 'add_spids' in asdl/gen_python.py.
- Comments about parser instantiation, and code cleanup.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 21, 2017
1 parent 0878d1b commit dfedf26cf8a66325d44e2ea6dc8edee0ca0eb042
Showing with 18 additions and 30 deletions.
  1. +2 −9 asdl/gen_python.py
  2. +11 −13 asdl/py_meta.py
  3. +0 −1 core/completion.py
  4. +1 −3 osh/cmd_parse.py
  5. +4 −4 osh/parse_lib.py
View
@@ -28,16 +28,13 @@ def VisitSimpleSum(self, sum, name, depth):
self.Emit(attr, depth)
self.Emit('', depth)
def _GenClass(self, desc, name, super_name, depth, tag_num=None,
add_spids=True):
def _GenClass(self, desc, name, super_name, depth, tag_num=None):
self.Emit('class %s(%s):' % (name, super_name), depth)
if tag_num is not None:
self.Emit(' tag = %d' % tag_num, depth)
field_names = [f.name for f in desc.fields]
if add_spids:
field_names.append('spids')
quoted_fields = repr(tuple(field_names))
# NOTE: FIELDS is a duplicate of __slots__, used for pretty printing and
@@ -53,6 +50,7 @@ def _GenClass(self, desc, name, super_name, depth, tag_num=None,
lookup = {}
self.Emit('', depth)
# TODO: leave out spids? Mark it as an attribute?
args = ', '.join('%s=None' % f.name for f in desc.fields)
self.Emit(' def __init__(self, %s):' % args, depth)
@@ -73,11 +71,6 @@ def _GenClass(self, desc, name, super_name, depth, tag_num=None,
default_str = (' or %s' % default) if default else ''
self.Emit(' self.%s = %s%s' % (f.name, f.name, default_str), depth)
# Like add_spids in _MakeFieldDescriptors. TODO: This should be optional
# for token and span! Also for runtime.asdl. We need to make it optional.
if add_spids:
self.Emit(' self.spids = []', depth)
self.Emit('', depth)
def VisitConstructor(self, cons, def_name, tag_num, depth):
View
@@ -121,12 +121,19 @@ def __repr__(self):
class CompoundObj(Obj):
# TODO: Remove this?
# Always set for constructor types, which are subclasses of sum types. Never
# set for product types.
# TODO: Remove tag?
# The tag is always set for constructor types, which are subclasses of sum
# types. Never set for product types.
tag = None
# NOTE: SimpleObj could share this.
def __repr__(self):
ast_f = fmt.TextOutput(util.Buffer()) # No color by default.
tree = fmt.MakeTree(self)
fmt.PrintTree(tree, ast_f)
s, _ = ast_f.GetRaw()
return s
class DebugCompoundObj(CompoundObj):
"""A CompoundObj that does dynamic type checks.
@@ -210,14 +217,6 @@ def __setattr__(self, name, value):
self._assigned[name] = True # check this later when encoding
self.__dict__[name] = value
def __repr__(self):
ast_f = fmt.TextOutput(util.Buffer()) # No color by default.
#ast_f = fmt.AnsiOutput(io.StringIO())
tree = fmt.MakeTree(self)
fmt.PrintTree(tree, ast_f)
s, _ = ast_f.GetRaw()
return s
def MakeTypes(module, root, type_lookup):
"""
@@ -271,7 +270,6 @@ def MakeTypes(module, root, type_lookup):
tag = i + 1 # zero reserved?
tag_num[cons.name] = tag # for enum
# Add 'int* spids' to every constructor.
class_attr = {
'ASDL_TYPE': cons, # asdl.Constructor
'tag': tag, # Does this API change?
View
@@ -775,7 +775,6 @@ def Init(pool, builtins, mem, funcs, comp_lookup, status_out, ev):
C1 = ChainedCompleter([A1, A2])
comp_lookup.RegisterName('grep', C1)
make_parser = parse_lib.MakeParserForCompletion
var_comp = VarAction(os.environ, mem)
root_comp = RootCompleter(pool, ev, comp_lookup, var_comp)
View
@@ -111,9 +111,7 @@ def _MaybeReadHereDocs(self):
# echo 3) 4
# EOF
# TODO: Move this import
from osh import parse_lib
# TODO: Thread arena. need self.arena
from osh import parse_lib # Avoid circular import
w_parser = parse_lib.MakeWordParserForHereDoc(lines, self.arena)
word = w_parser.ReadHereDocBody()
if not word:
View
@@ -85,15 +85,14 @@ def InitLexer(s, arena):
# - line_id = arena.AddLine()
# TODO:
# NOTE:
# - Does it make sense to create ParseState objects? They have no dependencies
# -- just pure data. Or just recreate them every time? One issue is that
# you need somewhere to store the side effects -- errors for parsers, and the
# actual values for the evaluators/executors.
def MakeParser(line_reader, arena):
"""Top level parser."""
# AtEnd() is true
line_lexer = lexer.LineLexer(_MakeMatcher(), '', arena)
lx = lexer.Lexer(line_lexer, line_reader)
w_parser = word_parse.WordParser(lx, line_reader)
@@ -138,5 +137,6 @@ def MakeParserForCommandSub(line_reader, lexer):
return c_parser
# More parser instantiations
# - For Array Literal -- instantiate WordParser
# Another parser instantiation:
# - For Array Literal in word_parse.py WordParser:
# w_parser = WordParser(self.lexer, self.line_reader)

0 comments on commit dfedf26

Please sign in to comment.