From 70e127ba7d9ec2ad7419085d6bcf0e8d96adef47 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 1 Nov 2018 17:12:31 +0100 Subject: parser::map_group(): Always parse to end of input Previously, the modified input in `map_group_with_invalid_input_fails()` in this commit would fail with a result of `Ok` instead of `Err`. The `map_group()` parser would parse the first, correct line, and ignore the second, incorrect one. That's wrong. We want the whole parser to fail because it contains an invalid sequence. Do this by ensuring `eof()` follows all `definitions()`. This way we're guaranteed to always parse the whole input, and any errors within get surfaced. --- src/parser.rs | 56 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/parser.rs b/src/parser.rs index 3866a44..fe5b413 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -642,32 +642,34 @@ where blank(), eof(), ).map(|_| MapGroup::default()), - definitions() - .map(|definitions| { - let mut map_group = MapGroup::default(); - - for definition in definitions { - match definition { - Definition::Map(map) => { - map_group.maps.insert( - map.trigger, - MapAction { - action: map.action, - kind: map.kind, - } - ); - }, - Definition::Mode(mode) => { - map_group.modes.insert( - mode.trigger, - mode.maps, - ); - }, - } + ( + definitions(), + eof(), + ).map(|(definitions, _)| { + let mut map_group = MapGroup::default(); + + for definition in definitions { + match definition { + Definition::Map(map) => { + map_group.maps.insert( + map.trigger, + MapAction { + action: map.action, + kind: map.kind, + } + ); + }, + Definition::Mode(mode) => { + map_group.modes.insert( + mode.trigger, + mode.maps, + ); + }, } + } - map_group - }), + map_group + }), ) } @@ -1369,13 +1371,14 @@ cmd /usr/bin/say hello #[test] fn map_group_with_invalid_input_fails() { - let text = "not-a-kind + let text = "map +not-a-kind "; let result = map_group().easy_parse(State::new(text)).map(|t| t.0); assert_eq!(result, Err(easy::Errors { position: SourcePosition { - line: 1, + line: 2, column: 1, }, errors: vec![ @@ -1387,6 +1390,7 @@ cmd /usr/bin/say hello easy::Error::Expected("whitespace".into()), easy::Error::Expected("tab".into()), easy::Error::Expected('#'.into()), + easy::Error::Expected("end of input".into()), ], })); } -- cgit v1.2.3