@@ -31,6 +31,7 @@
from .util_opy import log
from core import args
from core import util
# From lib2to3/pygram.py. This takes the place of the 'symbol' module.
@@ -63,13 +64,6 @@ def HostStdlibNames():
return names
def LoadGrammar (pickle_path ):
""" Load the grammar (maybe from a pickle)."""
g = grammar.Grammar()
g.load(pickle_path) # pickle.load()
return g
def WriteGrammar (grammar_path , pickle_path ):
log(" Generating grammar tables from %s " , grammar_path)
g = pgen.generate_grammar(grammar_path)
@@ -139,34 +133,35 @@ def Options():
p.add_option(
' -c' , dest = ' command' , default = None ,
help = ' Python command to run' )
p.add_option(
' -g' , dest = ' grammar' , default = None ,
help = ' Grammar pickle file to use for parsing' )
return p
# Made by the Makefile.
PICKLE_REL_PATH = ' _build/opy/py27.grammar.pickle'
def OpyCommandMain (argv ):
""" Dispatch to the right action."""
# TODO : Use core/args.
opts, argv = Options().parse_args(argv)
if opts.grammar:
gr = LoadGrammar(opts.grammar)
# In Python 2 code, always use from __future__ import print_function.
try :
del gr.keywords[" print" ]
except KeyError :
pass
loader = util.GetResourceLoader()
f = loader.open(PICKLE_REL_PATH )
gr = grammar.Grammar()
gr.load(f)
f.close()
FILE_INPUT = gr.symbol2number[' file_input' ]
# In Python 2 code, always use from __future__ import print_function.
try :
del gr.keywords[" print" ]
except KeyError :
pass
symbols = Symbols(gr)
pytree.Init(symbols) # for type_repr() pretty printing
transformer.Init(symbols) # for _names and other dicts
else :
gr = None
FILE_INPUT = None
symbols = None
FILE_INPUT = gr.symbol2number[' file_input' ]
symbols = Symbols(gr)
pytree.Init(symbols) # for type_repr() pretty printing
transformer.Init(symbols) # for _names and other dicts
# do_glue = False
do_glue = True
@@ -234,7 +229,9 @@ def py2st(gr, raw_node):
tree.PrettyPrint(sys.stdout)
log(' \t Children: %d ' % len (tree.children), file = sys.stderr)
elif action == ' old-compile' :
elif action == ' compile' :
# 'opy compile' is pgen2 + compiler2
py_path = argv[1 ]
out_path = argv[2 ]
@@ -245,8 +242,8 @@ def py2st(gr, raw_node):
else :
tr = transformer.Transformer()
f = open (py_path)
contents = f.read()
with open (py_path) as f:
contents = f.read()
co = pycodegen.compile(contents, py_path, ' exec' , transformer = tr)
log(" Code length: %d " , len (co.co_code))
@@ -256,10 +253,8 @@ def py2st(gr, raw_node):
out_f.write(h)
marshal.dump(co, out_f)
elif action == ' compile' :
# 'opy compile' is pgen2 + compiler2
# TODO : import compiler2
# raise NotImplementedError
# NOTE : Unused
elif action == ' old-compile' :
py_path = argv[1 ]
out_path = argv[2 ]
@@ -270,8 +265,8 @@ def py2st(gr, raw_node):
else :
tr = transformer.Transformer()
with open (py_path) as f:
contents = f.read()
f = open (py_path)
contents = f.read()
co = pycodegen.compile(contents, py_path, ' exec' , transformer = tr)
log(" Code length: %d " , len (co.co_code))
@@ -281,6 +276,7 @@ def py2st(gr, raw_node):
out_f.write(h)
marshal.dump(co, out_f)
# TODO : Not used
elif action == ' compile2' :
in_path = argv[1 ]
out_path = argv[2 ]
0 comments on commit
c4d603a