a
    ze<]                     @   s
  d dl Z d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZ g dZg dZg dZg dZd	d
ddZg dZeedddZeeeeeee f f  eee ee f dddZee\ZZee\ZZee\ZZee\ZZG dd dejZdS )    N)AnyUnionIterableTuple)dedent)support(  )Zannotated_assignmentzx: int = 42)Zannotated_assignment_with_tuplezx: tuple = 1, 2)Z annotated_assignment_with_parensz(paren): int = 3+2)Zannotated_assignment_with_yieldzx: int = yield 42)Zannotated_no_assignmentzx: int)Zannotation_with_multiple_parensz((parens)): int)Zannotation_with_parensz(parens): int)Zannotated_assignment_with_attrza.b: int)Z#annotated_assignment_with_subscriptz	a[b]: int)Z)annotated_assignment_with_attr_and_parensz
(a.b): int)Z.annotated_assignment_with_subscript_and_parensz(a[b]): int)assertzassert a)Zassert_messagezassert a, b)Zassignment_falsez	a = False)Zassignment_noneza = None)Zassignment_trueza = True)Zassignment_parenz(a) = 42)Zassignment_paren_multiplez(a, b) = (0, 1))Zasyncforz1
        async for i in a:
            pass
     )Zattribute_callza.b())Zattribute_multiple_nameszabcd.efg.hij)Zattribute_simpleza.b)Zattributes_subscriptza.b[0])Zaugmented_assignmentzx += 42)Zaugmented_assignment_attributeza.b.c += 42)Zaugmented_assignment_parenz	(x) += 42)Z$augmented_assignment_paren_subscriptz(x[0]) -= 42)Z	binop_addz1 + 1)Zbinop_add_multiplez1 + 1 + 1 + 1)Z	binop_allz1 + 2 * 5 + 3 ** 2 - -3)Zbinop_boolop_compz"1 + 1 == 2 or 1 + 1 == 3 and not b)Z	boolop_orza or b)Zboolop_or_multipleza or b or c)Zclass_def_basesz.
        class C(A, B):
            pass
     )Zclass_def_decoratorsz3
        @a
        class C:
            pass
     )Z#class_def_decorator_with_expressionz>
        @lambda x: 42
        class C:
            pass
     )Z.class_def_decorator_with_expression_and_walruszA
        @x:=lambda x: 42
        class C:
            pass
     )Zclass_def_keywordsz:
        class C(keyword=a+b, **c):
            pass
     )Zclass_def_mixedz>
        class C(A, B, keyword=0, **a):
            pass
     )Zclass_def_simplez(
        class C:
            pass
     )Zclass_def_starred_and_kwargz7
        class C(A, B, *x, **y):
            pass
     )Zclass_def_starred_in_kwargsz>
        class C(A, x=2, *[B, C], y=3):
            pass
     )Zcall_attributezf().b)Zcall_genexpzf(i for i in a))Zcall_mixed_argszf(a, b, *c, **d))Zcall_mixed_args_namedzf(a, b, *c, d=4, **v))Zcall_one_argzf(a))Zcall_posarg_genexpzf(a, (i for i in a)))Zcall_simplezf())Zcall_subscriptzf()[0])compza == b)Zcomp_multipleza == b == c)Zcomp_paren_endz
a == (b-1))Zcomp_paren_startz
(a-1) == b)	decoratorz3
        @a
        def f():
            pass
     )Zdecorator_asyncz9
        @a
        async def d():
            pass
     )Zdecorator_with_expressionz>
        @lambda x: 42
        def f():
            pass
     )Z$decorator_with_expression_and_walruszA
        @x:=lambda x: 42
        def f():
            pass
     )Zdel_attributezdel a.b)Zdel_call_attributez	del a().c)Zdel_call_genexp_attributezdel a(i for i in b).c)Z	del_emptyzdel())Zdel_listzdel a, [b, c])Z	del_mixedzdel a[0].b().c)Zdel_multiplezdel a, b)Zdel_multiple_calls_attributezdel a()().b)Z	del_parenzdel(a,b))Zdel_paren_single_targetzdel(a))Zdel_subscript_attributez
