Parse the lines of code one by one to find complete expressions in the code, and put them in a list.
split_source(x, merge_comments = FALSE, line_number = FALSE)
A list of character vectors, and each vector contains a complete R
expression, with an attribute lines
indicating the starting and ending
line numbers of the expression if the argument line_number = TRUE
.
code = c("# comment 1", "# comment 2", "if (TRUE) {", "1 + 1", "}", "print(1:5)")
xfun::split_source(code)
#> [[1]]
#> [1] "# comment 1"
#>
#> [[2]]
#> [1] "# comment 2"
#>
#> [[3]]
#> [1] "if (TRUE) {" "1 + 1" "}"
#>
#> [[4]]
#> [1] "print(1:5)"
#>
xfun::split_source(code, merge_comments = TRUE)
#> [[1]]
#> [1] "# comment 1" "# comment 2" "if (TRUE) {" "1 + 1" "}"
#>
#> [[2]]
#> [1] "print(1:5)"
#>