fFmdZdZddlZddlmZdZiadZGddeZ Gd d e Z Gd d e Z d Z GddeZ Gdde ZGdde ZGdde ZGdde ZdZdS)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)StringIOictsGddlm}|jD]'\}}t |t kr |t|<(t||S)N)python_symbols) _type_reprspygramr__dict__itemstypeint setdefault)type_numrnamevals 5/opt/alt/python311/lib64/python3.11/lib2to3/pytree.py type_reprrsq 9******(06688 9 9ID#CyyCDS!1  ! !(H 5 55ceZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ d Zd Zd Zd Zd ZdZedZedZdZdZdZejdkrdZdSdS)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. NFc6t|S)z7Constructor that prevents Base from being instantiated.object__new__clsargskwdss rrz Base.__new__1~~c"""rcV|j|jurtS||S)zW Compare two nodes for equality. This calls the method _eq(). ) __class__NotImplemented_eqselfothers r__eq__z Base.__eq__6s) > 0 0! !xxrct)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. NotImplementedErrorr$s rr#zBase._eqBs "!rct)zr Return a cloned (deep) copy of self. This must be implemented by the concrete subclass. r)r%s rclonez Base.cloneM "!rct)zx Return a post-order iterator for the tree. This must be implemented by the concrete subclass. r)r,s r post_orderzBase.post_orderUr.rct)zw Return a pre-order iterator for the tree. This must be implemented by the concrete subclass. r)r,s r pre_orderzBase.pre_order]r.rc<t|ts|g}g}d}|jjD]5}||ur|||d} ||6|j||j_|D]}|j|_d|_dS)z/Replace this node with a new one in the parent.FNT) isinstancelistparentchildrenextendappendchanged)r%new l_childrenfoundchxs rreplacez Base.replacees#t$$ %C +& & &BTzz?%%c***!!"%%%% )  # #A{AHH rc|}t|ts+|jsdS|jd}t|t+|jS)z9Return the line number which generated the invocant node.Nr)r4Leafr7linenor%nodes r get_linenozBase.get_lineno|sRT4(( $= =#DT4(( ${rcT|jr|jd|_dS)NT)r6r: was_changedr,s rr:z Base.changeds. ; " K   ! ! !rc|jrTt|jjD]<\}}||ur1|j|jj|=d|_|cS;dSdS)z Remove the node from the tree. Returns the position of the node in its parent's children before it was removed. N)r6 enumerater7r:)r%irEs rremovez Base.removes ; $T[%9::  44<<K''))) ,Q/"&DKHHH      rc|jdSt|jjD]3\}}||ur* |jj|dzcS#t$rYdSwxYw4dS)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)r6rJr7 IndexErrorr%rKchilds r next_siblingzBase.next_siblings ; 4"$+"677  HAu}} ;/!4444!   444   sA AAc|jdSt|jjD])\}}||ur |dkrdS|jj|dz cS*dS)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)r6rJr7rOs r prev_siblingzBase.prev_siblingsu ; 4"$+"677 1 1HAu}}6644{+AaC0000 1 1rc#RK|jD]}|Ed{VdSN)r7leavesr%rPs rrVz Base.leavessD] & &E||~~ % % % % % % % % & &rcL|jdSd|jzS)Nrr)r6depthr,s rrYz Base.depths( ; 14;$$&&&&rc&|j}|dS|jS)z Return the string immediately following the invocant node. This is effectively equivalent to node.next_sibling.prefix N)rQprefix)r%next_sibs r get_suffixzBase.get_suffixs $  2rrcFt|dS)Nascii)strencoder,s r__str__z Base.__str__st99##G,, ,r)__name__ __module__ __qualname____doc__r r6r7rH was_checkedrr'__hash__r#r-r0r2r@rFr:rLpropertyrQrSrVrYr^sys version_inforerrrrrs` D FHKK### H " " """""""""".       X  1 1X 1&&&'''  &   - - - - -! rrceZdZdZ ddZdZdZejdkreZ dZ dZ d Z d Z ed Zejd Zd ZdZdZdS)Nodez+Concrete implementation for interior nodes.Nc||_t||_|jD] }||_ |||_|r|dd|_dSd|_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)r r5r7r6r\fixers_applied)r%r r7contextr\rrr>s r__init__z Node.__init__sl X -  BBII   DK  '"0"3D   "&D   rcZ|jjdt|jd|jdSz)Return a canonical string representation.(, ))r!rfrr r7r,s r__repr__z Node.__repr__s6#~666(3333#}}}. .rc\dtt|jS)k Return a pretty string representation. This reproduces the input source exactly. r[)joinmaprcr7r,s r __unicode__zNode.__unicode__s" wws3 ..///rr_c>|j|jf|j|jfkSzCompare two nodes for equality.)r r7r$s rr#zNode._eqs 4=)ej%.-IIIrcXt|jd|jD|jS)$Return a cloned (deep) copy of self.c6g|]}|Sr)r-).0r>s r zNode.clone..s CCCr CCCrrr)rpr r7rrr,s rr-z Node.clones6DICCT]CCC#'#6888 8rc#ZK|jD]}|Ed{V|VdSz*Return a post-order iterator for the tree.N)r7r0rWs rr0zNode.post_ordersK] * *E'')) ) ) ) ) ) ) ) ) rc#ZK|V|jD]}|Ed{VdSz)Return a pre-order iterator for the tree.N)r7r2rWs rr2zNode.pre_order sO ] ) )E(( ( ( ( ( ( ( ( ( ) )rc8|jsdS|jdjS)zO The whitespace and comments preceding this node in the input. r[rr7r\r,s rr\z Node.prefixs# } 2}Q&&rc<|jr||jd_dSdSNrrr%r\s rr\z Node.prefixs+ = -&,DM!  # # # - -rct||_d|j|_||j|<|dS)z Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. N)r6r7r:rOs r set_childzNode.set_child s7  "& a  a rcr||_|j|||dS)z Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. N)r6r7insertr:rOs r insert_childzNode.insert_child*s4   Q&&& rcp||_|j||dS)z Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. N)r6r7r9r:rWs r append_childzNode.append_child3s2   U### rNNN)rfrgrhrirtrzrrmrnrer#r-r0r2rlr\setterrrrrrrrprps55 $''''2... 000 &  JJJ888  ))) ''X' ]--]-rrpceZdZdZdZdZdZddgfdZdZdZ e j dkre Z d Z d Zd Zd Zd ZedZejdZdS)rBz'Concrete implementation for leaf nodes.r[rNc||\|_\|_|_||_||_|||_|dd|_dS)z Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. N)_prefixrCcolumnr valuerr)r%r rrsr\rrs rrtz Leaf.__init__FsQ  7> 4DL44;    !DL,QQQ/rc@|jjd|jd|jdSrv)r!rfr rr,s rrzz Leaf.__repr__Ys,#~666#yyy#zzz+ +rc:|jt|jzS)r|)r\rcrr,s rrzLeaf.__unicode___s {S__,,rr_c>|j|jf|j|jfkSr)r rr$s rr#zLeaf._eqjs 4:&5:u{*CCCrclt|j|j|j|j|jff|jS)rr)rBr rr\rCrrrr,s rr-z Leaf.clonens:DItz[4; "<=#'#6888 8rc#K|VdSrUrr,s rrVz Leaf.leavests rc#K|VdSrrr,s rr0zLeaf.post_orderw rc#K|VdSrrr,s rr2zLeaf.pre_order{rrc|jS)zP The whitespace and comments preceding this token in the input. )rr,s rr\z Leaf.prefixs |rc<|||_dSrU)r:rrs rr\z Leaf.prefixs  r)rfrgrhrirrCrrtrzrrmrnrer#r-rVr0r2rlr\rrrrrBrB=s11G F F "0000&+++ --- &  DDD888 X  ]]rrBc|\}}}}|s ||jvr-t|dkr|dSt|||St|||S)z 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. rr)rs) number2symbollenrprB)grraw_noder rrsr7s rconvertrsn&."D%(242+++ x==A  A; D(G4444D%1111rcFeZdZdZdZdZdZdZdZdZ d dZ d dZ dZ dS) BasePatterna 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. Nc6t|S)z>Constructor that prevents BasePattern from being instantiated.rrs rrzBasePattern.__new__rrct|j|j|jg}|r|d |d=|r|d |jjddtt|dS)Nrwrxry) rr contentrr!rfr}r~repr)r%rs rrzzBasePattern.__repr__sw$)$$dlDI> tBx'R tBx'>222DIIc$oo4N4N4N4NOOrc|S)z A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. rr,s roptimizezBasePattern.optimizes  rc|j|j|jkrdS|j5d}|i}|||sdS|r||||jr |||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)r r _submatchupdater)r%rEresultsrs rmatchzBasePattern.matchs 9 TY$)%;%;5 < #A">>$** u "q!!!  49 !%GDI trcdt|dkrdS||d|S)z Does this pattern exactly match a sequence of nodes? Default implementation for non-wildcard patterns. rFr)rr)r%nodesrs r match_seqzBasePattern.match_seqs0 u::??5zz%(G,,,rc#^Ki}|r$||d|r d|fVdSdSdS)z} Generator yielding all matches for this pattern. Default implementation for non-wildcard patterns. rrN)r)r%rrs rgenerate_matcheszBasePattern.generate_matchessS   TZZa!,, Q$JJJJJ    rrU) rfrgrhrir rrrrzrrrrrrrrrs   DG D### PPP 2----rrc&eZdZddZddZddZdS) LeafPatternNc8||||_||_||_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. N)r rr)r%r rrs rrtzLeafPattern.__init__s)       rcht|tsdSt|||S)z*Override match() to insist on a leaf node.F)r4rBrrr%rErs rrzLeafPattern.match s1$%% 5  tW555rc"|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. )rrrs rrzLeafPattern._submatchs|tz))rrrU)rfrgrhrtrrrrrrrsP(6666 * * * * * *rrc"eZdZdZddZddZdS) NodePatternFNc||@t|}t|D]!\}}t|trd|_"||_||_||_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. NT)r5rJr4WildcardPattern wildcardsr rr)r%r rrrKitems rrtzNodePattern.__init__$si    7mmG$W-- * *4dO44*%)DN   rc|jrTt|j|jD]7\}}|t |jkr|||dS8dSt |jt |jkrdSt |j|jD]\}}|||sdSdS)rNTF)rrrr7rrzipr)r%rErcr subpatternrPs rrzNodePattern._submatchAs > (t}EE  1DM*****q)))44+5 t|  DM 2 2 2 25!$T\4=!A!A   J##E733 uu trrrU)rfrgrhrrtrrrrrr sAI:rrcPeZdZdZddedfdZdZd dZd dZdZ d Z d Z d Z 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. Nrc|'ttt|}|D]}||_||_||_||_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)* N)tupler~rminmaxr)r%rrrralts rrtzWildcardPattern.__init__ksT0  Cw//00G + +  rc<d}|jIt|jdkr1t|jddkr|jdd}|jdkrM|jdkrB|jt |jS|$|j|jkr|S|jdkrft|trQ|jdkrF|j|jkr6t|j|j|jz|j|jz|jS|S)z+Optimize certain stacked wildcard patterns.Nrr)r) rrrrrrrr4r)r%rs rrzWildcardPattern.optimizes L $    " "s4<?';';q'@'@a+J 8q==TX]]|#" 2222%49 +G+G!**,,, HMMj_EEM Na  DI$@$@":#5#'8JN#:#'8JN#:#-?44 4 rc0||g|S)z'Does this pattern exactly match a node?)rrs rrzWildcardPattern.matchs~~tfg...rc||D]P\}}|t|kr8|3|||jrt |||j<dSQdS)z4Does this pattern exactly match a sequence of nodes?NTF)rrrrr5)r%rrrrs rrzWildcardPattern.match_seqsy))%00  DAqCJJ&NN1%%%y9-1%[[ *tt  urc #.K|j^t|jdtt||jzD]#}i}|jr|d|||j<||fV$dS|jdkr||VdSttdr$tj }tt_ | |dD]$\}}|jr|d|||j<||fV%nJ#t$r=| |D]$\}}|jr|d|||j<||fV%YnwxYwttdr|t_ dSdS#ttdr |t_ wxYw)a" Generator yielding matches for a sequence of nodes. Args: nodes: sequence of nodes Yields: (count, results) tuples where: count: the match comprises nodes[:count]; results: dict containing named submatches. Nr bare_name getrefcountr)rrangerrrr_bare_name_matcheshasattrrmstderrr_recursive_matches RuntimeError_iterative_matches)r%rcountr save_stderrs rrz WildcardPattern.generate_matchess < txSUTX-F-F)FGG  91#(%=AdiLQh    Y+ % %))%00 0 0 0 0 0 sM** (!j %ZZ  - $ 7 7q A A##HE1y5',VeV}$) (NNNN#  # # #!% 7 7 > >##HE1y5',VeV}$) (NNNN## #3 ..-!,CJJJ--73 ..-!,CJ,,,,s+;DE1AE E1E  E11#Fc#Kt|}d|jkrdifVg}|jD]5}t||D]"\}}||fV|||f#6|rg}|D]\}} ||kr||jkr}|jD]u}t|||dD]Z\} } | dkrOi}|| || || z|fV||| z|f[v|}|dSdS)z(Helper to iteratively yield the matches.rN)rrrrr9rr) r%rnodelenrrrr new_resultsc0r0c1r1s rrz"WildcardPattern._iterative_matchesse** ==R%KKK< ' 'C(e44 ' '1d 1v&&&& '  "K! A AB<K||jkrdifV||jkr||jD]v}t||D]a\}}|||d|dzD]:\}}i}||||||z|fV;budSdS)z(Helper to recursively yield the matches.rNr)rrrrrr) r%rrrrrrrrs rrz"WildcardPattern._recursive_matches s DH  R%KKK 48  | ) ).sE::))FB"&"9"9%*eAg"N"N))B   2gqj(((( ))   ) )rrU) rfrgrhriHUGErtrrrrrrrrrrrr]s   $4!!!!F&////    +-+-+-Z""": ) ) ) ) )rrc(eZdZddZdZdZdZdS)NegatedPatternNc|||_dS)a Initializer. The argument is either a pattern or None. If it is None, this only matches an empty sequence (effectively '$' in regex lingo). If it is not None, this matches whenever the argument pattern doesn't have any matches. N)r)r%rs rrtzNegatedPattern.__init__s   rcdS)NFrrDs rrzNegatedPattern.match(surc(t|dkSr)r)r%rs rrzNegatedPattern.match_seq,s5zzQrc#K|jt|dkrdifVdSdS|j|D]\}}dSdifVdSr)rrr)r%rrrs rrzNegatedPattern.generate_matches0sr < 5zzQe  55e<<  1R%KKKKKrrU)rfrgrhrtrrrrrrrrsU         rrc#0K|sdifVdS|d|dd}}||D]a\}}|s||fVt|||dD]:\}}i}||||||z|fV;bdS)aR Generator yielding matches for a sequence of patterns and nodes. Args: patterns: a sequence of patterns nodes: a sequence of nodes Yields: (count, results) tuples where: count: the entire sequence of patterns matches nodes[:count]; results: dict containing named submatches. rrN)rr) patternsrprestrrrrrs rrr<s  %e 1+x|4((// % %FB %"f .tU233Z@@%%FBAHHRLLLHHRLLLr'1*$$$$ %  % %r)ri __author__rmiorrrrrrrprBrrrrrrrrrrrs3  666n-n-n-n-n-6n-n-n-`kkkkk4kkk\LLLLL4LLL\222&SSSSS&SSSl)*)*)*)*)*+)*)*)*X:::::+:::zy)y)y)y)y)ky)y)y)x     [   F%%%%%r