del a[0].b)Z	del_tuplezdel a, (b, c))deletezdel a)dictzO
        {
            a: 1,
            b: 2,
            c: 3
        }
     )Z	dict_compz{x:1 for x in a})Zdict_comp_ifz{x:1+2 for x in a if b})Z
dict_emptyz{})Zempty_line_after_linecontz+
        pass
        \

        pass
     )forz+
        for i in a:
            pass
     )Zfor_elsezJ
        for i in a:
            pass
        else:
            pass
     )Zfor_star_target_in_parenzfor (a) in b: pass)Zfor_star_targets_attributezfor a.b in c: pass)Zfor_star_targets_call_attributezfor a().c in b: pass)Zfor_star_targets_emptyzfor () in a: pass)Zfor_star_targets_mixedzfor a[0].b().c in d: pass)Zfor_star_targets_mixed_starredz7
        for a, *b, (c, d) in e:
            pass
     )Zfor_star_targets_multiplezfor a, b in c: pass)Zfor_star_targets_nested_starredzfor *[*a] in b: pass)Zfor_star_targets_starredzfor *a in b: pass)Z$for_star_targets_subscript_attributezfor a[0].b in c: pass)Zfor_star_targets_trailing_commaz4
        for a, (b, c), in d:
            pass
     )Zfor_star_targets_tuplezfor a, (b, c) in d: pass)Zfor_underscorez+
        for _ in a:
            pass
     )Zfunction_return_typez/
        def f() -> Any:
            pass
     )zf-string_slicez	f'{x[2]}')zf-string_slice_upperzf'{x[2:3]}')zf-string_slice_stepzf'{x[2:3:-2]}')zf-string_constantzf'{42}')zf-string_boolopzf'{x and y}')zf-string_named_exprzf'{(x:=42)}')zf-string_binopzf'{x+y}')zf-string_unaryopz
f'{not x}')f-string_lambdaz6f'{(lambda x, /, y, y2=42 , *z, k1, k2=34, **k3: 42)}')zf-string_lambda_callzf'{(lambda: 2)(2)}')zf-string_ifexprzf'{x if y else z}')zf-string_dictzf'{ {2:34, 3:34} }')zf-string_setzf'{ {2,-45} }')zf-string_listzf'{ [2,-45] }')zf-string_tuplezf'{ (2,-45) }')zf-string_listcompzf'{[x for x in y if z]}')zf-string_setcompzf'{ {x for x in y if z} }')zf-string_dictcompzf'{ {x:x for x in y if z} }')zf-string_genexprzf'{ (x for x in y if z) }')zf-string_yieldzf'{ (yield x) }')zf-string_yieldfromzf'{ (yield from x) }')zf-string_awaitzf'{ await x }')zf-string_comparezf'{ x == y }')zf-string_callzf'{ f(x,y,z) }')zf-string_attributezf'{ f.x.y.z }')zf-string_starredz
f'{ *x, }')zf-string_doublestarredzf'{ {**x} }')zf-string_escape_bracezf'{{Escape')zf-string_escape_closing_bracezf'Escape}}')zf-string_multilinez/
        f'''
        {hello}
        '''
     )zf-string_multiline_in_exprzA
        f'''
        {
        hello
        }
        '''
     )zf-string_multiline_in_callzJ
        f'''
        {f(
            a, b, c
        )}
        '''
     )globalzglobal a, b)groupz	(yield a))Zif_elifzF
        if a:
            pass
        elif b:
            pass
     )Zif_elif_elifzg
        if a:
            pass
        elif b:
            pass
        elif c:
            pass
     )Zif_elif_elsezd
        if a:
            pass
        elif b:
            pass
        else:
           pass
     )Zif_elsezD
        if a:
            pass
        else:
            pass
     )Z	if_simplez
