
    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mZ ddl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mZmZmZmZ erddl m!Z!m"Z"  G d d      Z#y)a  This module implements an Earley parser.

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 explained here: https://lark-parser.readthedocs.io/en/latest/_static/sppf/sppf.html
    )TYPE_CHECKINGCallableOptionalListAny)deque   )Token)Tree)UnexpectedEOFUnexpectedToken)logger
OrderedSet
dedup_list   )GrammarAnalyzer)NonTerminal)Item)ForestSumVisitor
SymbolNodeStableSymbolNode	TokenNodeForestToParseTree)	LexerConf
ParserConfc                       e Zd ZU ded<   ded<   eed<   ddedfddddded	eded
eeee	ge
f      defdZd ZddZd Zy)Parserr   
lexer_confr   parser_confdebugTFterm_matcherresolve_ambiguity
tree_classordered_setsc                 ^   t        |      }|| _        || _        || _        || _        || _        |rt        nt        | _        |rt        nt        | _
        |j                  | _        |j                  | _        |j                  | _        i | _        |j                  D 	
ch c]"  }	|	j                   D ]  }
|
j"                  s|
 $ c}
}	| _        |j                  D 	
ch c]"  }	|	j                   D ]  }
|
j"                  r|
 $ c}
}	| _        d | _        |j                  D ]  }|j*                  | j                  vrJ|j-                  |j*                        D cg c]  }|j.                   c}| j                  |j*                  <   | j(                  r|j0                  j2                  t4        | _         | j                  j6                  dk7  rG| j(                  ;| j                  j8                  D ]"  }|j2                  st4        | _         || _        y  || _        y c c}
}	w c c}
}	w c c}w )Nbasic)r   r   r   r"   r    r   r   setSetr   r   FIRSTNULLABLE	callbackspredictionsrules	expansionis_term	TERMINALSNON_TERMINALSforest_sum_visitororiginexpand_ruleruleoptionspriorityr   
lexer_type	terminalsr!   )selfr   r   r!   r"   r    r#   r$   analysisrsymr5   xterms                 G/var/www/html/myenv/lib/python3.12/site-packages/lark/parsers/earley.py__init__zParser.__init__    s    #;/$&!2
	!-:3.:*
^^
 ))$.. )4(9(9a1Q[[acTWT_T_3a3a,7,=,=iqi#\_\g\gsisi"&%% 		;D{{$"2"22AIAUAUVZVaVaAb0cA0c  - &&.4<<3H3H3T*:'		; ??%%0T5L5L5T11 ==.>D+(
 )/ bi
 1ds    H?H H$>H$H*c                 8
   i }i }||   }t        |      }|r|j                         }	|	j                  rd|	j                  |	j                  |	j
                  |f}
|
|v r||
   n|j                  |
 | j                  |
       |	_        |	j                  j                  |	j                  |	j                  |	j
                  dd       |	j                  j                  ||	j
                     v rA||	j
                     |	j                     }|j                  ||j                     v r||j                     |j                     }n|}t        |j                  |j                  |j
                        }|j                  |j
                  |f}
|
|v r||
   n|j                  |
 | j                  |
       |_        |j                  j                  ||	j                         |j                   | j"                  v r|j%                  |       n||vr
|j%                  |       |j'                  |       n|	j
                  |k(  }|r#|	j                  ||	j                  j                  <   ||	j
                     D cg c]+  }|j                   |j                   |	j                  k(  s*|- }}|D ]  }|j)                         }|j                  |j
                  |f}
|
|v r||
   n|j                  |
 | j                  |
       |_        |j                  j                  |j                  |j                  ||j                  |	j                         |j                   | j"                  v r|j%                  |       ||vs|j%                  |       |j'                  |        nx|	j                   | j*                  v r_g }| j,                  |	j                      D ]   }t        |d|      }|j'                  |       " |	j                   |v r|	j)                         }|j                  |	j
                  |f}
