From 7960c3270e1a338f4da40d044a6896df96d82c79 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 14 Mar 2021 12:27:28 -0400 Subject: Make string and bool parsing complete --- src/parser/expr.rs | 6 +++--- src/parser/mod.rs | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/parser/expr.rs b/src/parser/expr.rs index 2ec6d60cd0..fd37fcb9c6 100644 --- a/src/parser/expr.rs +++ b/src/parser/expr.rs @@ -160,8 +160,8 @@ where named!(int(&str) -> Literal, map!(flat_map!(digit1, parse_to!(u64)), Literal::Int)); named!(bool_(&str) -> Literal, alt!( - tag!("true") => { |_| Literal::Bool(true) } | - tag!("false") => { |_| Literal::Bool(false) } + complete!(tag!("true")) => { |_| Literal::Bool(true) } | + complete!(tag!("false")) => { |_| Literal::Bool(false) } )); fn string_internal(i: &str) -> nom::IResult<&str, Cow<'_, str>, nom::error::Error<&str>> { @@ -172,7 +172,7 @@ fn string_internal(i: &str) -> nom::IResult<&str, Cow<'_, str>, nom::error::Erro } named!(string(&str) -> Literal, preceded!( - char!('"'), + complete!(char!('"')), map!( string_internal, |s| Literal::String(s) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9ac590cee8..9c45987322 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,6 +1,6 @@ use nom::character::complete::{multispace0, multispace1}; use nom::error::{ErrorKind, ParseError}; -use nom::{alt, char, complete, do_parse, many0, named, separated_list0, tag}; +use nom::{alt, char, complete, do_parse, many0, named, separated_list0, tag, terminated}; #[macro_use] mod macros; @@ -81,7 +81,7 @@ named!(pub decl(&str) -> Decl, alt!( fun_decl )); -named!(pub toplevel(&str) -> Vec, many0!(decl)); +named!(pub toplevel(&str) -> Vec, terminated!(many0!(decl), multispace0)); #[cfg(test)] mod tests { @@ -114,5 +114,10 @@ mod tests { fn main = plus (id 2) 7" ); assert_eq!(res.len(), 3); + let res = test_parse!( + toplevel, + "fn id x = x\nfn plus x y = x + y\nfn main = plus (id 2) 7\n" + ); + assert_eq!(res.len(), 3); } } -- cgit 1.4.1