Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../lib64/python2..../lib2to3/pgen2
File: conv.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
"""Convert graminit.[ch] spit out by pgen to Python code.
[3] Fix | Delete
[4] Fix | Delete
Pgen is the Python parser generator. It is useful to quickly create a
[5] Fix | Delete
parser from a grammar file in Python's grammar notation. But I don't
[6] Fix | Delete
want my parsers to be written in C (yet), so I'm translating the
[7] Fix | Delete
parsing tables to Python data structures and writing a Python parse
[8] Fix | Delete
engine.
[9] Fix | Delete
[10] Fix | Delete
Note that the token numbers are constants determined by the standard
[11] Fix | Delete
Python tokenizer. The standard token module defines these numbers and
[12] Fix | Delete
their names (the names are not used much). The token numbers are
[13] Fix | Delete
hardcoded into the Python tokenizer and into pgen. A Python
[14] Fix | Delete
implementation of the Python tokenizer is also available, in the
[15] Fix | Delete
standard tokenize module.
[16] Fix | Delete
[17] Fix | Delete
On the other hand, symbol numbers (representing the grammar's
[18] Fix | Delete
non-terminals) are assigned by pgen based on the actual grammar
[19] Fix | Delete
input.
[20] Fix | Delete
[21] Fix | Delete
Note: this module is pretty much obsolete; the pgen module generates
[22] Fix | Delete
equivalent grammar tables directly from the Grammar.txt input file
[23] Fix | Delete
without having to invoke the Python pgen C program.
[24] Fix | Delete
[25] Fix | Delete
"""
[26] Fix | Delete
[27] Fix | Delete
# Python imports
[28] Fix | Delete
import re
[29] Fix | Delete
[30] Fix | Delete
# Local imports
[31] Fix | Delete
from pgen2 import grammar, token
[32] Fix | Delete
[33] Fix | Delete
[34] Fix | Delete
class Converter(grammar.Grammar):
[35] Fix | Delete
"""Grammar subclass that reads classic pgen output files.
[36] Fix | Delete
[37] Fix | Delete
The run() method reads the tables as produced by the pgen parser
[38] Fix | Delete
generator, typically contained in two C files, graminit.h and
[39] Fix | Delete
graminit.c. The other methods are for internal use only.
[40] Fix | Delete
[41] Fix | Delete
See the base class for more documentation.
[42] Fix | Delete
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
def run(self, graminit_h, graminit_c):
[46] Fix | Delete
"""Load the grammar tables from the text files written by pgen."""
[47] Fix | Delete
self.parse_graminit_h(graminit_h)
[48] Fix | Delete
self.parse_graminit_c(graminit_c)
[49] Fix | Delete
self.finish_off()
[50] Fix | Delete
[51] Fix | Delete
def parse_graminit_h(self, filename):
[52] Fix | Delete
"""Parse the .h file written by pgen. (Internal)
[53] Fix | Delete
[54] Fix | Delete
This file is a sequence of #define statements defining the
[55] Fix | Delete
nonterminals of the grammar as numbers. We build two tables
[56] Fix | Delete
mapping the numbers to names and back.
[57] Fix | Delete
[58] Fix | Delete
"""
[59] Fix | Delete
try:
[60] Fix | Delete
f = open(filename)
[61] Fix | Delete
except IOError, err:
[62] Fix | Delete
print "Can't open %s: %s" % (filename, err)
[63] Fix | Delete
return False
[64] Fix | Delete
self.symbol2number = {}
[65] Fix | Delete
self.number2symbol = {}
[66] Fix | Delete
lineno = 0
[67] Fix | Delete
for line in f:
[68] Fix | Delete
lineno += 1
[69] Fix | Delete
mo = re.match(r"^#define\s+(\w+)\s+(\d+)$", line)
[70] Fix | Delete
if not mo and line.strip():
[71] Fix | Delete
print "%s(%s): can't parse %s" % (filename, lineno,
[72] Fix | Delete
line.strip())
[73] Fix | Delete
else:
[74] Fix | Delete
symbol, number = mo.groups()
[75] Fix | Delete
number = int(number)
[76] Fix | Delete
assert symbol not in self.symbol2number
[77] Fix | Delete
assert number not in self.number2symbol
[78] Fix | Delete
self.symbol2number[symbol] = number
[79] Fix | Delete
self.number2symbol[number] = symbol
[80] Fix | Delete
return True
[81] Fix | Delete
[82] Fix | Delete
def parse_graminit_c(self, filename):
[83] Fix | Delete
"""Parse the .c file written by pgen. (Internal)
[84] Fix | Delete
[85] Fix | Delete
The file looks as follows. The first two lines are always this:
[86] Fix | Delete
[87] Fix | Delete
#include "pgenheaders.h"
[88] Fix | Delete
#include "grammar.h"
[89] Fix | Delete
[90] Fix | Delete
After that come four blocks:
[91] Fix | Delete
[92] Fix | Delete
1) one or more state definitions
[93] Fix | Delete
2) a table defining dfas
[94] Fix | Delete
3) a table defining labels
[95] Fix | Delete
4) a struct defining the grammar
[96] Fix | Delete
[97] Fix | Delete
A state definition has the following form:
[98] Fix | Delete
- one or more arc arrays, each of the form:
[99] Fix | Delete
static arc arcs_<n>_<m>[<k>] = {
[100] Fix | Delete
{<i>, <j>},
[101] Fix | Delete
...
[102] Fix | Delete
};
[103] Fix | Delete
- followed by a state array, of the form:
[104] Fix | Delete
static state states_<s>[<t>] = {
[105] Fix | Delete
{<k>, arcs_<n>_<m>},
[106] Fix | Delete
...
[107] Fix | Delete
};
[108] Fix | Delete
[109] Fix | Delete
"""
[110] Fix | Delete
try:
[111] Fix | Delete
f = open(filename)
[112] Fix | Delete
except IOError, err:
[113] Fix | Delete
print "Can't open %s: %s" % (filename, err)
[114] Fix | Delete
return False
[115] Fix | Delete
# The code below essentially uses f's iterator-ness!
[116] Fix | Delete
lineno = 0
[117] Fix | Delete
[118] Fix | Delete
# Expect the two #include lines
[119] Fix | Delete
lineno, line = lineno+1, f.next()
[120] Fix | Delete
assert line == '#include "pgenheaders.h"\n', (lineno, line)
[121] Fix | Delete
lineno, line = lineno+1, f.next()
[122] Fix | Delete
assert line == '#include "grammar.h"\n', (lineno, line)
[123] Fix | Delete
[124] Fix | Delete
# Parse the state definitions
[125] Fix | Delete
lineno, line = lineno+1, f.next()
[126] Fix | Delete
allarcs = {}
[127] Fix | Delete
states = []
[128] Fix | Delete
while line.startswith("static arc "):
[129] Fix | Delete
while line.startswith("static arc "):
[130] Fix | Delete
mo = re.match(r"static arc arcs_(\d+)_(\d+)\[(\d+)\] = {$",
[131] Fix | Delete
line)
[132] Fix | Delete
assert mo, (lineno, line)
[133] Fix | Delete
n, m, k = map(int, mo.groups())
[134] Fix | Delete
arcs = []
[135] Fix | Delete
for _ in range(k):
[136] Fix | Delete
lineno, line = lineno+1, f.next()
[137] Fix | Delete
mo = re.match(r"\s+{(\d+), (\d+)},$", line)
[138] Fix | Delete
assert mo, (lineno, line)
[139] Fix | Delete
i, j = map(int, mo.groups())
[140] Fix | Delete
arcs.append((i, j))
[141] Fix | Delete
lineno, line = lineno+1, f.next()
[142] Fix | Delete
assert line == "};\n", (lineno, line)
[143] Fix | Delete
allarcs[(n, m)] = arcs
[144] Fix | Delete
lineno, line = lineno+1, f.next()
[145] Fix | Delete
mo = re.match(r"static state states_(\d+)\[(\d+)\] = {$", line)
[146] Fix | Delete
assert mo, (lineno, line)
[147] Fix | Delete
s, t = map(int, mo.groups())
[148] Fix | Delete
assert s == len(states), (lineno, line)
[149] Fix | Delete
state = []
[150] Fix | Delete
for _ in range(t):
[151] Fix | Delete
lineno, line = lineno+1, f.next()
[152] Fix | Delete
mo = re.match(r"\s+{(\d+), arcs_(\d+)_(\d+)},$", line)
[153] Fix | Delete
assert mo, (lineno, line)
[154] Fix | Delete
k, n, m = map(int, mo.groups())
[155] Fix | Delete
arcs = allarcs[n, m]
[156] Fix | Delete
assert k == len(arcs), (lineno, line)
[157] Fix | Delete
state.append(arcs)
[158] Fix | Delete
states.append(state)
[159] Fix | Delete
lineno, line = lineno+1, f.next()
[160] Fix | Delete
assert line == "};\n", (lineno, line)
[161] Fix | Delete
lineno, line = lineno+1, f.next()
[162] Fix | Delete
self.states = states
[163] Fix | Delete
[164] Fix | Delete
# Parse the dfas
[165] Fix | Delete
dfas = {}
[166] Fix | Delete
mo = re.match(r"static dfa dfas\[(\d+)\] = {$", line)
[167] Fix | Delete
assert mo, (lineno, line)
[168] Fix | Delete
ndfas = int(mo.group(1))
[169] Fix | Delete
for i in range(ndfas):
[170] Fix | Delete
lineno, line = lineno+1, f.next()
[171] Fix | Delete
mo = re.match(r'\s+{(\d+), "(\w+)", (\d+), (\d+), states_(\d+),$',
[172] Fix | Delete
line)
[173] Fix | Delete
assert mo, (lineno, line)
[174] Fix | Delete
symbol = mo.group(2)
[175] Fix | Delete
number, x, y, z = map(int, mo.group(1, 3, 4, 5))
[176] Fix | Delete
assert self.symbol2number[symbol] == number, (lineno, line)
[177] Fix | Delete
assert self.number2symbol[number] == symbol, (lineno, line)
[178] Fix | Delete
assert x == 0, (lineno, line)
[179] Fix | Delete
state = states[z]
[180] Fix | Delete
assert y == len(state), (lineno, line)
[181] Fix | Delete
lineno, line = lineno+1, f.next()
[182] Fix | Delete
mo = re.match(r'\s+("(?:\\\d\d\d)*")},$', line)
[183] Fix | Delete
assert mo, (lineno, line)
[184] Fix | Delete
first = {}
[185] Fix | Delete
rawbitset = eval(mo.group(1))
[186] Fix | Delete
for i, c in enumerate(rawbitset):
[187] Fix | Delete
byte = ord(c)
[188] Fix | Delete
for j in range(8):
[189] Fix | Delete
if byte & (1<<j):
[190] Fix | Delete
first[i*8 + j] = 1
[191] Fix | Delete
dfas[number] = (state, first)
[192] Fix | Delete
lineno, line = lineno+1, f.next()
[193] Fix | Delete
assert line == "};\n", (lineno, line)
[194] Fix | Delete
self.dfas = dfas
[195] Fix | Delete
[196] Fix | Delete
# Parse the labels
[197] Fix | Delete
labels = []
[198] Fix | Delete
lineno, line = lineno+1, f.next()
[199] Fix | Delete
mo = re.match(r"static label labels\[(\d+)\] = {$", line)
[200] Fix | Delete
assert mo, (lineno, line)
[201] Fix | Delete
nlabels = int(mo.group(1))
[202] Fix | Delete
for i in range(nlabels):
[203] Fix | Delete
lineno, line = lineno+1, f.next()
[204] Fix | Delete
mo = re.match(r'\s+{(\d+), (0|"\w+")},$', line)
[205] Fix | Delete
assert mo, (lineno, line)
[206] Fix | Delete
x, y = mo.groups()
[207] Fix | Delete
x = int(x)
[208] Fix | Delete
if y == "0":
[209] Fix | Delete
y = None
[210] Fix | Delete
else:
[211] Fix | Delete
y = eval(y)
[212] Fix | Delete
labels.append((x, y))
[213] Fix | Delete
lineno, line = lineno+1, f.next()
[214] Fix | Delete
assert line == "};\n", (lineno, line)
[215] Fix | Delete
self.labels = labels
[216] Fix | Delete
[217] Fix | Delete
# Parse the grammar struct
[218] Fix | Delete
lineno, line = lineno+1, f.next()
[219] Fix | Delete
assert line == "grammar _PyParser_Grammar = {\n", (lineno, line)
[220] Fix | Delete
lineno, line = lineno+1, f.next()
[221] Fix | Delete
mo = re.match(r"\s+(\d+),$", line)
[222] Fix | Delete
assert mo, (lineno, line)
[223] Fix | Delete
ndfas = int(mo.group(1))
[224] Fix | Delete
assert ndfas == len(self.dfas)
[225] Fix | Delete
lineno, line = lineno+1, f.next()
[226] Fix | Delete
assert line == "\tdfas,\n", (lineno, line)
[227] Fix | Delete
lineno, line = lineno+1, f.next()
[228] Fix | Delete
mo = re.match(r"\s+{(\d+), labels},$", line)
[229] Fix | Delete
assert mo, (lineno, line)
[230] Fix | Delete
nlabels = int(mo.group(1))
[231] Fix | Delete
assert nlabels == len(self.labels), (lineno, line)
[232] Fix | Delete
lineno, line = lineno+1, f.next()
[233] Fix | Delete
mo = re.match(r"\s+(\d+)$", line)
[234] Fix | Delete
assert mo, (lineno, line)
[235] Fix | Delete
start = int(mo.group(1))
[236] Fix | Delete
assert start in self.number2symbol, (lineno, line)
[237] Fix | Delete
self.start = start
[238] Fix | Delete
lineno, line = lineno+1, f.next()
[239] Fix | Delete
assert line == "};\n", (lineno, line)
[240] Fix | Delete
try:
[241] Fix | Delete
lineno, line = lineno+1, f.next()
[242] Fix | Delete
except StopIteration:
[243] Fix | Delete
pass
[244] Fix | Delete
else:
[245] Fix | Delete
assert 0, (lineno, line)
[246] Fix | Delete
[247] Fix | Delete
def finish_off(self):
[248] Fix | Delete
"""Create additional useful structures. (Internal)."""
[249] Fix | Delete
self.keywords = {} # map from keyword strings to arc labels
[250] Fix | Delete
self.tokens = {} # map from numeric token values to arc labels
[251] Fix | Delete
for ilabel, (type, value) in enumerate(self.labels):
[252] Fix | Delete
if type == token.NAME and value is not None:
[253] Fix | Delete
self.keywords[value] = ilabel
[254] Fix | Delete
elif value is None:
[255] Fix | Delete
self.tokens[type] = ilabel
[256] Fix | Delete
[257] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function