|
|v r||
   n|j                  |
 | j                  |
       |_        |j                  j                  |j                  |j                  |j
                  |	j                  ||	j                             |j'                  |       |D ]S  }|j                   | j"                  v r|j%                  |       -||vs2|j%                  |       |j'                  |       U |ryyc c}w )a  The core Earley Predictor and Completer.

        At each stage of the input, we handling any completed items (things
        that matched on the last cycle) and use those to predict what should
        come next in the input stream. The completions and any predicted
        non-terminals are recursively processed until we reach a set of,
        which can be added to the scan list for the next scanner cycle.Nr   )r   popis_completenodesstart
setdefaultr   
add_familyr5   r3   previouscolumnr   ptradd_pathexpectr0   addappendadvancer1   r,   )r:   ito_scancolumnstransitives
node_cacheheld_completionsrK   itemsitemlabel
transitiveroot_transitivenew_itemis_empty_item
originatororiginators	new_itemsr5   s                      r@   predict_and_completezParser.predict_and_completeN   s    
f99;D 99$!VVTZZ3E5:j5H
5 1jNcNcdikzkokzkz  }B  lC  ODDIII((DJJdS
 99##{4::'>>!,TZZ!8!@J!**k*:K:K.LL*5j6G6G*HI\I\*]*4#JOOZ^^ZEUEUVH,..0E0EqIE9>*9LJu$5R\RgRghmo~oso~o~  AF  pG  SHHMMM**?DIIF$..8H-!/

8,X. %)JJ!OM$=AYY()9)9:@G

@S  #V*WaWhWhWt  zD  zK  zK  OS  OU  OU  zU:  #VK  #V&1 3
#-#5#5#7!)Z-=-=q A=Bj=P
5(9V`VkVklq  tCsw  tC  tC  EJ  tK  WL 00X]]Az`d`i`ij#??dnn<#KK1%V3"JJx0!LL23  2 22	 ,,T[[9 /D#D!Q/H$$X./
 ;;"22#||~H%ZZQ7E9>*9LJu$5R\RgRghmo~oso~o~  AF  pG  SHHMMM,,XZZX\XaXacstxtt  dA  B$$X. ) /H$..8H-!/

8,X./O T #Vs   0TTTNc                    
  fd}
 fd} j                   
 j                  j                  i g|D ch c]  }|j                   }}d}|j	                  |      D ]R  }	 j                  |        |||	|      }|dz  }|j                          ||D ch c]  }|j                   c}z  }T  j                  |       |t              dz
  k(  sJ |S c c}w c c}w )Nc                    | j                   ry| j                         }|j                   s_|j                  j                  vry|j                  j
                  k(  r|j                  k(  ry|j                         }|j                   s_y)NTF)rD   rQ   rN   r*   r5   r3   )rY   quasir:   start_symbols     r@   is_quasi_completez(Parser._parse.<locals>.is_quasi_complete   sq    LLNE''<<t}}4 ::$$49U  ''     c           	         j                         }j                         }j                  |       j                  i        i }j                  |      D ]+  } |j                  |      s|j                         }|j                  |j
                  | f}t        |t              rj                  |j                        nd}	t        ||	d      }
||v r||   n|j                  | j                  |       |_        |j                  j                  |j                  |j                  |j
                  |j                  |
       |j                  j                   v r|j#                  |       |j#                  |       . |sM|sK|D  ch c]  } | j                  j$                   }} t'        ||t)        |      t+        d |D                    |S 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.Nr   )r7   c              3   4   K   | ]  }|j                     y wNrF   .0rR   s     r@   	<genexpr>z.Parser._parse.<locals>.scan.<locals>.<genexpr>   s     cylmdedgdgcy   )considered_rulesstate)r(   rP   rN   rQ   rF   rG   
isinstancer
   gettyper   rH   r   rE   rI   r5   r0   rO   namer   r'   	frozenset)rR   tokenrS   next_to_scannext_setrV   rY   r]   rZ   r?   
