
    ZMhZ9                     X    d dl mZ d dlZd dlZddlmZmZmZmZm	Z	  G d d          Z
dS )    )contextmanagerN   )ParserElementParseExceptionKeyword__diag__
__compat__c                      e Zd ZdZ G d d          Z G d d          Ze	 	 	 	 	 	 dd	dd
dedej	        e
         dej	        e
         dededej	        e         dej	        e         dej        ee
f         dedefd            ZdS )pyparsing_testzB
    namespace class for classes useful in writing unit tests
    c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	&pyparsing_test.reset_pyparsing_contexta  
        Context manager to be used when writing unit tests that modify pyparsing config values:
        - packrat parsing
        - bounded recursion parsing
        - default whitespace characters.
        - default keyword characters
        - literal string auto-conversion class
        - __diag__ settings

        Example::

            with reset_pyparsing_context():
                # test that literals used to construct a grammar are automatically suppressed
                ParserElement.inlineLiteralsUsing(Suppress)

                term = Word(alphas) | Word(nums)
                group = Group('(' + term[...] + ')')

                # assert that the '()' characters are not included in the parsed tokens
                self.assertParseAndCheckList(group, "(abc 123 def)", ['abc', '123', 'def'])

            # after exiting context manager, literals are converted to Literal expressions again
        c                     i | _         d S N)_save_contextselfs    Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyparsing/testing.py__init__z/pyparsing_test.reset_pyparsing_context.__init__/   s    !#D    c                    t           j        | j        d<   t          j        | j        d<   t           j        | j        d<   t           j        | j        d<   t           j        | j        d<   t           j        rt           j        j	        | j        d<   n
d | j        d<   t           j
        | j        d<   t           j        | j        d<   d	 t          j        D             | j        d
<   dt          j        i| j        d<   | S )Ndefault_whitespacedefault_keyword_charsliteral_string_classverbose_stacktracepackrat_enabledpackrat_cache_sizepackrat_parserecursion_enabledc                 :    i | ]}|t          t          |          S  )getattrr   ).0names     r   
<dictcomp>z?pyparsing_test.reset_pyparsing_context.save.<locals>.<dictcomp>H   s1     . . .26gh--. . .r   r   collect_all_And_tokensr	   )r   DEFAULT_WHITE_CHARSr   r   DEFAULT_KEYWORD_CHARS_literalStringClassr   _packratEnabledpackrat_cachesize_parse_left_recursion_enabledr   
_all_namesr	   r%   r   s    r   savez+pyparsing_test.reset_pyparsing_context.save2   s   7D7XD34:A:WD67 1 56 8E7WD344A4QD01, @!/4 "#788 <@"#782?2FD/5 23. .:B:M. . .Dz*
 )**K0D|, Kr   c                    t           j        | j        d         k    rt          j        | j        d                    | j        d         t           _        | j        d         t
          _        t          j        | j        d                    | j        d                                         D ](\  }} |rt          j
        nt          j        |           )dt           _        | j        d         r t          j        | j        d                    n| j        d	         t           _        | j        d
         t           _        | j        d         t           _        | S )Nr   r   r   r   r   Fr   r   r   r   r	   )r   r&   r   set_default_whitespace_charsr   r   r'   inlineLiteralsUsingitemsr   enabledisabler)   enable_packratr,   r-   r	   r%   )r   r#   values      r   restorez.pyparsing_test.reset_pyparsing_context.restoreR   sE    1%&:;< < :&';<   04/ABV/WM,,0,>?V,WG)-"#9:    $1*=CCEE G Ge?E?x/?FFFF,1M)!"34 K,T-?@T-UVVVV'+'9/'J$484F#5M1 150B<0PJ-Kr   c                 r     t          |                       }|j                            | j                   |S r   )typer   update)r   rets     r   copyz+pyparsing_test.reset_pyparsing_context.copys   s2    $t**,,C$$T%7888Jr   c                 *    |                                  S r   )r/   r   s    r   	__enter__z0pyparsing_test.reset_pyparsing_context.__enter__x   s    99;;r   c                 .    |                                   d S r   )r8   )r   argss     r   __exit__z/pyparsing_test.reset_pyparsing_context.__exit__{   s    LLNNNNNr   N)
__name__
__module____qualname____doc__r   r/   r8   r=   r?   rB   r    r   r   reset_pyparsing_contextr      sz        	 	0	$ 	$ 	$	 	 	@	 	 	B	 	 	
	 	 		 	 	 	 	r   rG   c                   X    e Zd ZdZ	 d	dZ	 d
