ofc@sdZdZddlZddlZddlmZdZiadZdefdYZ d e fd YZ d e fd YZ d Z defdYZ de fdYZde fdYZde fdYZde fdYZdZdS(s Python parse tree definitions. This is a very concrete parse tree; we need to keep every token and even the comments and whitespace between tokens. There's also a pattern matching implementation here. s#Guido van Rossum iN(tStringIOicCshtsXddlm}x?|jjD]+\}}t|tkr&|t|((s&/usr/lib64/python2.7/lib2to3/pytree.pyt prev_siblings  ccs4x-|jD]"}x|jD] }|VqWq WdS(N(R)tleaves(RR>R3((s&/usr/lib64/python2.7/lib2to3/pytree.pyRAscCs$|jdkrdSd|jjS(Nii(R(R*tdepth(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRBscCs |j}|dkrdS|jS(s Return the string immediately following the invocant node. This is effectively equivalent to node.next_sibling.prefix uN(R?R*R"(Rtnext_sib((s&/usr/lib64/python2.7/lib2to3/pytree.pyt get_suffixs  iicCst|jdS(Ntascii(tunicodetencode(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyt__str__sN((ii(!t__name__t __module__t__doc__R*RR(R)R'R9t was_checkedRRt__hash__RRRRRR#R$R4R8R.R<tpropertyR?R@RARBRDtsyst version_infoRH(((s&/usr/lib64/python2.7/lib2to3/pytree.pyR s6            tNodecBseZdZddddZdZdZejdkrHeZ ndZ dZ dZ d Z d Zd ZeeeZd Zd ZdZRS(s+Concrete implementation for interior nodes.cCsm||_t||_x|jD]}||_q"W|dk rM||_n|r`||_n d|_dS(s Initializer. Takes a type constant (a symbol number >= 256), a sequence of child nodes, and an optional context keyword argument. As a side effect, the parent pointers of the children are updated. N(RR&R)R(R*R"tfixers_applied(RRR)tcontextR"RRR2((s&/usr/lib64/python2.7/lib2to3/pytree.pyt__init__s     cCs#d|jjt|j|jfS(s)Return a canonical string representation.s %s(%s, %r)(RRIR RR)(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyt__repr__ s  cCsdjtt|jS(sk Return a pretty string representation. This reproduces the input source exactly. u(tjointmapRFR)(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyt __unicode__siicCs"|j|jf|j|jfkS(sCompare two nodes for equality.(RR)(RR((s&/usr/lib64/python2.7/lib2to3/pytree.pyRscCs5t|jg|jD]}|j^qd|jS(s$Return a cloned (deep) copy of self.RR(RQRR)RRR(RR2((s&/usr/lib64/python2.7/lib2to3/pytree.pyR!s+ccs9x-|jD]"}x|jD] }|VqWq W|VdS(s*Return a post-order iterator for the tree.N(R)R(RR>R7((s&/usr/lib64/python2.7/lib2to3/pytree.pyR&s ccs9|Vx-|jD]"}x|jD] }|Vq"WqWdS(s)Return a pre-order iterator for the tree.N(R)R(RR>R7((s&/usr/lib64/python2.7/lib2to3/pytree.pyR-scCs|js dS|jdjS(sO The whitespace and comments preceding this node in the input. ti(R)R"(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyt_prefix_getter4s cCs |jr||jd_ndS(Ni(R)R"(RR"((s&/usr/lib64/python2.7/lib2to3/pytree.pyt_prefix_setter<s cCs4||_d|j|_||j|<|jdS(s Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. N(R(R*R)R.(RR;R>((s&/usr/lib64/python2.7/lib2to3/pytree.pyt set_childBs  cCs*||_|jj|||jdS(s Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. N(R(R)tinsertR.(RR;R>((s&/usr/lib64/python2.7/lib2to3/pytree.pyt insert_childLs cCs'||_|jj||jdS(s Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. N(R(R)R-R.(RR>((s&/usr/lib64/python2.7/lib2to3/pytree.pyt append_childUs N(ii(RIRJRKR*RTRURXRORPRHRRRRRZR[RNR"R\R^R_(((s&/usr/lib64/python2.7/lib2to3/pytree.pyRQs$           R5cBseZdZdZdZdZddgdZdZdZ e j dkrZe Z ndZ dZd Zd Zd Zd Zd ZeeeZRS(s'Concrete implementation for leaf nodes.RYicCsb|dk r*|\|_\|_|_n||_||_|dk rT||_n||_dS(s Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. N(R*t_prefixR6tcolumnRtvalueRR(RRRbRSR"RR((s&/usr/lib64/python2.7/lib2to3/pytree.pyRThs     cCsd|jj|j|jfS(s)Return a canonical string representation.s %s(%r, %r)(RRIRRb(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRU{s cCs|jt|jS(sk Return a pretty string representation. This reproduces the input source exactly. (R"RFRb(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRXsicCs"|j|jf|j|jfkS(sCompare two nodes for equality.(RRb(RR((s&/usr/lib64/python2.7/lib2to3/pytree.pyRscCs4t|j|j|j|j|jffd|jS(s$Return a cloned (deep) copy of self.RR(R5RRbR"R6RaRR(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRsccs |VdS(N((R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRAsccs |VdS(s*Return a post-order iterator for the tree.N((R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRsccs |VdS(s)Return a pre-order iterator for the tree.N((R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRscCs|jS(sP The whitespace and comments preceding this token in the input. (R`(R((s&/usr/lib64/python2.7/lib2to3/pytree.pyRZscCs|j||_dS(N(R.R`(RR"((s&/usr/lib64/python2.7/lib2to3/pytree.pyR[s N(ii(RIRJRKR`R6RaR*RTRURXRORPRHRRRARRRZR[RNR"(((s&/usr/lib64/python2.7/lib2to3/pytree.pyR5_s&           cCsk|\}}}}|s'||jkrTt|dkrA|dSt||d|St||d|SdS(s Convert raw node information to a Node or Leaf instance. This is passed to the parser driver which calls it whenever a reduction of a grammar rule produces a new complete node, so that the tree is build strictly bottom-up. iiRSN(t number2symboltlenRQR5(tgrtraw_nodeRRbRSR)((s&/usr/lib64/python2.7/lib2to3/pytree.pytconverts t BasePatterncBs\eZdZdZdZdZdZdZdZ ddZ ddZ dZ RS(s A pattern is a tree matching pattern. It looks for a specific node type (token or symbol), and optionally for a specific content. This is an abstract base class. There are three concrete subclasses: - LeafPattern matches a single leaf node; - NodePattern matches a single node (usually non-leaf); - WildcardPattern matches a sequence of nodes of variable length. cOs tj|S(s>Constructor that prevents BasePattern from being instantiated.(RR(RRR((s&/usr/lib64/python2.7/lib2to3/pytree.pyRscCsht|j|j|jg}x!|rA|ddkrA|d=q!Wd|jjdjtt |fS(Nis%s(%s)s, ( R RtcontentR R*RRIRVRWtrepr(RR((s&/usr/lib64/python2.7/lib2to3/pytree.pyRUs cCs|S(s A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. ((R((s&/usr/lib64/python2.7/lib2to3/pytree.pytoptimizescCs|jdk r%|j|jkr%tS|jdk r~d}|dk rOi}n|j||setS|r~|j|q~n|dk r|jr|||j= 256). If the type is None this matches *any* single node (leaf or not), except if content is not None, in which it only matches non-leaf nodes that also match the content pattern. The content, if not None, must be a sequence of Patterns that must match the node's children exactly. If the content is given, the type must not be None. If a name is given, the matching node is stored in the results dict under that key. N( R*R&R:R%tWildcardPatternR,t wildcardsRRiR (RRRiR R;titem((s&/usr/lib64/python2.7/lib2to3/pytree.pyRTFs     cCs|jrhxXt|j|jD]A\}}|t|jkr|dk r\|j|ntSqWtSt|jt|jkrtSx9t |j|jD]"\}}|j ||stSqWtS(s Match the pattern's content to the node's children. This assumes the node type matches and self.content is not None. Returns True if it matches, False if not. If results is not None, it must be a dict which will be updated with the nodes matching named subpatterns. When returning False, the results dict may still be updated. N( RwRsRiR)RdR*RmR,R'tzipRp(RR7RntcRot subpatternR>((s&/usr/lib64/python2.7/lib2to3/pytree.pyRlcs " "N(RIRJR'RwR*RTRl(((s&/usr/lib64/python2.7/lib2to3/pytree.pyRuBsRvcBsheZdZd ded dZdZd dZd dZdZ dZ dZ d Z RS( s A wildcard pattern can match zero or more nodes. This has all the flexibility needed to implement patterns like: .* .+ .? .{m,n} (a b c | d e | f) (...)* (...)+ (...)? (...){m,n} except it always uses non-greedy matching. icCs]|dk r5ttt|}x|D]}q(Wn||_||_||_||_dS(s Initializer. Args: content: optional sequence of subsequences of patterns; if absent, matches one node; if present, each subsequence is an alternative [*] min: optional minimum number of times to match, default 0 max: optional maximum number of times to match, default HUGE name: optional name assigned to this match [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is equivalent to (a b c | d e | f g h); if content is None, this is equivalent to '.' in regular expression terms. The min and max parameters work as follows: min=0, max=maxint: .* min=1, max=maxint: .+ min=0, max=1: .? min=1, max=1: . If content is not None, replace the dot with the parenthesized list of alternatives, e.g. (a b c | d e | f g h)* N(R*ttupleRWRitmintmaxR (RRiR}R~R talt((s&/usr/lib64/python2.7/lib2to3/pytree.pyRTs     c Cs/d}|jdk rWt|jdkrWt|jddkrW|jdd}n|jdkr|jdkr|jdkrtd|jS|dk r|j|jkr|jSn|jdkr+t|t r+|jdkr+|j|jkr+t |j|j|j|j|j|jS|S(s+Optimize certain stacked wildcard patterns.iiR N( R*RiRdR}R~RuR RkR%Rv(RR{((s&/usr/lib64/python2.7/lib2to3/pytree.pyRks . !    cCs|j|g|S(s'Does this pattern exactly match a node?(Rr(RR7Rn((s&/usr/lib64/python2.7/lib2to3/pytree.pyRpscCsuxn|j|D]]\}}|t|kr|dk ri|j||jrit|||j s"   pN V,=#