def parse(tokens):
        position = tokens.peek().position

        name = None
        type = QualifiedName.parse(tokens)

        if isinstance(type, Identifier):
            if tokens.peek() == 'as':
                name = type.name
                tokens.expect('as')

                type = QualifiedName.parse(tokens)

        return _ExceptFilter(position, name, type)
Exemple #2
0
    def testRenameIdentifier(self):
        from nine.lexer import lex
        from ast.qualifiedname import QualifiedName

        name = QualifiedName.parse(lex('Foo.Bar.Baz'))

        newName = Attribute.renameIdentifier(name)

        self.assertEqual(newName.lhs, name.lhs)
        self.assertEqual(newName.lhs.rhs.name, name.lhs.rhs.name)
        self.assertEqual(newName.rhs.name, 'BazAttribute')
    def parse(tokens):
        from ast.qualifiedname import QualifiedName

        if tokens.peek() != "import":
            return None

        tokens.expect("import")

        namespace = QualifiedName.parse(tokens)

        if namespace is None:
            raise Exception, "Expected namespace name, got %r" % tokens.peek()

        return ImportStatement(namespace)
    def parse(tokens):
        startPos = tokens.getPosition()

        if tokens.peek() != '[':
            return None

        tokens.expect('[')

        klass = QualifiedName.parse(tokens)
        if klass is None:
            tokens.setPosition(startPos)
            return None

        tokens.expect(']')

        return Attribute(klass)
    def parse(tokens):
        if tokens.peek() != 'interface':
            return None

        position = tokens.peek().position

        tokens.expect('interface')

        name = tokens.getNext()
        if name.type != 'identifier':
            tokens.unget()
            raise error.SyntaxError(position, 'Expected interface name, got %r' % name)

        bases = []

        if tokens.peek() == '(':
            tokens.expect('(')

            while True:
                base = QualifiedName.parse(tokens)
                if base is None:
                    raise error.SyntaxError(position, 'Expected base interface, got %r' % tokens.peek())

                bases.append(base)

                if tokens.peek() == ')':
                    break
                else:
                    tokens.expect(',')

            tokens.expect(')')

        tokens.expect(':')
        tokens.expect(token.END_OF_STATEMENT)

        body = InterfaceBody.parse(tokens)
        if body is None:
            raise error.SyntaxError(position, 'Expected interface body, got %r' % tokens.peek())

        return InterfaceDecl(position, name.value, bases, body)
    def parse(tokens):
        peek = tokens.peek()
        if peek.type == 'keyword':
            name = tokens.getNext()
            if peek.value in PrimitiveTypes:
                return PrimitiveTypes[peek.value]

            elif peek.value == 'array':
                tokens.unget()
                from ast.arraytype import ArrayType
                return ArrayType.parse(tokens)

            else:
                raise error.SyntaxError, 'Expected type name, got %r' % name

        else:
            from ast.qualifiedname import QualifiedName

            type = QualifiedName.parse(tokens)
            if type is None:
                raise error.SyntaxError, 'Expected type name, got %r' % tokens.peek()

            return type
    def testLeftAssociativity(self):
        result = QualifiedName.parse(lex('Foo.Bar.Baz'))

        assert isinstance(result.lhs, QualifiedName)
        assert result.rhs.name == 'Baz'
 def testFailParse2(self):
     self.failUnlessRaises(
         error.SyntaxError,
         lambda: QualifiedName.parse(lex('blah.39'))
     )
    def testParse(self):
        result = QualifiedName.parse(lex('Foo.Bar'))

        assert isinstance(result, QualifiedName), result