% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bracket-ts-tree.R
\name{[.ts_tree}
\alias{[.ts_tree}
\title{Convert ts_tree object to a data frame}
\usage{
\method{[}{ts_tree}(x, i, j, drop = FALSE)
}
\arguments{
\item{x}{A \code{ts_tree} object.}

\item{i, j}{Incides, passed to the regular data.frame indexing method, see
\code{\link[base:Extract]{Extract}}.}

\item{drop}{Passed to the regular data.frame indexing method, see
\code{\link[base:Extract]{Extract}}.}
}
\value{
A data frame with one row per token, and columns:
\itemize{
\item \code{id}: integer, the id of the token. The (root) document node has id 1.
\item \code{parent}: integer, the id of the parent token. The root token has
parent \code{NA}
\item \code{field_name}: character, the field name of the token in its parent.
\item \code{type}: character, the type of the token.
\item \code{code}: character, the actual code of the token.
\item \code{start_byte}, \code{end_byte}: integer, the byte positions of the token
in the input.
\item \code{start_row}, \code{start_column}, \code{end_row}, \code{end_column}: integer, the
position of the token in the input.
\item \code{is_missing}: logical, whether the token is a missing token added by
the parser to recover from errors.
\item \code{has_error}: logical, whether the token has a parse error.
\item \code{children}: list of integer vectors, the ids of the children tokens.
\item \code{dom_type}: character, the type of the node in the DOM tree. See
\code{\link[tsitter:ts_tree_dom]{ts_tree_dom()}}. Nodes that are not part
of the DOM tree have \code{NA_character_} here.
\item \code{dom_children}: list of integer vectors, the ids of the children in the
DOM tree. See \code{\link[tsitter:ts_tree_dom]{ts_tree_dom()}}.
\item \code{dom_parent}: integer, the parent of the node in the DOM tree. See
\code{\link[tsitter:ts_tree_dom]{ts_tree_dom()}}. Nodes that are not part
of the DOM tree and the document node have have \code{NA_integer_} here.
}

Other, undocumented columns may also be present, these are considered
internal and may change without notice.
}
\description{
Create a data frame for the syntax tree of a JSON document, by indexing
a ts_tree object with single brackets. This is occasionally useful for
exploration and debugging.
}
\details{
A tree-sitter tree object has at least four classes:
\itemize{
\item \verb{ts_tree_<parser_name>}, e.g. \code{ts_tree_tsjsonc},
\item \code{ts_tree},
\item \code{tbl}, from the pillar package, for better printing when converted
to a data frame, and
\item \code{data.frame}, since it is a data frame internally.
}

The \code{ts_tree} class has custom \code{\link[=format]{format()}} and \code{\link[=print]{print()}} methods, that
show (part of) the underlying document, and also the selected elements,
if any.\if{html}{\out{
</p><p><!-- ------------------------------------------------------>
}}


It is sometimes useful to treat a \code{tree} \code{ts_tree} object as a data
frame, and drop the \code{ts_tree} classes. This can be done by indexing with
single brackets, e.g. \code{tree[]}. This returns a data frame with one
row per token, and various columns with information about the tokens.
See details in the 'Value' section or this page.
}
\examples{
\dontshow{if (requireNamespace("tsjsonc", quietly = TRUE)) withAutoprint(\{ # examplesIf}
# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{"foo": 42, "bar": [1, 2, 3]}')

tree

tree[]
\dontshow{\}) # examplesIf}
}
\seealso{
Other ts_tree exploration:
\code{\link[=ts_tree_ast]{ts_tree_ast()}},
\code{\link[=ts_tree_dom]{ts_tree_dom()}},
\code{\link[=ts_tree_query]{ts_tree_query()}},
\code{\link[=ts_tree_sexpr]{ts_tree_sexpr()}}
}
\concept{ts_tree exploration}