dZ	 d
dZ	 ddZee	ddfd            Z
dS )&pyparsing_test.TestParseResultsAssertszk
        A mixin class to add parse results assertion methods to normal unittest.TestCase classes.
        Nc                     |*|                      ||                                |           |,|                      ||                                |           dS dS )z
            Unit test assertion to compare a :class:`ParseResults` object with an optional ``expected_list``,
            and compare any defined results names with an optional ``expected_dict``.
            Nmsg)assertEqualas_listas_dict)r   resultexpected_listexpected_dictrL   s        r   assertParseResultsEqualsz?pyparsing_test.TestParseResultsAsserts.assertParseResultsEquals   sh     (  0@0@c JJJ(  0@0@c JJJJJ )(r   Tc                     |                     |d          }|r"t          |                                           n!t          |                                           |                     |||           dS )z
            Convenience wrapper assert to test a parser element and input string, and assert that
            the resulting ``ParseResults.asList()`` is equal to the ``expected_list``.
            T)	parse_all)rQ   rL   Nparse_stringprintdumprN   rS   )r   exprtest_stringrQ   rL   verboserP   s          r   assertParseAndCheckListz>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckList   sw     &&{d&CCF (fkkmm$$$$fnn&&'''))&SV)WWWWWr   c                     |                     |d          }|r"t          |                                           n!t          |                                           |                     |||           dS )z
            Convenience wrapper assert to test a parser element and input string, and assert that
            the resulting ``ParseResults.asDict()`` is equal to the ``expected_dict``.
            T)parseAll)rR   rL   NrV   )r   rZ   r[   rR   rL   r\   rP   s          r   assertParseAndCheckDictz>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckDict   sw     &&{T&BBF (fkkmm$$$$fnn&&'''))&SV)WWWWWr   c                    |\  }}||                      |||nd           dS d t          ||          D             }|D ]\  }}}	t          d |	D             d          }
t          d |	D             d          }|J|                     ||
p|          5  t	          |t
                    r|	 ddd           n# 1 swxY w Y   t          d |	D             d          }t          d	 |	D             d          }||fd
k    r|                     ||||
p|           t          d|           |                      |||nd           dS )ah  
            Unit test assertion to evaluate output of ``ParserElement.runTests()``. If a list of
            list-dict tuples is given as the ``expected_parse_results`` argument, then these are zipped
            with the report tuples returned by ``runTests`` and evaluated using ``assertParseResultsEquals``.
            Finally, asserts that the overall ``runTests()`` success value is ``True``.

            :param run_tests_report: tuple(bool, [tuple(str, ParseResults or Exception)]) returned from runTests
            :param expected_parse_results (optional): [tuple(str, list, dict, Exception)]
            Nzfailed runTestsrK   c                 "    g | ]\  }}g ||R S r    r    )r"   rptexpecteds      r   
<listcomp>zOpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<listcomp>   s9       !C !# x    r   c              3   D   K   | ]}t          |t                    |V  d S r   )
isinstancestrr"   exps     r   	<genexpr>zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   s1       Q QJsC<P<P Q Q Q Q Q Q Qr   c              3   n   K   | ]0}t          |t                    t          |t                    ,|V  1d S r   )rg   r:   
issubclass	Exceptionri   s     r   rk   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   s[        %c400 6@Y5O5O     r   )expected_exceptionrL   c              3   D   K   | ]}t          |t                    |V  d S r   )rg   listri   s     r   rk   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   1      JJJsD4I4IJJJJJJJr   c              3   D   K   | ]}t          |t                    |V  d S r   )rg   dictri   s     r   rk   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   rr   r   NN)rQ   rR   rL   zno validation for )
assertTruezipnextassertRaisesrg   rn   rS   rX   )r   run_tests_reportexpected_parse_resultsrL   run_test_successrun_test_resultsmergedr[   rP   rd   fail_msgro   rQ   rR   s                 r   assertRunTestResultsz;pyparsing_test.TestParseResultsAsserts.assertRunTestResults   sZ    2B..%-$##FW       %()9;Q%R%R  F 28 #D #D-VX   Q Q Q Q QSWXX%) #+  
 & &" &1**+=8?s +   ) ) &fi88 )"(L)) ) ) ) ) ) ) ) ) ) ) ) ) ) ) %)JJJJJD% %M %)JJJJJD% %M &}5EE55"*7*7 (C	 6     B;BBCCCC OO S_ccBS      s   B;;B?	B?	c              #   6  K   |`t          |t                    rt          j        |          }|                     |||          5 }|V  d d d            d S # 1 swxY w Y   d S |                     ||          5 }|V  d d d            d S # 1 swxY w Y   d S )NrK   )rg   rh   reescapeassertRaisesRegexry   )r   exc_typeexpected_msgrL   ctxs        r   assertRaisesParseExceptionzApyparsing_test.TestParseResultsAsserts.assertRaisesParseException   s:      'lC00 ;#%9\#:#:L++Hl+LL PSIII                  &&xS&99 SIII                 s$   AAA<BBB)NNN)NTru   )rC   rD   rE   rF   rS   r]   r`   r   r   r   r   r    r   r   TestParseResultsAssertsrI   ~   s        	 	
 GK
	K 
	K 
	K 
	K GK	X 	X 	X 	X GK	X 	X 	X 	X FJ@	 @	 @	 @	D 
)$	 	 	 
	 	 	r   r   NT| )indentbase_1s
start_lineend_lineexpand_tabseol_markmark_spacesmark_controlr   r   returnc                   |r|                                  } t          t                    rdz                                   |t          j        t
          |          }|dk    rXd t          t          dd          t          dd                    D             }	d	|	d
