PL idZddlZddlmZddlmZGddZGddZGd d eZGd d ZGd deZ y)zjutilities for analyzing expressions and blocks of Python code, as well as generating Python from AST nodesN) exceptions)pyparserceZdZdZdZy) PythonCodez>$++-L;KLDD  $ $T >-= >  N__name__ __module__ __qualname____doc__rrrrrs FrrceZdZdZdZy) ArgumentListzBparses a fragment of code as a comma-separated list of expressionsc jg|_g|_t|_t|_t |t rItjd|rtjd|s|dz }tj|dfi|}n|}tj|tfi|}|j|y)Nz\Sz,\s*$,r)codeargsargsr r r r rrematchrr FindTuplerrrs rrzArgumentList.__init__6s  $'E!&)e# dC xxt$RXXh-E >>$C2BCDD   tZ D3C D  rNrr rrr"r"2s Lrr"c"eZdZdZfdZxZS)PythonFragmentzextends PythonCode to provide identifier lookups in partial control statements e.g.:: for x in 5: elif y==9: except (MyException, e): c tjd|jtj}|st j d|zfi||j dr|d|jd}|j dd\}}|dvr|dz}nJ|dk(r|d z}n?|d vr d |zdz}n2|d k(r d |zdz}n$|dk(r|dz}nt j d|zfi|t|$|fi|y)Nz^(\w+)(?:\s+(.*?))?:\s*(#|$)z0Fragment '%s' is not a partial control statement)forifwhilepasstryzpass except:pass)elifelsezif False:pass exceptz try:pass withz!Unsupported control keyword: '%s') r'r(stripSrCompileExceptiongroupstartsuperr)rr rmkeywordr __class__s rrzPythonFragment.__init__Us HH4djjlBDD I--BTI"  771: !''!*%D''!Q-$ , ,&=D  --D ( ($t+f4D  $&/D  &=D--3g="  2!12rrrrrr __classcell__rAs@rr+r+Hs 33rr+c0eZdZdZddZddZedZy) FunctionDeclzfunction declarationc B||_tj|dfi|}tj|fi|}|j |t |dst jd|zfi||s3|jr&t jd|jdzfi|yy)Nrfuncnamez'Code '%s' is not a function declarationz('**%s' keyword argument not allowed here) r rr ParseFuncrhasattrrr;kwargs kwargnames)rr allow_kwargsrrrs rrzFunctionDecl.__init__us ~~dF?.>?   t 8'7 8  tZ(--9D@"  --://"%&# !,|rc`g}|jddd}|jddd}|jddd}|jddd}|jr#|j d|j dz|D]}|r|j |d||r]|j d}||j |C|j |dtj|j{|j ||jr#|j d|j dz|D]`}|s|s|j ||j d}|j |dtj|jb|j|S)aEReturn the argument declarations of this FunctionDecl as a printable list. By default the return value is appropriate for writing in a ``def``; set `as_call` to true to build arguments to be passed to the function instead (assuming locals with the same names as the arguments exist). NrIz**r=*) argnamesrMdefaults kwdefaultsrLappendpoprExpressionGeneratorvaluevarargsreverse) ras_call namedeclsrRrMrSrTnamedefaults rget_argument_expressionsz%FunctionDecl.get_argument_expressionss ==2&__TrT* ==2&__TrT*  ;;   TJNN1$55 6 'D  D$!78$..+?$$T*$$!=!=g!F!L!L!NP   &# '( <<   S8<<?2 3 Dh  &",,q/  X99'BHHJL   rcXt|jt|jzS)N)tuplerRrM)rs r allargnameszFunctionDecl.allargnamessT]]#eDOO&<<r)rr rLrAs rrzFunctionArgs.__init__s ,t3>v>rrBrDs@rreres8??rre) rr'makorrrr"r+rFrer rrrhsP5 D,&3Z&3RQ=Q=h?<?r