asdl.py 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#-------------------------------------------------------------------------------
# Parser for ASDL [1] definition files. Reads in an ASDL description and parses
# it into an AST that describes it.
#
# The EBNF we're parsing here: Figure 1 of the paper [1]. Extended to support
# modules and attributes after a product. Words starting with Capital letters
# are terminals. Literal tokens are in "double quotes". Others are
# non-terminals. Id is either TokenId or ConstructorId.
#
# module        ::= "module" Id "{" [definitions] "}"
# definitions   ::= { TypeId "=" type }
# type          ::= product | sum
# product       ::= fields ["attributes" fields]
# fields        ::= "(" { field, "," } field ")"
# field         ::= TypeId ["?" | "*"] [Id]
# sum           ::= constructor { "|" constructor } ["attributes" fields]
# constructor   ::= ConstructorId [fields]
#
# [1] "The Zephyr Abstract Syntax Description Language" by Wang, et. al. See
#     http://asdl.sourceforge.net/
#-------------------------------------------------------------------------------
from collections import namedtuple
import re

__all__ = [
    'builtin_types', 'parse', 'AST', 'Module', 'Type', 'Constructor',
    'Field', 'Sum', 'Product', 'VisitorBase', 'Check', 'check']

# The following classes define nodes into which the ASDL description is parsed.
# Note: this is a "meta-AST". ASDL files (such as Python.asdl) describe the AST
# structure used by a programming language. But ASDL files themselves need to be
# parsed. This module parses ASDL files and uses a simple AST to represent them.
# See the EBNF at the top of the file to understand the logical connection
# between the various node types.

Victor Stinner's avatar
Victor Stinner committed
36 37
builtin_types = {'identifier', 'string', 'bytes', 'int', 'object', 'singleton',
                 'constant'}
38 39

class AST:
Jeremy Hylton's avatar
Jeremy Hylton committed
40
    def __repr__(self):
41
        raise NotImplementedError
Jeremy Hylton's avatar
Jeremy Hylton committed
42 43

class Module(AST):
44
    def __init__(self, name, dfns):
Jeremy Hylton's avatar
Jeremy Hylton committed
45 46
        self.name = name
        self.dfns = dfns
47
        self.types = {type.name: type.value for type in dfns}
Jeremy Hylton's avatar
Jeremy Hylton committed
48 49

    def __repr__(self):
50
        return 'Module({0.name}, {0.dfns})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
51 52 53 54 55 56 57

class Type(AST):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __repr__(self):
58
        return 'Type({0.name}, {0.value})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
59 60 61 62 63 64 65

class Constructor(AST):
    def __init__(self, name, fields=None):
        self.name = name
        self.fields = fields or []

    def __repr__(self):
66
        return 'Constructor({0.name}, {0.fields})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
67 68

class Field(AST):
Benjamin Peterson's avatar
Benjamin Peterson committed
69
    def __init__(self, type, name=None, seq=False, opt=False):
Jeremy Hylton's avatar
Jeremy Hylton committed
70 71 72 73 74 75 76
        self.type = type
        self.name = name
        self.seq = seq
        self.opt = opt

    def __repr__(self):
        if self.seq:
Benjamin Peterson's avatar
Benjamin Peterson committed
77
            extra = ", seq=True"
Jeremy Hylton's avatar
Jeremy Hylton committed
78
        elif self.opt:
Benjamin Peterson's avatar
Benjamin Peterson committed
79
            extra = ", opt=True"
Jeremy Hylton's avatar
Jeremy Hylton committed
80 81 82
        else:
            extra = ""
        if self.name is None:
83
            return 'Field({0.type}{1})'.format(self, extra)
Jeremy Hylton's avatar
Jeremy Hylton committed
84
        else:
85
            return 'Field({0.type}, {0.name}{1})'.format(self, extra)
Jeremy Hylton's avatar
Jeremy Hylton committed
86 87 88 89 90 91 92

class Sum(AST):
    def __init__(self, types, attributes=None):
        self.types = types
        self.attributes = attributes or []

    def __repr__(self):
93 94
        if self.attributes:
            return 'Sum({0.types}, {0.attributes})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
95
        else:
96
            return 'Sum({0.types})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
97 98

class Product(AST):
99
    def __init__(self, fields, attributes=None):
Jeremy Hylton's avatar
Jeremy Hylton committed
100
        self.fields = fields
101
        self.attributes = attributes or []
Jeremy Hylton's avatar
Jeremy Hylton committed
102 103

    def __repr__(self):
104 105
        if self.attributes:
            return 'Product({0.fields}, {0.attributes})'.format(self)
106
        else:
107
            return 'Product({0.fields})'.format(self)
