Skip to content

Commit 9de3379

Browse files
committed
copy_ast: Add option to not recurse on .source, clarify docs
1 parent 4d6a597 commit 9de3379

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ast.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,21 @@ end
428428

429429

430430
"""
431-
Copy AST `ex` into `ctx`
431+
Recursively copy AST `ex` into `ctx`. The resulting tree contains new nodes
432+
only, and does not contain nodes with multiple parents.
433+
434+
Special provenance handling: If `copy_source` is true, treat the `.source`
435+
attribute as a reference and recurse on its contents. Otherwise, treat it like
436+
any other attribute.
432437
"""
433-
function copy_ast(ctx, ex)
438+
function copy_ast(ctx, ex; copy_source=true)
434439
# TODO: Do we need to keep a mapping of node IDs to ensure we don't
435440
# double-copy here in the case when some tree nodes are pointed to by
436441
# multiple parents? (How much does this actually happen in practice?)
437-
s = ex.source
442+
s = get(ex, :source, nothing)
438443
# TODO: Figure out how to use provenance() here?
439-
srcref = s isa NodeId ? copy_ast(ctx, SyntaxTree(ex._graph, s)) :
444+
srcref = !copy_source ? s :
445+
s isa NodeId ? copy_ast(ctx, SyntaxTree(ex._graph, s)) :
440446
s isa Tuple ? map(i->copy_ast(ctx, SyntaxTree(ex._graph, i)), s) :
441447
s
442448
if !is_leaf(ex)

0 commit comments

Comments
 (0)