ts_tree_ast() prints the annotated syntax tree of a ts_tree object.
This syntax tree contains all tree-sitter nodes, and it shows the
source code associated with each node, along with line numbers.
Available tree-sitter parsers
This is the manual page of the ts_tree_ast() 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.
| Package | Version | Title | Method |
| tsjsonc | 0.0.0.9000 | Edit JSON Files. | |
| tstoml | 0.0.0.9000 | Edit TOML files. |
Value
Character vector, the formatted annotated syntax tree, line by line. It has class cli_tree, from the cli package. It may contain ANSI escape sequences for coloring and hyperlinks.
Details
The syntax tree and the DOM tree
This syntax tree contains all nodes of the tree-sitter parse tree, including both named and unnamed nodes and comments. E.g. for a JSON(C) document it includes the pairs, brackets, braces, commas, colons, double quotes and string escape sequences as separate nodes.
See ts::ts_tree_dom() for a tree that
shows the semantic structure of the parsed document, which may be
different from the syntax tree.
tree <- ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")ts_tree_ast(tree)
#> document (1) 1|
#> └─object (2) |
#> ├─{ (3) |{
#> ├─pair (4) |
#> │ ├─string (5) |
#> │ │ ├─" (6) | "
#> │ │ ├─string_content (7) | a
#> │ │ └─" (8) | "
#> │ ├─: (9) | :
#> │ └─true (10) | true
#> ├─, (11) | ,
#> ├─pair (12) |
#> │ ├─string (13) |
#> │ │ ├─" (14) | "
#> │ │ ├─string_content (15) | b
#> │ │ └─" (16) | "
#> │ ├─: (17) | :
#> │ └─array (18) |
#> │ ├─[ (19) | [
#> │ ├─number (20) | 1
#> │ ├─, (21) | ,
#> │ ├─number (22) | 2
#> │ ├─, (23) | ,
#> │ ├─number (24) | 3
#> │ └─] (25) | ]
#> └─} (26) | }
ts_tree_dom(tree)#> document (1) #> └─object (2) #> ├─true (10) # a #> └─array (18) # b #> ├─number (20) #> ├─number (22) #> └─number (24)
See also
ts_tree_dom() to show the document object model (DOM) of a
ts_tree object.
Other ts_tree exploration:
ts_tree-brackets,
ts_tree_dom(),
ts_tree_query(),
ts_tree_sexpr()
Other ts_tree generics:
[[.ts_tree(),
[[<-.ts_tree(),
format.ts_tree(),
print.ts_tree(),
select-set,
ts_tree_delete(),
ts_tree_dom(),
ts_tree_format(),
ts_tree_insert(),
ts_tree_new(),
ts_tree_query(),
ts_tree_select(),
ts_tree_sexpr(),
ts_tree_unserialize(),
ts_tree_update(),
ts_tree_write()
Examples
# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{"foo": 42, "bar": [1, 2, 3]}')
tree
#> # jsonc (1 line)
#> 1 | {"foo": 42, "bar": [1, 2, 3]}
ts_tree_ast(tree)
#> document (1) 1|
#> └─object (2) |
#> ├─{ (3) |{
#> ├─pair (4) |
#> │ ├─string (5) |
#> │ │ ├─" (6) | "
#> │ │ ├─string_content (7) | foo
#> │ │ └─" (8) | "
#> │ ├─: (9) | :
#> │ └─number (10) | 42
#> ├─, (11) | ,
#> ├─pair (12) |
#> │ ├─string (13) |
#> │ │ ├─" (14) | "
#> │ │ ├─string_content (15) | bar
#> │ │ └─" (16) | "
#> │ ├─: (17) | :
#> │ └─array (18) |
#> │ ├─[ (19) | [
#> │ ├─number (20) | 1
#> │ ├─, (21) | ,
#> │ ├─number (22) | 2
#> │ ├─, (23) | ,
#> │ ├─number (24) | 3
#> │ └─] (25) | ]
#> └─} (26) | }
ts_tree_dom(tree)
#> document (1)
#> └─object (2)
#> ├─number (10) # foo
#> └─array (18) # bar
#> ├─number (20)
#> ├─number (22)
#> └─number (24)
# Create a parse tree with tstoml --------------------------------------
tree <- tstoml::ts_parse_toml(r"(
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00
)")
tree
#> # toml (5 lines)
#> 1 |
#> 2 | title = "TOML Example"
#> 3 | [owner]
#> 4 | name = "Tom Preston-Werner"
#> 5 | dob = 1979-05-27T07:32:00-08:00
ts_tree_ast(tree)
#> document (1) 1|
#> ├─pair (2) 2|
#> │ ├─bare_key (3) | title
#> │ ├─= (4) | =
#> │ └─string (5) |
#> │ └─basic_string (6) |
#> │ ├─" (7) | "
#> │ ├─basic_string_content (8) | TOML Example
#> │ └─" (9) | "
#> └─table (10) 3|
#> ├─[ (11) | [
#> ├─bare_key (12) | owner
#> ├─] (13) | ]
#> ├─pair (14) 4|
#> │ ├─bare_key (15) | name
#> │ ├─= (16) | =
#> │ └─string (17) |
#> │ └─basic_string (18) |
#> │ ├─" (19) | "
#> │ ├─basic_string_content (20) | Tom Preston-Werner
#> │ └─" (21) | "
#> └─pair (22) 5|
#> ├─bare_key (23) | dob
#> ├─= (24) | =
#> └─offset_date_time (25) | 1979-05-27T07:32:00-08:00
ts_tree_dom(tree)
#> document (1)
#> ├─value (5) # title
#> └─table (10) # owner
#> ├─value (17) # name
#> └─value (25) # dob