
    'ie                        d dl mZ d dlmZ d dlZd dlmZmZmZm	Z	m
Z
 d dlmZmZmZmZmZmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ  G d	 d
e      Z G d de      Zd Z G d de      Z  G d de      Z!d Z"d Z#d Z$d Z%y)    )version_info)TestCaseN)
BadGrammarLeftRecursionError
ParseErrorUndefinedLabelVisitationError)Literal	LookaheadRegexSequenceTokenMatcheris_callable)rule_grammarrule_syntaxRuleVisitorGrammarTokenGrammarLazyReference)Node)Tokenc                   (    e Zd ZdZd Zd Zd Zd Zy)BootstrappingGrammarTestszVTests for the expressions in the grammar that parses the grammar
    definition syntaxc                 P   d}t         d   }| j                  |j                  |      t        ||ddt        |j                  d   |dd      t        t         d   |dd      g             d}| j                  |j                  |      t        ||ddt        |j                  d   |dd      t        t         d   |dd      g             d}| j                  |j                  |      t        ||ddt        |j                  d   |dd      t        t         d   |dd      g             y )	N*
quantifierr      _children?+)r   assertEqualparser   members)selftextr   s      P/var/www/br/venv/lib/python3.12/site-packages/parsimonious/tests/test_grammar.pytest_quantifierz)BootstrappingGrammarTests.test_quantifier   sB   !,/
))$/T1aZ''*D!Q7l3>OQUWXZ[9\3^ _	` ))$/T1aZ''*D!Q7l3>OQUWXZ[9\3^ _	` ))$/T1aZ''*D!Q7l3>OQUWXZ[9\3^ _	`    c                 f   d}t         d   }| j                  |j                  |      t        ||dt	        |      t        |j
                  d   |dt	        |            g             d}| j                  |j                  |      t        ||ddt        |j
                  d   |dd      g             y )Nz"anything but quotes#$*&^"spaceless_literalr   r   zr"\""   )r   r#   r$   r   lenr%   )r&   r'   r,   s      r(   test_spaceless_literalz0BootstrappingGrammarTests.test_spaceless_literal"   s    +()<=*006"D!SY&..q14CIFBH I	J *006"D!Q&..q14A>:@ A	Br*   c                    d}t         d   }| j                  t         d   j                  |      t        ||dt	        |      t        t        d      |dd      t        t         d   |ddt        t         d   j                  d   |dd      g      t        |j                  d	   |dd
      t        t         d   |d
d
      g             y )Nz~"[a-zA-Z_][a-zA-Z_0-9]*"LIregexr   ~r   r,      r         r   )r   r#   r$   r   r.   r
   r%   )r&   r'   r1   s      r(   
test_regexz$BootstrappingGrammarTests.test_regex-   s    ,W%g.44T:aTgclD!Q/l#67q",':;CCAFaQSTPV WemmA&b"5l3'r2668 9	:r*   c                     | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d	   j                  d
             | j                  t        d	   j                  d             | j                  t        d	   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        d   j                  d             | j                  t        j                  d             y) zAMake sure the PEG recognition grammar succeeds on various inputs.labelr   jeff_THIS_THINGatom
some_labelz"some literal"z~"some regex"i
quantifiedz~"some regex"i*zthing+z"hi"?termthiszthat+sequencezthis that? otheroredzthis / that+ / "other"lookahead_termz&this+
expressionzthis? that other*z&this / that+ / "other"zthis / that? / "other"+rulezthis = thatzthis = the? that other* 	zthe=~"hi*"
z
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            N)
assertTruer   r$   r&   s    r(   test_successesz(BootstrappingGrammarTests.test_successes8   s   W-33C89W-33F;<W-33MBCV,22<@AV,223CDEV,223CDE\2889JKL\288BC\288ABV,226:;V,227;<Z0667IJKV,223KLM 	%56<<XFG\288@A\2889LMN\2889RST\2889RST\2889LMNV,22?CDV,223QRSV,22>BC** ,  	r*   N)__name__
__module____qualname____doc__r)   r/   r6   rG    r*   r(   r   r      s    `	B	:(r*   r   c                   "    e Zd ZdZd Zd Zd Zy)RuleVisitorTestszTests for ``RuleVisitor``

    As I write these, Grammar is not yet fully implemented. Normally, there'd
    be no reason to use ``RuleVisitor`` directly.

    c           	          t        j                  d      }t               j                  |      \  }}d}| j	                  |j                  |      t        ||dd             y)a  Test a simple round trip.

        Parse a simple grammar, turn the parse tree into a map of expressions,
        and use that to parse another piece of text.

        Not everything was implemented yet, but it was a big milestone and a
        proof of concept.

        znumber = ~"[0-9]+"