<   t
                              |	          }
dnTt          |          t
                              fdt          t          dd                    d
gz   D                       }
|                     |
          } |U|dk    rO|dk    r3t
                              ddd          }
|                     |
          } n|                     d|          } |d}|t          |           }t          |t          |                     }t          t          d|          |          }|dk    r |                                 ||z
  |         }n*d |                     d          ||z
  |         D             }|sdS t          t          |                    t          d |D                       }ddz   z  z   }|dk    rP||rdndz   d                    d t          |rdndt          |dz  d                    D                       z   dz   }nd}|rdnd|z   d                    d t          | dz             D                       z   dz   }d}||rdndz   || dz   z  z   dz   }||z   d                    fdt'          |||z             D                       z   dz   S )u  
        Helpful method for debugging a parser - prints a string with line and column numbers.
        (Line and column numbers are 1-based by default - if debugging a parse action,
        pass base_1=False, to correspond to the loc value passed to the parse action.)

        :param s: tuple(bool, str - string to be printed with line and column numbers
        :param start_line: int - (optional) starting line number in s to print (default=1)
        :param end_line: int - (optional) ending line number in s to print (default=len(s))
        :param expand_tabs: bool - (optional) expand tabs to spaces, to match the pyparsing default
        :param eol_mark: str - (optional) string to mark the end of lines, helps visualize trailing spaces (default="|")
        :param mark_spaces: str - (optional) special character to display in place of spaces
        :param mark_control: str - (optional) convert non-printing control characters to a placeholding
                                 character; valid values:
                                 - "unicode" - replaces control chars with Unicode symbols, such as "␍" and "␊"
                                 - any single character string - replace control characters with given string
                                 - None (default) - string is displayed as-is
        :param indent: str | int - (optional) string to indent with line and column numbers; if an int
                                   is passed, converted to " " * indent
        :param base_1: bool - (optional) whether to label string using base 1; if False, string will be
                              labeled based at 0 (default=True)

        :return: str - input string with leading line numbers and column number headers
         Nunicodec                     i | ]\  }}||	S r    r    )r"   cus      r   r$   z4pyparsing_test.with_line_numbers.<locals>.<dictcomp>'  s+     " " "!QAq" " "r   r   !   i $  i3$  i!$     r   c                     i | ]}|S r    r    )r"   r   ord_mark_controls     r   r$   z4pyparsing_test.with_line_numbers.<locals>.<dictcomp>0  s    MMMQQ(MMMr       i	$  i#$  )	   r   c                     g | ]}|d z   S )   ␊r    r"   lines     r   re   z4pyparsing_test.with_line_numbers.<locals>.<listcomp>C  s)       !%u  r   r   c              3   4   K   | ]}t          |          V  d S r   )lenr   s     r   rk   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>J  s(      993t99999999r   r   c   c              3   .   K   | ]}d  |dz   dz   V  dS )zc                                                                                                   r   d   Nr    r"   is     r   rk   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>P  sI          0!a%300     r   r   
c              3   ,   K   | ]}d |dz   dz   V  dS )z	         r   
   Nr    r   s     r   rk   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>[  s3      XXQ01q5B,00XXXXXXr   r   
12345678900c              3   <   K   | ]\  }} | d d|  V  dS )d:Nr    )r"   r   r   r   r   lineno_widths      r   rk   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>e  s^        At ?1l?????t?X??     r   )start)
expandtabsrg   inttypingcastrh   rw   range	maketransordrq   	translatereplacer   minmax
splitlinessplitjoin	enumerate)r   r   r   r   r   r   r   r   r   transtable_maptbls_linesmax_line_lenleadheader0header1digitsheader2r   r   s       `  `          @@r   with_line_numbersz pyparsing_test.with_line_numbers   s   H  	Afc"" 	"6\F""$$#!;sL99Ly((" "%(q"uVV7L7L%M%M" " " '-s#mmN33#&|#4#4 mmMMMM$uQ||2D2Du2LMMM  C  A"{c'9'9i''mmF$;$;<<KK$$IIc;//J1vvHxQ((Q
++X66
9$$llnnZ&%88%CDGG )*
V8Kh8V)W  G  	23x==))9999999|a/002(22S*''  "#511As<3;NPQ7R7RSS      G G"RRsggXX%<-SUBU@V:W:WXXXXXY  	 &)BBc*V,"9L7M-NNQUU 	 ii      (
V8KLLL     	
r   )NNTr   NN)rC   rD   rE   rF   rG   r   staticmethodrh   r   Optionalr   boolUnionr   r    r   r   r   r      se        f f f f f f f fP{ { { { { { { {z  ,0)- ,0-1n
 *,n
 n
 n
n
OC(n
 /#&n
 	n

 n
 _S)n
 oc*n
 S#X&n
 n
 
n
 n
 n
 \n
 n
 n
r   r   )
contextlibr   r   r   corer   r   r   r   r	   r   r    r   r   <module>r      s    & % % % % % 				              Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
r   