
   ndV                   F   S r SSKr\" S5      u  rrrrr\R                  " S\R                  \R                  -  5      R                  r\R                  " S\R                  5      R                  r\R                  " S\R                  \R                  -  5      R                  r\R                  " S\R                  5      R                  r\R                  " S\R                  5      R                  r\R                  " S	\R                  5      R                  r " S
 S\5      r\R-                  \" S5      S5      r\R1                  S S 5       5        \R1                  S S 5       5        \R1                  S S 5       5         " S S5      r\S:X  a  SSKJr  \" SSS9  gg)a  Define partial Python code Parser used by editor and hyperparser.

Instances of ParseMap are used with str.translate.

The following bound search and match functions are defined:
_synchre - start of popular statement;
_junkre - whitespace or comment line;
_match_stringre: string, possibly without closer;
_itemre - line that may have bracket structure start;
_closere - line that must be followed by dedent.
_chew_ordinaryre - non-special characters.
    N   z
    ^
    [ \t]*
    (?: while
    |   else
    |   def
    |   return
    |   assert
    |   break
    |   class
    |   continue
    |   elif
    |   try
    |   except
    |   raise
    |   import
    |   yield
    )
    \b
z'
    [ \t]*
    (?: \# \S .* )?
    \n
aK  
    \""" [^"\\]* (?:
                     (?: \\. | "(?!"") )
                     [^"\\]*
                 )*
    (?: \""" )?

|   " [^"\\\n]* (?: \\. [^"\\\n]* )* "?

|   ''' [^'\\]* (?:
                   (?: \\. | '(?!'') )
                   [^'\\]*
                )*
    (?: ''' )?

|   ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
zM
    [ \t]*
    [^\s#\\]    # if we match, m.end()-1 is the interesting char
z_
    \s*
    (?: return
    |   break
    |   continue
    |   raise
    |   pass
    )
    \b
z
    [^[\](){}#'"\\]+
c                       \ rS rSrSrS rSrg)ParseMapf   aT  Dict subclass that maps anything not in dict to 'x'.

This is designed to be used with str.translate in study1.
Anything not specifically mapped otherwise becomes 'x'.
Example: replace everything except whitespace with 'x'.

>>> keepwhite = ParseMap((ord(c), ord(c)) for c in ' \t\n\r')
>>> "a + b\tc\nd".translate(keepwhite)
'x x x\tx\nx'
c                     g)Nx    )selfkeys     &/usr/lib/python3.13/idlelib/pyparse.py__missing__ParseMap.__missing__r   s        r	   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r	   r   r   r   r   f   s    	r   r      r   c              #   N   #    U  H  n[        U5      [        S 5      4v   M     g7f)(Nord.0cs     r   	<genexpr>r   x        /Ac!fc#h   #%z({[c              #   N   #    U  H  n[        U5      [        S 5      4v   M     g7f))Nr   r   s     r   r   r   y   r   r    z)}]c              #   N   #    U  H  n[        U5      [        U5      4v   M     g 7fNr   r   s     r   r   r   z   s     2z!c!fc!fzr    z"'\
#c                   h    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rSrg)Parser}   c                     Xl         X l        g r$   )indentwidthtabwidth)r
   r)   r*   s      r   __init__Parser.__init__   s    & r   c                 R    [        U5      S:X  d  US   S:X  d   eXl        SU l        g )Nr   
)lencodestudy_level)r
   ss     r   set_codeParser.set_code   s*    1v{aetm++	r   c                 \   U R                   Sp2[        U5      n[        S5       Hv  nUR                  SSU5      nUS:  a    O\UR                  SSU5      S-   n[	        X&U5      nU(       a-  U" UR                  5       5      (       d  UR                  5       n  OUnMx     Uc?  [	        U5      nU(       a+  U" UR                  5       5      (       d  UR                  5       nU$ US-   n[	        X&5      =n(       a5  UR                  5       u  pU" U5      (       d  Un[	        X&5      =n(       a  M5  U$ )a  
Return index of a good place to begin parsing, as close to the
end of the string as possible.  This will be the start of some
popular stmt like "if" or "def".  Return None if none found:
the caller should pass more prior context then, if possible, or
if not (the entire program text up until the point of interest
has already been tried) pass 0 to set_lo().

This will be reliable iff given a reliable is_char_in_string()
function, meaning that when it says "no", it's absolutely
guaranteed that the char is not in a string.
Nr   z:
r   r/      )r1   r0   rangerfind_synchrestartspan)	r
   is_char_in_stringr1   poslimittriesimr3   s	            r   find_good_parse_startParser.find_good_parse_start   s    IItc
 D	1XE

5!U+A1u

4A&*A%(A*177955ggiE  ; A*177955ggiJ !GD$$a$668DA$Q'' D$$a$ 
r   c                 v    US:X  d  U R                   US-
     S:X  d   eUS:  a  U R                   US U l         gg)zgThrow away the start of the string.

Intended to be called with the result of find_good_parse_start().
r   r7   r/   N)r1   )r
   los     r   set_loParser.set_lo   sA    
 Qw$))BqD/T1116		"#DI r   c                    U R                   S:  a  gSU l         U R                  nUR                  [        5      nUR	                  SS5      nUR	                  SS5      nUR	                  SS5      nUR	                  SS5      nUR	                  SS5      n[
        nS	=p4S	/=U l        nUR                  nS	[        U5      pXx:  Gak  X   n	US-   nU	S:X  a  M  U	S:X  a  US-   nUS	:X  a  U" U5        M2  U	S
:X  a  US-   nM?  U	S:X  a  U(       a  US-
  nMS  U	S:X  d  U	S:X  a  U	n
XS-
  US-    U
S-  :X  a  U
S-  n
Un[        U
5      S-
  nX|-   nXx:  am  X   n	US-   nU	S:X  a  M  XS-
  X|-    U
:X  a  X|-   nO`U	S:X  a  US-   nUS	:X  a  US	:X  a  U" U5        O@MJ  U	S:X  a  Xx:  d   eX   S:X  a  US-   nUS-   nMk  Xx:  a  Mm  US-
  U:X  a  [        nO[        nGM  U	S:X  a  UR                  SU5      nUS	:  d   eGM9  U	S:X  d   eXx:  d   eX   S:X  a  US-   nUS-   U:X  a  [        nUS-   nXx:  a  GMk  U[        :w  a  U[        :w  a  US	:  a  [        nX l        U[
        :H  US   U:H  :X  d   eUS   U:w  a	  U" U5        gg)zFind the line numbers of non-continuation lines.

As quickly as humanly possible <wink>, find the line numbers (0-
based) of the non-continuation lines.
Creates self.{goodlines, continuation}.
r7   Nxxxxxxxxxxxxxxxz
xr/   r   r   r"   "'      \#r.   )r2   r1   	translatetransreplaceC_NONE	goodlinesappendr0   C_STRING_FIRST_LINEC_STRING_NEXT_LINESfindC_BACKSLASH	C_BRACKETcontinuation)r
   r1   r_   levellnorX   	push_goodrA   nchquotefirstlnows                r   _study1Parser._study1   s    q  yy~~e$||J,||FC(||D#&||D#&||E4( &'S($$	#d)1eB!A SyTzAgA:cNSy	Sy!AIESyB#I!AaC=EAI-!AIEJNCeB!ASy aC}-CTz!Ag6$z )#! Tz uu7d?"%'CaC 3 e@ aH, (;':SyIIdA&Avv::5L5w$AgQ3!8#.L!Ak et // 33	$L( &IbMS,@AAAR=CcN  r   c                 :    U R                  5         U R                  $ r$   )rh   r_   r
   s    r   get_continuation_typeParser.get_continuation_typeK  s       r   c                    U R                   S:  a  gU R                  5         SU l         U R                  U R                  p![	        U5      S-
  n[	        U5      nU(       a^  U(       d   eUn[        X#S-
     X#   5       H  nUR                  SSUS-
  5      S-   nM     [        X5      (       a  US-
  nOO	U(       a  M^  US:X  a
  US:X  d   eUnUWsU l        U l	        Sn/ nUR                  n	US4/n
XE:  Ga  [        XU5      nU(       aH  UR                  5       nUS-
  nX4:  a  X   S;   a  US-
  nX4:  a
  X   S;   a  M  X4:  a  X   nUnXE:  a  GOcX   nUS;   a-  U	" U5        U
R                  U[	        U5      45        UnUS-   nM  US	;   a/  U(       a  US
	 UnUS-   nU
R                  U[	        U5      45        M  US:X  d  US:X  aZ  U
R                  U[	        U5      S-   45        Un[        XU5      R                  5       nU
R                  U[	        U5      45        GM3  US:X  a[  U
R                  U[	        U5      S-   45        UR                  SXE5      S-   nUS:  d   eU
R                  U[	        U5      45        GM  US:X  d   eUS-   nXE:  d   eX   S:w  a  XU   -   nUS-   nXE:  a  GM  Xpl        U(       a  US
   OSU l        [#        U
5      U l        g)a  
study1 was sufficient to determine the continuation status,
but doing more requires looking at every character.  study2
does this for the last interesting statement in the block.
Creates:
    self.stmt_start, stmt_end
        slice indices of last interesting stmt
    self.stmt_bracketing
        the bracketing structure of the last interesting stmt; for
        example, for the statement "say(boo) or die",
        stmt_bracketing will be ((0, 0), (0, 1), (2, 0), (2, 1),
        (4, 0)). Strings and comments are treated as brackets, for
        the matter.
    self.lastch
        last interesting character before optional trailing comment
    self.lastopenbracketpos
        if continuation is C_BRACKET, index of last open bracket
rP   Nr7   r/   r     	
([{)]}r.   rN   rO   rS   rR   )r2   rh   r1   rX   r0   r8   r9   _junkre
stmt_startstmt_endrY   _chew_ordinaryreend_match_stringrer\   lastchlastopenbracketpostuplestmt_bracketing)r
   r1   rX   rA   pqnothingry   stack
push_stack
bracketingrB   newprd   s                 r   _study2Parser._study2O  s   & q  ))T^^i	NQIH1 A Q3>JJtQ!,q0 ?
   aC a 66M6A)*A& \\
!fX
e !,Auuw1HfG!3!A fG!36!WF6BU{1!!1c%j/2aCU{b	aC!!1c%j/2SyB#I !!1c%jl"34#DQ/335!!1c%j/2Sy!!1c%jl"34IIdA)A-1uu!!1c%j/2::!A5L5w$1g!A{ eB /4%)$$Z0r   c                    U R                  5         U R                  [        :X  d   eU R                  nU R                  n[        U5      nUR                  SSU5      S-   =pEUS-   nX:  aE  [        X!5      nU(       a  UR                  5       S-
  nSnOCUR                  SU5      S-   =pQX:  a  ME  U=pX!   S;   a  US-   nX!   S;   a  M  U R                  n[        X%U R                  U R                  5      5      U-   $ )z`Return number of spaces the next line should be indented.

Line continuation must be C_BRACKET.
r/   r   r7    	)r   r_   r^   rz   r1   r0   r9   _itemrerw   r\   r)   
expandtabsr*   )r
   jr1   rc   origirA   rB   extras           r   compute_bracket_indentParser.compute_bracket_indent  s   
 	  I---##yyIJJtQ*Q..aCe AEEGaK 		$*Q.. e MA'U"aC 'U"$$E4!9''67%??r   c                 P    U R                  5         U R                  nUS   US   -
  $ )zReturn number of physical lines in last stmt.

The statement doesn't have to be an interesting statement.  This is
intended to be called when continuation is C_BACKSLASH.
r.   )rh   rX   )r
   rX   s     r   get_num_lines_in_stmtParser.get_num_lines_in_stmt  s)     	NN	}y},,r   c                    U R                  5         U R                  [        :X  d   eU R                  nU R                  nX   S;   a  US-   nX   S;   a  M  UnUR                  SU5      S-   nS=pVX$:  a  X   nUS;   a  US-   nUS-   nOvUS;   a  U(       a  US-
  nUS-   nO^US:X  d  US:X  a  [        XU5      R                  5       nO7US	:X  a  O7US:X  a%  US
:X  a  US:X  d  XS-
     S;  a  XS-      S
:w  a  SnOUS-   nX$:  a  M  U(       a   US-   n[        R                  " SXU 5      SL nU(       d  UnX   S;  a  US-   nX   S;  a  M  [        XR                  U R                  U R                  5      5      S-   $ )zReturn number of spaces the next line should be indented.

Line continuation must be C_BACKSLASH.  Also assume that the new
line is the first one following the initial line of the stmt.
r   r7   r/   r   rq   rr   rN   rO   rS   =z=<>!z\s*\\Nrp   )r   r_   r]   r1   rt   r\   rx   rw   rematchr0   r   r*   )r
   r1   rA   startposendposfoundr`   rd   s           r   compute_backslash_indentParser.compute_backslash_indent  s    	  K///yyOOg!A g 4*Q.jBU{	aCu!AIEaCsbCi#DV488:s !c	FdQ3iv5!9#aC+ j.  !AHHXtf~6$>E A'(aC '( 4*55%)]]4 5789 	9r   c                     U R                  5         U R                  U R                  p!UnU R                  nX2:  a  XC   S;   a  US-   nX2:  a
  XC   S;   a  M  XAU $ )zPReturn the leading whitespace on the initial line of the last
interesting stmt.
r   r7   )r   rt   ru   r1   )r
   rA   rc   r   r1   s        r   get_base_indent_stringParser.get_base_indent_string,  s[     	1yye5(AA e5(ayr   c                 @    U R                  5         U R                  S:H  $ )z<Return True if the last interesting statement opens a block.:)r   ry   rk   s    r   is_block_openerParser.is_block_opener8  s    {{c!!r   c                 f    U R                  5         [        U R                  U R                  5      SL$ )z=Return True if the last interesting statement closes a block.N)r   _closerer1   rt   rk   s    r   is_block_closerParser.is_block_closer=  s%    		4??34??r   c                 :    U R                  5         U R                  $ )zzReturn bracketing structure of the last interesting statement.

The returned tuple is in the format defined in _study2().
)r   r|   rk   s    r   get_last_stmt_bracketingParser.get_last_stmt_bracketingB  s    
 	###r   )r1   r_   rX   r)   ry   rz   r|   ru   rt   r2   r*   N)r   r   r   r   r+   r4   rC   rG   rh   rl   r   r   r   r   r   r   r   r   r   r	   r   r   r&   r&   }   sP    !
2h'DL!y1v@>-79r
"
@
$r   r&   __main__)mainzidlelib.idle_test.test_pyparserP   )	verbosity)r   r   r8   rW   r]   rZ   r[   r^   compileVERBOSE	MULTILINEsearchr:   r   rs   DOTALLrx   r   r   rv   dictr   fromkeysrU   updater&   r   unittestr   r	   r   r   <module>r      s   
 $)8!)i :: & ZZ",,' & !'' 	. **  ZZ	 	  **   ZZ"))!  #U! * **  ZZ   :: 	 ZZ	  	  :: ZZ 
t " 	%*c* // / // / 2z2 2K$ K$\ z	)Q7 r   