From 79af94a21e0834639431cf2e62fee746acc967d6 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 1 Jul 2024 09:26:14 -0500 Subject: [PATCH] Simplify handling of parse failures --- R/eval.R | 8 +++++--- R/utils.R | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/R/eval.R b/R/eval.R index 9ea7101..2b688ec 100644 --- a/R/eval.R +++ b/R/eval.R @@ -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), diff --git a/R/utils.R b/R/utils.R index 107a441..9a29c0f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 + ) +}