if a: pass)importzimport a)Zimport_aliaszimport a as b)Zimport_dottedz
import a.b)Zimport_dotted_aliaszimport a.b as c)Zimport_dotted_multicharzimport ab.cd)Zimport_fromzfrom a import b)Zimport_from_aliaszfrom a import b as c)Zimport_from_dottedzfrom a.b import c)Zimport_from_dotted_aliaszfrom a.b import c as d)Zimport_from_multiple_aliaseszfrom a import b as c, d as e)Zimport_from_one_dotzfrom .a import b)Zimport_from_one_dot_aliaszfrom .a import b as c)Zimport_from_starzfrom a import *)Zimport_from_three_dotszfrom ...a import b)Zimport_from_trailing_commazfrom a import (b,))kwargz+
        def f(**a):
            pass
     )Zkwonly_argsz/
        def f(*, a, b):
            pass
     )Zkwonly_args_with_defaultz1
        def f(*, a=2, b):
            pass
     )Zlambda_kwargzlambda **a: 42)Zlambda_kwonly_argszlambda *, a, b: 42)Zlambda_kwonly_args_with_defaultzlambda *, a=2, b: 42)Zlambda_mixed_argszlambda a, /, b, *, c: 42)Zlambda_mixed_args_with_defaultz%lambda a, b=2, /, c=3, *e, f, **g: 42)Zlambda_no_argsz
lambda: 42)Zlambda_pos_argszlambda a,b: 42)Zlambda_pos_args_with_defaultzlambda a, b=2: 42)Zlambda_pos_only_argszlambda a, /: 42)Z!lambda_pos_only_args_with_defaultzlambda a=0, /: 42)Zlambda_pos_posonly_argszlambda a, b, /, c, d: 42)Z$lambda_pos_posonly_args_with_defaultzlambda a, b=0, /, c=2: 42)Zlambda_varargzlambda *a: 42)Zlambda_vararg_kwonly_argszlambda *a, b: 42)listz	[1, 2, a])Z	list_compz[i for i in a])Zlist_comp_ifz[i for i in a if b])Zlist_trailing_commaz[1+2, a, 3+4,])Z
mixed_argsz5
        def f(a, /, b, *, c):
            pass
     )Zmixed_args_with_defaultzB
        def f(a, b=2, /, c=3, *e, f, **g):
            pass
     )Zmultipart_string_byteszb"Hola" b"Hello" b"Bye")Zmultipart_string_triplez"""Something here""" "and now")Z#multipart_string_different_prefixesz(u"Something" "Other thing" r"last thing")Zmultiple_assignmentszx = y = z = 42)Zmultiple_assignments_with_yieldzx = y = z = yield 42)Zmultiple_passz&
        pass; pass
        pass
     )Z	namedexprz(x := [1, 2, 3]))Znamedexpr_falsez(x := False))Znamedexpr_nonez(x := None))Znamedexpr_truez(x := True))nonlocalznonlocal a, b)Znumber_complexz	-2.234+1j)Znumber_floatz-34.2333)Znumber_imaginary_literalz1.1234j)Znumber_integerz-234)Znumber_underscoresZ	1_234_567)passr   )Zpos_argsz,
        def f(a, b):
            pass
     )Zpos_args_with_defaultz.
        def f(a, b=2):
            pass
     )Zpos_only_argsz,
        def f(a, /):
            pass
     )Zpos_only_args_with_defaultz.
        def f(a=0, /):
            pass
     )Zpos_posonly_argsz5
        def f(a, b, /, c, d):
            pass
     )Zpos_posonly_args_with_defaultz6
        def f(a, b=0, /, c=2):
            pass
     )Zprimary_mixedza.b.c().d[0])raiser   )Zraise_ellipsisz	raise ...)Z
