Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3
File: pytree.py
# Copyright 2006 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""
[3] Fix | Delete
Python parse tree definitions.
[4] Fix | Delete
[5] Fix | Delete
This is a very concrete parse tree; we need to keep every token and
[6] Fix | Delete
even the comments and whitespace between tokens.
[7] Fix | Delete
[8] Fix | Delete
There's also a pattern matching implementation here.
[9] Fix | Delete
"""
[10] Fix | Delete
[11] Fix | Delete
__author__ = "Guido van Rossum <guido@python.org>"
[12] Fix | Delete
[13] Fix | Delete
import sys
[14] Fix | Delete
import warnings
[15] Fix | Delete
from StringIO import StringIO
[16] Fix | Delete
[17] Fix | Delete
HUGE = 0x7FFFFFFF # maximum repeat count, default max
[18] Fix | Delete
[19] Fix | Delete
_type_reprs = {}
[20] Fix | Delete
def type_repr(type_num):
[21] Fix | Delete
global _type_reprs
[22] Fix | Delete
if not _type_reprs:
[23] Fix | Delete
from .pygram import python_symbols
[24] Fix | Delete
# printing tokens is possible but not as useful
[25] Fix | Delete
# from .pgen2 import token // token.__dict__.items():
[26] Fix | Delete
for name, val in python_symbols.__dict__.items():
[27] Fix | Delete
if type(val) == int: _type_reprs[val] = name
[28] Fix | Delete
return _type_reprs.setdefault(type_num, type_num)
[29] Fix | Delete
[30] Fix | Delete
class Base(object):
[31] Fix | Delete
[32] Fix | Delete
"""
[33] Fix | Delete
Abstract base class for Node and Leaf.
[34] Fix | Delete
[35] Fix | Delete
This provides some default functionality and boilerplate using the
[36] Fix | Delete
template pattern.
[37] Fix | Delete
[38] Fix | Delete
A node may be a subnode of at most one parent.
[39] Fix | Delete
"""
[40] Fix | Delete
[41] Fix | Delete
# Default values for instance variables
[42] Fix | Delete
type = None # int: token number (< 256) or symbol number (>= 256)
[43] Fix | Delete
parent = None # Parent node pointer, or None
[44] Fix | Delete
children = () # Tuple of subnodes
[45] Fix | Delete
was_changed = False
[46] Fix | Delete
was_checked = False
[47] Fix | Delete
[48] Fix | Delete
def __new__(cls, *args, **kwds):
[49] Fix | Delete
"""Constructor that prevents Base from being instantiated."""
[50] Fix | Delete
assert cls is not Base, "Cannot instantiate Base"
[51] Fix | Delete
return object.__new__(cls)
[52] Fix | Delete
[53] Fix | Delete
def __eq__(self, other):
[54] Fix | Delete
"""
[55] Fix | Delete
Compare two nodes for equality.
[56] Fix | Delete
[57] Fix | Delete
This calls the method _eq().
[58] Fix | Delete
"""
[59] Fix | Delete
if self.__class__ is not other.__class__:
[60] Fix | Delete
return NotImplemented
[61] Fix | Delete
return self._eq(other)
[62] Fix | Delete
[63] Fix | Delete
__hash__ = None # For Py3 compatibility.
[64] Fix | Delete
[65] Fix | Delete
def __ne__(self, other):
[66] Fix | Delete
"""
[67] Fix | Delete
Compare two nodes for inequality.
[68] Fix | Delete
[69] Fix | Delete
This calls the method _eq().
[70] Fix | Delete
"""
[71] Fix | Delete
if self.__class__ is not other.__class__:
[72] Fix | Delete
return NotImplemented
[73] Fix | Delete
return not self._eq(other)
[74] Fix | Delete
[75] Fix | Delete
def _eq(self, other):
[76] Fix | Delete
"""
[77] Fix | Delete
Compare two nodes for equality.
[78] Fix | Delete
[79] Fix | Delete
This is called by __eq__ and __ne__. It is only called if the two nodes
[80] Fix | Delete
have the same type. This must be implemented by the concrete subclass.
[81] Fix | Delete
Nodes should be considered equal if they have the same structure,
[82] Fix | Delete
ignoring the prefix string and other context information.
[83] Fix | Delete
"""
[84] Fix | Delete
raise NotImplementedError
[85] Fix | Delete
[86] Fix | Delete
def clone(self):
[87] Fix | Delete
"""
[88] Fix | Delete
Return a cloned (deep) copy of self.
[89] Fix | Delete
[90] Fix | Delete
This must be implemented by the concrete subclass.
[91] Fix | Delete
"""
[92] Fix | Delete
raise NotImplementedError
[93] Fix | Delete
[94] Fix | Delete
def post_order(self):
[95] Fix | Delete
"""
[96] Fix | Delete
Return a post-order iterator for the tree.
[97] Fix | Delete
[98] Fix | Delete
This must be implemented by the concrete subclass.
[99] Fix | Delete
"""
[100] Fix | Delete
raise NotImplementedError
[101] Fix | Delete
[102] Fix | Delete
def pre_order(self):
[103] Fix | Delete
"""
[104] Fix | Delete
Return a pre-order iterator for the tree.
[105] Fix | Delete
[106] Fix | Delete
This must be implemented by the concrete subclass.
[107] Fix | Delete
"""
[108] Fix | Delete
raise NotImplementedError
[109] Fix | Delete
[110] Fix | Delete
def set_prefix(self, prefix):
[111] Fix | Delete
"""
[112] Fix | Delete
Set the prefix for the node (see Leaf class).
[113] Fix | Delete
[114] Fix | Delete
DEPRECATED; use the prefix property directly.
[115] Fix | Delete
"""
[116] Fix | Delete
warnings.warn("set_prefix() is deprecated; use the prefix property",
[117] Fix | Delete
DeprecationWarning, stacklevel=2)
[118] Fix | Delete
self.prefix = prefix
[119] Fix | Delete
[120] Fix | Delete
def get_prefix(self):
[121] Fix | Delete
"""
[122] Fix | Delete
Return the prefix for the node (see Leaf class).
[123] Fix | Delete
[124] Fix | Delete
DEPRECATED; use the prefix property directly.
[125] Fix | Delete
"""
[126] Fix | Delete
warnings.warn("get_prefix() is deprecated; use the prefix property",
[127] Fix | Delete
DeprecationWarning, stacklevel=2)
[128] Fix | Delete
return self.prefix
[129] Fix | Delete
[130] Fix | Delete
def replace(self, new):
[131] Fix | Delete
"""Replace this node with a new one in the parent."""
[132] Fix | Delete
assert self.parent is not None, str(self)
[133] Fix | Delete
assert new is not None
[134] Fix | Delete
if not isinstance(new, list):
[135] Fix | Delete
new = [new]
[136] Fix | Delete
l_children = []
[137] Fix | Delete
found = False
[138] Fix | Delete
for ch in self.parent.children:
[139] Fix | Delete
if ch is self:
[140] Fix | Delete
assert not found, (self.parent.children, self, new)
[141] Fix | Delete
if new is not None:
[142] Fix | Delete
l_children.extend(new)
[143] Fix | Delete
found = True
[144] Fix | Delete
else:
[145] Fix | Delete
l_children.append(ch)
[146] Fix | Delete
assert found, (self.children, self, new)
[147] Fix | Delete
self.parent.changed()
[148] Fix | Delete
self.parent.children = l_children
[149] Fix | Delete
for x in new:
[150] Fix | Delete
x.parent = self.parent
[151] Fix | Delete
self.parent = None
[152] Fix | Delete
[153] Fix | Delete
def get_lineno(self):
[154] Fix | Delete
"""Return the line number which generated the invocant node."""
[155] Fix | Delete
node = self
[156] Fix | Delete
while not isinstance(node, Leaf):
[157] Fix | Delete
if not node.children:
[158] Fix | Delete
return
[159] Fix | Delete
node = node.children[0]
[160] Fix | Delete
return node.lineno
[161] Fix | Delete
[162] Fix | Delete
def changed(self):
[163] Fix | Delete
if self.parent:
[164] Fix | Delete
self.parent.changed()
[165] Fix | Delete
self.was_changed = True
[166] Fix | Delete
[167] Fix | Delete
def remove(self):
[168] Fix | Delete
"""
[169] Fix | Delete
Remove the node from the tree. Returns the position of the node in its
[170] Fix | Delete
parent's children before it was removed.
[171] Fix | Delete
"""
[172] Fix | Delete
if self.parent:
[173] Fix | Delete
for i, node in enumerate(self.parent.children):
[174] Fix | Delete
if node is self:
[175] Fix | Delete
self.parent.changed()
[176] Fix | Delete
del self.parent.children[i]
[177] Fix | Delete
self.parent = None
[178] Fix | Delete
return i
[179] Fix | Delete
[180] Fix | Delete
@property
[181] Fix | Delete
def next_sibling(self):
[182] Fix | Delete
"""
[183] Fix | Delete
The node immediately following the invocant in their parent's children
[184] Fix | Delete
list. If the invocant does not have a next sibling, it is None
[185] Fix | Delete
"""
[186] Fix | Delete
if self.parent is None:
[187] Fix | Delete
return None
[188] Fix | Delete
[189] Fix | Delete
# Can't use index(); we need to test by identity
[190] Fix | Delete
for i, child in enumerate(self.parent.children):
[191] Fix | Delete
if child is self:
[192] Fix | Delete
try:
[193] Fix | Delete
return self.parent.children[i+1]
[194] Fix | Delete
except IndexError:
[195] Fix | Delete
return None
[196] Fix | Delete
[197] Fix | Delete
@property
[198] Fix | Delete
def prev_sibling(self):
[199] Fix | Delete
"""
[200] Fix | Delete
The node immediately preceding the invocant in their parent's children
[201] Fix | Delete
list. If the invocant does not have a previous sibling, it is None.
[202] Fix | Delete
"""
[203] Fix | Delete
if self.parent is None:
[204] Fix | Delete
return None
[205] Fix | Delete
[206] Fix | Delete
# Can't use index(); we need to test by identity
[207] Fix | Delete
for i, child in enumerate(self.parent.children):
[208] Fix | Delete
if child is self:
[209] Fix | Delete
if i == 0:
[210] Fix | Delete
return None
[211] Fix | Delete
return self.parent.children[i-1]
[212] Fix | Delete
[213] Fix | Delete
def leaves(self):
[214] Fix | Delete
for child in self.children:
[215] Fix | Delete
for x in child.leaves():
[216] Fix | Delete
yield x
[217] Fix | Delete
[218] Fix | Delete
def depth(self):
[219] Fix | Delete
if self.parent is None:
[220] Fix | Delete
return 0
[221] Fix | Delete
return 1 + self.parent.depth()
[222] Fix | Delete
[223] Fix | Delete
def get_suffix(self):
[224] Fix | Delete
"""
[225] Fix | Delete
Return the string immediately following the invocant node. This is
[226] Fix | Delete
effectively equivalent to node.next_sibling.prefix
[227] Fix | Delete
"""
[228] Fix | Delete
next_sib = self.next_sibling
[229] Fix | Delete
if next_sib is None:
[230] Fix | Delete
return u""
[231] Fix | Delete
return next_sib.prefix
[232] Fix | Delete
[233] Fix | Delete
if sys.version_info < (3, 0):
[234] Fix | Delete
def __str__(self):
[235] Fix | Delete
return unicode(self).encode("ascii")
[236] Fix | Delete
[237] Fix | Delete
class Node(Base):
[238] Fix | Delete
[239] Fix | Delete
"""Concrete implementation for interior nodes."""
[240] Fix | Delete
[241] Fix | Delete
def __init__(self,type, children,
[242] Fix | Delete
context=None,
[243] Fix | Delete
prefix=None,
[244] Fix | Delete
fixers_applied=None):
[245] Fix | Delete
"""
[246] Fix | Delete
Initializer.
[247] Fix | Delete
[248] Fix | Delete
Takes a type constant (a symbol number >= 256), a sequence of
[249] Fix | Delete
child nodes, and an optional context keyword argument.
[250] Fix | Delete
[251] Fix | Delete
As a side effect, the parent pointers of the children are updated.
[252] Fix | Delete
"""
[253] Fix | Delete
assert type >= 256, type
[254] Fix | Delete
self.type = type
[255] Fix | Delete
self.children = list(children)
[256] Fix | Delete
for ch in self.children:
[257] Fix | Delete
assert ch.parent is None, repr(ch)
[258] Fix | Delete
ch.parent = self
[259] Fix | Delete
if prefix is not None:
[260] Fix | Delete
self.prefix = prefix
[261] Fix | Delete
if fixers_applied:
[262] Fix | Delete
self.fixers_applied = fixers_applied[:]
[263] Fix | Delete
else:
[264] Fix | Delete
self.fixers_applied = None
[265] Fix | Delete
[266] Fix | Delete
def __repr__(self):
[267] Fix | Delete
"""Return a canonical string representation."""
[268] Fix | Delete
return "%s(%s, %r)" % (self.__class__.__name__,
[269] Fix | Delete
type_repr(self.type),
[270] Fix | Delete
self.children)
[271] Fix | Delete
[272] Fix | Delete
def __unicode__(self):
[273] Fix | Delete
"""
[274] Fix | Delete
Return a pretty string representation.
[275] Fix | Delete
[276] Fix | Delete
This reproduces the input source exactly.
[277] Fix | Delete
"""
[278] Fix | Delete
return u"".join(map(unicode, self.children))
[279] Fix | Delete
[280] Fix | Delete
if sys.version_info > (3, 0):
[281] Fix | Delete
__str__ = __unicode__
[282] Fix | Delete
[283] Fix | Delete
def _eq(self, other):
[284] Fix | Delete
"""Compare two nodes for equality."""
[285] Fix | Delete
return (self.type, self.children) == (other.type, other.children)
[286] Fix | Delete
[287] Fix | Delete
def clone(self):
[288] Fix | Delete
"""Return a cloned (deep) copy of self."""
[289] Fix | Delete
return Node(self.type, [ch.clone() for ch in self.children],
[290] Fix | Delete
fixers_applied=self.fixers_applied)
[291] Fix | Delete
[292] Fix | Delete
def post_order(self):
[293] Fix | Delete
"""Return a post-order iterator for the tree."""
[294] Fix | Delete
for child in self.children:
[295] Fix | Delete
for node in child.post_order():
[296] Fix | Delete
yield node
[297] Fix | Delete
yield self
[298] Fix | Delete
[299] Fix | Delete
def pre_order(self):
[300] Fix | Delete
"""Return a pre-order iterator for the tree."""
[301] Fix | Delete
yield self
[302] Fix | Delete
for child in self.children:
[303] Fix | Delete
for node in child.pre_order():
[304] Fix | Delete
yield node
[305] Fix | Delete
[306] Fix | Delete
def _prefix_getter(self):
[307] Fix | Delete
"""
[308] Fix | Delete
The whitespace and comments preceding this node in the input.
[309] Fix | Delete
"""
[310] Fix | Delete
if not self.children:
[311] Fix | Delete
return ""
[312] Fix | Delete
return self.children[0].prefix
[313] Fix | Delete
[314] Fix | Delete
def _prefix_setter(self, prefix):
[315] Fix | Delete
if self.children:
[316] Fix | Delete
self.children[0].prefix = prefix
[317] Fix | Delete
[318] Fix | Delete
prefix = property(_prefix_getter, _prefix_setter)
[319] Fix | Delete
[320] Fix | Delete
def set_child(self, i, child):
[321] Fix | Delete
"""
[322] Fix | Delete
Equivalent to 'node.children[i] = child'. This method also sets the
[323] Fix | Delete
child's parent attribute appropriately.
[324] Fix | Delete
"""
[325] Fix | Delete
child.parent = self
[326] Fix | Delete
self.children[i].parent = None
[327] Fix | Delete
self.children[i] = child
[328] Fix | Delete
self.changed()
[329] Fix | Delete
[330] Fix | Delete
def insert_child(self, i, child):
[331] Fix | Delete
"""
[332] Fix | Delete
Equivalent to 'node.children.insert(i, child)'. This method also sets
[333] Fix | Delete
the child's parent attribute appropriately.
[334] Fix | Delete
"""
[335] Fix | Delete
child.parent = self
[336] Fix | Delete
self.children.insert(i, child)
[337] Fix | Delete
self.changed()
[338] Fix | Delete
[339] Fix | Delete
def append_child(self, child):
[340] Fix | Delete
"""
[341] Fix | Delete
Equivalent to 'node.children.append(child)'. This method also sets the
[342] Fix | Delete
child's parent attribute appropriately.
[343] Fix | Delete
"""
[344] Fix | Delete
child.parent = self
[345] Fix | Delete
self.children.append(child)
[346] Fix | Delete
self.changed()
[347] Fix | Delete
[348] Fix | Delete
[349] Fix | Delete
class Leaf(Base):
[350] Fix | Delete
[351] Fix | Delete
"""Concrete implementation for leaf nodes."""
[352] Fix | Delete
[353] Fix | Delete
# Default values for instance variables
[354] Fix | Delete
_prefix = "" # Whitespace and comments preceding this token in the input
[355] Fix | Delete
lineno = 0 # Line where this token starts in the input
[356] Fix | Delete
column = 0 # Column where this token tarts in the input
[357] Fix | Delete
[358] Fix | Delete
def __init__(self, type, value,
[359] Fix | Delete
context=None,
[360] Fix | Delete
prefix=None,
[361] Fix | Delete
fixers_applied=[]):
[362] Fix | Delete
"""
[363] Fix | Delete
Initializer.
[364] Fix | Delete
[365] Fix | Delete
Takes a type constant (a token number < 256), a string value, and an
[366] Fix | Delete
optional context keyword argument.
[367] Fix | Delete
"""
[368] Fix | Delete
assert 0 <= type < 256, type
[369] Fix | Delete
if context is not None:
[370] Fix | Delete
self._prefix, (self.lineno, self.column) = context
[371] Fix | Delete
self.type = type
[372] Fix | Delete
self.value = value
[373] Fix | Delete
if prefix is not None:
[374] Fix | Delete
self._prefix = prefix
[375] Fix | Delete
self.fixers_applied = fixers_applied[:]
[376] Fix | Delete
[377] Fix | Delete
def __repr__(self):
[378] Fix | Delete
"""Return a canonical string representation."""
[379] Fix | Delete
return "%s(%r, %r)" % (self.__class__.__name__,
[380] Fix | Delete
self.type,
[381] Fix | Delete
self.value)
[382] Fix | Delete
[383] Fix | Delete
def __unicode__(self):
[384] Fix | Delete
"""
[385] Fix | Delete
Return a pretty string representation.
[386] Fix | Delete
[387] Fix | Delete
This reproduces the input source exactly.
[388] Fix | Delete
"""
[389] Fix | Delete
return self.prefix + unicode(self.value)
[390] Fix | Delete
[391] Fix | Delete
if sys.version_info > (3, 0):
[392] Fix | Delete
__str__ = __unicode__
[393] Fix | Delete
[394] Fix | Delete
def _eq(self, other):
[395] Fix | Delete
"""Compare two nodes for equality."""
[396] Fix | Delete
return (self.type, self.value) == (other.type, other.value)
[397] Fix | Delete
[398] Fix | Delete
def clone(self):
[399] Fix | Delete
"""Return a cloned (deep) copy of self."""
[400] Fix | Delete
return Leaf(self.type, self.value,
[401] Fix | Delete
(self.prefix, (self.lineno, self.column)),
[402] Fix | Delete
fixers_applied=self.fixers_applied)
[403] Fix | Delete
[404] Fix | Delete
def leaves(self):
[405] Fix | Delete
yield self
[406] Fix | Delete
[407] Fix | Delete
def post_order(self):
[408] Fix | Delete
"""Return a post-order iterator for the tree."""
[409] Fix | Delete
yield self
[410] Fix | Delete
[411] Fix | Delete
def pre_order(self):
[412] Fix | Delete
"""Return a pre-order iterator for the tree."""
[413] Fix | Delete
yield self
[414] Fix | Delete
[415] Fix | Delete
def _prefix_getter(self):
[416] Fix | Delete
"""
[417] Fix | Delete
The whitespace and comments preceding this token in the input.
[418] Fix | Delete
"""
[419] Fix | Delete
return self._prefix
[420] Fix | Delete
[421] Fix | Delete
def _prefix_setter(self, prefix):
[422] Fix | Delete
self.changed()
[423] Fix | Delete
self._prefix = prefix
[424] Fix | Delete
[425] Fix | Delete
prefix = property(_prefix_getter, _prefix_setter)
[426] Fix | Delete
[427] Fix | Delete
def convert(gr, raw_node):
[428] Fix | Delete
"""
[429] Fix | Delete
Convert raw node information to a Node or Leaf instance.
[430] Fix | Delete
[431] Fix | Delete
This is passed to the parser driver which calls it whenever a reduction of a
[432] Fix | Delete
grammar rule produces a new complete node, so that the tree is build
[433] Fix | Delete
strictly bottom-up.
[434] Fix | Delete
"""
[435] Fix | Delete
type, value, context, children = raw_node
[436] Fix | Delete
if children or type in gr.number2symbol:
[437] Fix | Delete
# If there's exactly one child, return that child instead of
[438] Fix | Delete
# creating a new node.
[439] Fix | Delete
if len(children) == 1:
[440] Fix | Delete
return children[0]
[441] Fix | Delete
return Node(type, children, context=context)
[442] Fix | Delete
else:
[443] Fix | Delete
return Leaf(type, value, context=context)
[444] Fix | Delete
[445] Fix | Delete
[446] Fix | Delete
class BasePattern(object):
[447] Fix | Delete
[448] Fix | Delete
"""
[449] Fix | Delete
A pattern is a tree matching pattern.
[450] Fix | Delete
[451] Fix | Delete
It looks for a specific node type (token or symbol), and
[452] Fix | Delete
optionally for a specific content.
[453] Fix | Delete
[454] Fix | Delete
This is an abstract base class. There are three concrete
[455] Fix | Delete
subclasses:
[456] Fix | Delete
[457] Fix | Delete
- LeafPattern matches a single leaf node;
[458] Fix | Delete
- NodePattern matches a single node (usually non-leaf);
[459] Fix | Delete
- WildcardPattern matches a sequence of nodes of variable length.
[460] Fix | Delete
"""
[461] Fix | Delete
[462] Fix | Delete
# Defaults for instance variables
[463] Fix | Delete
type = None # Node type (token if < 256, symbol if >= 256)
[464] Fix | Delete
content = None # Optional content matching pattern
[465] Fix | Delete
name = None # Optional name used to store match in results dict
[466] Fix | Delete
[467] Fix | Delete
def __new__(cls, *args, **kwds):
[468] Fix | Delete
"""Constructor that prevents BasePattern from being instantiated."""
[469] Fix | Delete
assert cls is not BasePattern, "Cannot instantiate BasePattern"
[470] Fix | Delete
return object.__new__(cls)
[471] Fix | Delete
[472] Fix | Delete
def __repr__(self):
[473] Fix | Delete
args = [type_repr(self.type), self.content, self.name]
[474] Fix | Delete
while args and args[-1] is None:
[475] Fix | Delete
del args[-1]
[476] Fix | Delete
return "%s(%s)" % (self.__class__.__name__, ", ".join(map(repr, args)))
[477] Fix | Delete
[478] Fix | Delete
def optimize(self):
[479] Fix | Delete
"""
[480] Fix | Delete
A subclass can define this as a hook for optimizations.
[481] Fix | Delete
[482] Fix | Delete
Returns either self or another node with the same effect.
[483] Fix | Delete
"""
[484] Fix | Delete
return self
[485] Fix | Delete
[486] Fix | Delete
def match(self, node, results=None):
[487] Fix | Delete
"""
[488] Fix | Delete
Does this pattern exactly match a node?
[489] Fix | Delete
[490] Fix | Delete
Returns True if it matches, False if not.
[491] Fix | Delete
[492] Fix | Delete
If results is not None, it must be a dict which will be
[493] Fix | Delete
updated with the nodes matching named subpatterns.
[494] Fix | Delete
[495] Fix | Delete
Default implementation for non-wildcard patterns.
[496] Fix | Delete
"""
[497] Fix | Delete
if self.type is not None and node.type != self.type:
[498] Fix | Delete
return False
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function