Ejemplo n.º 1
0
def parse_python(space, source, mode):
    info = pyparse.CompileInfo("<string>", mode)
    parser = pyparse.PythonParser(space)
    try:
        tree = parser.parse_source(source, info)
    except error.IndentationError, e:
        raise OperationError(space.w_IndentationError, e.wrap_info(space))
Ejemplo n.º 2
0
def bench(title):
    a = time.clock()
    info = pyparse.CompileInfo("<string>", "exec")
    parser = pyparse.PythonParser(fakespace)
    tree = parser._parse(s, info)
    b = time.clock()
    print title, (b - a)
Ejemplo n.º 3
0
def validate_node(space, tree):
    assert tree.type >= 256
    type = tree.type - 256
    parser = pyparse.PythonParser(space)
    if type >= len(parser.grammar.dfas):
        raise parse_error(space, "Unrecognized node type %d." % type)
    dfa = parser.grammar.dfas[type]
    # Run the DFA for this nonterminal
    arcs, is_accepting = dfa.states[0]
    for pos in range(tree.num_children()):
        ch = tree.get_child(pos)
        for i, next_state in arcs:
            label = parser.grammar.labels[i]
            if label == ch.type:
                # The child is acceptable; validate it recursively
                if ch.type >= 256:
                    validate_node(space, ch)
                # Update the state, and move on to the next child.
                arcs, is_accepting = dfa.states[next_state]
                break
        else:
            raise parse_error(space, "Illegal node")
    if not is_accepting:
        raise parse_error(space,
                          "Illegal number of children for %d node" % tree.type)
Ejemplo n.º 4
0
def parse_python(space, source, mode):
    info = pyparse.CompileInfo("<string>", mode)
    parser = pyparse.PythonParser(space)
    try:
        tree = parser.parse_source(source, info)
    except error.IndentationError as e:
        raise OperationError(space.w_IndentationError, e.wrap_info(space))
    except error.SyntaxError as e:
        raise OperationError(space.w_SyntaxError, e.wrap_info(space))
    return W_STType(tree, mode, recursive_parser=parser)
Ejemplo n.º 5
0
def generate_function_code(expr, space):
    p = pyparse.PythonParser(space)
    info = pyparse.CompileInfo("<test>", 'exec')
    cst = p.parse_source(expr, info)
    ast = astbuilder.ast_from_node(space, cst, info)
    function_ast = optimize.optimize_ast(space, ast.body[0], info)
    function_ast = ast.body[0]
    symbols = symtable.SymtableBuilder(space, ast, info)
    generator = codegen.FunctionCodeGenerator(space, 'function', function_ast,
                                              1, symbols, info)
    blocks = generator.first_block.post_order()
    generator._resolve_block_targets(blocks)
    return generator, blocks
Ejemplo n.º 6
0
 def setup_class(self):
     self.parser = pyparse.PythonParser(self.space)
Ejemplo n.º 7
0
 def __init__(self, space, override_version=None):
     PyCodeCompiler.__init__(self, space)
     self.future_flags = future.futureFlags_3_7
     self.parser = pyparse.PythonParser(space, self.future_flags)
     self.additional_rules = {}
     self.compiler_flags = self.future_flags.allowed_flags
Ejemplo n.º 8
0
def compile_with_astcompiler(expr, mode, space):
    p = pyparse.PythonParser(space)
    info = pyparse.CompileInfo("<test>", mode)
    cst = p.parse_source(expr, info)
    ast = astbuilder.ast_from_node(space, cst, info)
    return codegen.compile_ast(space, ast, info)
Ejemplo n.º 9
0
 def setup_class(cls):
     cls.parser = pyparse.PythonParser(cls.space)
Ejemplo n.º 10
0
def tuple2st(space, w_sequence):
    # Convert the tree to the internal form before checking it
    tree = build_node_tree(space, w_sequence)
    parser = pyparse.PythonParser(space)
    validate_node(space, tree, parser)
    return W_STType(tree, 'eval')
Ejemplo n.º 11
0
# <codecell>

from pypy.interpreter.pyparser import pyparse
from pypy.interpreter.astcompiler import ast
from pypy.interpreter.astcompiler.astbuilder import ast_from_node
from pypy.tool.pytest.objspace import TinyObjSpace

from .codecommon import normalize_name, assert_tree
from .ogm import OG

# <codecell>
# Source code to tree (AST)
space = TinyObjSpace()
pyparser = pyparse.PythonParser(space)
compile_info = pyparse.CompileInfo("<filename>", "exec")


def source_to_tree(source):
    parsetree = pyparser.parse_source(source, compile_info)
    tree = ast_from_node(space, parsetree, compile_info)
    return tree


# <codecell>
# Find nodes that define new names
def make_def_finders():

    # Class Definition
    def ClassDef(node, links):
        yield node.name
Ejemplo n.º 12
0
 def setup_class(self):
     self.parser = pyparse.PythonParser(None)