raise_exprzraise a)Z
raise_fromzraise a from b)returnr   )Zreturn_exprzreturn a)setz{1, 2+4, 3+5})Zset_compz{i for i in a})Zset_trailing_commaz
{1, 2, 3,})Zsimple_assignmentzx = 42)Zsimple_assignment_with_yieldzx = yield 42)Zstring_byteszb"hello")Zstring_concatenation_byteszb"hello" b"world")Zstring_concatenation_simplez"abcd" "efgh")Zstring_format_simplezf"hello")Z"string_format_with_formatted_valuezf"hello {world}")Zstring_simplez"hello")Zstring_unicodezu"hello")Zsubscript_attributeza[0].b)Zsubscript_callza[b]())Zsubscript_multiple_slicesza[0:a:2, 1])Zsubscript_simpleza[0])Zsubscript_single_element_tupleza[0,])Zsubscript_trailing_commaza[0, 1, 2,])Zsubscript_tuplez
a[0, 1, 2])Zsubscript_whole_slicez
a[0+1:b:c])Z
try_exceptzE
        try:
            pass
        except:
            pass
     )Ztry_except_elsezd
        try:
            pass
        except:
            pass
        else:
            pass
     )Ztry_except_else_finallyz
        try:
            pass
        except:
            pass
        else:
            pass
        finally:
            pass
     )Ztry_except_exprzG
        try:
            pass
        except a:
            pass
     )Ztry_except_expr_targetzL
        try:
            pass
        except a as b:
            pass
     )Ztry_except_finallyzg
        try:
            pass
        except:
            pass
        finally:
            pass
     )Ztry_finallyzF
        try:
            pass
        finally:
            pass
     )Zunpacking_binopz[*([1, 2, 3] + [3, 4, 5])])Zunpacking_callz[*b()])Zunpacking_comparez
[*(x < y)])Zunpacking_constantz[*3])Zunpacking_dictz[*{1: 2, 3: 4}])Zunpacking_dict_comprehensionz[*{x:y for x,y in z}])Zunpacking_ifexprz[*([1, 2, 3] if x else y)])Zunpacking_listz
[*[1,2,3]])Zunpacking_list_comprehensionz[*[x for x in y]])Zunpacking_namedexprz[*(x:=[1, 2, 3])])Zunpacking_setz
[*{1,2,3}])Zunpacking_set_comprehensionz[*{x for x in y}])Zunpacking_stringz[*"myvalue"])Zunpacking_tuplez
[*(1,2,3)])Zunpacking_unaryopz[*(not [1, 2, 3])])Zunpacking_yieldz[*(yield 42)])Zunpacking_yieldfromz[*(yield from x)])tuplez	(1, 2, 3))varargz*
        def f(*a):
            pass
     )Zvararg_kwonly_argsz-
        def f(*a, b):
            pass
     )whilez(
        while a:
            pass
     )Z
while_elsezG
        while a:
            pass
        else:
             pass
    )withz'
        with a:
            pass
     )Zwith_asz,
        with a as b:
            pass
     )Zwith_as_parenz.
        with a as (b):
            pass
     )Zwith_as_emptyzwith a as (): pass)Zwith_list_recursivez6
        with a as [x, [y, z]]:
            pass
     )Zwith_tuple_recursivez6
        with a as ((x, y), z):
            pass
     )Zwith_tuple_targetz1
        with a as (x, y):
            pass
     )Zwith_list_targetz1
        with a as [x, y]:
            pass
     )yieldr   )Z
