Esempio n. 1
0
    def parse(tokens):
        if tokens.peek() != 'try':
            return None

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

        position = tokens.peek().position

        token = tokens.expect('try')
        eatColon()

        tryBlock = BlockStatement.parse(tokens)

        while tokens.peek() == '\n': tokens.expect('\n')

        exceptBlocks = []

        while tokens.peek() == 'except':
            tokens.expect('except')

            filter = None

            if tokens.peek() != ":":
                filter = _ExceptFilter.parse(tokens)

            eatColon()

            exceptBlock = BlockStatement.parse(tokens)
            exceptBlocks.append((filter, exceptBlock))

        while tokens.peek() == '\n': tokens.expect('\n')

        finallyBlock = None

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

            finallyBlock = BlockStatement.parse(tokens)

        while tokens.peek() == '\n': tokens.expect('\n')

        if not exceptBlocks and finallyBlock is None:
            raise error.SyntaxError(position, 'Try statement requires except or finally block')

        return TryStatement(position, tryBlock, exceptBlocks, finallyBlock)
Esempio n. 2
0
    def parse(tokens):
        statementList = []

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

        tokens.getNext()
        if keyword == "if":
            arg = Expression.parse(tokens)
            if arg is None:
                raise error.SyntaxError, "Expected expression at %r" % tokens.peek()

        if tokens.peek() != ":":
            raise error.SyntaxError, "Expected ':' at end of line for '" + str(keyword) + " " + repr(arg) + "'"

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

        block = BlockStatement.parse(tokens)

        statementList.append((keyword, arg, block))

        while True:
            keyword = tokens.peek()

            if keyword not in ("elif", "else"):
                break

            tokens.expect(keyword)

            if keyword in ("elif",):
                arg = Expression.parse(tokens)
                if arg is None:
                    raise error.SyntaxError, "Expected value at " + repr(tokens.peek())
            else:
                arg = None

            if tokens.peek() != ":":
                raise error.SyntaxError, "Expected ':' at end of line for '" + str(keyword) + " " + repr(arg) + "'"

            tokens.expect(":")
            tokens.expect(token.END_OF_STATEMENT)
            block = BlockStatement.parse(tokens)

            statementList.append((keyword, arg, block))

        return IfStatement(statementList)
    def parse(tokens):
        if tokens.peek() != 'while':
            return None
        tokens.getNext()
        arg = Expression.parse(tokens)
        if arg is None:
            raise SyntaxError(tokens.peek().position, "Expected value at " + repr(tokens.peek()))

        tokens.expect(':')
        tokens.getNext()

        block = BlockStatement.parse(tokens)
        return WhileStatement(arg, block)
Esempio n. 4
0
    def testGoodParse(self):
        tokens = lex(util.source('''
            print 'blah'
                print '1!'
                print '2!'
            un_indent_ahoy_and_something_fish'''
        ))

        preamble = PrintStatement.parse(tokens)
        assert isinstance(preamble, PrintStatement)
        assert tokens.getNext() is END_OF_STATEMENT

        assert tokens.peek() is BEGIN_BLOCK, tokens.peek()

        block = BlockStatement.parse(tokens)
        assert block is not None, tokens
Esempio n. 5
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)
Esempio n. 6
0
    def parse(tokens):
        from ast.parameter import Parameter

        oldPos = tokens.getPosition()
        flags = memberflags.MemberFlags()

        position = tokens.peek().position

        while True:
            tok = tokens.getNext()
            if tok == 'static':
                flags.static = True
            elif tok == 'virtual':
                flags.virtual = True
            elif tok == 'abstract':
                flags.abstract = True
            elif tok == 'override':
                flags.override = True
            elif tok == 'sealed':
                flags.sealed = True
            else:
                tokens.unget()
                break

        if tokens.peek() != 'def':
            tokens.setPosition(oldPos)
            return None

        tokens.getNext()

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

        tokens.expect('(')

        # parameters
        params = []
        while True:
            param = Parameter.parse(tokens)
            if param is not None:
                params.append(param)

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

            elif peek == ')':
                break

            else:
                raise error.SyntaxError, "Expected ',' or ')', got %r" % peek

        tokens.expect(')')

        if tokens.peek() == 'as':
            tokens.expect('as')
            returnType = vartypes.Type.parse(tokens)
            if returnType is None:
                raise error.SyntaxError(position, 'Expected return type, got %r' % tokens.peek())
        else:
            returnType = vartypes.VoidType

        if flags.abstract:
            if tokens.peek() == ':':
                raise error.SyntaxError(position, 'Abstract function cannot have definition.')
            tokens.expect(token.END_OF_STATEMENT)
            body = None

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

            body = BlockStatement.parse(tokens)

            if body is None:
                raise error.SyntaxError, 'Expected indented block and function body, got %r' % tokens.peek()

        return FunctionDecl(name.value, name.position, returnType, params, body, flags)