def parse(tokens):
        from ast.statement import Statement
        if tokens.peek() is not BEGIN_BLOCK:
            return None
        tokens.getNext()

        children = list()

        while tokens.peek() is not END_BLOCK:
            stmt = Statement.parse(tokens)
            if stmt is None:
                break
            children.append(stmt)
        tokens.expect(END_BLOCK)

        return BlockStatement(children)
Esempio n. 2
0
def parse(tokens):
    '''Parses a token stream into an abstract syntax tree.
    '''
    statements = []
    while not tokens.eof():
        while tokens.peek() is token.END_OF_STATEMENT:
            tokens.expect(token.END_OF_STATEMENT)

        if tokens.peek() is token.END_OF_FILE:
            break

        decl = Declaration.parse(tokens)
        if decl is not None:
            statements.append(decl)
            continue

        stmt = Statement.parse(tokens)
        if stmt is not None:
            statements.append(stmt)
            continue

        raise error.SyntaxError(tokens.peek().position, 'Syntax error at %r' % tokens.peek())

    return statements