ZL i UdZddlmZmZddlmZmZmZmZddl m Z dZ ddl Z ddl mZdZeed <iaeeeeeffed <d ed eeeffd ZeddZedZeeeeeffZeeeeeeeeefZGddZGddeZGddeZde ded efdZeeefZ GddZ!Gdde!Z"Gdde!Z#Gd d!e!Z$Gd"d#e!Z%d$ee!d%eed eeee ffd&Z&y)'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. )IterableIterator)AnyOptionalTypeVarUnion)Grammarz#Guido van Rossum N)StringIOiHUGE _type_reprstype_numreturnctspddlm}t|ds|j dt |j D]4}t|j |}t|tk(s,|t|<6tj||S)N)pygrampython_symbols) cache_dir) r rhasattr initializedirrgetattrtypeint setdefault)r rnamevals U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/blib2to3/pytree.py type_reprrs} v/0     ---. (D&//6CCyC#' C  (  ! !(H 55_PBase)bound)NodeLeafceZdZUdZeed<dZeded<ee ed<dZ e ed<dZ e ed <d Z d ed e fd Zed efdZded ed e fdZdeded efdZded efdZd ee fdZd ee fdZdee ee fd dfdZd eefdZd!dZd eefdZed ee fdZed ee fdZ d edfdZ!d efdZ"d efd Z#y)"r"z 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. rNr$parentchildrenF was_changed was_checkedcJ|tusJdtj|S)z7Constructor that prevents Base from being instantiated.zCannot instantiate Base)r"object__new__clsargskwdss rr-z Base.__new__Gs#$9 99~~c""r otherrc`|j|jurtS|j|S)zW Compare two nodes for equality. This calls the method _eq(). ) __class__NotImplemented_eqselfr2s r__eq__z Base.__eq__Ls( >> 0! !xxr ctNNotImplementedErrorr8s rprefixz Base.prefixVs!!r r8ct)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. r<r7s rr6zBase._eqZs "!r memoc"|jSr;)clone)r8rAs r __deepcopy__zBase.__deepcopy__eszz|r ct)zr Return a cloned (deep) copy of self. This must be implemented by the concrete subclass. r<r>s rrCz Base.cloneh "!r ct)zx Return a post-order iterator for the tree. This must be implemented by the concrete subclass. r<r>s r post_orderzBase.post_orderprFr ct)zw Return a pre-order iterator for the tree. This must be implemented by the concrete subclass. r<r>s r pre_orderzBase.pre_orderxrFr newc2|jJt||Jt|ts|g}g}d}|jjD]M}||ur6|rJ|jj||f||j |d}=|j |O|sJ|j||f||j_|jj|jj|D]}|j|_d|_y)z/Replace this node with a new one in the parent.NFT) r'str isinstancelistr(extendappendchangedinvalidate_sibling_maps)r8rK l_childrenfoundchxs rreplacez Base.replaces {{&1D 1&#t$%C ++&& &BTz C4;;#7#7s"CCy?%%c*!!"% &0t}}dC00u)   ++- #A{{AH # r c|}t|ts-|jsy|jd}t|ts-|jS)z9Return the line number which generated the invocant node.Nr)rNr%r(lineno)r8nodes r get_linenozBase.get_linenosAT4(====#DT4({{r cx|jry|jr|jjd|_y)NT)r)r'rRr>s rrRz Base.changeds-     ;; KK   !r c|jrt|jjD]_\}}||us |jj|=|jj|jj d|_|cSy)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(rRrS)r8ir[s rremovez Base.removesw ;;$T[[%9%9: 44< ,,Q/KK'')KK779"&DKH  r c|jy|jj|jj|jjJ|jjt|S)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 N)r'next_sibling_mapupdate_sibling_mapsidr>s r next_siblingzBase.next_sibling` ;;  ;; ' ' / KK + + -{{++777{{++BtH55r c|jy|jj|jj|jjJ|jjt|S)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. N)r'prev_sibling_maprdrer>s r prev_siblingzBase.prev_siblingrgr r%c#bK|jD]}|jEd{y7wr;)r(leavesr8childs rrlz Base.leavess+]] &E||~ % % & %s #/-/cV|jyd|jjzS)Nrr)r'depthr>s rrpz Base.depths' ;; 4;;$$&&&r c<|j}|y|j}|S)z Return the string immediately following the invocant node. This is effectively equivalent to node.next_sibling.prefix r)rfr?)r8next_sibr?s r get_suffixzBase.get_suffixs& $$   r rN)$__name__ __module__ __qualname____doc__r__annotations__r'rrONLr)boolr*r-rr9propertyrMr?r!r6rDrCrrHrJrrXr\rRrarfrjrlrprsr rr"r"6s I#FHV #2hKK# CD""" "" "R "D "2SR"B"2""HRL""8B<"5T"X.40HSM    6hrl 6 6 6hrl 6 6&(&'s' C r c deZdZUdZeeeed<eee ed< dde dee deedee deeed df d Z d e fd Z d e fd Zd ed efdZddZd ee fdZd ee fdZed e fdZej0de d dfdZde de d dfdZde de d dfdZde d dfdZddZddZy)r$z+Concrete implementation for interior nodes.fixers_applied used_namesNrr(contextr?rc |dk\sJ|||_t||_|jD]%}|jJt |||_'|j |||_|r |dd|_yd|_y)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)rrOr(r'reprrSr?r)r8rr(rr?rrVs r__init__z Node.__init__s s{ D { X -- B99$ .d2h .$BI  $$&   DK "0"3D "&D r c|jJ|jjdt|jd|jdS))Return a canonical string representation.(, ))rr4rurr(r>s r__repr__z Node.__repr__ sFyy$$$..))*!Idii,@+ADMMCTTUVVr cTdjtt|jS)k Return a pretty string representation. This reproduces the input source exactly. r)joinmaprMr(r>s r__str__z Node.__str__s wws3 .//r r2cd|j|jf|j|jfk(SzCompare two nodes for equality.)rr(r7s rr6zNode._eqs' 4==)ejj%..-IIIr c|jJ t|j|jDcgc]}|jc}|jScc}wN)r)rr$r(rCr)r8rVs rrCz Node.clonesMyy$$$2 II"&-- 0BRXXZ 0..  0sA c#jK|jD]}|jEd{|y7 wz*Return a post-order iterator for the tree.N)r(rHrms rrHzNode.post_order&s5]] *E'') ) ) *  *s #31 3c#jK||jD]}|jEd{y7wz)Return a pre-order iterator for the tree.N)r(rJrms rrJzNode.pre_order,s1 ]] )E( ( ( ) (s '313cN|jsy|jdjS)zO The whitespace and comments preceding this node in the input. rrr(r?r>s rr?z Node.prefix2s# }}}}Q&&&r cF|jr||jd_yyNrrr8r?s rr?z Node.prefix;s ==&,DMM!  # r r`rnc||_d|j|_||j|<|j|jy)z Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. N)r'r(rRrSr8r`rns r set_childzNode.set_child@s?  "& a  a  $$&r c||_|jj|||j|j y)z Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. N)r'r(insertrRrSrs r insert_childzNode.insert_childKs4   Q&  $$&r c||_|jj||j|j y)z Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. N)r'r(rQrRrSrms r append_childzNode.append_childUs2   U#  $$&r c d|_d|_yr;)rircr>s rrSzNode.invalidate_sibling_maps_sCGCGr ci}i}||_||_d}|jD] }||t|<||t|<|}"d|t<yr;)rircr(re)r8_prev_nextpreviouscurrents rrdzNode.update_sibling_mapscse)+)+ % %!%}} G!)E"W+ ")E"X, H "bkr NNN)rr$rt)rurvrwrxrrOrrysetrMrrzrrrr"r{r6rCrrHrJr|r?setterrrrrSrdr}r rr$r$sj5T#Y''S"" "& $.2 ''r('# '  ' !c+ ' '<W#W 00JJ$J HRL )8B<) ''' ]]-S-T-- '3 'r 'd ''c'"'''"''H "r r$ceZdZUdZeed<eeed<eed<dZ e ded<e e eed<dZ d Z eed <d Zeed <dZe ded <ddgddfd edede ede edeede dd e dddfdZdefdZdefdZdddefdZddZdedfdZdedfdZdedfdZedefdZej:deddfdZy)r%z'Concrete implementation for leaf nodes.valuer bracket_depthNopening_bracketrrrrZcolumnfmt_pass_converted_first_leafrrr?rcd|cxkr dksJ|J|||\|_\|_|_||_||_|||_|dd|_g|_||_||_y)z Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. rrN) _prefixrZrrrrr(rr)r8rrrr?rrrs rrz Leaf.__init__s}"D3$$$$  7> 4DL44;    !DL3A!3D .-J*r cddlm}|jJ|jjd|j |j|jd|j dS)rr)tok_namerrr) pgen2.tokenrrr4rugetr)r8rs rrz Leaf.__repr__sZ)yy$$$~~&&'qdii)K(LM ~Q  r cF|jt|jzS)r)rrMrr>s rrz Leaf.__str__s ||c$**o--r r2cd|j|jf|j|jfk(Sr)rrr7s rr6zLeaf._eqs' 4::&5::u{{*CCCr c|jJ t|j|j|j|j|j ff|j Sr)rr%rr?rZrrr>s rrCz Leaf.clonesRyy$$$2 II JJ [[4;; 4 5..   r c#K|ywr;r}r>s rrlz Leaf.leavess  c#K|ywrr}r>s rrHzLeaf.post_order  rc#K|ywrr}r>s rrJzLeaf.pre_orderrrc|jS)zP The whitespace and comments preceding this token in the input. )rr>s rr?z Leaf.prefixs ||r c2|j||_yr;)rRrrs rr?z Leaf.prefixs  r )rr%)rurvrwrxrMryrOrrrrrrrZrrContextrrrr{r6rCrrlrHrJr|r?rr}r rr%r%ps1 JI(,OXf%,S""GFCOFCO7;!8F#3: &* $$&,0:>KKK'" K  K S K"&)K(0'7K K: # ..DDDD (HV,8F+  ]]STr r%grraw_nodec|\}}}}|s||jvr%|Jt|dk(r|dSt|||St||xsd|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)rr) number2symbollenr$r%)rrrrrr(s rconvertrsh&."D%(42+++### x=A A; D(G44D%+2w77r ceZdZUdZeeed<dZdZe ed<dZ ee ed<dZ de fdZ ddefd Zdd Zdd ed eedefd Zddeed eedefdZdeedeeeeffdZy) 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. rNcontentrcJ|tusJdtj|S)z>Constructor that prevents BasePattern from being instantiated.zCannot instantiate BasePattern)rr,r-r.s rr-zBasePattern.__new__s%+%G'GG%~~c""r rc |jJt|j|j|jg}|r|d |d=|r|d |jj ddj tt|dS)Nrrr) rrrrr4rurrr)r8r0s rrzBasePattern.__repr__s~yy$$$$))$dllDII>tBx'RtBx'..))*!DIIc$o,F+GqIIr ctr;r<r8r[resultss r _submatchzBasePattern._submatchs!!r c|S)z A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. r}r>s roptimizezBasePattern.optimizes  r r[rc|j|j|jk7ry|j0d}|i}|j||sy|r|J|j|||jr|||j<y)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)rrrupdater)r8r[rrs rmatchzBasePattern.matchs 99 TYY$))%; << #$(A">>$****q!  499!%GDII r nodescJt|dk7ry|j|d|S)z Does this pattern exactly match a sequence of nodes? Default implementation for non-wildcard patterns. rFr)rrr8rrs r match_seqzBasePattern.match_seq4s' u:?zz%(G,,r c#NKi}|r|j|d|rd|fyyyw)z} Generator yielding all matches for this pattern. Default implementation for non-wildcard patterns. rrN)r)r8rrs rgenerate_matcheszBasePattern.generate_matches>s1  TZZa!,Q$J-5s#%r;)rr)rurvrwrxrrryrrrrrMr-rr{rrrz_ResultsrrOrrtuplerr}r rrrs  3- DGSD(3-# J#J"t""x'9T4-tBx-(82D-PT-d2h8E#x-= 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. NrT) rNrMrrOr_rWildcardPatternrrrr)r8rrr newcontentr`items rrzNodePattern.__init__|s*  3; $ $;  !'3/ >g >/gJ$Z0 *4!$ 4?q$i?4dO4%)DN  * !  r c|jrVt|j|jD]2\}}|t |jk(s||j |yyt |jt |jk7ryt |j|jD]\}}|j||ryy)rTF)rrrr(rrzipr)r8r[rcr subpatternrns rrzNodePattern._submatchs >>(t}}E 1DMM***q)   t|| DMM 2 2!$T\\4==!A  J##E73 r rr;) rurvrwrr{ryrrrrMrrr}r rrrys`It#+/" #sm#(3-(#sm #  #Jtr rc eZdZUdZeed<eed<ddedfdeedededeeddf d Z de fd Z dde fd Z dde fd Zdeeeeffd ZdeeeeffdZdeeeffdZdeeeeffdZy)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. minmaxNrrrrc*d|cxkr|cxkr tks nJ||f|Ud}tt||}t|sJt ||D]}t|rJt ||_||_||_||_y)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)* rNct|Sr;)r)ss rz*WildcardPattern.__init__..s %(r ) r rrrrrrrr)r8rrrrfwrapped_contentalts rrzWildcardPattern.__init__s:C&3&$&2c 2&  "A#C7O4O' * '' +3x*c*x +&  r cd}|jEt|jdk(r-t|jddk(r|jdd}|jdk(r\|jdk(rM|jt |j S|)|j |j k(r|j S|jdkrt|trx|jdkri|j |j k(rPt|j|j|jz|j|jz|j S|S)z+Optimize certain stacked wildcard patterns.Nrr)r) rrrrrrrrNr)r8rs rrzWildcardPattern.optimizes LL $DLL!Q&DLLO$)a+J 88q=TXX]||#" 22%$))z*F!**,, HHM:7!# Z__,""":>>):>>)    r c(|j|g|S)z'Does this pattern exactly match a node?)rrs rrzWildcardPattern.matchs~~tfg..r c|j|D]L\}}|t|k(s|5|j||jrt |||j<yy)z4Does this pattern exactly match a sequence of nodes?TF)rrrrrO)r8rrrrs rrzWildcardPattern.match_seqs^))%0 DAqCJ&NN1%yy-1%[ *  r c #&K|jbt|jdtt||jzD](}i}|j r|d|||j <||f*y|j dk(r|j |yttdr#tj}tt_ |j|dD])\}}|j r|d|||j <||f+ ttdr t_ yy#t$r@|j|D])\}}|j r|d|||j <||f+YewxYw#ttdr t_ wwxYww)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_matchesrsysstderrr _recursive_matches RuntimeError_iterative_matches)r8rcountr save_stderrs rrz WildcardPattern.generate_matches$sq << txxSUTXX-F)FG 99#(%=AdiiLQh   YY+ %))%0 0 sM*!jj %Z  - $ 7 7q A#HE1yy',Ve}$)) (N#3 .!,CJ/  #!% 7 7 >#HE1yy',Ve}$)) (N# #3 .!,CJ/s=CF>D$E0F$AE-*E0,E--E00FFc#Kt|}d|jk\rdifg}|jD]/}t||D]\}}||f|j ||f 1|rg}|D]\}} ||ks ||j ks|jD]b}t|||dD]N\} } | dkDs i}|j | |j | || z|f|j || z|fPd|}|ryyw)z(Helper to iteratively yield the matches.rN)rrrrrQrr) r8rnodelenrrrr new_resultsc0r0c1r1s rr z"WildcardPattern._iterative_matchesQs'e* =R%K<< 'C(e4 '1d 1v& ' ' K! ABr*s/00* 2  c*, T#uS#X& ',66c3h6" T  > U38_$ % Xc]HW$5xR7II JoodE"4E"Pd4dN8878r8( R=XXv-*+-*`A+AHN)kN)b[D%;%(,R% eCM"#%r