B
„ fëM ã @ s d Z ddlZedƒ\ZZZZZe dej ej
B ¡jZe dej ¡j
Ze dej ejB ¡j
Ze dej ¡j
Ze dej ¡j
Ze d ej ¡j
ZG d
d„ deƒZe edƒd
¡Ze dd„ dD ƒ¡ e dd„ dD ƒ¡ e dd„ dD ƒ¡ G dd„ dƒZedk�rddlmZ eddd� dS )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 @ s e Zd ZdZdd„ ZdS )ÚParseMapap 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 C s dS )Néx © )ÚselfÚkeyr r ú4/opt/alt/python37/lib64/python3.7/idlelib/pyparse.pyÚ__missing__r s zParseMap.__missing__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r r r r r r f s
r é€ r c c s | ]}t |ƒt d ƒfV qdS )ú(N)Úord)Ú.0Úcr r r ú x s r z({[c c s | ]}t |ƒt d ƒfV qdS )ú)N)r )r r r r r r y s z)}]c c s | ]}t |ƒt |ƒfV qd S )N)r )r r r r r r z s z"'\
#c @ s| e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd d
„ Zdd„ Zd
d„ Z dd„ Z
dd„ Zdd„ Zdd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚParserc C s || _ || _d S )N)ÚindentwidthÚtabwidth)r r r r r r Ú__init__ s zParser.__init__c C s, t |ƒdks|d dkst‚|| _d| _d S )Nr éÿÿÿÿÚ
)ÚlenÚAssertionErrorÚcodeÚstudy_level)r Úsr r r Úset_codeƒ s zParser.set_codec C sâ | j d }}t|ƒ}xdtdƒD ]X}| dd|¡}|dk r:P | dd|¡d }t|||ƒ}|rr|| ¡ ƒsr| ¡ }P |}qW |dkr¦t|ƒ}|r¢|| ¡ ƒs¢| ¡ }|S |d }x.t||ƒ}|rØ| ¡ \}}||ƒsÚ|}q°P q°W |S )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 é )r r ÚrangeÚrfindÚ_synchreÚstartÚspan) r Zis_char_in_stringr ÚposÚlimitZtriesÚiÚmr r r r Úfind_good_parse_startˆ s4
zParser.find_good_parse_startc C s: |dks| j |d dkst‚|dkr6| j |d… | _ dS )zx Throw away the start of the string.
Intended to be called with the result of find_good_parse_start().
r r! r N)r r )r Úlor r r Úset_loÀ s z
Parser.set_loc
C s | j dkrdS d| _ | j}| t¡}| dd¡}| dd¡}| dd¡}| dd¡}| dd¡}t}d }}d g | _}|j}d t|ƒ }}�x||k �r¤|| } |d }| dkr´q�| dkrÖ|d }|d kr�||ƒ q�| d
krè|d }q�| dk�r |r�|d }q�| dk�s| d
k�r2| }
||d |d … |
d k�r>|
d }
|}t|
ƒd }|| }xØ||k �r|| } |d }| dk�r€�qX||d || … |
k�r¤|| }P | dk�rØ|d }|d k�rX|d k�rÒ||ƒ P �qX| dk�rX||k �sðt ‚|| dk�r|d }|d }�qX�qXW |d |k�r,t
}q�t}q�| dk�rV| d|¡}|d ks�t ‚q�| dk�sdt ‚||k �srt ‚|| dk�rš|d }|d |k�ršt
}|d }q�W |t
k�rÈ|tk�rÈ|d k�rÈt}|| _|tk|d |kk�sèt ‚|d |k�rþ||ƒ dS )záFind the line numbers of non-continuation lines.
As quickly as humanly possible , find the line numbers (0-
based) of the non-continuation lines.
Creates self.{goodlines, continuation}.
r! NZxxxxxxxxÚxZxxxxZxxz
xr r r r ú"ú'é é ú\ú#r )r r Ú translateÚtransÚreplaceÚC_NONEÚ goodlinesÚappendr r ÚC_STRING_FIRST_LINEÚC_STRING_NEXT_LINESÚfindÚC_BACKSLASHÚ C_BRACKETÚcontinuation)
r r r@ ÚlevelZlnor9 Z push_goodr) ÚnÚchZquoteZfirstlnoÚwr r r Ú_study1É s¤
zParser._study1c C s | ¡ | jS )N)rE r@ )r r r r Úget_continuation_typeO s zParser.get_continuation_typec C sà | j dkrdS | ¡ d| _ | j| j }}t|ƒd }t|ƒ}x`|rž|sLt‚|}x4t||d || ƒD ]}| dd|d ¡d }qhW t||ƒrš|d }q@P q@W |dkr¸|dks´t‚|}|| | _ | _
d}g }|j} |dfg}
�xÖ||k �r¶t|||ƒ}|�rX|
¡ }|d }x&||k�r4|| dk�r4|d }�qW ||k�rH|| }|}||k�rXP || }
|
dk�r’| |ƒ |
|t|ƒf¡ |
}|d }qâ|
d k�rÈ|�r¨|d
= |
}|d }|
|t|ƒf¡ qâ|
dk�sÜ|
dk�r|
|t|ƒd f¡ |
}t|||ƒ
¡ }|
|t|ƒf¡ qâ|
d
k�rn|
|t|ƒd f¡ | d||¡d }|dk�sZt‚|
|t|ƒf¡ qâ|
dk�s|t‚|d }||k �s’t‚|| dk�r¬|
|| }|d }qâW || _|�rÌ|d
nd| _t|
ƒ| _dS )am
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
r1 Nr! r r Ú z
z([{z)]}r r/ r0 r4 r3 )r rE r r9 r r r" r# Ú_junkreÚ
stmt_startÚstmt_endr: Ú_chew_ordinaryreÚendÚ_match_stringrer= ÚlastchÚlastopenbracketposÚtupleÚstmt_bracketing)r r r9 r) ÚpÚqZnothingrN ÚstackZ
push_stackZ
bracketingr* ZnewprC r r r Ú_study2S sŒ
zParser._study2c C sÐ | ¡ | jtkst‚| j}| j}t|ƒ}| dd|¡d }}|d }xj||k rŠt||ƒ}|rt| ¡ d }d}P qJ|
d|¡d }}qJW | }}x|| dkr¬|d }q–W | j}t|||… | j
¡ƒ| S )zpReturn number of spaces the next line should be indented.
Line continuation must be C_BRACKET.
r r r! z )rU r@ r? r rO r r r# Ú_itemrerL r= r Ú
expandtabsr )r Újr rB Zorigir) r* Zextrar r r Úcompute_bracket_indentÎ s&