Skip to contents

Walk Along a Parse Tree

Usage

walk_tree(
  x,
  path = "",
  depth = 0L,
  max_depth = 20L,
  dbg = TRUE,
  config = list(),
  context = NULL
)

Arguments

x

parse tree as returned by parse or a sub-tree of the parse tree

path

for internal use only. Path to the element in the parse tree.

depth

for internal use only. Recursion depth.

max_depth

maximum recursion level. Default: 20L

dbg

whether or not to show debug messages

config

list defining modifications of nodes in the node tree. TODO: describe further

context

if not NULL (the default) this is expected to be a list containing additional data. Currently list element "file" is used to pass the name of the script that the current tree was read from.

Examples

walk_tree(parse(text = "x <- 1:n"))
#> path:
#> [1] ""
#> props:
#>   is_atomic is_recursive is_call       type       mode      class length
#> 1     FALSE         TRUE   FALSE expression expression expression      1
#>                   text
#> 1 expression(x <- 1:n)
#> path:
#> [1] "/1"
#> props:
#>   is_atomic is_recursive is_call     type mode class length     text
#> 1     FALSE         TRUE    TRUE language call    <-      3 x <- 1:n
#> path:
#> [1] "/1/1"
#> props:
#>   is_atomic is_recursive is_call   type mode class length text
#> 1     FALSE        FALSE   FALSE symbol name  name      1   <-
#> Leaf reached: '<-'
#> path:
#> [1] "/1/2"
#> props:
#>   is_atomic is_recursive is_call   type mode class length text
#> 1     FALSE        FALSE   FALSE symbol name  name      1    x
#> Leaf reached: 'x'
#> path:
#> [1] "/1/3"
#> props:
#>   is_atomic is_recursive is_call     type mode class length text
#> 1     FALSE         TRUE    TRUE language call  call      3  1:n
#> path:
#> [1] "/1/3/1"
#> props:
#>   is_atomic is_recursive is_call   type mode class length text
#> 1     FALSE        FALSE   FALSE symbol name  name      1    :
#> Leaf reached: ':'
#> path:
#> [1] "/1/3/2"
#> props:
#>   is_atomic is_recursive is_call   type    mode   class length text
#> 1      TRUE        FALSE   FALSE double numeric numeric      1    1
#> Leaf reached: '1'
#> path:
#> [1] "/1/3/3"
#> props:
#>   is_atomic is_recursive is_call   type mode class length text
#> 1     FALSE        FALSE   FALSE symbol name  name      1    n
#> Leaf reached: 'n'
#> expression(x <- 1:n)