use granit_parser::{
ErrorKind, Event, Marker, Parser, ParserStack, ParserTrait, ReplayParser, ScalarStyle,
SourceId, Span, StrInput,
};
#[test]
fn source_queries_borrow_names_and_preserve_distinct_keys_for_equal_names() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser_with_source_id(
Parser::new_from_str("parent"),
"same".into(),
SourceId(1),
);
stack.push_str_parser_with_source_id(Parser::new_from_str("child"), "same".into(), SourceId(2));
assert_eq!(stack.depth(), 2);
assert_eq!(stack.current_source_id(), Some(SourceId(2)));
let mut names = stack.source_names();
assert_eq!(names.len(), 2);
assert_eq!(names.next(), Some("same"));
assert_eq!(names.next_back(), Some("same"));
assert_eq!(names.len(), 0);
assert_eq!(names.next_back(), None);
assert_eq!(
stack.source_ids().collect::<Vec<_>>(),
[Some(SourceId(1)), Some(SourceId(2))]
);
assert_eq!(
stack.source_ids().rev().collect::<Vec<_>>(),
[Some(SourceId(2)), Some(SourceId(1))]
);
let mut scalars = Vec::new();
let mut final_source = None;
while let Some(sourced) = stack.next_sourced() {
let (event, _) = sourced.value.unwrap();
if let Event::Scalar { value, .. } = event {
scalars.push((value.into_owned(), sourced.source_id));
} else if matches!(event, Event::StreamEnd { .. }) {
final_source = sourced.source_id;
}
}
assert_eq!(
scalars,
[
("child".into(), Some(SourceId(2))),
("parent".into(), Some(SourceId(1)))
]
);
assert_eq!(final_source, Some(SourceId(1)));
assert_eq!(stack.depth(), 0);
assert_eq!(stack.current_source_id(), None);
assert_eq!(stack.source_names().len(), 0);
}
#[test]
fn peeked_parent_event_keeps_its_key_when_another_source_is_pushed() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser_with_source_id(
Parser::new_from_str("parent"),
"parent".into(),
SourceId(1),
);
assert!(matches!(
stack.peek().unwrap().unwrap().0,
Event::StreamStart { .. }
));
stack.push_str_parser_with_source_id(
Parser::new_from_str("child"),
"child".into(),
SourceId(2),
);
let first = stack.next_sourced().unwrap();
assert_eq!(first.source_id, Some(SourceId(1)));
assert!(matches!(first.value.unwrap().0, Event::StreamStart { .. }));
let child = stack.next_sourced().unwrap();
assert_eq!(child.source_id, Some(SourceId(2)));
assert!(matches!(child.value.unwrap().0, Event::Scalar { value, .. } if value == "child"));
}
#[test]
fn primed_child_preserves_parent_lookahead_and_filters_nested_boundaries() {
for primed_index in 0..=2 {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser_with_source_id(
Parser::new_from_str("[parent]"),
"parent".into(),
SourceId(1),
);
assert!(matches!(
stack.nth(2).unwrap().unwrap().0,
Event::SequenceStart { .. }
));
let parent = stack.peek().unwrap().unwrap().clone();
assert_eq!(parent.0.scalar().unwrap().0, "parent");
let mut child = Parser::new(StrInput::new("child"));
let current = child.nth(primed_index).unwrap().unwrap();
stack.push_custom_parser_with_current_and_source_id(
child,
"child".into(),
current,
SourceId(2),
);
assert_eq!(stack.peek().unwrap().unwrap(), &parent);
let sourced = stack.next_sourced().unwrap();
assert_eq!(sourced.source_id, Some(SourceId(1)));
assert_eq!(sourced.value.unwrap(), parent);
let sourced = stack.next_sourced().unwrap();
assert_eq!(sourced.source_id, Some(SourceId(2)));
assert_eq!(sourced.value.unwrap().0.scalar().unwrap().0, "child");
let sourced = stack.next_sourced().unwrap();
assert_eq!(sourced.source_id, Some(SourceId(1)));
assert!(matches!(
sourced.value.unwrap().0,
Event::SequenceEnd { .. }
));
}
}
#[test]
fn peeked_error_keeps_failing_source_after_pop_and_push() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser_with_source_id(
Parser::new_from_str("parent"),
"same".into(),
SourceId(1),
);
stack.push_str_parser_with_source_id(Parser::new_from_str("["), "same".into(), SourceId(2));
while stack.peek().unwrap().is_ok() {
stack.next().unwrap().unwrap();
}
assert_eq!(stack.depth(), 1);
assert_eq!(stack.current_source_id(), Some(SourceId(1)));
stack.push_custom_parser_with_current_and_source_id(
Parser::new(StrInput::new("new")),
"replacement".into(),
(
Event::new_scalar("primed".into(), ScalarStyle::plain(), 0, None),
Span::empty(Marker::new(0, 1, 0)),
),
SourceId(3),
);
let peeked_error = stack.peek().unwrap().unwrap_err();
let failed = stack.next_sourced().unwrap();
assert_eq!(failed.source_id, Some(SourceId(2)));
let error = failed.value.unwrap_err();
assert_eq!(error, peeked_error);
assert_eq!(error.source_stack(), ["same", "same"]);
assert!(stack.next_sourced().is_none());
}
#[test]
fn primed_child_preserves_allocated_anchor_ids_and_propagates_its_next_id() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser(
Parser::new_from_str("[&before one, &after two]"),
"parent".into(),
);
let before = stack
.find_map(|result| result.unwrap().0.anchor_id())
.unwrap();
let mut child = Parser::new(StrInput::new("&seq [ &item value, *seq, *item ]"));
child.set_anchor_offset(child.anchor_offset().max(stack.current_anchor_offset()));
let current = child.nth(2).unwrap().unwrap();
let seq = current.0.anchor_id().unwrap();
assert_eq!(seq, before + 1);
stack.push_custom_parser_with_current_and_source_id(
child,
"child".into(),
current,
SourceId(2),
);
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(seq));
let item = stack.next().unwrap().unwrap().0.anchor_id().unwrap();
assert_eq!(item, seq + 1);
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(seq));
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(item));
let after = stack.find_map(|result| result.unwrap().0.anchor_id());
assert_eq!(after, Some(item + 1));
}
#[test]
fn stale_primed_anchor_reports_the_child_source_after_parent_lookahead() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser_with_source_id(
Parser::new_from_str("[&p first, &q second, pending]"),
"parent".into(),
SourceId(1),
);
let first = stack.find_map(|event| event.unwrap().0.anchor_id());
assert_eq!(first, Some(1));
let mut child = Parser::new_from_str("&c child");
child.set_anchor_offset(stack.current_anchor_offset());
let current = child.nth(2).unwrap().unwrap();
assert_eq!(current.0.anchor_id(), Some(2));
let marker = current.1.start;
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(2));
let pending = stack.peek().unwrap().unwrap().clone();
assert_eq!(pending.0.scalar().unwrap().0, "pending");
stack.push_custom_parser_with_current_and_source_id(
child,
"child".into(),
current,
SourceId(2),
);
let parent = stack.next_sourced().unwrap();
assert_eq!(parent.source_id, Some(SourceId(1)));
assert_eq!(parent.value.unwrap(), pending);
let error = stack.peek().unwrap().unwrap_err();
assert_eq!(
error.kind(),
&ErrorKind::AnchorIdCollision {
anchor_id: 2,
first_free_id: 3,
}
);
assert_eq!(error.marker(), &marker);
#[cfg(feature = "error_messages")]
{
let message = error.info();
assert!(message.contains("anchor ID 2"));
assert!(message.contains("first free ID 3"));
}
assert_eq!(error.source_stack(), ["parent", "child"]);
assert_eq!(stack.depth(), 1);
assert_eq!(stack.peek().unwrap().unwrap_err(), error);
let failed = stack.next_sourced().unwrap();
assert_eq!(failed.source_id, Some(SourceId(2)));
assert_eq!(failed.value.unwrap_err(), error);
assert!(stack.next_event().is_none());
}
#[test]
fn unseeded_primed_child_rejects_previous_anchor_ids_on_definitions_and_aliases() {
const CHILD: &str = "[&c def, plain, *c]";
for (current_index, collision_index, peek_in_child) in [
(3, 3, false), (5, 5, false), (4, 5, false), (2, 3, true), ] {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser(Parser::new_from_str("&p parent"), "parent".into());
assert_eq!(stack.nth(2).unwrap().unwrap().0.anchor_id(), Some(1));
let mut child = Parser::new_from_str(CHILD);
let current = child.nth(current_index).unwrap().unwrap();
if peek_in_child {
assert_eq!(child.peek().unwrap().unwrap().0.anchor_id(), Some(1));
}
let preceding = (current_index < collision_index).then(|| current.clone());
let (_, span) = Parser::new_from_str(CHILD)
.nth(collision_index)
.unwrap()
.unwrap();
stack.push_custom_parser_with_current(child, "child".into(), current);
if let Some(preceding) = preceding {
assert_eq!(stack.next().unwrap().unwrap(), preceding);
}
let error = stack.next().unwrap().unwrap_err();
assert_eq!(error.marker(), &span.start);
assert_eq!(
error.kind(),
&ErrorKind::AnchorIdCollision {
anchor_id: 1,
first_free_id: 2,
}
);
}
}
#[test]
fn unseeded_primed_child_can_allocate_its_first_anchor_after_push() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser(Parser::new_from_str("&p parent"), "parent".into());
assert_eq!(stack.nth(2).unwrap().unwrap().0.anchor_id(), Some(1));
let mut child = Parser::new_from_str("[&c child, *c]");
let current = child.nth(2).unwrap().unwrap();
stack.push_custom_parser_with_current(child, "child".into(), current);
assert!(matches!(
stack.next().unwrap().unwrap().0,
Event::SequenceStart { .. }
));
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(2));
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(2));
}
#[test]
fn primed_child_keeps_unread_replay_anchors_distinct() {
let mut recording = Parser::new_from_str("[&p parent, *p]");
let events = recording.by_ref().collect::<Result<Vec<_>, _>>().unwrap();
let replay = ReplayParser::new(events, recording.anchor_offset());
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_replay_parser(replay, "parent".into());
assert!(matches!(
stack.nth(2).unwrap().unwrap().0,
Event::SequenceStart { .. }
));
let mut child = Parser::new_from_str("&c [*c]");
child.set_anchor_offset(child.anchor_offset().max(stack.current_anchor_offset()));
let current = child.nth(2).unwrap().unwrap();
stack.push_custom_parser_with_current(child, "child".into(), current);
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(2));
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(2));
assert!(matches!(
stack.next().unwrap().unwrap().0,
Event::SequenceEnd { .. }
));
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(1));
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(1));
}
#[test]
fn primed_parser_keeps_its_first_anchor_id_on_an_empty_stack_or_zero_offset_replay() {
for replay_parent in [false, true] {
let mut stack: ParserStack<'_> = ParserStack::new();
if replay_parent {
stack.push_replay_parser(ReplayParser::new(Vec::new(), 0), "parent".into());
}
assert_eq!(stack.current_anchor_offset(), 0);
let mut child = Parser::new_from_str("&c [*c]");
child.set_anchor_offset(child.anchor_offset().max(stack.current_anchor_offset()));
let current = child.nth(2).unwrap().unwrap();
stack.push_custom_parser_with_current(child, "child".into(), current);
assert_eq!(stack.next().unwrap().unwrap().0.anchor_id(), Some(1));
assert_eq!(stack.next().unwrap().unwrap().0.alias_id(), Some(1));
}
}
#[test]
fn custom_current_event_and_subsequent_events_share_the_supplied_key() {
let mut stack: ParserStack<'_> = ParserStack::new();
let mut parser = Parser::new(StrInput::new("value"));
let current = parser.next().unwrap().unwrap();
stack.push_custom_parser_with_current_and_source_id(
parser,
"custom".into(),
current,
SourceId(8),
);
assert!(stack.peek().unwrap().is_ok());
let mut events = 0;
while let Some(sourced) = stack.next_sourced() {
assert_eq!(sourced.source_id, Some(SourceId(8)));
sourced.value.unwrap();
events += 1;
}
assert_eq!(events, 5);
}
#[test]
fn replay_without_end_marker_keeps_key_for_synthesized_stream_end() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_replay_parser_with_source_id(
ReplayParser::new(
vec![(
Event::new_scalar("value".into(), ScalarStyle::plain(), 0, None),
Span::empty(Marker::new(0, 1, 0)),
)],
0,
),
"replay".into(),
SourceId(9),
);
assert_eq!(stack.next_sourced().unwrap().source_id, Some(SourceId(9)));
let end = stack.next_sourced().unwrap();
assert_eq!(end.source_id, Some(SourceId(9)));
assert!(matches!(end.value.unwrap().0, Event::StreamEnd { .. }));
assert_eq!(stack.depth(), 0);
assert!(stack.next_sourced().is_none());
}
#[test]
fn iterator_parser_results_keep_their_key() {
let text = String::from("value");
let mut stack: ParserStack<'_, _> = ParserStack::new();
stack.push_iter_parser_with_source_id(
Parser::new_from_iter(text.chars()),
"iter".into(),
SourceId(10),
);
while let Some(sourced) = stack.next_sourced() {
assert_eq!(sourced.source_id, Some(SourceId(10)));
sourced.value.unwrap();
}
}
#[test]
fn resolver_results_and_eager_errors_keep_supplied_keys() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.set_resolver(|name| Ok(if name == "bad" { "[" } else { "value" }.to_owned()));
let error = stack
.push_include_with_source_id("bad", SourceId(11))
.unwrap_err();
assert_eq!(error.source_id, Some(SourceId(11)));
assert_eq!(error.value.source_stack(), ["bad"]);
assert_eq!(stack.depth(), 0);
stack
.push_include_with_source_id("good", SourceId(12))
.unwrap();
while let Some(sourced) = stack.next_sourced() {
assert_eq!(sourced.source_id, Some(SourceId(12)));
sourced.value.unwrap();
}
}
#[test]
fn ordinary_pushes_keep_legacy_results_and_have_no_source_key() {
let mut stack: ParserStack<'_> = ParserStack::new();
stack.push_str_parser(Parser::new_from_str("value"), "legacy".into());
assert_eq!(stack.source_ids().collect::<Vec<_>>(), [None]);
assert!(matches!(
stack.next().unwrap().unwrap().0,
Event::StreamStart { .. }
));
while let Some(sourced) = stack.next_sourced() {
assert_eq!(sourced.source_id, None);
sourced.value.unwrap();
}
}