3

U'ícô
ã@sZdZddlZddlmZejr8ddlZGdd„dejƒZ	Gdd„dƒZ
Gd	d
„d
e
ƒZdS)zVAPI for traversing the AST nodes. Implemented by the compiler and
meta introspection.
éNé)ÚNodec@s&eZdZeejejejdœdd„ZdS)Ú
VisitCallable)ÚnodeÚargsÚkwargsÚreturncOsdS)N©)Úselfrrrr	r	ú0/tmp/pip-build-gk9425m9/Jinja2/jinja2/visitor.pyÚ__call__szVisitCallable.__call__N)Ú__name__Ú
__module__Ú__qualname__rÚtÚAnyrr	r	r	rrsrc@sTeZdZdZeddœdd„Zeejejejdœdd„Zeejejejdœd	d
„Z	dS)ÚNodeVisitoraWalks the abstract syntax tree and call visitor functions for every
    node found.  The visitor functions may return values which will be
    forwarded by the `visit` method.

    Per default the visitor functions for the nodes are ``'visit_'`` +
    class name of the node.  So a `TryFinally` node visit function would
    be `visit_TryFinally`.  This behavior can be changed by overriding
    the `get_visitor` function.  If no visitor function exists for a node
    (return value `None`) the `generic_visit` visitor is used instead.
    zt.Optional[VisitCallable])rrcCst|dt|ƒj›dƒS)zªReturn the visitor function for this node or `None` if no visitor
        exists for this node.  In that case the generic visit function is
        used instead.
        Úvisit_N)ÚgetattrÚtyper
)r
rr	r	rÚget_visitorszNodeVisitor.get_visitor)rrrrcOs4|j|ƒ}|dk	r"||f|ž|ŽS|j|f|ž|ŽS)z
Visit a node.N)rÚ
generic_visit)r
rrrÚfr	r	rÚvisit#s
zNodeVisitor.visitcOs(x"|jƒD]}|j|f|ž|Žq
WdS)z9Called if no explicit visitor function exists for a node.N)Úiter_child_nodesr)r
rrrr	r	rr,szNodeVisitor.generic_visitN)
r
rrÚ__doc__rrrrrrr	r	r	rrs
	rc@sFeZdZdZeejejedœdd„Zeejejejedœdd„Z	dS)ÚNodeTransformeraËWalks the abstract syntax tree and allows modifications of nodes.

    The `NodeTransformer` will walk the AST and use the return value of the
    visitor functions to replace or remove the old node.  If the return
    value of the visitor function is `None` the node will be removed
    from the previous location otherwise it's replaced with the return
    value.  The return value may be the original node in which case no
    replacement takes place.
    )rrrrc	OsÈxÂ|jƒD]¶\}}t|tƒr„g}xT|D]L}t|tƒrh|j|f|ž|Ž}|dkrRq&nt|tƒsh|j|ƒq&|j|ƒq&W||dd…<q
t|tƒr
|j|f|ž|Ž}|dkr´t||ƒq
t|||ƒq
W|S)N)	Úiter_fieldsÚ
isinstanceÚlistrrÚextendÚappendÚdelattrÚsetattr)	r
rrrÚfieldÚ	old_valueÚ
new_valuesÚvalueÚnew_noder	r	rr=s&





zNodeTransformer.generic_visitcOs&|j|f|ž|Ž}t|tƒs"|gS|S)z{As transformers may return lists in some places this method
        can be used to enforce a list as return value.
        )rrr)r
rrrÚrvr	r	rÚ
visit_listSs
zNodeTransformer.visit_listN)
r
rrrrrrrÚListr*r	r	r	rr2s	r)rÚtypingrZnodesrZ
TYPE_CHECKINGZtyping_extensionsÚteZProtocolrrrr	r	r	rÚ<module>s"