Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

next_terminal

Tell me what the next terminal is


Description

If the first is a terminal, return it. If not, go inside it and search the next terminal

Usage

next_terminal(
  pd,
  stack = FALSE,
  vars = c("pos_id", "token", "text"),
  tokens_exclude = c()
)

Arguments

pd

A nest.

stack

Whether or not to also return information on the tokens that are between pd and the first terminal, so the returned tibble can be understood as a transition path from pd to the next terminal, instead of the information at the terminal only. The order is inside-out, i.e. the first non-terminal on top, the terminal last.

vars

The variables to return.

tokens_exclude

A vector with tokens to exclude. This can be helpful if one wants to find the next token that is not a comment for example.

Value

Returns a tibble (which is not a valid parse table for stack = TRUE), with vars and another variable position that denotes the index each element in the transition. This can be helpful in conjunction with purrr::pluck() or purrr::modify_in() to reach the terminal in the nested structure.

Examples

withr::with_options(
  list(styler.cache_name = NULL), # temporarily deactivate cache
  {
    pd <- styler:::compute_parse_data_nested("if (TRUE) f()")
    styler:::next_terminal(pd)
  }
)

styler

Non-Invasive Pretty Printing of R Code

v1.4.1
MIT + file LICENSE
Authors
Kirill Müller [aut], Lorenz Walthert [cre, aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.