
    i&                        S r SSKJr  SSKrSSKJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJrJr  S/rS\\   S\\   S\\   S\ \   4S jr! " S S\RD                  5      r#S r$\\" SS9SS\RJ                  " SS9S.S\R                  R                  S\S-  S\&\\RD                  \   4   S-  S\RD                  \   S\RD                  \   4
S  jj5       5       r'g)!a  
This extra can be used to generate strings matching any context-free grammar,
using the `Lark parser library <https://github.com/lark-parser/lark>`_.

It currently only supports Lark's native EBNF syntax, but we plan to extend
this to support other common syntaxes such as ANTLR and :rfc:`5234` ABNF.
Lark already `supports loading grammars
<https://lark-parser.readthedocs.io/en/stable/tools.html#importing-grammars-from-nearley-js>`_
from `nearley.js <https://nearley.js.org/>`_, so you may not have to write
your own at all.
    )	signatureN)NonTerminalRuleSymbolTerminal)Lark)TerminalDef)
strategies)InvalidArgument)ConjectureData)calc_label_from_name)
check_type)IncompatibleWithAlphabet)	cacheabledefines_strategy	from_lark	terminalsrulesignore_namesreturnc           	          U  Vs1 s H  o3R                   iM     sn[        U5      -  nU HC  nXER                   Vs1 s H&  n[        U[        5      (       d  M  UR                   iM(     sn-  nME     U$ s  snf s  snf )a/  Get names of all terminals in the grammar.

The arguments are the results of calling ``Lark.grammar.compile()``,
so you would think that the ``terminals`` and ``ignore_names`` would
have it all... but they omit terminals created with ``@declare``,
which appear only in the expansion(s) of nonterminals.
)nameset	expansion
isinstancer   )r   r   r   tnamesrules         T/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/hypothesis/extra/lark.pyget_terminal_namesr    )   se     ''YVVY'#l*;;E..L.QJq(4K&!&&.LL L (Ls   A4A9A9c            
          ^  \ rS rSrSrS\S\S-  S\\\R                  \   4   S\R                  \   SS4
U 4S	 jjr
S
\S\4S jrS\S\4S jrS
\S\S\\   SS4S jrS
\S\\   SS4S jrS rSrU =r$ )LarkStrategy9   z[Low-level strategy implementation wrapping a Lark grammar.

See ``from_lark`` for details.
grammarstartNexplicitalphabetr   c                 
  >^ ^ [         TT ]  5         [        U[        R                  R                  5      (       d   eUc  UR
                  R                  OU/n[        UR                  R                  5      R                  nSU;   a   UR                  R                  US5      u  pgnOBSU;   a  UR                  R                  U5      u  pgnOUR                  R                  5       u  pgn0 T l        U H0  n	U	R                  T R                  U	R                  R                  '   M2     [        5       n
0 T l        U H  n[!        UR                  5      T R                  UR                  '   ["        R$                  " UR&                  R)                  5       SUS9n UR+                  5         UT R                  UR                  '   M     [1        U 4S jU 5       5      T l        [5        XgU5      n[7        [        U5      U-
  5      =n(       a  [9        SU S35      e[7        X Vs1 s H  oR                  iM     sn-
  [        U5      -
  5      =n(       a2  [9        S	S
[;        U5      S:  -   S[7        U5      < SUS   < S35      eT R                  R=                  U5        0 nU Ht  nU
R?                  S UR@                   5       5      (       d  M,  URC                  UR                  R                  / 5      RE                  [1        UR@                  5      5        Mv     1 T R                  kUkm[G        U5      URI                  5        VVVs0 s HA  u  nnU Vs/ s H!  n[K        U4S jU 5       5      (       d  M  UPM#     sn=n(       d  M>  UU_MC     snnn=n:w  a  1 T R                  kUkm[G        U5      URI                  5        VVVs0 s HA  u  nnU Vs/ s H!  n[K        U4S jU 5       5      (       d  M  UPM#     sn=n(       d  M>  UU_MC     snnn=n:w  a  M  [        U5      R?                  T5      (       a  [9        S[1        U5       SU< 35      e["        RL                  " U Vs/ s H  oT;   d  M
  T R                  U   PM     sn5      T l        URI                  5        VVs0 s H)  u  nnU["        RL                  " [7        U[:        S95      _M+     snnT l'        0 T l(        g ! [,         a     U
R/                  UR                  5         GM  f = fs  snf s  snf s  snnnf s  snf s  snnnf s  snf s  snnf )Nterminals_to_keep r%   T)	fullmatchr'   c              3   B   >#    U  H  nTR                   U   v   M     g 7fN)names_to_symbols).0nselfs     r   	<genexpr>(LarkStrategy.__init__.<locals>.<genexpr>h   s     $T|!T%:%:1%=|s   zLThe following arguments were passed as explicit_strategies, but there is no z% terminal production in this grammar.zUndefined terminals    z}. Generation does not currently support use of %declare unless you pass `explicit`, a dict of names-to-strategies, such as `{r   z: st.just("")}}`c              3   8   #    U  H  oR                   v   M     g 7fr-   r   )r/   rs     r   r2   r3      s     $D^VV^s   c              3   @   >#    U  H  oR                   T;   v   M     g 7fr-   r8   )r/   r9   allowed_ruless     r   r2   r3      s     1USTa&&M2ISTs   zNo start rule z is allowed by alphabet=)key))super__init__r   larkr   optionsr%   r   r$   compile
parametersr.   originr   r   terminal_strategiesr   st
from_regexpattern	to_regexpvalidater   addtupleignored_symbolsr    sortedr   lenupdate
isdisjointr   
setdefaultappenddictitemsallsampled_fromnonterminal_strategies_LarkStrategy__rule_labels)r1   r$   r%   r&   r'   compile_argsr   r   r   r9   
disallowedr   r4   all_terminalsunknown_explicitmissing_declarednonterminalsr   kvxcleanr;   	__class__s   `                     @r   r>   LarkStrategy.__init__?   s    	'499>>222249M7??00w
 !!8!89DD,.-4__-D-DUB-O*Il$-4__-D-DU-K*Il .5__-D-D-F*Il35A3488D!!!((--0  U
FH A,4QVV,<D!!!&&)aii113thWA5

 45((0   %$T|$TT*9\J%c(mm&CDDD!/00UW   &Y7YVVY77#h-G 
 
 
 "$SC0@,AA,E%F$Gq*+. /33CA3F2I J!!  	  ''1 =?D$$$DT^^$DDD''(8(8"=DD$..)  C$22B\B<  )..00DAq)*VAc1UST1U.UaVVEV 50 L
 Gd66FFM <  )..00DAq)*VAc1UST1U.UaVVEV 50 L
 u:  //! u.Gh[I  __/4Ku!]8J%T""1%uK


 @L?Q?Q?S'
?Stq!ArvaS122?S'
# .0q , 'qvv&&' 82 W W L'
sr   S+!T
	T"
T8T>T"

T"
	T.
T)8T)>T.

T.
+	T58T5*0T:+%TTT"
)T.
datac                     / nUR                  U R                  5      nU R                  XU5        SR                  U5      $ )N )drawr%   draw_symboljoin)r1   re   stater%   s       r   do_drawLarkStrategy.do_draw   s7    		$**%e,wwu~    r   c                      U R                   U   $ ! [         a+    U R                   R                  U[        SU 35      5      s $ f = f)NzLARK:)rX   KeyErrorrQ   r   )r1   r   s     r   
rule_labelLarkStrategy.rule_label   sQ    	%%d++ 	%%00*U4&>: 	s    2AAsymbol
draw_statec                    [        U[        5      (       a:  U R                  UR                     nUR	                  UR                  U5      5        g [        U[        5      (       d   eUR                  U R                  UR                  5      5        UR                  U R                  UR                     5      nU H&  nU R                  XU5        U R                  X5        M(     UR                  5         g r-   )r   r   rD   r   rR   rh   r   
start_spanrq   rW   ri   
gen_ignore	stop_span)r1   re   rs   rt   strategyr   es          r   ri   LarkStrategy.draw_symbol   s     fh''//<Hdii12fk2222OODOOFKK89		$"="=fkk"JKI  *51  NNrn   c                     U R                   (       aY  UR                  S5      (       aB  UR                  [        R                  " U R                   5      5      nU R                  XU5        g g g )Ng      ?)rL   draw_booleanrh   rE   rV   ri   )r1   re   rt   emits       r   rw   LarkStrategy.gen_ignore   sP    D$5$5e$<$<99R__T-A-ABCDT4 %=rn   c                     g)NTr*   )r1   recurs     r   calc_has_reusable_values%LarkStrategy.calc_has_reusable_values   s    rn   )__rule_labelsrL   r.   rW   r%   rD   )__name__
__module____qualname____firstlineno____doc__r   strrS   rE   SearchStrategyr>   r   rl   intrq   r   listri   rw   r   __static_attributes____classcell__)rc   s   @r   r"   r"   9   s    
\0\0 Tz\0 sB--c223	\0
 ##C(\0 
\0|N s s s   I	
 
$5~ 549 5 5
 rn   r"   c                    ^  U 4S jnU$ )Nc                 0   > [        [        U ST-   5        U $ )Nzvalue drawn from )r   r   )valuer   s    r   innercheck_explicit.<locals>.inner   s    32T9:rn   r*   )r   r   s   ` r   check_explicitr      s     Lrn   T)force_reusable_valueszutf-8)codec)r%   r&   r'   r$   r%   r&   r'   c                &   [        [        R                  R                  U S5        Uc  0 nOU[        [        US5        UR	                  5        VVs0 s H'  u  pEXER                  [        SU< SU< 35      5      _M)     nnn[        XX#5      $ s  snnf )a  A strategy for strings accepted by the given context-free grammar.

``grammar`` must be a ``Lark`` object, which wraps an EBNF specification.
The Lark EBNF grammar reference can be found
`here <https://lark-parser.readthedocs.io/en/latest/grammar.html>`_.

``from_lark`` will automatically generate strings matching the
nonterminal ``start`` symbol in the grammar, which was supplied as an
argument to the Lark class.  To generate strings matching a different
symbol, including terminals, you can override this by passing the
``start`` argument to ``from_lark``.  Note that Lark may remove unreachable
productions when the grammar is compiled, so you should probably pass the
same value for ``start`` to both.

Currently ``from_lark`` does not support grammars that need custom lexing.
Any lexers will be ignored, and any undefined terminals from the use of
``%declare`` will result in generation errors.  To define strategies for
such terminals, pass a dictionary mapping their name to a corresponding
strategy as the ``explicit`` argument.

The :pypi:`hypothesmith` project includes a strategy for Python source,
based on a grammar and careful post-processing.
r$   r&   z	explicit[z]=)r   r?   r   rS   rT   mapr   r"   )r$   r%   r&   r'   r_   r`   s         r   r   r      s    @ tyy~~w	24:. !(
( uu^iuBqe$<=>>( 	 
 ;;	
s   .B)(r   inspectr   r?   lark.grammarr   r   r   r   	lark.larkr   
lark.lexerr	   
hypothesisr
   rE   hypothesis.errorsr   #hypothesis.internal.conjecture.datar   $hypothesis.internal.conjecture.utilsr   hypothesis.internal.validationr   %hypothesis.strategies._internal.regexr   %hypothesis.strategies._internal.utilsr   r   __all__r   r   r   r    r   r"   r   
charactersrS   r   r*   rn   r   <module>r      s+  
   < <  " ' - > E 5 J M-K )-dCG9X J2$$ JZ - 9=')}}7'C'<YY^^'< :'< 3))#../$6	'<
 $'< s'< . '<rn   