B
+ @ s d Z ddlZed\ZZZZZedej ej
B jZedej j
Zedej ejB j
Zedej j
Zedej j
Zed ej j
ZG d
d deZeedd
Zedd dD edd dD edd dD G dd dZedkrddlmZ 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 )Nx )selfkeyr 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).0cr 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)indentwidthtabwidth)r r r r r r __init__ s zParser.__init__c C s || _ d| _d S )Nr )codestudy_level)r sr r r set_code s zParser.set_codec C s | j d }}t|}xdtdD ]X}|dd|}|dk r:P |dd|d }t|||}|rr|| sr| }P |}qW |dkrt|}|r|| s| }|S |d }x.t||}|r| \}}||s|}qP qW |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 lenrangerfind_synchrestartspan) r Zis_char_in_stringr poslimitZtriesimr r r r find_good_parse_start s4
zParser.find_good_parse_startc C s |dkr| j |d | _ dS )zx Throw away the start of the string.
Intended to be called with the result of find_good_parse_start().
r N)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 rn|| } |d }| dkrq| dkr|d }|d kr|| q| d
kr|d }q| dkr |r|d }q| dks| d
kr$| }
||d |d |
d kr>|
d }
|}t|
d }|| }x||k r|| } |d }| dkrqX||d || |
kr|| }P | dkr|d }|d krX|d kr|| P qX| dkrX|| dkr|d }|d }qXqXW |d |krt }qt
}q| dkr<|d|}q|| dkrd|d }|d |krdt}|d }qW |t kr|t
kr|d krt
}|| _|d |kr|| dS )zFind 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 NZxxxxxxxxxZxxxxZxxz
xr r r r "' \#)r r translatetransreplaceC_NONE goodlinesappendr C_STRING_FIRST_LINEC_STRING_NEXT_LINESfindC_BACKSLASH C_BRACKETcontinuation)
r r r? levelZlnor8 Z push_goodr' nchZquoteZfirstlnowr r r _study1 s
zParser._study1c C s | | jS )N)rD r? )r r r r get_continuation_typeO s zParser.get_continuation_typec C s | j dkrdS | d| _ | j| j }}t|d }t|}xX|r|}x4t||d || D ]}|dd|d d }q`W t||r|d }q@P q@W |dkr|}|| | _| _ d}g }|j
} |dfg}
x||k rxt|||}|rD| }|d }x&||kr || dkr |d } qW ||kr4|| }|}||krDP || }
|
dkr~| | |
|t|f |
}|d }q|
d kr|r|d
= |
}|d }|
|t|f q|
dks|
dkr|
|t|d f |
}t
||| }|
|t|f q|
d
krL|
|t|d f |d||d }|
|t|f q|d }|| dkrn|
|| }|d }qW || _|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
r/ Nr r r z
z([{z)]}r3 r- r. r2 )r rD r r8 r r r! _junkre
stmt_startstmt_endr9 _chew_ordinaryreend_match_stringrer<