token_noderN   rT   matchr:   r9   rU   s               r@   scanzParser._parse.<locals>.scan   s     88:LxxzHNN8$r"J) /e,#||~H%ZZ;E 9C5%8P9==4VZD
 "+5$!CJ9>*9LJu$5R\RgRghmo~oso~o~  AF  pG  SHHMMM,,XZZHNNTXT]T]_ij$..8$((2 !X.+/. L189A!((--99%eVc'lZccyqxcyZyzz :s   Gr   r   )r!   r   terminals_by_namerN   lexrb   clearlen)r:   lexerrT   rS   rf   rg   r}   rR   expectsrx   r|   r9   rU   s   ` ` `     @@@r@   _parsezParser._parse   s    	 )	  )	 Z !!OO55	 d &--188--YYw' 	3E%%a';G1eW-GFAMMO'2Q22G	3 	!!!Wg{C CLN""" . 3s   C%"C*c                 |   |sJ |       t        |      | j                         g}| j                         }| j                     D ]M  }t        |dd      }|j                  | j
                  v r|j                  |       :|d   j                  |       O | j                  |||      }t        fd|d   D              }|s@|D cg c]  }|j                  j                   }	}t        |	t        d |D                    | j                  r8ddlm}
 	  |
       }t        |      D ]  \  }}|j!                  |d| d	        	 | j(                  | j*                   }t-        | j(                  | j.                  | j0                  xr | j1                         | j*                  |      }|D cg c]  }|j3                  |       }}t5        |      dkD  r1| j*                  s%| j)                  d|      }|j7                  d       |S |d   S |d   S c c}w # t"        $ r t%        j&                  d
       Y w xY wc c}w )Nr   c              3      K   | ]J  }|j                   s|j                  |j                  k(  s-|j                  dk(  s=|j                   L y w)Nr   )rD   rE   rF   rG   )rn   nrf   s     r@   ro   zParser.parse.<locals>.<genexpr>  s_       M!ammPQPVPVPbghgjgjnzgz  @A  @G  @G  KL  @Lqvv  Ms   AAAAAc              3   4   K   | ]  }|j                     y wrk   rl   rm   s     r@   ro   zParser.parse.<locals>.<genexpr>   s     CYAACCCYrp   )rr   r   )ForestToPyDotVisitorsppfz.pngzBCannot find dependency 'pydot', will not generate sppf debug image_ambig)r   r(   r,   r   rN   r0   rO   r   r   rv   r   rw   r    earley_forestr   	enumeratevisitImportErrorr   warningr   r"   r   r+   r2   	transformr   expand_kids_by_data)r:   r   rG   rT   rS   r5   rY   	solutionstexpected_terminalsr   debug_walkerrR   rF   	use_cachetransformerrf   s                   @r@   parsezParser.parse  s;   eu"5)88:,((*
 $$\2 	%Da#D{{dnn,D!
t$	% ++eWg|D
   Mwr{  M  M	9@!AA!((--!A!A 2)CYQXCY:YZZ::;:35 &i0 :DAq &&qD4.9: 99  !222I+DIIt~~tG^G^G|cgczczc|  C  U  U  W`  aK;DEa..q1EIE9~!$*@*@))Hi8%%h/Q< |= "B  ecde Fs   HH -H9H65H6rk   )__name__
__module____qualname____annotations__boolr   r   r   strr   r   rA   rb   r   r    rh   r@   r   r      s    K *.5BF[_+); +)\ +)Ya +)$(+)6:+)%hT{C/?&@A+)VZ+)\[/zZx6rh   r   N)$__doc__typingr   r   r   r   r   collectionsr   r   r
   treer   
exceptionsr   r   utilsr   r   r   grammar_analysisr   grammarr   earley_commonr   r   r   r   r   r   r   commonr   r   r   r   rh   r@   <module>r      sF   	 @ ?    7 2 2 - !  g g.b brh   