Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3/pgen2
File: driver.py
# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
# Modifications:
[3] Fix | Delete
# Copyright 2006 Google, Inc. All Rights Reserved.
[4] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[5] Fix | Delete
[6] Fix | Delete
"""Parser driver.
[7] Fix | Delete
[8] Fix | Delete
This provides a high-level interface to parse a file into a syntax tree.
[9] Fix | Delete
[10] Fix | Delete
"""
[11] Fix | Delete
[12] Fix | Delete
__author__ = "Guido van Rossum <guido@python.org>"
[13] Fix | Delete
[14] Fix | Delete
__all__ = ["Driver", "load_grammar"]
[15] Fix | Delete
[16] Fix | Delete
# Python imports
[17] Fix | Delete
import codecs
[18] Fix | Delete
import os
[19] Fix | Delete
import logging
[20] Fix | Delete
import pkgutil
[21] Fix | Delete
import StringIO
[22] Fix | Delete
import sys
[23] Fix | Delete
[24] Fix | Delete
# Pgen imports
[25] Fix | Delete
from . import grammar, parse, token, tokenize, pgen
[26] Fix | Delete
[27] Fix | Delete
[28] Fix | Delete
class Driver(object):
[29] Fix | Delete
[30] Fix | Delete
def __init__(self, grammar, convert=None, logger=None):
[31] Fix | Delete
self.grammar = grammar
[32] Fix | Delete
if logger is None:
[33] Fix | Delete
logger = logging.getLogger()
[34] Fix | Delete
self.logger = logger
[35] Fix | Delete
self.convert = convert
[36] Fix | Delete
[37] Fix | Delete
def parse_tokens(self, tokens, debug=False):
[38] Fix | Delete
"""Parse a series of tokens and return the syntax tree."""
[39] Fix | Delete
# XXX Move the prefix computation into a wrapper around tokenize.
[40] Fix | Delete
p = parse.Parser(self.grammar, self.convert)
[41] Fix | Delete
p.setup()
[42] Fix | Delete
lineno = 1
[43] Fix | Delete
column = 0
[44] Fix | Delete
type = value = start = end = line_text = None
[45] Fix | Delete
prefix = u""
[46] Fix | Delete
for quintuple in tokens:
[47] Fix | Delete
type, value, start, end, line_text = quintuple
[48] Fix | Delete
if start != (lineno, column):
[49] Fix | Delete
assert (lineno, column) <= start, ((lineno, column), start)
[50] Fix | Delete
s_lineno, s_column = start
[51] Fix | Delete
if lineno < s_lineno:
[52] Fix | Delete
prefix += "\n" * (s_lineno - lineno)
[53] Fix | Delete
lineno = s_lineno
[54] Fix | Delete
column = 0
[55] Fix | Delete
if column < s_column:
[56] Fix | Delete
prefix += line_text[column:s_column]
[57] Fix | Delete
column = s_column
[58] Fix | Delete
if type in (tokenize.COMMENT, tokenize.NL):
[59] Fix | Delete
prefix += value
[60] Fix | Delete
lineno, column = end
[61] Fix | Delete
if value.endswith("\n"):
[62] Fix | Delete
lineno += 1
[63] Fix | Delete
column = 0
[64] Fix | Delete
continue
[65] Fix | Delete
if type == token.OP:
[66] Fix | Delete
type = grammar.opmap[value]
[67] Fix | Delete
if debug:
[68] Fix | Delete
self.logger.debug("%s %r (prefix=%r)",
[69] Fix | Delete
token.tok_name[type], value, prefix)
[70] Fix | Delete
if p.addtoken(type, value, (prefix, start)):
[71] Fix | Delete
if debug:
[72] Fix | Delete
self.logger.debug("Stop.")
[73] Fix | Delete
break
[74] Fix | Delete
prefix = ""
[75] Fix | Delete
lineno, column = end
[76] Fix | Delete
if value.endswith("\n"):
[77] Fix | Delete
lineno += 1
[78] Fix | Delete
column = 0
[79] Fix | Delete
else:
[80] Fix | Delete
# We never broke out -- EOF is too soon (how can this happen???)
[81] Fix | Delete
raise parse.ParseError("incomplete input",
[82] Fix | Delete
type, value, (prefix, start))
[83] Fix | Delete
return p.rootnode
[84] Fix | Delete
[85] Fix | Delete
def parse_stream_raw(self, stream, debug=False):
[86] Fix | Delete
"""Parse a stream and return the syntax tree."""
[87] Fix | Delete
tokens = tokenize.generate_tokens(stream.readline)
[88] Fix | Delete
return self.parse_tokens(tokens, debug)
[89] Fix | Delete
[90] Fix | Delete
def parse_stream(self, stream, debug=False):
[91] Fix | Delete
"""Parse a stream and return the syntax tree."""
[92] Fix | Delete
return self.parse_stream_raw(stream, debug)
[93] Fix | Delete
[94] Fix | Delete
def parse_file(self, filename, encoding=None, debug=False):
[95] Fix | Delete
"""Parse a file and return the syntax tree."""
[96] Fix | Delete
stream = codecs.open(filename, "r", encoding)
[97] Fix | Delete
try:
[98] Fix | Delete
return self.parse_stream(stream, debug)
[99] Fix | Delete
finally:
[100] Fix | Delete
stream.close()
[101] Fix | Delete
[102] Fix | Delete
def parse_string(self, text, debug=False):
[103] Fix | Delete
"""Parse a string and return the syntax tree."""
[104] Fix | Delete
tokens = tokenize.generate_tokens(StringIO.StringIO(text).readline)
[105] Fix | Delete
return self.parse_tokens(tokens, debug)
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
def _generate_pickle_name(gt):
[109] Fix | Delete
head, tail = os.path.splitext(gt)
[110] Fix | Delete
if tail == ".txt":
[111] Fix | Delete
tail = ""
[112] Fix | Delete
return head + tail + ".".join(map(str, sys.version_info)) + ".pickle"
[113] Fix | Delete
[114] Fix | Delete
[115] Fix | Delete
def load_grammar(gt="Grammar.txt", gp=None,
[116] Fix | Delete
save=True, force=False, logger=None):
[117] Fix | Delete
"""Load the grammar (maybe from a pickle)."""
[118] Fix | Delete
if logger is None:
[119] Fix | Delete
logger = logging.getLogger()
[120] Fix | Delete
gp = _generate_pickle_name(gt) if gp is None else gp
[121] Fix | Delete
if force or not _newer(gp, gt):
[122] Fix | Delete
logger.info("Generating grammar tables from %s", gt)
[123] Fix | Delete
g = pgen.generate_grammar(gt)
[124] Fix | Delete
if save:
[125] Fix | Delete
logger.info("Writing grammar tables to %s", gp)
[126] Fix | Delete
try:
[127] Fix | Delete
g.dump(gp)
[128] Fix | Delete
except IOError as e:
[129] Fix | Delete
logger.info("Writing failed: %s", e)
[130] Fix | Delete
else:
[131] Fix | Delete
g = grammar.Grammar()
[132] Fix | Delete
g.load(gp)
[133] Fix | Delete
return g
[134] Fix | Delete
[135] Fix | Delete
[136] Fix | Delete
def _newer(a, b):
[137] Fix | Delete
"""Inquire whether file a was written since file b."""
[138] Fix | Delete
if not os.path.exists(a):
[139] Fix | Delete
return False
[140] Fix | Delete
if not os.path.exists(b):
[141] Fix | Delete
return True
[142] Fix | Delete
return os.path.getmtime(a) >= os.path.getmtime(b)
[143] Fix | Delete
[144] Fix | Delete
[145] Fix | Delete
def load_packaged_grammar(package, grammar_source):
[146] Fix | Delete
"""Normally, loads a pickled grammar by doing
[147] Fix | Delete
pkgutil.get_data(package, pickled_grammar)
[148] Fix | Delete
where *pickled_grammar* is computed from *grammar_source* by adding the
[149] Fix | Delete
Python version and using a ``.pickle`` extension.
[150] Fix | Delete
[151] Fix | Delete
However, if *grammar_source* is an extant file, load_grammar(grammar_source)
[152] Fix | Delete
is called instead. This facilitates using a packaged grammar file when needed
[153] Fix | Delete
but preserves load_grammar's automatic regeneration behavior when possible.
[154] Fix | Delete
[155] Fix | Delete
"""
[156] Fix | Delete
if os.path.isfile(grammar_source):
[157] Fix | Delete
return load_grammar(grammar_source)
[158] Fix | Delete
pickled_name = _generate_pickle_name(os.path.basename(grammar_source))
[159] Fix | Delete
data = pkgutil.get_data(package, pickled_name)
[160] Fix | Delete
g = grammar.Grammar()
[161] Fix | Delete
g.loads(data)
[162] Fix | Delete
return g
[163] Fix | Delete
[164] Fix | Delete
[165] Fix | Delete
def main(*args):
[166] Fix | Delete
"""Main program, when run as a script: produce grammar pickle files.
[167] Fix | Delete
[168] Fix | Delete
Calls load_grammar for each argument, a path to a grammar text file.
[169] Fix | Delete
"""
[170] Fix | Delete
if not args:
[171] Fix | Delete
args = sys.argv[1:]
[172] Fix | Delete
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
[173] Fix | Delete
format='%(message)s')
[174] Fix | Delete
for gt in args:
[175] Fix | Delete
load_grammar(gt, save=True, force=True)
[176] Fix | Delete
return True
[177] Fix | Delete
[178] Fix | Delete
if __name__ == "__main__":
[179] Fix | Delete
sys.exit(int(not main()))
[180] Fix | Delete
[181] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function