def testSemantic(self):
        '''Also tests variable declaration/expression things.
        The basic jist is that, once semantic testing has been done,
        an Identifier should no longer be an Identifier; it should be
        a VariableExpression.
        '''
        from ast.vardecl import VarDecl
        from ast.variableexpression import VariableExpression

        decl = VarDecl('testvar', (0, '<test>'), vartypes.IntType)

        scope = Scope(parent=None)
        scope['testvar'] = decl

        tokens = lex('testvar nonexist')
        expr1 = Identifier.parse(tokens)
        expr2 = Identifier.parse(tokens)

        result = expr1.semantic(scope)

        self.failUnlessEqual(type(result), VariableExpression)
        self.failUnlessEqual(result.variable, decl)

        self.failUnlessRaises(
            error.NameError,
            lambda: expr2.semantic(scope)
        )
Esempio n. 2
0
    def parse(tokens):
        from ast.primitiveexpression import PrimitiveExpression

        lhs = PrimitiveExpression.parse(tokens)

        if lhs is None:
            return None

        if tokens.peek() != '.':
            return lhs

        tokens.expect('.')
        expr = lhs

        while True:
            position = tokens.peek().position

            rhs = Identifier.parse(tokens)

            if rhs is None:
                raise error.SyntaxError, 'Expected identifier, got %r' % tokens.peek()

            expr = DotExpression(position, expr, rhs)

            if tokens.peek() != '.':
                break

            tokens.expect('.')
            continue

        return expr
Esempio n. 3
0
    def parse(tokens):
        if tokens.peek() is not token.BEGIN_BLOCK:
            return None

        decls = []

        tokens.expect(token.BEGIN_BLOCK)
        while tokens.peek() is not token.END_BLOCK:
            decl = (
                AssignStatement.parse(tokens) or
                Identifier.parse(tokens) or
                PassStatement.parse(tokens)
            )

            if decl is None:
                raise error.SyntaxError, 'Expected end block, value declaration, or pass statement.  Got %r' % tokens.peek()

            while tokens.peek() is token.END_OF_STATEMENT:
                tokens.getNext()

            if isinstance(decl, PassStatement): continue # Don't store pass statements.  They're not useful.

            decls.append(decl)

        tokens.expect(token.END_BLOCK)

        return EnumBody(decls)
Esempio n. 4
0
    def parse(tokens):
        from ast.identifier import Identifier

        expr = Identifier.parse(tokens)
        if expr is None:
            raise error.SyntaxError, 'Expected identifier, got %r' % tokens.peek()

        while tokens.peek() == '.':
            tokens.expect('.')
            rhs = Identifier.parse(tokens)
            if rhs is None:
                raise error.SyntaxError, 'Expected identifier after ".", got %r' % tokens.peek()

            expr = QualifiedName(expr, rhs)

        return expr
Esempio n. 5
0
    def testSemantic(self):
        # Also tests Identifier and name resolution.
        decl = VarDecl('testvar', (0, '<test>'), vartypes.IntType)

        scope = Scope(parent=None)
        scope['testvar'] = decl

        tokens = lex('testvar')
        expr = Identifier.parse(tokens)

        result = expr.semantic(scope)
        self.failUnlessEqual(type(result), VariableExpression)
        self.failUnlessEqual(result.variable, decl)
Esempio n. 6
0
    def parse(tokens):
        from ast.blockstatement import BlockStatement
        from ast.expression import Expression
        from ast.identifier import Identifier
        from ast.vardecl import VarDecl
        from ast.vartypes import Type

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

        position = tokens.peek().position
        tokens.expect('for')

        ident = Identifier.parse(tokens)
        if ident is None:
            raise error.SyntaxError(position, 'Expected iterator name, got %r' % tokens.peek())

        type = None

        """if tokens.peek() == 'as':
            tokens.expect('as')
            type = Type.parse(tokens)
            if type is None:
                raise error.SyntaxError(tokens.peek().position, 'Expected type after "as", got %r' % tokens.peek())"""

        tokens.expect('in')

        sequence = Expression.parse(tokens)
        if sequence is None:
            raise error.SyntaxError(tokens.peek().position, 'Expected expression after "in", got %r' % tokens.peek())

        tokens.expect(':')
        tokens.expect('\n')

        body = BlockStatement.parse(tokens)
        if body is None:
            raise error.SyntaxError(tokens.peek().position, 'Expected loop body after ":", got %r' % tokens.peek())

        return ForStatement(position, VarDecl(ident.name, ident.position, type), sequence, body)
    def parse(tokens):
        from ast.primitiveexpression import PrimitiveExpression
        from ast.dotexpression import DotExpression
        from ast.functioncallexpression import FunctionCallExpression
        from ast.indexexpression import IndexExpression
        from ast.castexpression import CastExpression

        position = tokens.peek().position

        lhs = PrimitiveExpression.parse(tokens)
        if lhs is None:
            return None

        while True:

            if tokens.peek() == '(':
                args = FunctionCallExpression.parseArgList(tokens)
                lhs = FunctionCallExpression(lhs.position, lhs, args)

            elif tokens.peek() == '[':
                indicies = IndexExpression.parseIndex(tokens)
                lhs = IndexExpression(lhs.position, lhs, indicies)

            elif tokens.peek() == 'as':
                type = CastExpression.parseCast(tokens)
                lhs = CastExpression(lhs.position, lhs, type)

            elif tokens.peek() == '.':
                from ast.identifier import Identifier

                tokens.expect('.')
                rhs = Identifier.parse(tokens)
                if rhs is None:
                    raise error.SyntaxError(tokens.peek().position, 'Expected identifier, got %r' % tokens.peek())

                lhs = DotExpression(lhs.position, lhs, rhs)

            else:
                return lhs
    def testParseKeywords(self):
        from ast import vartypes
        tokens = lex('int string x y')

        result = [Identifier.parse(tokens) for i in range(4)]
    def testFailParseKeyword(self):
        tokens = lex('print')

        result = Identifier.parse(tokens)
        self.failUnless(result is None)
Esempio n. 10
0
    def testGoodParse(self):
        tokens = lex('foo')

        result = Identifier.parse(tokens)
        self.failIf(result is None)