98r   r4   N)r   r$   r   visitr#   r   )r&   treerulesdefault_ruler'   s        r(   test_round_tripz RuleVisitorTests.test_round_tripj   sW     !!"<=)m11$7|++D14dAq3QRr*   c                     t        j                  d      }| j                  t        t	               j
                  |       y)z:Make sure we throw the right exception on undefined rules.zboy = howdy
N)r   r$   assertRaisesr   r   rQ   )r&   rR   s     r(   test_undefined_rulez$RuleVisitorTests.test_undefined_rulez   s,    !!/2.+-*=*=tDr*   c                     t        j                  d      }t               j                  |      \  }}d}| j	                  |j                  |      t        ||ddt        t        d      |dd      g             y )Nzboy = "howdy"?
howdyr   r-   r   )r   r$   r   rQ   r#   r   r
   )r&   rR   rS   rT   rZ   s        r(   test_optionalzRuleVisitorTests.test_optional   sx    !!"45)m11$7| 	++E2DuaQR+/0@%A+N^P 5Q 	Rr*   N)rH   rI   rJ   rK   rU   rX   r[   rL   r*   r(   rN   rN   c   s    S E
	Rr*   rN   c                 N    d}| |d j                  |      r|t        |      z   S dS )zfThis is an example of a grammar rule implemented as a function, and is
    provided as a test fixture.functionN
startswithr.   r'   postokens      r(   function_rulerc      s0     E#CDz44U;3UEEr*   c                       e Zd ZdZd Zed        ZdeiZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%y#)$GrammarTestsz@Integration-test ``Grammar``: feed it a PEG and see if it works.c                 N    d}||d j                  |      r|t        |      z   S dS )zhThis is an example of a grammar rule implemented as a method, and is
        provided as a test fixture.methodNr^   )r&   r'   ra   rb   s       r(   method_rulezGrammarTests.method_rule   s0     #':#8#8#?sSZITIr*   c                 N    d}| |d j                  |      r|t        |      z   S dS )zlThis is an example of a grammar rule implemented as a descriptor,
        and is provided as a test fixture.
descriptorNr^   r`   s      r(   descriptor_rulezGrammarTests.descriptor_rule   s0     #':#8#8#?sSZITIr*   rk   c                     t        d      }|j                  d      }| j                  |t        |d   dddt        t	        d      ddd      g             y)a,  Test the ``Grammar`` base class's ability to compile an expression
        tree from rules.

        That the correct ``Expression`` tree is built is already tested in
        ``RuleGrammarTests``. This tests only that the ``Grammar`` base class's
        ``_expressions_from_rules`` works.

        zgreeting = "hi" / "howdy"higreetingr   r4   r   N)r   r$   r#   r   r
   )r&   greeting_grammarrR   s      r(   test_expressions_from_rulesz(GrammarTests.test_expressions_from_rules   s]     ##>?%%d+t$4Z$@$1GDM4A6X8  9 	:r*   c                 :   t        d      }t        |      j                         }| j                  |d   d       | j	                  dt
        dk\  rdndz  |v        | j	                  d|v        | j	                  d	|v        | j                  t        |      d
       y)zVAssert that a ``Grammar`` can convert into a string-formatted series
        of rules.
                          bold_text  = bold_open text bold_close
                          text       = ~"[A-Z 0-9]*"i
                          bold_open  = "(("
                          bold_close = "))"
                          r   z%bold_text = bold_open text bold_closetext = ~'[A-Z 0-9]*'i%s   u zbold_open = '(('zbold_close = '))'   N)r   str
splitlinesr#   rE   r   r.   )r&   grammarliness      r(   test_unicodezGrammarTests.test_unicode   s        G'')q#JK1LD<PSVXY 	*e34+u45UQ'r*   c                     t        d      }d}| j                  |j                  |d      t        |d   |ddt        |d   |dd      t        |d	   |dd
      t        |d   |d
d      g             y)z,Make sure partial-matching (with pos) works.rr   z ((boo))yahr   )ra   	bold_text   	bold_openru   r'      
bold_closer   N)r   r#   matchr   r&   r{   ss      r(   
test_matchzGrammarTests.test_match   s       qa0$w{7KQPQST)-gk.BAq!)L)-gfoq!Q)G)-gl.CQ1)M`O 3P 	Qr*   c                 :    | j                  t        t        d       y)z>Constructing a Grammar with bad rules should raise ParseError.zjust a bunch of junkN)rW   r   r   rF   s    r(   test_bad_grammarzGrammarTests.test_bad_grammar   s    *g/EFr*   c           	          t        d      }| j                  t        t        t	        |      j                                     dddt        dk\  rdndz  g       y)	z?Test tolerance of comments and blank lines in and around rules.a4  # This is a grammar.

                          # It sure is.
                          bold_text  = stars text stars  # nice
                          text       = ~"[A-Z 0-9]*"i #dude


                          stars      = "**"
                          # Pretty good
                          #Oh yeah.#zbold_text = stars text starszstars = '**'rs   rt   rv   rw   N)r   r#   listsortedry   rz   r   r&   r{   s     r(   test_commentszGrammarTests.test_comments   s[     	( 	) 	fS\%<%<%>?@/  *\T5Ic357	8	9r*   c                 ^    t        d      }| j                  |j                  d      du       y)zaMake sure we tolerate all sorts of crazy line breaks and comments in
        the middle of rules.z
            bold_text  = bold_open  # commenty comment
                         text  # more comment
                         bold_close
            text       = ~"[A-Z 0-9]*"i
            bold_open  = "((" bold_close =  "))"
            z