Jeremy Hylton's avatar
Jeremy Hylton committed
108

109 110 111 112 113
# A generic visitor for the meta-AST that describes ASDL. This can be used by
# emitters. Note that this visitor does not provide a generic visit method, so a
# subclass needs to define visit methods from visitModule to as deep as the
# interesting node.
# We also define a Check visitor that makes sure the parsed ASDL is well-formed.
Jeremy Hylton's avatar
Jeremy Hylton committed
114

115
class VisitorBase(object):
116 117
    """Generic tree visitor for ASTs."""
    def __init__(self):
Jeremy Hylton's avatar
Jeremy Hylton committed
118 119
        self.cache = {}

120 121
    def visit(self, obj, *args):
        klass = obj.__class__
Jeremy Hylton's avatar
Jeremy Hylton committed
122 123 124
        meth = self.cache.get(klass)
        if meth is None:
            methname = "visit" + klass.__name__
125
            meth = getattr(self, methname, None)
Jeremy Hylton's avatar
Jeremy Hylton committed
126
            self.cache[klass] = meth
127 128 129 130 131 132
        if meth:
            try:
                meth(obj, *args)
            except Exception as e:
                print("Error visiting %r: %s" % (obj, e))
                raise
Jeremy Hylton's avatar
Jeremy Hylton committed
133 134

class Check(VisitorBase):
135
    """A visitor that checks a parsed ASDL tree for correctness.
Jeremy Hylton's avatar
Jeremy Hylton committed
136

137 138
    Errors are printed and accumulated.
    """
Jeremy Hylton's avatar
Jeremy Hylton committed
139
    def __init__(self):
140
        super(Check, self).__init__()
Jeremy Hylton's avatar
Jeremy Hylton committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        self.cons = {}
        self.errors = 0
        self.types = {}

    def visitModule(self, mod):
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type):
        self.visit(type.value, str(type.name))

    def visitSum(self, sum, name):
        for t in sum.types:
            self.visit(t, name)

    def visitConstructor(self, cons, name):
        key = str(cons.name)
        conflict = self.cons.get(key)
        if conflict is None:
            self.cons[key] = name
        else:
162 163
            print('Redefinition of constructor {}'.format(key))
            print('Defined in {} and {}'.format(conflict, name))
Jeremy Hylton's avatar
Jeremy Hylton committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177
            self.errors += 1
        for f in cons.fields:
            self.visit(f, key)

    def visitField(self, field, name):
        key = str(field.type)
        l = self.types.setdefault(key, [])
        l.append(name)

    def visitProduct(self, prod, name):
        for f in prod.fields:
            self.visit(f, name)

def check(mod):
178 179 180 181 182
    """Check the parsed ASDL tree for correctness.

    Return True if success. For failure, the errors are printed out and False
    is returned.
    """
Jeremy Hylton's avatar
Jeremy Hylton committed
183 184 185 186
    v = Check()
    v.visit(mod)

    for t in v.types:
187
        if t not in mod.types and not t in builtin_types:
Jeremy Hylton's avatar
Jeremy Hylton committed
188 189
            v.errors += 1
            uses = ", ".join(v.types[t])
190
            print('Undefined type {}, used in {}'.format(t, uses))
Jeremy Hylton's avatar
Jeremy Hylton committed
191 192
    return not v.errors

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
# The ASDL parser itself comes next. The only interesting external interface
# here is the top-level parse function.

def parse(filename):
    """Parse ASDL from the given file and return a Module node describing it."""
    with open(filename) as f:
        parser = ASDLParser()
        return parser.parse(f.read())

# Types for describing tokens in an ASDL specification.
class TokenKind:
    """TokenKind is provides a scope for enumerated token kinds."""
    (ConstructorId, TypeId, Equals, Comma, Question, Pipe, Asterisk,
     LParen, RParen, LBrace, RBrace) = range(11)

    operator_table = {
        '=': Equals, ',': Comma,    '?': Question, '|': Pipe,    '(': LParen,
        ')': RParen, '*': Asterisk, '{': LBrace,   '}': RBrace}

Token = namedtuple('Token', 'kind value lineno')

class ASDLSyntaxError(Exception):
    def __init__(self, msg, lineno=None):
        self.msg = msg
        self.lineno = lineno or '<unknown>'

    def __str__(self):
        return 'Syntax error on line {0.lineno}: {0.msg}'.format(self)

