
|Nc           @   sE   d  Z  d d l Z d d l Z d d l m Z d d d     YZ d S(   sk  
HyperParser
===========
This module defines the HyperParser class, which provides advanced parsing
abilities for the ParenMatch and other extensions.
The HyperParser uses PyParser. PyParser is intended mostly to give information
on the proper indentation of code. HyperParser gives some information on the
structure of code, used by extensions to help the user.
iN(   t   PyParset   HyperParserc           B   st   e  Z d    Z d   Z d   Z d   Z d e d  Z d Z e	 j
 e	 j d Z e	 j
 d Z d   Z d	   Z RS(
   c         C   s  | |  _  | j |  _ } t j | j | j  } d   } | | j |   } | j s x | j D] } t	 | | d  } t
 |  d }	 d | }
 | j | j |	 |
  d  | j | j |	   } | d
 k	 s | d k rb Pqb qb W| j | p d  n_ | j d |  } | r%| d }	 n d }	 d | }
 | j | j |	 |
  d  | j d  | j d	  |  _ |
 |  _ | j   |  _ g  t t |  j   D]8 } | d k o|  j | d |  j | d d k ^ q|  _ |  j |  d
 S(   s[   Initialize the HyperParser to analyze the surroundings of the given
        index.
        c         S   s   t  t |    S(   N(   t   intt   float(   t   index(    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt
   index2line   s    i   s   .0s   %d.ends    
i    t   consoles   1.0iN(   t   editwint   textR    t   Parsert   indentwidtht   tabwidthR   t   context_use_ps1t   num_context_linest   maxt   reprt   set_strt   gett   find_good_parse_startt   _build_char_in_string_funct   Nonet   set_lot   tag_prevranget   strt   rawtextt   stopatindext   get_last_stmt_bracketingt
   bracketingt   ranget   lent   isopenert	   set_index(   t   selfR   R   R   t   parserR   t   lnot   contextt   startatt   startatindexR   t   bodt   rt   i(    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   __init__   s:    			

	Tc         C   s  t  |  j  t  |  j j | |  j   } | d k  rF t d   n  | |  _ d |  _ xP |  j t  |  j  d k  r |  j |  j d d |  j k  r |  j d 7_ q[ W|  j t  |  j  d k  r|  j |  j d d |  j k r|  j	 |  j d r|  j d 7_ n  d S(   si   Set the index to which the functions relate. Note that it must be
        in the same statement.
        i    s0   The index given is before the analyzed statementi   N(
   R   R   R   R   R   t
   ValueErrort   indexinrawtextt   indexbracketR   R   (   R    R   R+   (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyR   H   s    +		!!c         C   s/   |  j  |  j o. |  j |  j |  j d d k S(   s5   Is the index given to the HyperParser is in a string?i    t   "t   '(   R-   R.   (   R   R,   R   R   (   R    (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   is_in_string[   s    c         C   s0   |  j  |  j p/ |  j |  j |  j d d k S(   s:   Is the index given to the HyperParser is in a normal code?i    t   #R-   R.   (   R0   R-   R.   (   R   R,   R   R   (   R    (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt
   is_in_codeb   s    s   ([{c      
   C   s  |  j  |  j d } |  j } x{ |  j | sc |  j |  j  | d | k sc |  j  | d | k r | d 8} | d k  r} d St | |  j  | d  } q  W|  j d } x: | t |  j   k  r |  j  | d | k r | d 7} q W|  j j d |  j	 t |  j  |  j  | d f  } | t |  j   k sQ|  j  | d t |  j  k rg| r[d S|  j	 } n; |  j j d |  j	 t |  j  |  j  | d d f  } | | f S(   s  If the index given to the HyperParser is surrounded by a bracket
        defined in openers (or at least has one before it), return the
        indices of the opening bracket and the closing bracket (or the
        end of line, whichever comes first).
        If it is not surrounded by brackets, or the end of line comes before
        the closing bracket and mustclose is True, returns None.
        i   i    s   %s-%dcN(
   R   R,   R   R   R   t   minR   R   R   R   (   R    t   openerst	   mustcloset   bracketinglevelt   beforet   aftert   beforeindext
   afterindex(    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   get_surrounding_bracketsh   s0    	
+ )s    	
\t   _c         C   s   | } x1 | | k r9 | | d |  j  k r9 | d 8} q	 W| | k  rx | | |  j k so t j | | | ! rx | } n  | | S(   Ni   (   t	   _id_charst   _id_first_charst   keywordt	   iskeyword(   R    R   t   limitt   posR(   (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   _eat_identifier   s    &	c   
   	   C   s(  |  j    s t d   n  |  j } |  j } |  j } | | d } |  j } | } t } xx | | k r | | d |  j k r | d 8} q_ | r | | k r | | d d k r | d 8} t } q_ | | k r*| d k r*| | | d d d k r*| d 8} | | d } | | d d } q_ Pq_ | s8Pn  |  j | | |  } | rl| | } | } t	 } q\ | | k r| | d }	 x2 | d k r| | d d |	 k r| d 8} qW| | d | k rPn  | | d } | d 8} | | d } | } | | d k rqPq\ Pq\ | | |  j !S(   s   Return a string with the Python expression which ends at the given
        index, which is empty if there is no real one.
        s?   get_expression should only be called if index is inside a code.i    i   t   .R0   i   s   ([(
   R1   R*   R   R   R,   R+   t   Truet   _whitespace_charsRB   t   False(
   R    R   R   t
   brck_indext
   brck_limitRA   t   last_identifier_post   postdot_phaset   rett   level(    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   get_expression   sZ    				# 
	

	'
(   t   __name__t
   __module__R)   R   R/   R1   RF   R:   RE   t   stringt   ascii_letterst   digitsR<   R=   RB   RM   (    (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyR      s   	7			'		(    (   t   __doc__RP   R>   t   idlelibR    R   (    (    (    s(   C:\RCS\Python\lib\idlelib\HyperParser.pyt   <module>	   s   