((booyah))Nr   rE   r$   r   s     r(   test_multi_linezGrammarTests.test_multi_line   s/        	l34?@r*   c                     t        d      }| j                  t        |j                  d       | j	                  |j                  d      du       y)z8Make sure "not" predicates get parsed and work properly.znot_arp = !"arp" ~"[a-z]+"arpargleN)r   rW   r   r$   rE   r   s     r(   test_notzGrammarTests.test_not   s=    ;<*gmmU;g.d:;r*   c                 .   t        d      }| j                  t        |j                  d       d}| j	                  |j                  d      t        |d   |ddt        t        t        d            |dd      t        t        d      |dd      g	             y )
Nzstarts_with_a = &"a" ~"[a-z]+"burpr   starts_with_ar   ru   az[a-z]+r   )	r   rW   r   r$   r#   r   r   r
   r   r   s      r(   test_lookaheadzGrammarTests.test_lookahead   s    ?@*gmmV<u-tGO4LaQRTU&*9WS\+BAq!&L&*5+;Q1&EaG 0H 	Ir*   c                     t        d      }| j                  t        |j                  d       d}| j	                  t        |j                  |            d       y )Nz!sequence = "chitty" (" " "bang")+zchitty bangbangzchitty bang banga8  <Node called "sequence" matching "chitty bang bang">
    <Node matching "chitty">
    <Node matching " bang bang">
        <Node matching " bang">
            <Node matching " ">
            <Node matching "bang">
        <Node matching " bang">
            <Node matching " ">
            <Node matching "bang">)r   rW   r   r$   r#   ry   r   s      r(   test_parenszGrammarTests.test_parens  sL    BC*gmm5FGW]]1-.&		'r*   c                 <    t        d      }|j                  d       y)zCSmoke-test a circumstance where lazy references don't get resolved.z
            expression = "(" terms ")"
            terms = term+
            term = number
            number = ~r"[0-9]+"
            z(34)Nr   r$   r   s     r(   test_resolve_refs_orderz$GrammarTests.test_resolve_refs_order  s       	fr*   c                 <    t        d      }|j                  d       y)zISmoke-test another circumstance where lazy references don't get resolved.a>  
            block = "{" _ item* "}" _

            # An item is an element of a block.
            item = number / word / block / paren

            # Parens are for delimiting subexpressions.
            paren = "(" _ item* ")" _

            # Words are barewords, unquoted things, other than literals, that can live
            # in lists. We may renege on some of these chars later, especially ".". We
            # may add Unicode.
            word = spaceless_word _
            spaceless_word = ~r"[-a-z`~!@#$%^&*_+=|\\;<>,.?][-a-z0-9`~!@#$%^&*_+=|\\;<>,.?]*"i

            number = ~r"[0-9]+" _ # There are decimals and strings and other stuff back on the "parsing" branch, once you get this working.

            _ = meaninglessness*
            meaninglessness = whitespace
            whitespace = ~r"\s+"
            z{log (add 3 to 5)}Nr   r   s     r(   test_resolve_refs_completenessz+GrammarTests.test_resolve_refs_completeness   s       * 	*+r*   c                     t        d       y)zSmoke-test a grammar that was causing infinite loops while building.

        This was going awry because the "int" rule was never getting marked as
        resolved, so it would just keep trying to resolve it over and over.

        z
            digits = digit+
            int = digits
            digit = ~"[0-9]"
            number = int
            main = number
            N)r   rF   s    r(   test_infinite_loopzGrammarTests.test_infinite_loop9  s     	  	r*   c                 Z   t        j                  t              5  t        d       d d d        t        j                  t              5  t        d       d d d        t        j                  t              5  t        d       d d d        y # 1 sw Y   dxY w# 1 sw Y   CxY w# 1 sw Y   y xY w)NzA
                foo = bar
                bar = foo
            zA
                foo = foo
                bar = foo
            z[
                foo = bar
                bar = baz
                baz = foo
            )pytestraisesr	   r   rF   s    r(    test_circular_toplevel_referencez-GrammarTests.test_circular_toplevel_referenceH  s    ]]?+ 	  	
 ]]?+ 	  	
 ]]?+ 	  	 		 	
	 	
	 	s#   B	B4B!	BB!B*c                 ^    t        d      }| j                  |j                  d      du       y)z$Right-recursive refs should resolve.zN
            digits = digit digits?
            digit = ~r"[0-9]"
            12Nr   r   s     r(   test_right_recursivez!GrammarTests.test_right_recursiveZ  s-       	d+478r*   c                 <    | j                  d       t        d       y)zQUselessly circular references should be detected by the grammar
        compiler.z6We have yet to make the grammar compiler detect these.z<
             foo = bar
             bar = foo
             N)skipTestr   rF   s    r(   test_badly_circularz GrammarTests.test_badly_circularb  s     	NO  	r*   c                 8    t        d      j                  d       y)zwMake sure a parenthesized expression is allowed to have leading
        whitespace when nested directly inside another.zfoo = ( ("c") )cNr   rF   s    r(   #test_parens_with_leading_whitespacez0GrammarTests.test_parens_with_leading_whitespacek  s     	%&,,S1r*   c                 8    t        d      j                  d       y )Nzfoo = 'a' '"'za"r   rF   s    r(   test_single_quoted_literalsz(GrammarTests.test_single_quoted_literalsp  s    #$**40r*   c                     t        dd       }d}| j                  |j                  |      t        |d   |ddt        |d   |dd	      t        |d
   |d	d      t        |d   |dd      g             y)z1Run 2-arg custom-coded rules through their paces.z\
            bracketed_digit = start digit end
            start = '['
            end = ']'c                 6    | |   j                         r|dz   S d S Nr   )isdigitr'   ra   s     r(   <lambda>z7GrammarTests.test_simple_custom_rules.<locals>.<lambda>y  s%    !%c!2!2!4S1W :> r*   digit[6]bracketed_digitr   ru   startr   r   r4   endr   Nr   r#   r$   r   r   s      r(   test_simple_custom_rulesz%GrammarTests.test_simple_custom_ruless  s     ?	@ q)*+Q1WW%q!Q/WW%q!Q/WU^Q1-@/ 0	1r*   c                     t        dd       }d}| j                  |j                  |      t        |d   |ddt        |d   |dd	      t        |d
   |d	d      t        |d   |dd      g             y)zRun 5-arg custom rules through their paces.

        Incidentally tests returning an actual Node from the custom rule.

        zy
            bracketed_digit = start digit end
            start = '['
            end = ']'
            real_digit = '6'c                 0    |d   j                  | |||      S )N
