Skip to content

Commit

Permalink
Simplify handling of parse failures (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Jul 2, 2024
1 parent f82a9e3 commit 1109e0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ evaluate <- function(input,
# Capture output
watcher <- watchout(output_handler, new_device = new_device, debug = debug)

parsed <- parse_all(input, filename, on_error != "error")
if (inherits(err <- attr(parsed, 'PARSE_ERROR'), 'error')) {
watcher$push_source(parsed$src, expression())
if (on_error != "error" && !can_parse(input)) {
err <- tryCatch(parse(text = input), error = function(cnd) cnd)
watcher$push_source(input, expression())
watcher$push(err)
return(watcher$get())
}

parsed <- parse_all(input, filename = filename)
# "Transpose" parsed so we get a list that's easier to iterate over
tles <- Map(
function(src, exprs) list(src = src, exprs = exprs),
Expand Down
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ seq2 <- function(start, end, by = 1) {
seq(start, end, by = 1)
}
}

can_parse <- function(x) {
if (!is.character(x)) {
return(TRUE)
}

tryCatch(
{
parse(text = x)
TRUE
},
error = function(e) FALSE
)
}

0 comments on commit 1109e0e

Please sign in to comment.