Skip to content

Commit

Permalink
Merge pull request #482 from posit-dev/feature/parse-status-srcref
Browse files Browse the repository at this point in the history
Add `SrcRef` type and create srcrefs with `parse_status()`
  • Loading branch information
lionel- authored Aug 30, 2024
2 parents bc760f1 + 22fc91c commit 5d2afac
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 58 deletions.
2 changes: 1 addition & 1 deletion crates/ark/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Shell {
&self,
req: &IsCompleteRequest,
) -> Result<IsCompleteReply, Exception> {
match harp::parse_status(req.code.as_str()) {
match harp::parse_status(&harp::ParseInput::Text(req.code.as_str())) {
Ok(ParseResult::Complete(_)) => Ok(IsCompleteReply {
status: IsComplete::Complete,
indent: String::from(""),
Expand Down
2 changes: 2 additions & 0 deletions crates/harp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod line_ending;
pub mod modules;
pub mod object;
pub mod parse;
pub mod parser;
pub mod polled_events;
pub mod protect;
pub mod r_version;
Expand All @@ -41,6 +42,7 @@ pub mod vector;
pub use eval::*;
pub use object::*;
pub use parse::*;
pub use parser::*;
pub use source::*;
pub use table::*;
pub use vector::list::*;
Expand Down
167 changes: 115 additions & 52 deletions crates/harp/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,38 @@

use std::ffi::CStr;

use libr::SEXP;
use itertools::Itertools;

use crate::exec::RFunction;
use crate::exec::RFunctionExt;
use crate::line_ending::convert_line_endings;
use crate::line_ending::LineEnding;
use crate::protect::RProtect;
use crate::r_string;
use crate::srcref;
use crate::try_catch;
use crate::vector::CharacterVector;
use crate::vector::Vector;
use crate::RObject;

pub struct RParseOptions {
pub srcfile: Option<RObject>,
}

pub enum ParseResult {
Complete(SEXP),
Complete(RObject),
Incomplete,
}

pub enum ParseInput<'a> {
Text(&'a str),
SrcFile(RObject),
}

impl Default for RParseOptions {
fn default() -> Self {
Self { srcfile: None }
}
}

/// Returns a single expression
pub fn parse_expr(code: &str) -> crate::Result<RObject> {
unsafe {
Expand All @@ -42,47 +58,43 @@ pub fn parse_expr(code: &str) -> crate::Result<RObject> {
}

/// Returns an EXPRSXP vector
pub fn parse_exprs(code: &str) -> crate::Result<RObject> {
match parse_status(code)? {
ParseResult::Complete(x) => {
return Ok(RObject::from(x));
},
ParseResult::Incomplete => {
return Err(crate::Error::ParseError {
code: code.to_string(),
message: String::from("Incomplete code"),
});
},
};
pub fn parse_exprs(text: &str) -> crate::Result<RObject> {
parse_exprs_ext(&ParseInput::Text(text))
}

/// This uses the R-level function `parse()` to create the srcrefs
pub fn parse_exprs_with_srcrefs(code: &str) -> crate::Result<RObject> {
unsafe {
let mut protect = RProtect::new();

// Because `parse(text =)` doesn't allow `\r\n` even on Windows
let code = convert_line_endings(code, LineEnding::Posix);
let code = r_string!(code, protect);
/// Same but creates srcrefs
pub fn parse_exprs_with_srcrefs(text: &str) -> crate::Result<RObject> {
let srcfile = srcref::new_srcfile_virtual(text)?;
parse_exprs_ext(&ParseInput::SrcFile(srcfile))
}

RFunction::new("base", "parse")
.param("text", code)
.param("keep.source", true)
.call()
fn parse_exprs_ext<'a>(input: &ParseInput<'a>) -> crate::Result<RObject> {
let status = parse_status(input)?;
match status {
ParseResult::Complete(x) => Ok(RObject::from(x)),
ParseResult::Incomplete => Err(crate::Error::ParseError {
code: parse_input_as_string(input).unwrap_or(String::from("Concersion error")),
message: String::from("Incomplete code"),
}),
}
}

pub fn parse_status(code: &str) -> crate::Result<ParseResult> {
pub fn parse_status<'a>(input: &ParseInput<'a>) -> crate::Result<ParseResult> {
unsafe {
let mut ps: libr::ParseStatus = libr::ParseStatus_PARSE_NULL;
let mut protect = RProtect::new();
let r_code = r_string!(convert_line_endings(code, LineEnding::Posix), &mut protect);
// TODO: set keep.parse.data

let mut status: libr::ParseStatus = libr::ParseStatus_PARSE_NULL;

let (text, srcfile) = match input {
ParseInput::Text(text) => (as_parse_text(text), RObject::null()),
ParseInput::SrcFile(srcfile) => (srcref::srcfile_lines(srcfile.sexp)?, srcfile.clone()),
};

let result: RObject =
try_catch(|| libr::R_ParseVector(r_code, -1, &mut ps, libr::R_NilValue).into())?;
try_catch(|| libr::R_ParseVector(text.sexp, -1, &mut status, srcfile.sexp).into())?;

match ps {
libr::ParseStatus_PARSE_OK => Ok(ParseResult::Complete(result.sexp)),
match status {
libr::ParseStatus_PARSE_OK => Ok(ParseResult::Complete(result)),
libr::ParseStatus_PARSE_INCOMPLETE => Ok(ParseResult::Incomplete),
libr::ParseStatus_PARSE_ERROR => Err(crate::Error::ParseSyntaxError {
message: CStr::from_ptr(libr::get(libr::R_ParseErrorMsg).as_ptr())
Expand All @@ -91,36 +103,71 @@ pub fn parse_status(code: &str) -> crate::Result<ParseResult> {
line: libr::get(libr::R_ParseError) as i32,
}),
_ => {
// should not get here
// Should not get here
Err(crate::Error::ParseError {
code: code.to_string(),
code: parse_input_as_string(input).unwrap_or(String::from("Conversion error")),
message: String::from("Unknown parse error"),
})
},
}
}
}

pub fn as_parse_text(text: &str) -> RObject {
unsafe {
let mut protect = RProtect::new();
let input = r_string!(convert_line_endings(text, LineEnding::Posix), &mut protect);
input.into()
}
}

fn parse_input_as_string<'a>(input: &ParseInput<'a>) -> crate::Result<String> {
Ok(match input {
ParseInput::Text(text) => text.to_string(),
ParseInput::SrcFile(srcfile) => {
let lines = srcref::srcfile_lines(srcfile.sexp)?;
let lines = unsafe { CharacterVector::new(lines)? };

lines
.iter()
.map(|x| x.unwrap_or(String::from("NA")))
.join("\n")
},
})
}

#[cfg(test)]
mod tests {
use crate::assert_match;
use crate::parse::parse_input_as_string;
use crate::parse::ParseInput;
use crate::parse_status;
use crate::r_length;
use crate::r_stringify;
use crate::r_symbol;
use crate::r_test;
use crate::r_typeof;
use crate::srcref;
use crate::ParseResult;

#[test]
fn test_parse_status() {
r_test! {
// complete
assert_match!(
parse_status("force(42)"),
parse_status(&ParseInput::Text("")),
Ok(ParseResult::Complete(out)) => {
assert_eq!(r_typeof(out.sexp), libr::EXPRSXP as u32);
assert_eq!(r_length(out.sexp), 0);
}
);

// Complete
assert_match!(
parse_status(&ParseInput::Text("force(42)")),
Ok(ParseResult::Complete(out)) => {
assert_eq!(r_typeof(out), libr::EXPRSXP as u32);
assert_eq!(r_typeof(out.sexp), libr::EXPRSXP as u32);

let call = libr::VECTOR_ELT(out, 0);
let call = libr::VECTOR_ELT(out.sexp, 0);
assert_eq!(r_typeof(call), libr::LANGSXP as u32);
assert_eq!(libr::Rf_xlength(call), 2);
assert_eq!(libr::CAR(call), r_symbol!("force"));
Expand All @@ -131,21 +178,21 @@ mod tests {
}
);

// incomplete
// Incomplete
assert_match!(
parse_status("force(42"),
parse_status(&ParseInput::Text("force(42")),
Ok(ParseResult::Incomplete)
);

// error
// Error
assert_match!(
parse_status("42 + _"),
parse_status(&ParseInput::Text("42 + _")),
Err(_) => {}
);

// "normal" syntax error
assert_match!(
parse_status("1+1\n*42"),
parse_status(&ParseInput::Text("1+1\n*42")),
Err(crate::Error::ParseSyntaxError {message, line}) => {
assert!(message.contains("unexpected"));
assert_eq!(line, 2);
Expand All @@ -154,21 +201,37 @@ mod tests {

// CRLF in the code string, like a file with CRLF line endings
assert_match!(
parse_status("x<-\r\n1\r\npi"),
parse_status(&ParseInput::Text("x<-\r\n1\r\npi")),
Ok(ParseResult::Complete(out)) => {
assert_eq!(r_typeof(out), libr::EXPRSXP as u32);
assert_eq!(r_stringify(out, "").unwrap(), "expression(x <- 1, pi)");
assert_eq!(r_typeof(out.sexp), libr::EXPRSXP as u32);
assert_eq!(r_stringify(out.sexp, "").unwrap(), "expression(x <- 1, pi)");
}
);

// CRLF inside a string literal in the code
assert_match!(
parse_status(r#"'a\r\nb'"#),
parse_status(&ParseInput::Text(r#"'a\r\nb'"#)),
Ok(ParseResult::Complete(out)) => {
assert_eq!(r_typeof(out), libr::EXPRSXP as u32);
assert_eq!(r_stringify(out, "").unwrap(), r#"expression("a\r\nb")"#);
assert_eq!(r_typeof(out.sexp), libr::EXPRSXP as u32);
assert_eq!(r_stringify(out.sexp, "").unwrap(), r#"expression("a\r\nb")"#);
}
);
}
}

#[test]
fn test_parse_input_as_string() {
r_test! {
assert_eq!(
parse_input_as_string(&ParseInput::Text("foo\nbar")).unwrap(),
"foo\nbar"
);

let input = srcref::new_srcfile_virtual("foo\nbar").unwrap();
assert_eq!(
parse_input_as_string(&ParseInput::SrcFile(input)).unwrap(),
"foo\nbar"
);
}
}
}
1 change: 1 addition & 0 deletions crates/harp/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod srcref;
Loading

0 comments on commit 5d2afac

Please sign in to comment.