
                         n    S SK JrJr  S SKJr  S SKrS SKJr  S SKJrJ	r	   " S S5      r
 " S S	\
5      rg)
    )IteratorListcopyN)UnexpectedToken)TokenLexerThreadc                       \ rS rSrSrS\4S jr\S\4S j5       rS\	4S jr
S\\	   4S	 jrS\\	   4S
 jrSS jrS rS rS rS rS rS rS rS rSrg)InteractiveParser   zInteractiveParser gives you advanced control over parsing and error handling when parsing with LALR.

For a simpler interface, see the ``on_error`` argument to ``Lark.parse()``.
lexer_threadc                 6    Xl         X l        X0l        S U l        g N)parserparser_stater   result)selfr   r   r   s       7lib/third_party/lark/parsers/lalr_interactive_parser.py__init__InteractiveParser.__init__   s    ((    returnc                 P    [         R                  " S[        5        U R                  $ )NzMlexer_state will be removed in subsequent releases. Use lexer_thread instead.)warningswarnDeprecationWarningr   r   s    r   lexer_stateInteractiveParser.lexer_state   s    egyz   r   tokenc                 R    U R                   R                  XR                  S:H  5      $ )zFeed the parser with a token, and advance it to the next state, as if it received it from the lexer.

Note that ``token`` has to be an instance of ``Token``.
$END)r   
feed_tokentyper   r    s     r   r#   InteractiveParser.feed_token   s$    
   ++E::3GHHr   c              #      #    U R                   R                  U R                  5       H  nUv   U R                  U5      U l        M     g7f)a  Step through the different stages of the parse, by reading tokens from the lexer
and feeding them to the parser, one per iteration.

Returns an iterator of the tokens it encounters.

When the parse is over, the resulting tree can be found in ``InteractiveParser.result``.
N)r   lexr   r#   r   r%   s     r   
iter_parseInteractiveParser.iter_parse"   s=      &&**4+<+<=EK//%0DK >s   AA
c                 4    [        U R                  5       5      $ )zTry to feed the rest of the lexer state into the interactive parser.

Note that this modifies the instance in place and does not feed an '$END' Token
)listr)   r   s    r   exhaust_lexerInteractiveParser.exhaust_lexer.   s    
 DOO%&&r   Nc                     Ub  [         R                  " SSU5      OU R                  R                  SSSSS5      nU R	                  U5      $ )z8Feed a '$END' Token. Borrows from 'last_token' if given.r"    r      )r   new_borrow_posr   _Tokenr#   )r   
last_tokeneofs      r   feed_eofInteractiveParser.feed_eof6   s[    >H>Te""62z:Z^ZkZkZrZrsy{}  @A  CD  FG  [Hs##r   c                     [        U 5      " U R                  [        U R                  5      [        U R                  5      5      $ )z}Create a new interactive parser with a separate state.

Calls to feed_token() won't affect the old instance, and vice-versa.
)r$   r   r   r   r   r   s    r   __copy__InteractiveParser.__copy__<   s8    
 DzKK""#""#
 	
r   c                     [        U 5      $ r   r   r   s    r   r   InteractiveParser.copyG   s    Dzr   c                     [        U[        5      (       d  gU R                  UR                  :H  =(       a    U R                  UR                  :H  $ )NF)
isinstancer   r   r   )r   others     r   __eq__InteractiveParser.__eq__J   sA    %!233  E$6$66b4;L;LPUPbPb;bbr   c                 n    [        U 5      n[        UR                  UR                  UR                  5      $ )z-Convert to an ``ImmutableInteractiveParser``.)r   ImmutableInteractiveParserr   r   r   r   ps     r   as_immutableInteractiveParser.as_immutableP   s&    J)!((ANNANNSSr   c                    S/nU R                  5       R                  5        H  u  p#UR                  SU< SU< 35        M      UR                  S[        U R                  R
                  5      -  5        SR                  U5      $ )zAPrint the output of ``choices()`` in a way that's easier to read.zParser choices:z	- z -> zstack size: %s
)choicesitemsappendlenr   state_stackjoin)r   outkvs       r   prettyInteractiveParser.prettyU   sg     !LLN((*DAJJA./ +

#c$*;*;*G*G&HHIyy~r   c                     U R                   R                  R                  R                  U R                   R                     $ )zReturns a dictionary of token types, matched to their action in the parser.

Only returns token types that are accepted by the current state.

Updated by ``feed_token()``.
)r   
parse_confparse_tablestatespositionr   s    r   rJ   InteractiveParser.choices]   s3       ++77>>t?P?P?Y?YZZr   c                 (   [        5       nU R                  5        Hb  nUR                  5       (       d  M  [        U 5      n UR	                  U R
                  R                  US5      5        UR                  U5        Md     U$ ! [         a     Mu  f = f)zWReturns the set of possible tokens that will advance the parser into a new valid state.r0   )	setrJ   isupperr   r#   r   r3   addr   )r   acceptst
new_cursors       r   r_   InteractiveParser.acceptsf   s{    %Ayy{{!$Z
#))$*;*;*B*B1b*IJ KKN   	 ' s   +B
BBc                     U R                   R                  U R                  U R                  R                  R
                  S9$ )z9Resume automated parsing from the current state.
        )r4   )r   parse_from_stater   r   stater4   r   s    r   resume_parseInteractiveParser.resume_parset   s6     {{++D,=,=$J[J[JaJaJlJl+mmr   )r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r	   r   propertyr   r   r#   r   r)   r   r-   r6   r9   r   r@   rF   rS   rJ   r_   rf   __static_attributes__ r   r   r   r      s    ;  ![ ! !I I
1HUO 
1'tE{ '$	
cT
[nr   r   c                   4    \ rS rSrSrSrS rS rS rS r	Sr
g)	rC   {   zeSame as ``InteractiveParser``, but operations create a new instance instead
of changing it in-place.
Nc                 D    [        U R                  U R                  45      $ r   )hashr   r   r   s    r   __hash__#ImmutableInteractiveParser.__hash__   s    T&&(9(9:;;r   c                 P    [        U 5      n[        R                  X!5      Ul        U$ r   )r   r   r#   r   )r   r    cs      r   r#   %ImmutableInteractiveParser.feed_token   s"    J$//9r   c                 b    U R                  5       nUR                  5         UR                  5       $ )zTry to feed the rest of the lexer state into the parser.

Note that this returns a new ImmutableInteractiveParser and does not feed an '$END' Token)
as_mutabler-   rF   )r   cursors     r   r-   (ImmutableInteractiveParser.exhaust_lexer   s+     """$$r   c                 n    [        U 5      n[        UR                  UR                  UR                  5      $ )z$Convert to an ``InteractiveParser``.)r   r   r   r   r   rD   s     r   rz   %ImmutableInteractiveParser.as_mutable   s&    J 1>>1>>JJr   ro   )rh   ri   rj   rk   rl   r   rt   r#   r-   rz   rn   ro   r   r   rC   rC   {   s#     F<
%Kr   rC   )typingr   r   r   r   lark.exceptionsr   
lark.lexerr   r	   r   rC   ro   r   r   <module>r      s4    "   + )ln ln`K!2 Kr   