Skip to content

Show the structure of a tree-sitter tree as an S-expression.

Available tree-sitter parsers

This is the manual page of the ts_tree_sexpr() S3 generic function. Methods in parser packages may override this generic. For the ones that do see the links to their manual pages in the table.

PackageVersionTitleMethod
tsjsonc0.0.0.9000Edit JSON Files.
tstoml0.0.0.9000Edit TOML files.

Usage

ts_tree_sexpr(tree)

Arguments

tree

A ts_tree object.

Value

A string representing the S-expression of the syntax tree.

Details

This function returns a nested list representation of the syntax tree, where each node is represented as a list with its type and children.

Examples

# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc(
  "{ \"a\": //comment\ntrue, \"b\": [1, 2, 3] }"
)

ts_tree_sexpr(tree)
#> [1] "(document (object (pair key: (string (string_content)) (comment) value: (true)) (pair key: (string (string_content)) value: (array (number) (number) (number)))))"

# Create a parse tree with tstoml --------------------------------------
tree <- tstoml::ts_parse_toml(r"(
  [servers]
  alpha = { ip = "127.0.0.1", dc = "eqdc10" }
  beta = { ip = "127.0.0.2", dc = "eqdc20" }
)")

ts_tree_sexpr(tree)
#> [1] "(document (table (bare_key) (pair (bare_key) (inline_table (pair (bare_key) (string (basic_string (basic_string_content)))) (pair (bare_key) (string (basic_string (basic_string_content)))))) (pair (bare_key) (inline_table (pair (bare_key) (string (basic_string (basic_string_content)))) (pair (bare_key) (string (basic_string (basic_string_content))))))))"