ó
MšV]c           @   s¡   d  Z  d d l m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ	 d
 e
 f d „  ƒ  YZ d „  Z d e
 f d „  ƒ  YZ d S(   s·  
The ``Parser`` tries to convert the available Python code in an easy to read
format, something like an abstract syntax tree. The classes who represent this
tree, are sitting in the :mod:`parso.tree` module.

The Python module ``tokenize`` is a very important part in the ``Parser``,
because it splits the code into different words (tokens).  Sometimes it looks a
bit messy. Sorry for that! You might ask now: "Why didn't you use the ``ast``
module for this? Well, ``ast`` does a very good job understanding proper Python
code, but fails to work as soon as there's a single line of broken code.

There's one important optimization that needs to be known: Statements are not
being parsed completely. ``Statement`` is just a representation of the tokens
within the statement. This lowers memory usage and cpu time and reduces the
complexity of the ``Parser`` (there's another parser sitting inside
``Statement``, which produces ``Array`` and ``Call``).
iÿÿÿÿ(   t   tree(   t   ReservedStringt   ParserSyntaxErrorc           B   s   e  Z d  Z d „  Z RS(   s_   
    Contains error information about the parser tree.

    May be raised as an exception.
    c         C   s   | |  _  | |  _ d  S(   N(   t   messaget
   error_leaf(   t   selfR   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   __init__$   s    	(   t   __name__t
   __module__t   __doc__R   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyR      s   t   InternalParseErrorc           B   s   e  Z d  Z d „  Z RS(   s¦   
    Exception to signal the parser is stuck and error recovery didn't help.
    Basically this shouldn't happen. It's a sign that something is really
    wrong.
    c         C   sK   t  j |  d | | j | | f ƒ | |  _ t |  _ | |  _ | |  _ d  S(   Ns#   %s: type=%r, value=%r, start_pos=%r(   t	   ExceptionR   t   namet   msgt   typet   valuet	   start_pos(   R   R   t   type_R   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyR   0   s    			(   R   R   R	   R   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyR
   )   s   t   Stackc           B   s   e  Z d  „  Z RS(   c            s   ‡  f d †  } t  | ƒ  ƒ S(   Nc          3   sa   xZ t  ˆ  ƒ D]L }  x3 |  j j D]% } t | t ƒ r@ | j Vq  | Vq  W|  j j s Pq q Wd  S(   N(   t   reversedt   dfat   transitionst
   isinstanceR   R   t   is_final(   t
   stack_nodet
   transition(   R   (    s+   lib/python2.7/site-packages/parso/parser.pyt   iterate;   s    	(   t   list(   R   R   (    (   R   s+   lib/python2.7/site-packages/parso/parser.pyt)   _allowed_transition_names_and_token_types:   s    (   R   R   R   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyR   9   s   t	   StackNodec           B   s)   e  Z d  „  Z e d „  ƒ Z d „  Z RS(   c         C   s   | |  _  g  |  _ d  S(   N(   R   t   nodes(   R   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyR   K   s    	c         C   s
   |  j  j S(   N(   R   t	   from_rule(   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   nonterminalO   s    c         C   s   d |  j  j |  j |  j f S(   Ns
   %s(%s, %s)(   t	   __class__R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   __repr__S   s    (   R   R   R   t   propertyR    R"   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyR   J   s   	c         C   s3   | j  r/ y |  j | SWq/ t k
 r+ q/ Xn  | S(   N(   t   contains_syntaxt   reserved_syntax_stringst   KeyError(   t   grammarR   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   _token_to_transitionW   s    	t
   BaseParserc           B   sq   e  Z d  Z i  Z e j Z i  Z e j Z	 d e
 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(	   sI  Parser engine.

    A Parser instance contains state pertaining to the current token
    sequence, and should not be used concurrently by different threads
    to parse separate token sequences.

    See python/tokenize.py for how to get input tokens by a string.

    When a syntax error occurs, error_recovery() is called.
    t
   file_inputc         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _pgen_grammart   _start_nonterminalt   _error_recovery(   R   t   pgen_grammart   start_nonterminalt   error_recovery(    (    s+   lib/python2.7/site-packages/parso/parser.pyR   v   s    		c         C   sÐ   |  j  j |  j d } t t | ƒ g ƒ |  _ x | D] } |  j | ƒ q6 Wx| t rË |  j d } | j j	 s t
 d | j | j | j ƒ ‚ n  t |  j ƒ d k r² |  j ƒ  qP |  j | j | j ƒ SqP Wd  S(   Ni    iÿÿÿÿs   incomplete inputi   (   R+   t   nonterminal_to_dfasR,   R   R   t   stackt
   _add_tokent   TrueR   R   R
   R   R   R   t   lent   _popt   convert_nodeR    R   (   R   t   tokenst	   first_dfat   tokent   tos(    (    s+   lib/python2.7/site-packages/parso/parser.pyt   parse{   s    	c         C   sU   |  j  r t d ƒ ‚ n9 | \ } } } } t j | | | | ƒ } t d | ƒ ‚ d  S(   Ns!   Error Recovery is not implementeds   SyntaxError: invalid syntax(   R-   t   NotImplementedErrorR    t	   ErrorLeafR   (   R   R:   R   R   R   t   prefixR   (    (    s+   lib/python2.7/site-packages/parso/parser.pyR0   ‘   s
    	c         C   s[   y |  j  | | ƒ } Wn# t k
 r< |  j | | ƒ } n Xx | D] } | | _ qD W| S(   N(   t   node_mapR&   t   default_nodet   parent(   R   R    t   childrent   nodet   c(    (    s+   lib/python2.7/site-packages/parso/parser.pyR7   ™   s    c         C   sC   y |  j  | | | | ƒ SWn! t k
 r> |  j | | | ƒ SXd  S(   N(   t   leaf_mapR&   t   default_leaf(   R   R   R   R?   R   (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   convert_leaf¢   s    c         C   s*  |  j  } |  j } | \ } } } } t | | | ƒ } xŠ t rÂ y | d j j | }	 PWq9 t k
 r™ | d j j r… |  j ƒ  q¿ |  j	 | ƒ d Sq9 t
 k
 r¾ t d | | | ƒ ‚ q9 Xq9 W|	 j | d _ x$ |	 j D] }
 | j t |
 ƒ ƒ qÝ W|  j | | | | ƒ } | d j j | ƒ d S(   sÐ   
        This is the only core function for parsing. Here happens basically
        everything. Everything is well prepared by the parser generator and we
        only apply the necessary steps here.
        iÿÿÿÿNs   too much input(   R+   R2   R(   R4   R   R   R&   R   R6   R0   t
   IndexErrorR
   t   next_dfat
   dfa_pushest   appendR   RH   R   (   R   R:   R'   R2   R   R   R   R?   R   t   plant   pusht   leaf(    (    s+   lib/python2.7/site-packages/parso/parser.pyR3   ¨   s(    			c         C   sj   |  j  j ƒ  } t | j ƒ d k r4 | j d } n |  j | j j | j ƒ } |  j  d j j | ƒ d  S(   Ni   i    iÿÿÿÿ(   R2   t   popR5   R   R7   R   R   RL   (   R   R;   t   new_node(    (    s+   lib/python2.7/site-packages/parso/parser.pyR6   È   s
    (   R   R   R	   R@   R    t   NodeRA   RF   t   LeafRG   t   FalseR   R<   R0   R7   RH   R3   R6   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyR)   c   s   
								 N(   R	   t   parsoR    t   parso.pgen2.generatorR   R   R   R
   R   R   t   objectR   R(   R)   (    (    (    s+   lib/python2.7/site-packages/parso/parser.pyt   <module>   s   	