ASTCopy
provides a way to copy all or part of an AST.
INTERFACEASTCopy ; IMPORT AST, ASTWalk; TYPE Closure <: Closure_public; Closure_public = OBJECT METHODS callback(n, ncopy: AST.NODE; vm: ASTWalk.VisitMode) RAISES ANY; init(): Closure; END; PROCEDURE Nodes( n: AST.NODE; ): AST.NODE RAISES {};
Copies the tree rooted at n
and returns the root of the copy.
PROCEDURE ModeNodes( n: AST.NODE; c: Closure; vm : ASTWalk.VisitModeControl; ): AST.NODE RAISES ANY;
Similar to Nodes
but supports a per-node callback.
PROCEDURE IgnoreChildren(c: Closure) RAISES {};
Suppress the copy of the children of the current node.
END ASTCopy.The
ASTCopy
interface allows an AST to be copied, with provision
for the caller to be called back at each node, using a similar
mechanism to the ASTWalk
interface. The callback can be used to copy
or share additional attributes that are not copied by the underlying
copy
method, for example attributes that cause the AST to form a
graph rather than a tree.
The Nodes
procedure provides for a simple copy of the tree rooted at
its argument, n
, without any callbacks.
The ModeNodes
takes a closure argument that should be created with a
call of NEW(Closure, callback := YourCallback).init()
. The closure can be
subtyped to provide for shared state to be accessed during the
callbacks. The vm
argument can be used to control when the callback
are made in the way described in the ASTWalk
interface. On an
{\em entry} callback to a node, the value of the ncopy
will be NIL
.
On an {\em exit} callback, ncopy
will refer to the newly created copy.
The IgnoreChildren
procedure can be called to suppress the copying
of a node's children. Once control leaves the node from which the call
was made, the suppression is disabled. It is only effective when
called in {\em entry} mode.