def tokenize_asdl(buf):
    """Tokenize the given buffer. Yield Token objects."""
    for lineno, line in enumerate(buf.splitlines(), 1):
        for m in re.finditer(r'\s*(\w+|--.*|.)', line.strip()):
            c = m.group(1)
            if c[0].isalpha():
                # Some kind of identifier
                if c[0].isupper():
                    yield Token(TokenKind.ConstructorId, c, lineno)
                else:
                    yield Token(TokenKind.TypeId, c, lineno)
            elif c[:2] == '--':
                # Comment
                break
            else:
                # Operators
                try:
                    op_kind = TokenKind.operator_table[c]
                except KeyError:
                    raise ASDLSyntaxError('Invalid operator %s' % c, lineno)
                yield Token(op_kind, c, lineno)

class ASDLParser:
    """Parser for ASDL files.

    Create, then call the parse method on a buffer containing ASDL.
    This is a simple recursive descent parser that uses tokenize_asdl for the
    lexing.
    """
    def __init__(self):
        self._tokenizer = None
        self.cur_token = None

    def parse(self, buf):
        """Parse the ASDL in the buffer and return an AST with a Module root.
        """
        self._tokenizer = tokenize_asdl(buf)
        self._advance()
        return self._parse_module()

    def _parse_module(self):
        if self._at_keyword('module'):
            self._advance()
Jeremy Hylton's avatar
Jeremy Hylton committed
265
        else:
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
            raise ASDLSyntaxError(
                'Expected "module" (found {})'.format(self.cur_token.value),
                self.cur_token.lineno)
        name = self._match(self._id_kinds)
        self._match(TokenKind.LBrace)
        defs = self._parse_definitions()
        self._match(TokenKind.RBrace)
        return Module(name, defs)

    def _parse_definitions(self):
        defs = []
        while self.cur_token.kind == TokenKind.TypeId:
            typename = self._advance()
            self._match(TokenKind.Equals)
            type = self._parse_type()
            defs.append(Type(typename, type))
        return defs

    def _parse_type(self):
        if self.cur_token.kind == TokenKind.LParen:
            # If we see a (, it's a product
            return self._parse_product()
        else:
            # Otherwise it's a sum. Look for ConstructorId
            sumlist = [Constructor(self._match(TokenKind.ConstructorId),
                                   self._parse_optional_fields())]
            while self.cur_token.kind  == TokenKind.Pipe:
                # More constructors
                self._advance()
                sumlist.append(Constructor(
                                self._match(TokenKind.ConstructorId),
                                self._parse_optional_fields()))
            return Sum(sumlist, self._parse_optional_attributes())

    def _parse_product(self):
        return Product(self._parse_fields(), self._parse_optional_attributes())

    def _parse_fields(self):
        fields = []
        self._match(TokenKind.LParen)
        while self.cur_token.kind == TokenKind.TypeId:
            typename = self._advance()
            is_seq, is_opt = self._parse_optional_field_quantifier()
            id = (self._advance() if self.cur_token.kind in self._id_kinds
                                  else None)
            fields.append(Field(typename, id, seq=is_seq, opt=is_opt))
            if self.cur_token.kind == TokenKind.RParen:
                break
            elif self.cur_token.kind == TokenKind.Comma:
                self._advance()
        self._match(TokenKind.RParen)
        return fields

    def _parse_optional_fields(self):
        if self.cur_token.kind == TokenKind.LParen:
            return self._parse_fields()
        else:
            return None

    def _parse_optional_attributes(self):
        if self._at_keyword('attributes'):
            self._advance()
            return self._parse_fields()
        else:
            return None

    def _parse_optional_field_quantifier(self):
        is_seq, is_opt = False, False
        if self.cur_token.kind == TokenKind.Asterisk:
            is_seq = True
            self._advance()
        elif self.cur_token.kind == TokenKind.Question:
            is_opt = True
            self._advance()
        return is_seq, is_opt

    def _advance(self):
        """ Return the value of the current token and read the next one into
            self.cur_token.
        """
        cur_val = None if self.cur_token is None else self.cur_token.value
        try:
            self.cur_token = next(self._tokenizer)
        except StopIteration:
            self.cur_token = None
        return cur_val

    _id_kinds = (TokenKind.ConstructorId, TokenKind.TypeId)

    def _match(self, kind):
        """The 'match' primitive of RD parsers.

        * Verifies that the current token is of the given kind (kind can
          be a tuple, in which the kind must match one of its members).
        * Returns the value of the current token
        * Reads in the next token
        """
        if (isinstance(kind, tuple) and self.cur_token.kind in kind or
            self.cur_token.kind == kind
            ):
            value = self.cur_token.value
            self._advance()
            return value
        else:
            raise ASDLSyntaxError(
                'Unmatched {} (found {})'.format(kind, self.cur_token.kind),
                self.cur_token.lineno)

    def _at_keyword(self, keyword):
        return (self.cur_token.kind == TokenKind.TypeId and
                self.cur_token.value == keyword)