diff options
Diffstat (limited to 'Parser/asdl.py')
-rw-r--r-- | Parser/asdl.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/Parser/asdl.py b/Parser/asdl.py index 08aa05b897..fc1b16c668 100644 --- a/Parser/asdl.py +++ b/Parser/asdl.py @@ -16,8 +16,9 @@ import traceback import spark -def output(string): - sys.stdout.write(string + "\n") +def output(*strings): + for s in strings: + sys.stdout.write(str(s) + "\n") class Token(object): @@ -158,11 +159,19 @@ class ASDLParser(spark.GenericParser, object): msg="expected attributes, found %s" % id) return Sum(sum, attributes) - def p_product(self, info): + def p_product_0(self, info): " product ::= ( fields ) " _0, fields, _1 = info return Product(fields) + def p_product_1(self, info): + " product ::= ( fields ) Id ( fields ) " + _0, fields, _1, id, _2, attributes, _3 = info + if id.value != "attributes": + raise ASDLSyntaxError(id.lineno, + msg="expected attributes, found %s" % id) + return Product(fields, attributes) + def p_sum_0(self, constructor): " sum ::= constructor " return [constructor[0]] @@ -222,7 +231,7 @@ class ASDLParser(spark.GenericParser, object): " field ::= Id ? " return Field(type[0], opt=True) -builtin_types = ("identifier", "string", "bytes", "int", "object") +builtin_types = ("identifier", "string", "bytes", "int", "object", "singleton") # below is a collection of classes to capture the AST of an AST :-) # not sure if any of the methods are useful yet, but I'm adding them @@ -289,11 +298,15 @@ class Sum(AST): return "Sum(%s, %s)" % (self.types, self.attributes) class Product(AST): - def __init__(self, fields): + def __init__(self, fields, attributes=None): self.fields = fields + self.attributes = attributes or [] def __repr__(self): - return "Product(%s)" % self.fields + if self.attributes is None: + return "Product(%s)" % self.fields + else: + return "Product(%s, %s)" % (self.fields, self.attributes) class VisitorBase(object): @@ -385,7 +398,11 @@ def parse(file): scanner = ASDLScanner() parser = ASDLParser() - buf = open(file).read() + f = open(file) + try: + buf = f.read() + finally: + f.close() tokens = scanner.tokenize(buf) try: return parser.parse(tokens) @@ -416,4 +433,4 @@ if __name__ == "__main__": output("Check failed") else: for dfn in mod.dfns: - output(dfn.type) + output(dfn.name, dfn.value) |