jupyter-xeus / xeus-r

Jupyter kernel for the R programming language
Other
42 stars 6 forks source link

`parse(text = )` before evaluating, so that we can catch parse errors #4

Closed romainfrancois closed 1 year ago

romainfrancois commented 1 year ago

The idea is to be able to catch parse errors before we try to evaluate, I've tried to do this through the internal R_ParseVector() function, but unfortunately because of how things are implemented in R, this does not give access to error messages, so instead this use an R call behind a tryCatch() , i.e. if the cell [7] has 1+1 this will evaluate :

tryCatch(parse(text = "1+1", srcfile = "<cell [7]>"), error = identity) 

so that we either get

> parse(text = "rnorm(10) ; 2")
expression(rnorm(10), 2)
> tryCatch(parse(text = "1 |> 2", srcfile = "<cell [7]>"), error = identity)
<RHSnotFnCall in 2: The pipe operator requires a function call as RHS (<input>:1:6)>
>
>
> tryCatch(parse(text = "1++", srcfile = "<cell [7]>"), error = identity)
<simpleError in parse(text = "1++", srcfile = "<cell [7]>"): <cell [7]>:2:0: fin d'entrée inattendue
1: 1++
   ^>
> tryCatch(parse(text = "foo()2", srcfile = "<cell [7]>"), error = identity)
<simpleError in parse(text = "foo()2", srcfile = "<cell [7]>"): <cell [7]>:1:6: constante numérique inattendue
1: foo()2
         ^>
image

This PR does not handle yet the case when the expression does parse, it just echoes. Will do in a follow up