yield_exprzyield a)Z
yield_fromzyield from a)#)Zannotation_multiple_targetsz(a, b): int = 42)Zannotation_nested_tuplez((a, b)): int)Zannotation_listz[a]: int)Zannotation_lambdazlambda: int = 42)Zannotation_tuplez	(a,): int)Zannotation_tuple_without_parenza,: int)Zassignment_keywordza = if)Zaugmented_assignment_listz[a, b] += 1)Zaugmented_assignment_tuplez	a, b += 1)Z augmented_assignment_tuple_parenz(a, b) += (1, 2))Zcomprehension_lambdaz(a for a in lambda: b))Zcomprehension_elsez(a for a in b if c else d)Zdel_callzdel a())Zdel_call_genexpzdel a(i for i in b))Zdel_subscript_callz
del a[b]())Zdel_attribute_callz	del a.b())Zdel_mixed_callzdel a[0].b().c.d())Zfor_star_targets_callzfor a() in b: pass)Zfor_star_targets_subscript_callzfor a[b]() in c: pass)Zfor_star_targets_attribute_callzfor a.b() in c: pass)Zfor_star_targets_mixed_callzfor a[0].b().c.d() in e: pass)Zfor_star_targets_inzfor a, in in b: pass)zf-string_assignmentzf'{x = 42}')zf-string_emptyzf'{}')zf-string_function_defzf'{def f(): pass}')r   zf'{lambda x: 42}')zf-string_singe_bracezf'{')zf-string_single_closing_bracezf'}')Zfrom_import_invalidzfrom import import a)Zfrom_import_trailing_commazfrom a import b,)Zimport_non_ascii_syntax_erroru   import ä £)zinvalid indentationz.
     def f():
         a
             a
     )Znot_terminated_stringza = 'example)Ztry_except_attribute_targetzJ
     try:
         pass
     except Exception as a.b:
         pass
     )Ztry_except_subscript_targetzK
     try:
         pass
     except Exception as a[0]:
         pass
     ))zf(x, y, z=1, **b, *az>iterable argument unpacking follows keyword argument unpacking)zf(x, y=1, *z, **a, bz6positional argument follows keyword argument unpacking)zf(x, y, z=1, a=2, bz,positional argument follows keyword argument)zTrue = 1zcannot assign to True)za() = 1zcannot assign to function call)z(a, b): intz/only single target (not tuple) can be annotated)z[a, b]: intz.only single target (not list) can be annotated)za(): intzillegal target for annotation)z1 += 1z;'literal' is an illegal expression for augmented assignment)zpass
    passzunexpected indent)zdef f():
passzexpected an indented block)zdef f(*): pass"named arguments must follow bare *)zdef f(*,): passr   )zdef f(*, **a): passr   )zlambda *: passr   )zlambda *,: passr   )zlambda *, **a: passr   )zf(g()=2=expression cannot contain assignment, perhaps you meant "=="?)zf(a, b, *c, d.e=2r   )zf(*a, **b, c=0, d[1]=3)r   ))Zstring_concatenation_formatz%f"{hello} world" f"again {and_again}")Zstring_concatenation_multiplez5
        f"hello" f"{world} again" f"and_again"
     )zf-string_multiline_compzI
        f'''
        {(i for i in a
            if b)}
        '''
     )z<
            f'''
            {a$b}
            '''
        z(a$b)
)z<
            f'''
            {a$b
            }'''
        z(a$b
)z<
            f'''
            {
            a$b}'''
        za$b)
)Z'multiline_fstrings_same_line_with_braceZ$multiline_fstring_brace_on_next_lineZ(multiline_fstring_brace_on_previous_line))Zexpression_addz1+1)Zexpression_add_2za+b)Zexpression_callzf(a, b=2, **kw))Zexpression_tuplez1, 2, 3)Zexpression_tuple_one_valuez1,)sourcer   c                 C   s@   t | trt| }n(t | ttfs.d| }ntd|  |S )N
zInvalid type for test source: )
isinstancestrr   r   r   join	TypeError)r    result r'   */usr/lib/python3.9/test/test_peg_parser.pycleanup_source  s    

