This is the main function to create a tree-sitter parse tree, using a
ts parser implemented in another package.
The result is a ts_tree object. A ts_tree object may be queried,
edited, formatted, written to file, etc. using ts_tree methods.
Usage
ts_tree_new(
language,
file = NULL,
text = NULL,
ranges = NULL,
fail_on_parse_error = TRUE,
...
)Arguments
- language
Language of the file or string, a
ts_languageobject,e.g. the return value oftsjsonc::ts_language_jsonc().- file
Path of a file to parse. Use either
fileortext, but not both.- text
String to parse. Use either
fileortext, but not both.- ranges
Can be used to parse part(s) of the input. It must be a data frame with integer columns
start_row,start_col,end_row,end_col,start_byte,end_byte, in this order.- fail_on_parse_error
Logical, whether to error if there are parse errors in the document. Default is
TRUE.- ...
Additional arguments for methods.
Value
A ts_tree object representing the parse tree of the input. You can
use the single bracket `[`
operator to convert it to a data frame.
Details
A package that implements a tree-sitter parser provides a function that
creates a ts_language object for that parser. E.g.
tsjsonc has tsjsonc::ts_language_jsonc().
You need to use the returned ts_language object as the language
argument of ts_tree_new().
jsonc <- ts::ts_tree_new(
tsjsonc::ts_language_jsonc(),
text = "{ \"a\": true, // comment\n \"b\": [1, 2, 3], }"
)
jsonc#> # jsonc (2 lines) #> 1 | { "a": true, // comment #> 2 | "b": [1, 2, 3], }
See also
The tree-sitter parser packages typically include shortcuts to
create parse trees from strings and file, e.g.
tsjsonc::ts_parse_jsonc() and tsjsonc::ts_read_jsonc().
Other ts_tree generics:
[[.ts_tree(),
[[<-.ts_tree(),
format.ts_tree(),
print.ts_tree(),
select-set,
ts_tree_ast(),
ts_tree_delete(),
ts_tree_dom(),
ts_tree_format(),
ts_tree_insert(),
ts_tree_query(),
ts_tree_select(),
ts_tree_sexpr(),
ts_tree_unserialize(),
ts_tree_update(),
ts_tree_write()
Examples
# JSONC example, needs the tsjsonc package -----------------------------
json <- ts_tree_new(
tsjsonc::ts_language_jsonc(),
text = '{ "a": 1, "b": 2 }'
)
json
#> # jsonc (1 line)
#> 1 | { "a": 1, "b": 2 }
json |> ts_tree_format()
#> # jsonc (4 lines)
#> 1 | {
#> 2 | "a": 1,
#> 3 | "b": 2
#> 4 | }
# TOML example, needs the tstoml package -------------------------------
toml <- ts_tree_new(
tstoml::ts_language_toml(),
text = '[section]\nkey = "value"\nnumber = 42\n'
)
toml
#> # toml (3 lines)
#> 1 | [section]
#> 2 | key = "value"
#> 3 | number = 42
toml |> ts_tree_format()
#> # toml (3 lines)
#> 1 | [section]
#> 2 | key = "value"
#> 3 | number = 42