def parse(tokens): from ast.vartypes import Type from ast.expression import Expression if tokens.peek() != "array": return None position = tokens.peek().position tokens.expect("array") tokens.expect("(") arrayType = Type.parse(tokens) if arrayType is None: raise error.SyntaxError(position, 'Expected type after "(", got %r' % tokens.peek()) tokens.expect(",") size = [] while True: s = Expression.parse(tokens) if s is None: raise error.SyntaxError(position, "Expected expression for array size, got %r" % tokens.peek()) size.append(s) if tokens.peek() == ",": tokens.expect(",") else: break tokens.expect(")") return NewArrayExpression(position, arrayType, size)
def parseCast(tokens): from ast.vartypes import Type tokens.expect('as') type = Type.parse(tokens) if type is None: raise error.SyntaxError, "Expected type name after 'as', got %r" % tokens.peek() return type
def parse(tokens): flags = memberflags.MemberFlags() position = tokens.peek().position oldPosition = tokens.getPosition() while True: tok = tokens.getNext() if tok == 'abstract': flags.abstract = True elif tok == 'sealed': flags.sealed = True else: tokens.unget() break if tokens.peek() != 'class': tokens.setPosition(oldPosition) return None tokens.expect('class') name = tokens.getNext() if name.type != 'identifier': tokens.unget() raise error.SyntaxError(position, 'Expected class name, got %r' % name) bases = [] if tokens.peek() == '(': tokens.expect('(') # parse base classes while tokens.peek() != ')': base = Type.parse(tokens) if base is None: raise error.SyntaxError(tokens.peek().position, 'Expected type, got %r' % tokens.peek()) bases.append(base) if tokens.peek() == ',': tokens.expect(',') else: tokens.expect(')') break assert tokens.peek() != '(', 'Internal error at %r: Inheritance is NYI' % tokens.peek() tokens.expect(':') tokens.expect(token.END_OF_STATEMENT) body = ClassBody.parse(tokens) if body is None: raise error.SyntaxError, 'Expected class body, got %r' % tokens.peek() return ClassDecl(position, name.value, bases, body, flags)
def parse(tokens): from ast.vartypes import Type tok = tokens.getNext() if tok.type != 'identifier': tokens.unget() return None position = tok.position name = tok.value type = None if tokens.peek() == 'as': tokens.expect('as') type = Type.parse(tokens) if type is None: raise error.SyntaxError(position, 'Expected type after "as", got %r' % tokens.peek()) else: raise error.InternalError(position, 'Untyped parameters are not yet implemented') return Parameter(position, name, type)