r)   )
test_casesr   c                 C   s>   t |  \}}t|}t|D ]\}}t|}|||< q||fS N)zipr   	enumerater)   )r*   Ztest_idsZ_test_sourcesZtest_sourcesindexr    r&   r'   r'   r(   prepare_test_cases  s    
r/   c                   @   s   e Zd ZddddZddddZddddZejddd	d
ZddddZ	ddddZ
ddddZddddZdS )ASTGenerationTestN)r   c                 C   sT   d | _ tD ]D}t|}tj|dd}| tj|ddtj|ddd|  q
d S NT	oldparserZinclude_attributes!Wrong AST generation for source: )ZmaxDiffTEST_SOURCES
peg_parserparse_stringassertEqualastdumpselfr    Z
actual_astZexpected_astr'   r'   r(   +test_correct_ast_generation_on_source_files  s    
z=ASTGenerationTest.test_correct_ast_generation_on_source_filesc              	   C   sN   t D ]D}| jtd| dd t| W d    q1 s>0    Y  qd S )NzParsing z did not raise an exception)msg)FAIL_SOURCESassertRaisesSyntaxErrorr7   r8   )r=   r    r'   r'   r(   -test_incorrect_ast_generation_on_source_files  s    z?ASTGenerationTest.test_incorrect_ast_generation_on_source_filesc              	   C   sp   t D ]f\}}d|v rtnt}| |}t| W d    n1 sF0    Y  | ||jjv d|  qd S )Nindentz1Actual error message does not match expexted for )	FAIL_SPECIALIZED_MESSAGE_CASESIndentationErrorrB   rA   r7   r8   Z
assertTrue	exceptionr?   )r=   r    
error_textexcser'   r'   r(   5test_incorrect_ast_generation_with_specialized_errors  s    (
zGASTGenerationTest.test_incorrect_ast_generation_with_specialized_errorsc                 C   sN   t D ]D}t|}tj|dd}| tj|ddtj|ddd|  qd S r1   GOOD_BUT_FAIL_SOURCESr7   r8   r9   r:   r;   r<   r'   r'   r(   =test_correct_but_known_to_fail_ast_generation_on_source_files  s    
zOASTGenerationTest.test_correct_but_known_to_fail_ast_generation_on_source_filesc                 C   sF   t D ]<}t|}tj|dd}| t|t|d|  qd S )NTr2   r5   rL   r<   r'   r'   r(   ,test_correct_ast_generation_without_pos_info  s    
z>ASTGenerationTest.test_correct_ast_generation_without_pos_infoc              	   C   s^   t  D ]P\}}| t}tt| W d    n1 s>0    Y  | ||jj	 qd S r+   )
FSTRINGS_TRACEBACKSvaluesrA   rB   r7   r8   r   r9   rG   text)r=   r    rH   rJ   r'   r'   r(   #test_fstring_parse_error_tracebacks  s    ,z5ASTGenerationTest.test_fstring_parse_error_tracebacksc                 C   sT   t D ]J}tj|dd}tj|ddd}| tj|ddtj|ddd|  qd S )Neval)modeT)rU   r3   r4   r5   )EXPRESSIONS_TEST_SOURCESr7   r8   r9   r:   r;   r<   r'   r'   r(   !test_correct_ast_generatrion_eval  s    z3ASTGenerationTest.test_correct_ast_generatrion_evalc                 C   sJ   d}|  td& t|d d|   W d    n1 s<0    Y  d S )N   ztoo many nested parentheses())ZassertRaisesRegexrB   r7   r8   )r=   nr'   r'   r(   $test_tokenizer_errors_are_propagated  s    z6ASTGenerationTest.test_tokenizer_errors_are_propagated)__name__
__module____qualname__r>   rC   rK   unittestZexpectedFailurerN   rO   rS   rW   r\   r'   r'   r'   r(   r0     s   



r0   ) r:   Z_peg_parserr7   r`   typingr   r   r   r   textwrapr   testr   Z
TEST_CASESZFAIL_TEST_CASESrE   ZGOOD_BUT_FAIL_TEST_CASESrP   ZEXPRESSIONS_TEST_CASESr#   r)   r/   ZTEST_IDSr6   ZGOOD_BUT_FAIL_TEST_IDSrM   ZFAIL_TEST_IDSr@   ZEXPRESSIONS_TEST_IDSrV   ZTestCaser0   r'   r'   r'   r(   <module>   s>       09	