
    Xh}                     z    d dl mZmZ d dlmZ d dlZd dlmZ d dlmZm	Z	 ddl
mZ  G d d	      Z G d
 de      Zy)    )IteratorListcopyN)UnexpectedToken)TokenLexerThread   )ParserStatec                       e Zd ZdZdedefdZedefd       Zde	fdZ
dee	   fd	Zdee	   fd
ZddZd ZddZd Zd Zd Zd Zd Zd Zy)InteractiveParserzInteractiveParser 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()``.
    parser_statelexer_threadc                 <    || _         || _        || _        d | _        y N)parserr   r   result)selfr   r   r   s       X/var/www/html/myenv/lib/python3.12/site-packages/lark/parsers/lalr_interactive_parser.py__init__zInteractiveParser.__init__   s     ((    returnc                 N    t        j                  dt               | j                  S )NzMlexer_state will be removed in subsequent releases. Use lexer_thread instead.)warningswarnDeprecationWarningr   r   s    r   lexer_statezInteractiveParser.lexer_state   s    egyz   r   tokenc                 T    | j                   j                  ||j                  dk(        S )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"   zInteractiveParser.feed_token   s&    
   ++E5::3GHHr   c              #      K   | j                   j                  | j                        D ]  }| | j                  |      | _         yw)a0  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_parsezInteractiveParser.iter_parse$   sC      &&**4+<+<= 	1EK//%0DK	1s   AAc                 4    t        | j                               S )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_lexerzInteractiveParser.exhaust_lexer0   s    
 DOO%&&r   Nc                     |t        j                  dd|      n| j                  j                  ddddd      }| j	                  |      S )z8Feed a '$END' Token. Borrows from 'last_token' if given.r!    r   r
   )r   new_borrow_posr   _Tokenr"   )r   
last_tokeneofs      r   feed_eofzInteractiveParser.feed_eof8   s[    >H>Te""62z:Z^ZkZkZrZrsy{}  @A  CD  FG  [Hs##r   c                 "    | j                         S )zCreate a new interactive parser with a separate state.

        Calls to feed_token() won't affect the old instance, and vice-versa.
        r   r   s    r   __copy__zInteractiveParser.__copy__>   s    
 yy{r   c                      t        |       | j                  | j                  j                  |      t        | j                              S )Ndeepcopy_values)r#   r   r   r   r   )r   r6   s     r   r   zInteractiveParser.copyE   sA    tDzKK""?"C""#
 	
r   c                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )NF)
isinstancer   r   r   )r   others     r   __eq__zInteractiveParser.__eq__L   s>    %!23  E$6$66b4;L;LPUPbPb;bbr   c                 n    t        |       }t        |j                  |j                  |j                        S )z-Convert to an ``ImmutableInteractiveParser``.)r   ImmutableInteractiveParserr   r   r   r   ps     r   as_immutablezInteractiveParser.as_immutableR   s&    J)!((ANNANNSSr   c                    dg}| j                         j                         D ]  \  }}|j                  d|d|        |j                  dt        | j                  j
                        z         dj                  |      S )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   prettyzInteractiveParser.prettyW   sn     !LLN((* 	0DAqJJA./	0

#c$*;*;*G*G&HHIyy~r   c                     | j                   j                  j                  j                  | j                   j                     S )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   rB   zInteractiveParser.choices_   s3       ++77>>t?P?P?Y?YZZr   c                    t               }t        | j                  j                        }i |_        | j                         D ]s  }|j                         s| j                  d      }||j                  _        	 |j                  | j                  j                  |d             |j                  |       u |S # t        $ r Y w xY w)zWReturns the set of possible tokens that will advance the parser into a new valid state.Fr5   r,   )setr   r   rM   	callbacksrB   isupperr"   r   r.   addr   )r   acceptsconf_no_callbackst
new_cursors        r   rV   zInteractiveParser.acceptsh   s    % !2!2!=!=> ')# 		#Ayy{!YYuY=
5F
''2#))$*;*;*B*B1b*IJ KKN		# 	 ' s   9+B99	CCc                     | j                   j                  | j                  | j                  j                  j
                        S )z9Resume automated parsing from the current state.
        )r/   )r   parse_from_stater   r   stater/   r   s    r   resume_parsezInteractiveParser.resume_parse{   s6     {{++D,=,=$J[J[JaJaJlJl+mmr   r   )T)__name__
__module____qualname____doc__r   r	   r   propertyr   r   r"   r   r'   r   r*   r1   r3   r   r:   r?   rK   rB   rV   r]    r   r   r   r      s    [   ![ ! !I I
1HUO 
1'tE{ '$
cT
[&nr   r   c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)r<   zmSame as ``InteractiveParser``, but operations create a new instance instead
    of changing it in-place.
    Nc                 D    t        | j                  | j                  f      S r   )hashr   r   r   s    r   __hash__z#ImmutableInteractiveParser.__hash__   s    T&&(9(9:;;r   c                 R    t        |       }t        j                  ||      |_        |S r   )r   r   r"   r   )r   r   cs      r   r"   z%ImmutableInteractiveParser.feed_token   s$    J$//59r   c                 b    | j                         }|j                          |j                         S )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*   r?   )r   cursors     r   r*   z(ImmutableInteractiveParser.exhaust_lexer   s+     """$$r   c                 n    t        |       }t        |j                  |j                  |j                        S )z$Convert to an ``InteractiveParser``.)r   r   r   r   r   r=   s     r   rk   z%ImmutableInteractiveParser.as_mutable   s&    J 1>>1>>JJr   )	r^   r_   r`   ra   r   rg   r"   r*   rk   rc   r   r   r<   r<      s#     F<
%Kr   r<   )typingr   r   r   r   lark.exceptionsr   
lark.lexerr   r	   lalr_parser_stater   r   r<   rc   r   r   <module>rr      s7    "   + ) *qn qnjK!2 Kr   