split_source: Split source lines into complete expressions

View source: R/string.R

split_sourceR Documentation

Split source lines into complete expressions

Description

Parse the lines of code one by one to find complete expressions in the code, and put them in a list.

Usage

split_source(
  x,
  merge_comments = FALSE,
  line_number = FALSE,
  skip = getOption("xfun.split_source.skip")
)

Arguments

x

A character vector of R source code.

merge_comments

Whether to merge consecutive lines of comments as a single expression to be combined with the next non-comment expression (if any).

line_number

Whether to store the starting line number of each expression in the returned value.

skip

A token to skip the rest of code. When provided as a character string, the split will stop at the this token.

Value

A list of character vectors, and each vector contains a complete R expression, with an attribute line_start indicating the starting line number of the expression if the argument line_number = TRUE.

Examples

xfun::split_source(c("if (TRUE) {", "1 + 1", "}", "print(1:5)"))
xfun::split_source(c("print(1:5)", "#--#", "if (TRUE) {", "1 + 1", "}"), skip = "#--#")

yihui/xfun documentation built on April 29, 2024, 12:16 p.m.