From 7f168045acd1408b943396953fa41024d5f41a78 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Sun, 7 Feb 2021 02:03:51 -0500 Subject: [PATCH] [Parser] Support code with trailing comments The previous implementation using ess::parse_one and then checking that there was no trailing text would fail if there were trailing comments. Parsing and then checking that there was only one expression returned avoids this issue. --- src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 955e63d..f8271ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,18 @@ pub mod ast; pub mod trans; pub fn parse(input: &str) -> Result { - let (sexp, rest) = ess::parse_one(input)?; - if !rest.trim().is_empty() { - return Err(ParseError::Unexpected('\0', sexp.get_loc().1)); + let (mut sexps, error) = ess::parse(input); + if let Some(err) = error { + return Err(err); } - Ok(sexp) + + if sexps.len() > 1 { + return Err(ParseError::Sexp( + Box::new(ParseError::Unexpected('\0', sexps[1].get_loc().0)), + *sexps[1].get_loc(), + )); + } + Ok(sexps.remove(0)) } pub static BUILTINS: &[&str] = &[ -- 2.47.0