U AT^8m@sdZdZddlZddlmZdZiaddZGdd d eZ Gd d d e Z Gd d d e Z ddZ GdddeZ Gddde ZGddde ZGddde ZGddde ZddZdS)z 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. z#Guido van Rossum N)StringIOicCsDts8ddlm}|jD]\}}t|tkr|t|<qt||S)N)python_symbols) _type_reprsZpygramr__dict__itemstypeint setdefault)Ztype_numrnamevalr &/usr/lib64/python3.8/lib2to3/pytree.py type_reprs   rc@seZdZdZdZdZdZdZdZddZ ddZ dZ d d Z d d Z d dZddZddZddZddZddZeddZeddZddZdd Zd!d"Zejd#krd$d%ZdS)&Basez Abstract base class for Node and Leaf. This provides some default functionality and boilerplate using the template pattern. A node may be a subnode of at most one parent. Nr FcOs|tk stdt|S)z7Constructor that prevents Base from being instantiated.zCannot instantiate Base)rAssertionErrorobject__new__clsargskwdsr r rr1sz Base.__new__cCs|j|jk rtS||S)zW Compare two nodes for equality. This calls the method _eq(). ) __class__NotImplemented_eqselfotherr r r__eq__6s z Base.__eq__cCstdS)a_ Compare two nodes for equality. This is called by __eq__ and __ne__. It is only called if the two nodes have the same type. This must be implemented by the concrete subclass. Nodes should be considered equal if they have the same structure, ignoring the prefix string and other context information. NNotImplementedErrorrr r rrBs zBase._eqcCstdS)zr Return a cloned (deep) copy of self. This must be implemented by the concrete subclass. Nrrr r rcloneMsz Base.clonecCstdS)zx Return a post-order iterator for the tree. This must be implemented by the concrete subclass. Nrr!r r r post_orderUszBase.post_ordercCstdS)zw Return a pre-order iterator for the tree. This must be implemented by the concrete subclass. Nrr!r r r pre_order]szBase.pre_ordercCs|jdk stt||dk s"tt|ts2|g}g}d}|jjD]D}||kr||rdt|jj||f|dk rv||d}qB||qB|st|j||f|j||j_|D] }|j|_qd|_dS)z/Replace this node with a new one in the parent.NFT) parentrstr isinstancelistchildrenextendappendchanged)rnewZ l_childrenfoundchxr r rreplacees&       z Base.replacecCs*|}t|ts$|jsdS|jd}q|jS)z9Return the line number which generated the invocant node.Nr)r'Leafr)linenornoder r r get_lineno|s   zBase.get_linenocCs|jr|jd|_dS)NT)r%r, was_changedr!r r rr,s z Base.changedcCsJ|jrFt|jjD]2\}}||kr|j|jj|=d|_|SqdS)z Remove the node from the tree. Returns the position of the node in its parent's children before it was removed. N)r% enumerater)r,)rir5r r rremoves  z Base.removec Cs`|jdkrdSt|jjD]@\}}||krz|jj|dWStk rXYdSXqdS)z The node immediately following the invocant in their parent's children list. If the invocant does not have a next sibling, it is None Nr)r%r8r) IndexErrorrr9childr r r next_siblings zBase.next_siblingcCsR|jdkrdSt|jjD]2\}}||kr|dkr8dS|jj|dSqdS)z The node immediately preceding the invocant in their parent's children list. If the invocant does not have a previous sibling, it is None. Nrr)r%r8r)r<r r r prev_siblings zBase.prev_siblingccs|jD]}|EdHqdSN)r)leavesrr=r r rrAs z Base.leavescCs|jdkrdSd|jS)Nrr)r%depthr!r r rrCs z Base.depthcCs|j}|dkrdS|jS)z Return the string immediately following the invocant node. This is effectively equivalent to node.next_sibling.prefix N)r>prefix)rZnext_sibr r r get_suffixszBase.get_suffixrcCst|dS)Nascii)r&encoder!r r r__str__sz Base.__str__)__name__ __module__ __qualname____doc__rr%r)r7Z was_checkedrr__hash__rr"r#r$r1r6r,r:propertyr>r?rArCrFsys version_inforKr r r rrs4        rc@seZdZdZdddZddZddZejd kr4eZ d d Z d d Z ddZ ddZ eddZejddZddZddZddZdS)Nodez+Concrete implementation for interior nodes.NcCst|dkst|||_t||_|jD] }|jdks@tt|||_q&|dk rV||_|rj|dd|_nd|_dS)z 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)rrr(r)r%reprrEfixers_applied)rrr)contextrErWr/r r r__init__s   z Node.__init__cCsd|jjt|j|jfS))Return a canonical string representation.z %s(%s, %r))rrLrrr)r!r r r__repr__sz Node.__repr__cCsdtt|jS)k Return a pretty string representation. This reproduces the input source exactly. rD)joinmapr&r)r!r r r __unicode__szNode.__unicode__rGcCs|j|jf|j|jfkSzCompare two nodes for equality.)rr)rr r rrszNode._eqcCst|jdd|jD|jdS)$Return a cloned (deep) copy of self.cSsg|] }|qSr )r").0r/r r r szNode.clone..rW)rTrr)rWr!r r rr"sz Node.cloneccs$|jD]}|EdHq|VdSz*Return a post-order iterator for the tree.N)r)r#rBr r rr#s zNode.post_orderccs$|V|jD]}|EdHq dSz)Return a pre-order iterator for the tree.N)r)r$rBr r rr$ s zNode.pre_ordercCs|js dS|jdjS)zO The whitespace and comments preceding this node in the input. rDrr)rEr!r r rrEsz Node.prefixcCs|jr||jd_dSNrrgrrEr r rrEscCs(||_d|j|_||j|<|dS)z Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. N)r%r)r,r<r r r set_child s  zNode.set_childcCs ||_|j|||dS)z Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. N)r%r)insertr,r<r r r insert_child*szNode.insert_childcCs||_|j||dS)z Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. N)r%r)r+r,rBr r r append_child3s zNode.append_child)NNN)rLrMrNrOrYr[r_rRrSrKrr"r#r$rQrEsetterrjrlrmr r r rrTs(      rTc@seZdZdZdZdZdZddgfddZddZd d Z e j d krFe Z d d Z ddZddZddZddZeddZejddZdS)r2z'Concrete implementation for leaf nodes.rDrNcCsdd|krdksnt||dk r8|\|_\|_|_||_||_|dk rR||_|dd|_dS)z Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. rrUN)r_prefixr3columnrvaluerW)rrrqrXrErWr r rrYFs z Leaf.__init__cCsd|jj|j|jfS)rZz %s(%r, %r))rrLrrqr!r r rr[Ysz Leaf.__repr__cCs|jt|jS)r\)rEr&rqr!r r rr__szLeaf.__unicode__rGcCs|j|jf|j|jfkSr`)rrqrr r rrjszLeaf._eqcCs$t|j|j|j|j|jff|jdS)rard)r2rrqrEr3rprWr!r r rr"ns z Leaf.cloneccs |VdSr@r r!r r rrAtsz Leaf.leavesccs |VdSrer r!r r rr#wszLeaf.post_orderccs |VdSrfr r!r r rr${szLeaf.pre_ordercCs|jS)zP The whitespace and comments preceding this token in the input. )ror!r r rrEsz Leaf.prefixcCs|||_dSr@)r,rorir r rrEs)rLrMrNrOror3rprYr[r_rRrSrKrr"rAr#r$rQrErnr r r rr2=s*   r2cCsN|\}}}}|s||jkrConstructor that prevents BasePattern from being instantiated.zCannot instantiate BasePattern)rtrrrrr r rrszBasePattern.__new__cCsHt|j|j|jg}|r,|ddkr,|d=qd|jjdtt|fS)Nz%s(%s)z, ) rrcontentr rrLr]r^rV)rrr r rr[szBasePattern.__repr__cCs|S)z A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. r r!r r roptimizeszBasePattern.optimizecCsn|jdk r|j|jkrdS|jdk rRd}|dk r4i}|||sDdS|rR|||dk rj|jrj|||j<dS)a# Does this pattern exactly match a node? 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. Default implementation for non-wildcard patterns. NFT)rrv _submatchupdater )rr5resultsrr r rmatchs     zBasePattern.matchcCs t|dkrdS||d|S)z Does this pattern exactly match a sequence of nodes? Default implementation for non-wildcard patterns. rFr)rrr|)rnodesrzr r r match_seqs zBasePattern.match_seqccs&i}|r"||d|r"d|fVdS)z} Generator yielding all matches for this pattern. Default implementation for non-wildcard patterns. rrN)r|)rr}r{r r rgenerate_matchesszBasePattern.generate_matches)N)N) rLrMrNrOrrvr rr[rwr|r~rr r r rrts  rtc@s*eZdZdddZd ddZd ddZdS) LeafPatternNcCsZ|dk r&d|krdks&nt||dk rDt|tsDtt|||_||_||_dS)ap Initializer. Takes optional type, content, and name. The type, if given must be a token type (< 256). If not given, this matches any *leaf* node; the content may still be required. The content, if given, must be a string. If a name is given, the matching node is stored in the results dict under that key. NrrU)rr'r&rVrrvr )rrrvr r r rrYs zLeafPattern.__init__cCst|tsdSt|||S)z*Override match() to insist on a leaf node.F)r'r2rtr|rr5rzr r rr| s zLeafPattern.matchcCs |j|jkS) 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. )rvrqrr r rrxs zLeafPattern._submatch)NNN)N)N)rLrMrNrYr|rxr r r rrs  rc@s$eZdZdZdddZdddZdS) NodePatternFNcCs|dk r|dkst||dk rvt|tr6tt|t|}t|D].\}}t|tsdt||ft|trFd|_qF||_ ||_ ||_ dS)ad Initializer. Takes optional type, content, and name. The type, if given, must be a symbol type (>= 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. NrUT) rr'r&rVr(r8rtWildcardPattern wildcardsrrvr )rrrvr r9itemr r rrY$s zNodePattern.__init__cCs|jrHt|j|jD].\}}|t|jkr|dk r<||dSqdSt|jt|jkr`dSt|j|jD]\}}|||sndSqndS)rNTF)rrrvr)rrryzipr|)rr5rzcr{ subpatternr=r r rrxAs   zNodePattern._submatch)NNN)N)rLrMrNrrYrxr r r rr s rc@s^eZdZdZddedfddZddZddd Zdd d Zd d Z ddZ ddZ ddZ dS)ra 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. NrcCsd|kr|krtks,nt||f|dk rtttt|}t|sVtt||D]}t|sZtt|qZ||_||_||_||_ dS)a 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)* rN) HUGErtupler^rrrVrvminmaxr )rrvrrr altr r rrYks,zWildcardPattern.__init__cCsd}|jdk rs$   1nNV,==#