
    Xh                         d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ d	d
lmZ d	dlmZ erddlmZmZ  G d de      Zy)a  This module implements an Earley parser with a dynamic lexer

The core Earley algorithm used here is based on Elizabeth Scott's implementation, here:
    https://www.sciencedirect.com/science/article/pii/S1571066108001497

That is probably the best reference for understanding the algorithm here.

The Earley parser outputs an SPPF-tree as per that document. The SPPF tree format
is better documented here:
    http://www.bramvandersanden.com/post/2014/06/shared-packed-parse-forest/

Instead of running a lexer beforehand, or using a costy char-by-char method, this parser
uses regular expressions by necessity, achieving high-performance while maintaining all of
Earley's power in parsing any CFG.
    )TYPE_CHECKINGCallableOptionalListAny)defaultdict   )Tree)UnexpectedCharacters)Token)Terminal   )Parser)	TokenNode)	LexerConf
ParserConfc                   \    e Zd Zdddedfdddddeded	ed
edeeeege	f      defdZ
ddZy)r   TF
lexer_confr   parser_confr   term_matcherresolve_ambiguitycomplete_lexdebug
tree_classordered_setsc	           
          t        j                  | |||||||       |j                  D 	cg c]  }	t        |	       c}	| _        || _        y c c}	w N)
BaseParser__init__ignorer   r   )
selfr   r   r   r   r   r   r   r   ts
             H/var/www/html/myenv/lib/python3.12/site-packages/lark/parsers/xearley.pyr   zParser.__init__   sM     	D*k<IZ!:|	=,6,=,=>qx{>( ?s   ANc           
      n   	
 	 
f
d}t        t               j                  	 j                  j                  
i gddd}D ]6  } j                  ||        |||      }|dk(  rdz  dndz  |dz  }8  j                  ||       |t              dz
  k(  sJ |S )Nc                   
 i }j                  |      D ]  } |j                  |       }|st        |j                  j                  |j	                  d      |       }|j                            j                  || |f       j                  s|j	                  d      }t        dt        |            D ]v  } |j                  |d|        }|st        |j                  j                  |j	                  d      |       }| |j                         z      j                  || |f       x   j                  D ]  } ||       }|s|j                            j                  |D cg c]  }|| df	 c}       |j                            j                  |    D cg c]$  }|j                  s|j                  k(  s || df& c}        j                         }	j                         }
j                  |
       j                  i        | dz      D ]  \  }}}|Չ|_        dz   |_        | dz   |_        |j#                         }|j                  |j$                  | dz   f}t'        ||j(                           }||v r||   n|j+                  | j,                  |       |_        |j.                  j1                  |j                  |j2                  |j$                  |j.                  |       n|}|j                  j4                  v r|	j7                  |       |
j7                  |        | dz   = |
sisg|	set9        t;        |d             }t=        | |D ch c]  }|j                  j                   c}t?        |      tA        d |D              |      |	S c c}w c c}w c c}w )a|  The core Earley Scanner.

            This is a custom implementation of the scanner that uses the
            Lark lexer to match tokens. The scan list is built by the
            Earley predictor, based on the previously completed tokens.
            This ensures that at each phase of the parse we have a custom
            lexer context, allowing for more complex ambiguities.r   r   Nc                 B    | j                   j                  j                  S r   )ruleoriginnamekeys    r#   <lambda>z-Parser._parse.<locals>.scan.<locals>.<lambda>|   s    H\H\     r*   c              3   4   K   | ]  }|j                     y wr   )s).0is     r#   	<genexpr>z.Parser._parse.<locals>.scan.<locals>.<genexpr>~   s     H^QRH^s   )stateconsidered_rules)!Setexpectr   r)   groupendappendr   rangelenr    extendis_completer/   end_line
end_columnend_posadvancestartr   type
setdefault
SymbolNodenode
add_familyr'   	TERMINALSaddlistsortedr   set	frozenset)r1   to_scan
node_cacheitemmr"   r/   jxnext_to_scannext_setrB   tokennew_itemlabel
token_noder4   columnsdelayed_matchesmatchr!   start_symbolstream	terminalstext_column	text_linetransitivess                    r#   scanzParser._parse.<locals>.scan)   s    J ) R$++vq1dkk..
Ay+VA#AEEG,33dAq\C((GGAJ!&q#a&!1 RA %dkk1Sqb6 :A $)$++*:*:AGGAJ9Va$b /!%%'	 : A AD!Q< Q	RR. [[ M!VQ'#AEEG,33QX4ZdAt_4Z[ $AEEG,33QXYZQ[  5L_c_o_otxtztz  K  uKdAt_  5L  MM  88:LxxzHNN8$r" '6ac&: +"eU$%.EN'2QE$$%EEM#||~H%ZZQ?E!*5)EJJ2G!HJ9>*9LJu$5R\RgRghmo~oso~o~  AF  pG  SHHMMM,,XZZHNNTXT]T]_ij#H??dnn4 $$X. LL*'+*  !$OL#'w<\(]#^ *61ilsNtdht{{O_O_Nt+.w<yH^V]H^?^<L- -
  [ 5[ 5LJ Ous   2N7
(N<
:N<

N<
4Or   r   
)r   rJ   r   r   terminals_by_namepredict_and_completer;   )r!   r^   rZ   rN   r]   rc   r1   rV   r[   r\   r_   r`   ra   rb   s   ``` `   @@@@@@r#   _parsezParser._parse'   s    Y	  Y	 x &d+!!OO55	 d	  
	E%%a';G1g&G}Q	q FA
	 	!!!Wg{C CLN"""r-   r   )__name__
__module____qualname__r
   r   boolr   strr   r   r   rg    r-   r#   r   r      sl    )-%UZBF[_); )\ )Ya )$()=A)PT)%hT{C/?&@A)VZ)~r-   r   N)__doc__typingr   r   r   r   r   collectionsr   treer
   
exceptionsr   lexerr   grammarr   earleyr   r   earley_forestr   commonr   r   rm   r-   r#   <module>rx      s<     @ ? #  -   ( $.GZ Gr-   