real_digit)
match_core)r'   ra   cacheerrorr{   s        r(   r   z8GrammarTests.test_complex_custom_rules.<locals>.<lambda>  s     L)44T3uM r*   r   r   r   r   ru   r   r   r   r4   r   r   Nr   r   s      r(   test_complex_custom_rulesz&GrammarTests.test_complex_custom_rules  s       N
O q)*+Q1WW%q!Q/W\*Aq!4WU^Q1-@/ 0	1r*   c                     t        dt        t        d      t        d      d            j                  d      }d}| j	                  |j                  |      t        |d   |dd	t        |d   |dd
      t        |d   |d
d	      g             y)zMake sure LazyReferences manually shoved into custom rules are
        resolved.

        Incidentally test passing full-on Expressions as custom rules and
        having a custom rule as the default one.

        z.
            four = '4'
            five = '5'fourfive
forty_five)name)r   45r   r4   r   r   N)r   r   r   defaultr#   r$   r   r   s      r(   test_lazy_custom_rulesz#GrammarTests.test_lazy_custom_rules  s        f 5 -f 5%134
 5<GL4I 	 q)&1aWV_aA.WV_aA.;0 1	2r*   c           	          t        d       j                  d      }d}| j                  |j                  |      t	        |d   |dd             y)zMake sure custom rules that aren't hooked to any other rules still
        get included in the grammar and that lone ones get set as the
        default.

        Incidentally test Grammar's `rules` default arg.

        c                     |dz   S r   rL   r   s     r(   r   z<GrammarTests.test_unconnected_custom_rules.<locals>.<lambda>  s
    S1W r*   )one_charr   4r   r   N)r   r   r#   r$   r   r   s      r(   test_unconnected_custom_rulesz*GrammarTests.test_unconnected_custom_rules  sK     #<=EEjQq)$aA.	0r*   c                     | j                  t        t                     | j                  t        | j                               | j                  t        | j                  d                y )Nrk   )rE   r   rc   rh   rS   rF   s    r(   test_callability_of_routinesz)GrammarTests.test_callability_of_routines  sE    M23D$4$456DJJ/@$ABCr*   c                    t        dt        | j                  | j                  d         }|j	                  d      }|j
                  D cg c]  }|j                  j                   }}| j                  |g d       yc c}w )zxConfirms that functions, methods and method descriptors can all be
        used to supply custom grammar rules.
        z>
            default = function method descriptor
            rk   )r]   rg   rj   functionmethoddescriptorN)	r   rc   rh   rS   r$   r    exprr   r#   )r&   r{   resultnode
