correctly parse grammars that don't have a final newline

This commit is contained in:
xenofem 2020-06-20 04:28:20 -04:00
parent f99f22f477
commit dab6774a47
3 changed files with 15 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "purrchance" name = "purrchance"
version = "0.4.0" version = "0.4.1"
authors = ["xenofem <xenofem@xeno.science>"] authors = ["xenofem <xenofem@xeno.science>"]
description = "An unofficial Rust implementation of the Perchance grammar engine" description = "An unofficial Rust implementation of the Perchance grammar engine"
repository = "https://git.xeno.science/xenofem/purrchance" repository = "https://git.xeno.science/xenofem/purrchance"

View file

@ -172,6 +172,7 @@ test
let nt = Symbol::NonTerminal(String::from("test")); let nt = Symbol::NonTerminal(String::from("test"));
assert_eq!(nt.eval(&g), Some(String::from("foo"))); assert_eq!(nt.eval(&g), Some(String::from("foo")));
} }
#[test] #[test]
fn eval_loaded_grammar_multiple_lists() { fn eval_loaded_grammar_multiple_lists() {
let g = load_grammar(" let g = load_grammar("
@ -184,4 +185,12 @@ test1
let nt = Symbol::NonTerminal(String::from("test")); let nt = Symbol::NonTerminal(String::from("test"));
assert_eq!(nt.eval(&g), Some(String::from("foo"))); assert_eq!(nt.eval(&g), Some(String::from("foo")));
} }
#[test]
fn eval_loaded_grammar_no_trailing_newline() {
let g = load_grammar("test
foo").unwrap();
let nt = Symbol::NonTerminal(String::from("test"));
assert_eq!(nt.eval(&g), Some(String::from("foo")));
}
} }

View file

@ -26,8 +26,12 @@ where
} }
} }
fn take_all(input: &str) -> IResult<&str, &str> {
take_while(|_| true)(input)
}
fn terminal(input: &str) -> IResult<&str, Symbol> { fn terminal(input: &str) -> IResult<&str, Symbol> {
let (input, mut terminal_val) = verify(take_until_any(vec!["[", "//", "^", "\n"]), |s: &str| s.len() > 0)(input)?; let (input, mut terminal_val) = verify(alt((take_until_any(vec!["[", "//", "^", "\n"]), take_all)), |s: &str| s.len() > 0)(input)?;
if !input.starts_with("[") { if !input.starts_with("[") {
terminal_val = terminal_val.trim_end(); terminal_val = terminal_val.trim_end();
} }