Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle newer error in R_ParseVector(), i.e. those related to |> #49

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 48 additions & 19 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,56 @@ void interpreter::configure_impl()
r::invoke_xeusr_fn("configure");
}

nl::json interpreter::is_complete_request_impl(const std::string& code)
nl::json interpreter::is_complete_request_impl(const std::string& code_)
{
SEXP s_code = PROTECT(Rf_mkString(code.c_str()));
ParseStatus status;

// Currently ignore the result of R_ParseVector, and only care about status
R_ParseVector(s_code, -1, &status, R_NilValue);
UNPROTECT(1); // s_code

switch(status) {
case PARSE_EOF:
case PARSE_NULL:
case PARSE_OK:
return xeus::create_is_complete_reply("complete", "");

case PARSE_INCOMPLETE:
return xeus::create_is_complete_reply("incomplete", "");
// initially code holds the string, but then it is being
// replaced by incomplete, invalid or complete either in the
// body handler or the error handler
SEXP code = PROTECT(Rf_mkString(code_.c_str()));

// we can't simply use an R callback because the R parse(text =)
// approach does not distinguish between invalid code and
// incomplete: they both just throw an error
R_tryCatchError(
[](void* void_code) { // body
ParseStatus status;
SEXP code = reinterpret_cast<SEXP>(void_code);

R_ParseVector(code, -1, &status, R_NilValue);

switch(status) {
case PARSE_INCOMPLETE:
SET_STRING_ELT(code, 0, Rf_mkChar("incomplete"));
break;

case PARSE_ERROR:
SET_STRING_ELT(code, 0, Rf_mkChar("invalid"));
break;

default:
SET_STRING_ELT(code, 0, Rf_mkChar("complete"));
}

return R_NilValue;
},
reinterpret_cast<void*>(code),

[](SEXP, void* void_code) { // handler
// some parse error cases are not propagated to PARSE_ERROR
// but rather throw an error, so we need to catch it
// and set the result to invalid
SEXP code = reinterpret_cast<SEXP>(void_code);
SET_STRING_ELT(code, 0, Rf_mkChar("invalid"));

return R_NilValue;
},
reinterpret_cast<void*>(code)
);

case PARSE_ERROR:
return xeus::create_is_complete_reply("invalid", "");
}
// eventually we just have to extract the string from code (which has been replaced)
auto result = xeus::create_is_complete_reply(CHAR(STRING_ELT(code, 0)), "");
UNPROTECT(1);
return result;
}

nl::json interpreter::complete_request_impl(const std::string& code,
Expand Down
2 changes: 1 addition & 1 deletion test/test_xr_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _execute_code(self, code, tests=True, silent=False, store_history=True):

complete_code_samples = ["fun()", "1 + 2", "a %>% b", "a |> b()", "a |> b(c = _)"]
incomplete_code_samples = ["fun(", "1 + "]
invalid_code_samples = ["fun())"]
invalid_code_samples = ["fun())", "a |> b", "a |> b(_)", "a |> b(c(_))"]

def test_stdout(self):
self.flush_channels()
Expand Down
Loading