rule_namess        r(   test_callability_custom_rulesz*GrammarTests.test_callability_custom_rules  su       ###zz"34
 9:17AdiinnA
A%IJ Bs   
A>c           	      z    t        d      }| j                  |j                  d      t        |d   ddd             y)zMake sure we get an actual rule set as our default rule, even when
        the first rule has forward references and is thus a LazyReference at
        some point during grammar compilation.

        zK
            styled_text = text
            text        = "hi"
            rm   r'   r   r4   Nr   r   s     r(   test_lazy_default_rulez#GrammarTests.test_lazy_default_rule  s=        	t,d76?D!Q.OPr*   c                     t        d      }d }| j                  t        ||g       d }| j                  t        ||g       y)z:Make sure that a Grammar is immutable after being created.z!
            foo = 'bar'
        c                     d| d<   y )Nr   foorL   )r{   s    r(   mod_grammarz8GrammarTests.test_immutable_grammar.<locals>.mod_grammar  s    GENr*   c                 <    t        d      }| j                  |       y )Nz*
                baz = 'biff'
            )r   update)r{   new_grammars     r(   r   z8GrammarTests.test_immutable_grammar.<locals>.mod_grammar  s    ! # K NN;'r*   N)r   rW   	TypeErrorAttributeError)r&   r{   r   s      r(   test_immutable_grammarz#GrammarTests.test_immutable_grammar  sE      	)[7)<	(
 	.+yAr*   c                 J    | j                  t        t        d                   y )Nz	foo = "a")rE   reprr   rF   s    r(   	test_reprzGrammarTests.test_repr  s    W\234r*   c                     t        dj                  d t        d      D                    }| j                  t	        |j                               t        d      D cg c]  }d|z  	 c}       y c c}w )N
