3
/*^2                 @   s  d Z ddlmZ ddlmZmZ ddlmZ ddl	m
Z
 G dd de
ZG dd	 d	eeZed
kre Zej  ddlmZmZ edkrej \ZZZZZddlmZ eeeZeejjjedj  Z!eee! Z"ddl#Z#edd e"D Z"edd e"D Z"ee"e! Z"e$e" dS )z;
spark grammar differences over Python 3.4 for Python 3.5.
    )print_function)PythonParserSinglenop_func)DEFAULT_DEBUG)Python34Parserc                   s@   e Zd Zef fdd	Zdd Z fddZ fddZ  ZS )	Python35Parserc                s   t t| j| i | _d S )N)superr   __init__Z
customized)selfZdebug_parser)	__class__ W/home/psgendb/BIRCHDEV/python/lib/python3.6/site-packages/uncompyle6/parsers/parse35.pyr	      s    zPython35Parser.__init__c             C   s   dS )a4  

        # FIXME! isolate this to only loops!
        _ifstmts_jump  ::= c_stmts_opt come_froms
        ifelsestmt ::= testexpr c_stmts_opt jump_forward_else else_suite _come_froms

        pb_ja ::= POP_BLOCK JUMP_ABSOLUTE

        # The number of canned instructions in new statements is mind boggling.
        # I'm sure by the time Python 4 comes around these will be turned
        # into special opcodes

        while1stmt     ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK
                           POP_BLOCK COME_FROM_LOOP
        while1stmt     ::= SETUP_LOOP l_stmts POP_BLOCK COME_FROM_LOOP
        while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK
                           POP_BLOCK else_suite COME_FROM_LOOP

        # The following rule is for Python 3.5+ where we can have stuff like
        # while ..
        #     if
        #     ...
        # the end of the if will jump back to the loop and there will be a COME_FROM
        # after the jump
        l_stmts ::= lastl_stmt come_froms l_stmts

        # Python 3.5+ Await statement
        expr       ::= await_expr
        await_expr ::= expr GET_AWAITABLE LOAD_CONST YIELD_FROM

        stmt       ::= await_stmt
        await_stmt ::= await_expr POP_TOP

        # Python 3.5+ has WITH_CLEANUP_START/FINISH

        with       ::= expr
                       SETUP_WITH POP_TOP suite_stmts_opt
                       POP_BLOCK LOAD_CONST COME_FROM_WITH
                       WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY

        withasstmt ::= expr
                       SETUP_WITH store suite_stmts_opt
                       POP_BLOCK LOAD_CONST COME_FROM_WITH
                       WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY

        # Python 3.5+ async additions
        stmt               ::= async_for_stmt
        async_for_stmt     ::= SETUP_LOOP expr
                               GET_AITER
                               LOAD_CONST YIELD_FROM SETUP_EXCEPT GET_ANEXT LOAD_CONST
                               YIELD_FROM
                               store
                               POP_BLOCK jump_except COME_FROM_EXCEPT DUP_TOP
                               LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_FALSE
                               POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
                               JUMP_ABSOLUTE END_FINALLY COME_FROM
                               for_block POP_BLOCK JUMP_ABSOLUTE
                               COME_FROM_LOOP

        async_for_stmt     ::= SETUP_LOOP expr
                               GET_AITER
                               LOAD_CONST YIELD_FROM SETUP_EXCEPT GET_ANEXT LOAD_CONST
                               YIELD_FROM
                               store
                               POP_BLOCK jump_except COME_FROM_EXCEPT DUP_TOP
                               LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_FALSE
                               POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
                               JUMP_ABSOLUTE END_FINALLY JUMP_BACK
                               pass POP_BLOCK JUMP_ABSOLUTE
                               COME_FROM_LOOP

        stmt               ::= async_forelse_stmt
        async_forelse_stmt ::= SETUP_LOOP expr
                               GET_AITER
                               LOAD_CONST YIELD_FROM SETUP_EXCEPT GET_ANEXT LOAD_CONST
                               YIELD_FROM
                               store
                               POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT DUP_TOP
                               LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_FALSE
                               POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
                               JUMP_ABSOLUTE END_FINALLY COME_FROM
                               for_block pb_ja
                               else_suite COME_FROM_LOOP


        inplace_op       ::= INPLACE_MATRIX_MULTIPLY
        binary_operator  ::= BINARY_MATRIX_MULTIPLY

        # Python 3.5+ does jump optimization
        # In <.3.5 the below is a JUMP_FORWARD to a JUMP_ABSOLUTE.

        return_if_stmt ::= ret_expr RETURN_END_IF POP_BLOCK
        return_if_lambda   ::= RETURN_END_IF_LAMBDA COME_FROM

        jb_else     ::= JUMP_BACK ELSE
        ifelsestmtc ::= testexpr c_stmts_opt JUMP_FORWARD else_suitec
        ifelsestmtl ::= testexpr c_stmts_opt jb_else else_suitel

        # 3.5 Has jump optimization which can route the end of an
        # "if/then" back to to a loop just before an else.
        jump_absolute_else ::= jb_else
        jump_absolute_else ::= CONTINUE ELSE

        # Our hacky "ELSE" determination doesn't do a good job and really
        # determine the start of an "else". It could also be the end of an
        # "if-then" which ends in a "continue". Perhaps with real control-flow
        # analysis we'll sort this out. Or call "ELSE" something more appropriate.
        _ifstmts_jump ::= c_stmts_opt ELSE

        # ifstmt ::= testexpr c_stmts_opt

        iflaststmt ::= testexpr c_stmts_opt JUMP_FORWARD

        # Python 3.3+ also has yield from. 3.5 does it
        # differently than 3.3, 3.4

        yield_from ::= expr GET_YIELD_FROM_ITER LOAD_CONST YIELD_FROM
        Nr   )r
   argsr   r   r   p_35on   s    zPython35Parser.p_35onc                s@  | j d tt| j|| xt|D ]\}}|j}|dkrZd|krXd}| j|| q(|dkr| jdk r| jdt	 |j
d }d	| }|d
 d|  }	| j|	t	 d||f }	| j|	t	 ||d  }
d|
j }	| j|	t	 q(|dko| jdk rd}| j|t	 q(|dkr | jdt	 q(|dkr(d}| j|t	 q(W d S )Na  
          yield_from ::= expr GET_ITER LOAD_CONST YIELD_FROM
          yield_from ::= expr expr YIELD_FROM
          with       ::= expr SETUP_WITH POP_TOP suite_stmts_opt
                         POP_BLOCK LOAD_CONST COME_FROM_WITH
                         WITH_CLEANUP END_FINALLY
          withasstmt ::= expr SETUP_WITH store suite_stmts_opt
                         POP_BLOCK LOAD_CONST COME_FROM_WITH
                         WITH_CLEANUP END_FINALLY
        ZLOAD_ASSERTPyPyzT
                    stmt ::= JUMP_IF_NOT_DEBUG stmts COME_FROM
                    BUILD_MAP_UNPACK_WITH_CALLg@zexpr ::= unmapexpr   zmap_unpack_%sz ::= zexpr zunmapexpr ::=  %s %s   zcall ::= expr unmapexpr BEFORE_ASYNC_WITHgffffff@a  
                   async_with_stmt    ::= expr
                   stmt               ::= async_with_stmt
                   async_with_pre     ::= BEFORE_ASYNC_WITH GET_AWAITABLE LOAD_CONST YIELD_FROM SETUP_ASYNC_WITH
                   async_with_post    ::= COME_FROM_ASYNC_WITH
                                          WITH_CLEANUP_START GET_AWAITABLE LOAD_CONST YIELD_FROM
                                          WITH_CLEANUP_FINISH END_FINALLY

                   async_with_stmt    ::= expr
                                          async_with_pre
                                          POP_TOP
                                          suite_stmts_opt
                                          POP_BLOCK LOAD_CONST
                                          async_with_post
                   async_with_stmt    ::= expr
                                          async_with_pre
                                          POP_TOP
                                          suite_stmts_opt
                                          async_with_post

                   stmt               ::= async_with_as_stmt

                   async_with_as_stmt ::= expr
                                          async_with_pre
                                          store
                                          suite_stmts_opt
                                          POP_BLOCK LOAD_CONST
                                          async_with_post
                BUILD_MAP_UNPACKz~
                   expr       ::= unmap_dict
                   unmap_dict ::= dict_comp BUILD_MAP_UNPACK
                   
SETUP_WITHa  
                  with ::= expr
                           SETUP_WITH POP_TOP suite_stmts_opt
                           POP_BLOCK LOAD_CONST COME_FROM_WITH
                           WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY

                  withasstmt ::= expr
                       SETUP_WITH store suite_stmts_opt
                       POP_BLOCK LOAD_CONST COME_FROM_WITH
                       WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
                )Zremove_rulesr   r   customize_grammar_rules	enumeratekindZadd_unique_doc_rulesversionZaddRuler   attr)r
   tokens	customizeitokenopnameZ	rules_strnargsZmap_unpack_nruleZ
call_token)r   r   r   r      s@    	




z&Python35Parser.customize_grammar_rulesc                s   | j |\}}t|td d }|| }tdj| jrdd|  d|  d|  |j d	 }	| j|	|j|| | jd
|j|| |jdr| j||_|j	drd}
nd}
dd|  d|  |
 |j }	| j|	|j|| nt
t| j|||f|  d S )NCALL_FUNCTION   GET_AWAITABLE
YIELD_FROMzasync_call ::= expr zpos_arg zkwarg zexpr z$ GET_AWAITABLE LOAD_CONST YIELD_FROMzexpr ::= async_callZCALL_FUNCTION_VARZKW zcall ::= expr expr )r%   r&   )Z
get_pos_kwlen	frozensetissubsetZseen_opsr   Zadd_unique_rule
startswithZcall_fn_nameendswithr   r   custom_classfunc_rule)r
   r    r   r   r   Zargs_posZargs_kwZnakZ
uniq_paramr"   kw)r   r   r   r-      s      

z$Python35Parser.custom_classfunc_rule)	__name__
__module____qualname__PARSER_DEFAULT_DEBUGr	   r   r   r-   __classcell__r   r   )r   r   r      s   xYr   c               @   s   e Zd ZdS )Python35ParserSingleN)r/   r0   r1   r   r   r   r   r4     s   r4   __main__)PYTHON_VERSIONIS_PYPYg      @)get_scannerzJUMP_BACK CONTINUE RETURN_END_IF COME_FROM
               LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
               LAMBDA_MARKER RETURN_LAST
            Nc             C   s   g | ]}t jd d|qS )z_\d+$r'   )resub).0tr   r   r   
<listcomp>!  s    r=   c             C   s   g | ]}t jd d|qS )z_CONT$r'   )r9   r:   )r;   r<   r   r   r   r=   "  s    )%__doc__
__future__r   Zuncompyle6.parserr   r   Zspark_parserr   r2   Zuncompyle6.parsers.parse34r   r   r4   r/   pZcheck_grammar
uncompyle6r6   r7   Z
check_setslhsrhsr   Zright_recursiveZdup_rhsZuncompyle6.scannerr8   ssetZopcr    unionsplitZ
opcode_setZremain_tokensr9   printr   r   r   r   <module>   s0     


