Exemple #1
0
def ct_mypgen (name, src, env):
    assert name is not None
    grammar_obj = pgen_to_grammar_obj(src)
    env = env.copy()
    env["quote_%s" % name] = makequote(
        PyPgen.PyPgenParser(grammar_obj).parseString)
    ast, env = env["myfrontend"]("from basil.parsing.PyPgen import "
                                 "PyPgenParser\n%s_gobj = %s\n"
                                 "%s_parser = PyPgenParser(%s_gobj)\n" %
                                 (name, pprint.pformat(grammar_obj), name,
                                  name), env)
    return ast.body, env
Exemple #2
0
 def test_makequote_with_mutation (self):
     DUMMY_VALUE = "kookamungous"
     def myotherprocessor (source, env):
         if "sideeffectuals" in env:
             env["sideeffectuals"]["otherval"] = DUMMY_VALUE
         return DUMMY_VALUE, env
     env = mybuiltins.initial_environment()
     env["sideeffectuals"] = {}
     env["myquote2"] = mybuiltins.makequote(myotherprocessor, True)
     _, env = env["myfront"](None, "quote [myquote2] myval2: anything2\n\n",
                             env)
     self.assertEquals(env["myval2"], env["sideeffectuals"]["otherval"])
     self.assertEquals(env["myval2"], DUMMY_VALUE)
     del env["myval2"]
     env["sideeffectuals"] = {}
     _, env = env["myfront"](None, "quote [myquote2]:\n\tI'm serious,\n\t"
                             "anything.\n\n", env)
     self.assertEquals(env["sideeffectuals"]["otherval"], DUMMY_VALUE)
     self.failUnlessRaises(KeyError, env.__getitem__, "myval2")
Exemple #3
0
grammar_obj_to_python_src = pprint.pformat

def pgen_to_python (source):
    return grammar_obj_to_python_src(pgen_to_grammar_obj(source))

def ct_mypgen (name, src, env):
    assert name is not None
    grammar_obj = pgen_to_grammar_obj(src)
    env = env.copy()
    env["quote_%s" % name] = makequote(
        PyPgen.PyPgenParser(grammar_obj).parseString)
    ast, env = env["myfrontend"]("from basil.parsing.PyPgen import "
                                 "PyPgenParser\n%s_gobj = %s\n"
                                 "%s_parser = PyPgenParser(%s_gobj)\n" %
                                 (name, pprint.pformat(grammar_obj), name,
                                  name), env)
    return ast.body, env

mypgen = makequote(pgen_to_grammar_obj)

DEMO_GRAMMAR = """start: term ('+' term)*
term: atom ('*' atom)*
atom: NAME | NUMBER
"""

if __name__ == "__main__":
    print pgen_to_python(DEMO_GRAMMAR)

# ______________________________________________________________________
# End of mypgen.py
Exemple #4
0
#! /usr/bin/env python
# ______________________________________________________________________

from basil.lang.mython import mybuiltins

def nonquote_myboth (src, env = None):
    if env is None:
        env = mybuiltins.initial_environment()
    ast, env = env["myfrontend"](src, env)
    env = env.copy()
    _, env = env["myeval"](ast, env)
    return ast.body, env

def myboth (name, src, env):
    ast_list, env = nonquote_myboth(src, env)
    if name is not None:
        env[name] = ast_list
    return ast_list, env

mystring = mybuiltins.makequote(str)

# ______________________________________________________________________
# End of myutils.py