c              3   &   K   | ]	  }d |z    ywzr%s = "something"NrL   .0is     r(   	<genexpr>z?GrammarTests.test_rule_ordering_is_preserved.<locals>.<genexpr>       #P$7!$;#P   d   r%s)r   joinranger#   r   keysr&   r{   r   s      r(   test_rule_ordering_is_preservedz,GrammarTests.test_rule_ordering_is_preserved  sT    $))#PU3Z#PPQ  %c
+1UQY+	-+s   A/
c                    t        dj                  d t        d      D                    j                         }| j	                  t        |j                               t        d      D cg c]  }d|z  	 c}       y c c}w )Nr   c              3   &   K   | ]	  }d |z    ywr   rL   r   s     r(   r   zQGrammarTests.test_rule_ordering_is_preserved_on_shallow_copies.<locals>.<genexpr>  r   r   r   r   )r   r   r   _copyr#   r   r   r   s      r(   1test_rule_ordering_is_preserved_on_shallow_copiesz>GrammarTests.test_rule_ordering_is_preserved_on_shallow_copies  s]    $))#PU3Z#PPQWWY  %c
+1UQY+	-+s   )A=
c                 t   t        d      }dt        d      D cg c]  }d|z  	 c}fdt        dd      D cg c]  }d|z  	 c}fdd	gfd
t        dd      D cg c]  }d|z  	 c}fdddgfdddgfdg dfg}|D ]"  \  }}|D ]  }||   j                  |      rJ  $ ddgfdt        d      D cg c]  }d|z  	 c}fdt        t        d            t        t        dd            z   D cg c]  }d|z  	 c}fd
t        t        d            t        t        dd            z   D cg c]  }d|z  	 c}fddgfddgfddgfg}|D ]B  \  }}|D ]8  }t	        j
                  t              5  ||   j                  |       d d d        : D y c c}w c c}w c c}w c c}w c c}w c c}w # 1 sw Y   exY w)Nz
            left_missing = "a"{,5}
            right_missing = "a"{5,}
            exact = "a"{5}
            range = "a"{2,5}
            optional = "a"?
            plus = "a"+
            star = "a"*
        left_missingr   r   right_missingr-   r   exactaaaaar   r4   optionalrw   plusaastar)rw   r   r	  aaaaaa
   b)r   r   r$   r   r   r   r   )r&   r{   r   should_parserD   examplesexampleshould_not_parses           r(   test_repetitionszGrammarTests.test_repetitions  s      uQx8!cAg89a<1sQw<=wi a41sQw45"c#c4[!_%
 + 	4ND(# 4t}**73334	4
 gY'a91sQw9:U1XeArl9K(KL1sQwLMU1XeArl9K(KL1sQwLM$ bTNcUO
 / 	1ND(# 1]]:. 1DM''01 11	1+ 9<4 :LL1 1s.   FFF3F/F$
+F).F..F7c                 j   d}t        |      t        |      k(  sJ | j                  t        t              t        t                     | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        d      t        d             y )NzP
            x = y / z / ""
            y = "y" x
            z = "z" x
        zexpr = ~"[a-z]{1,3}"zexpr = ~"[a-z]{2,3}"zexpr = ~"[a-z]{1,4}"zexpr = &"a"zexpr = !"a")r   r#   r   assertNotEqual)r&   grammar_defs     r(   
test_equalzGrammarTests.test_equal   s    
 {#w{';;;;-w{/CDG$:;WE[=\]G$:;WE[=\]GM2GM4JKr*   N)&rH   rI   rJ   rK   rh   staticmethodrk   rS   rp   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  rL   r*   r(   re   re      s    JJ J J 0E:("QG9*
A<I'",2$92
11102(0D
K
QB"5--#1JLr*   re   c                   (    e Zd ZdZd Zd Zd Zd Zy)TokenGrammarTestsz9Tests for the TokenGrammar class and associated machineryc                     t        d      t        d      g}t        d      }| j                  |j                  |      t	        |d   |ddt	        |d   |dd      t	        t        d      |dd      g             y	)
zToken literals should work.token1token2zM
            foo = token1 "token2"
            token1 = "token1"
            r   r   r4   r   r   N)r   r   r#   r$   r   r   )r&   r   r{   s      r(   test_parse_successz$TokenGrammarTests.test_parse_success1  s}    8_eHo.    	q)AqWX&1a0\(+Q1547 8	9r*   c                     t        d      }t        j                  t              5 }|j	                  t        d      t        d      g       ddd       dt        j                        v sJ y# 1 sw Y   #xY w)z8Parse failures should work normally with token literals.z1
            foo = "token1" "token2"
            tokenBOOr  NzRule 'foo' didn't match at)r   r   r   r   r$   r   ry   value)r&   r{   es      r(   test_parse_failurez$TokenGrammarTests.test_parse_failure=  sj        ]]:& 	@!MM5,eHo>?	@+s177|;;;	@ 	@s   &A--A6c                     t        d      }| j                  t        |j                         t                     | j                  d|j                                y )Nu   💣u   <Token "💣">)r   rE   
isinstance__repr__ry   r#   )r&   ts     r(   test_token_reprz!TokenGrammarTests.test_token_reprF  s;    &M
1::<56)1::<8r*   c                    t        d      }t        d      }t        d      }|d   j                  g       J |d   j                  |g      J |d   j                  ||g      J t        j                  t
              5  |d   j                  ||g       d d d        t        j                  t
              5  |d   j                  |g       d d d        |d   j                  |g      J t        j                  t
              5  |d   j                  ||g       d d d        t        j                  t
              5  |d   j                  |g       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   YxY w# 1 sw Y   y xY w)Nr   r  z7
            foo = "a"*
            bar = "a"+
        r   bar)r   r   r$   r   r   r   )r&   r   r  r{   s       r(    test_token_star_plus_expressionsz2TokenGrammarTests.test_token_star_plus_expressionsK  sx   #J#J    u~##B'333u~##QC(444u~##QF+777]]:& 	)EN  !Q(	)]]:& 	&EN  !%	& u~##QC(444]]:& 	)EN  !Q(	)]]:& 	&EN  !%	& 	&	) 	)	& 	&	) 	)	& 	&s0    E8E)E5>FE&)E25E>F
N)rH   rI   rJ   rK   r  r"  r'  r*  rL   r*   r(   r  r  .  s    C
9<9
&r*   r  c                  &   t        d      } t        | d   t              sJ | d   j                  dk(  sJ t	        j
                  t              5  | j                  d       d d d        t        d      }|j                  d      sJ y # 1 sw Y   (xY w)Nz;
        escaped_bell = r"\b"
        r = "irrelevant"
    escaped_bellz\bzirrelevantz"
        escaped_bell = r"\b"
    )r   r$  r
   literalr   r   r   r$   )gg2s     r(   #test_precedence_of_string_modifiersr0  b  s     	  		A a'111^$$---	z	"  	  
  
	B 88E??   s   BBc                  D    t        d      } d}| j                  d      J y )Nz
        file = header body terminator
        header = b"\xFF" length b"~"
        length = ~rb"\d+"
        body = ~b"[^\xFF]*"
        terminator = b"\xFF"
       s   22~aaaaaaaaaaaaaaaaaaaaaar   )r.  lengths     r(   test_binary_grammarr4  t  s.      		A F7756BBBr*   c                     t        j                  t              5 } t        d       d d d         j                  j
                  t        u sJ t        j                  t              5 } t        d       d d d        | j                  j
                  t        u sJ t        d       t        d       y # 1 sw Y   xY w# 1 sw Y   JxY w)Nz:
            foo = b"foo"
            bar = "bar"
        z;
            foo = ~b"foo"
            bar = "bar"
        z/
        foo = b"foo"
        bar = b"bar"
    z-
        foo = "foo"
        bar = "bar"
    )r   r   r	   r   r   original_classr   )r!  s    r(   )test_inconsistent_string_types_in_grammarr7    s    		' 1  	
 77!!Z///		' 1  	
 77!!Z///   	   	'  s   B.%B:.B7:Cc                      d} t        |       }t        j                  t              5 }|d   j	                  d       d d d        dt        j                        v sJ y # 1 sw Y   #xY w)Nz
    expression = operator_expression / non_operator_expression
    non_operator_expression = number_expression

    operator_expression = expression "+" non_operator_expression

    number_expression = ~"[0-9]+"
    operator_expressionz1+2zDParsimonious is a packrat parser, so it can't handle left recursion.)r   r   r   r   r$   ry   r   )language_grammarr{   r!  s      r(   test_left_associativer;    sj     &'G	)	* 4a%&,,U34QUXYZY`Y`Uaaaa4 4s   AA')&sysr   unittestr   r   parsimonious.exceptionsr   r   r   r   r	   parsimonious.expressionsr
   r   r   r   r   r   parsimonious.grammarr   r   r   r   r   r   parsimonious.nodesr   parsimonious.utilsr   r   rN   rc   re   r  r0  r4  r7  r;  rL   r*   r(   <module>rC     s       o o c c m m # $Q Qh%Rx %RPFYL8 YLx1& 1&h$	C	4br*   