chore(varcon): Add parse tracing

This commit is contained in:
Ed Page 2023-07-14 12:32:16 -05:00
parent e98fc52b0d
commit 0bde06af9a

View file

@ -1,4 +1,5 @@
use winnow::prelude::*; use winnow::prelude::*;
use winnow::trace::trace;
use crate::*; use crate::*;
@ -62,6 +63,7 @@ A Cv: acknowledgment's / Av B C: acknowledgement's
impl Cluster { impl Cluster {
pub fn parse(input: &str) -> IResult<&str, Self> { pub fn parse(input: &str) -> IResult<&str, Self> {
trace("cluster", move |input| {
let header = ( let header = (
"#", "#",
winnow::ascii::space0, winnow::ascii::space0,
@ -83,7 +85,8 @@ impl Cluster {
), ),
winnow::combinator::repeat(0.., note), winnow::combinator::repeat(0.., note),
); );
let (input, (header, entries, notes)): (_, (_, _, Vec<_>)) = cluster.parse_next(input)?; let (input, (header, entries, notes)): (_, (_, _, Vec<_>)) =
cluster.parse_next(input)?;
let header = header.map(|s| s.2.to_owned()); let header = header.map(|s| s.2.to_owned());
let notes = notes.into_iter().map(|s| s.to_owned()).collect(); let notes = notes.into_iter().map(|s| s.to_owned()).collect();
@ -93,6 +96,8 @@ impl Cluster {
notes, notes,
}; };
Ok((input, c)) Ok((input, c))
})
.parse_next(input)
} }
} }
@ -145,6 +150,7 @@ A B C: coloration's / B. Cv: colouration's
impl Entry { impl Entry {
pub fn parse(input: &str) -> IResult<&str, Self> { pub fn parse(input: &str) -> IResult<&str, Self> {
trace("entry", move |input| {
let var_sep = (winnow::ascii::space0, '/', winnow::ascii::space0); let var_sep = (winnow::ascii::space0, '/', winnow::ascii::space0);
let (input, variants) = let (input, variants) =
winnow::combinator::separated1(Variant::parse, var_sep).parse_next(input)?; winnow::combinator::separated1(Variant::parse, var_sep).parse_next(input)?;
@ -175,9 +181,12 @@ impl Entry {
e.variants = variants; e.variants = variants;
e.comment = comment.map(|c| c.2.to_owned()); e.comment = comment.map(|c| c.2.to_owned());
Ok((input, e)) Ok((input, e))
})
.parse_next(input)
} }
fn parse_description(input: &str) -> IResult<&str, Self> { fn parse_description(input: &str) -> IResult<&str, Self> {
trace("description", move |input| {
let (input, (pos, archaic, note, description)) = ( let (input, (pos, archaic, note, description)) = (
winnow::combinator::opt((winnow::ascii::space1, Pos::parse)), winnow::combinator::opt((winnow::ascii::space1, Pos::parse)),
winnow::combinator::opt((winnow::ascii::space1, "(-)")), winnow::combinator::opt((winnow::ascii::space1, "(-)")),
@ -203,6 +212,8 @@ impl Entry {
comment: None, comment: None,
}; };
Ok((input, e)) Ok((input, e))
})
.parse_next(input)
} }
} }
@ -298,19 +309,25 @@ mod test_entry {
impl Variant { impl Variant {
pub fn parse(input: &str) -> IResult<&str, Self> { pub fn parse(input: &str) -> IResult<&str, Self> {
trace("variant", move |input| {
let types = winnow::combinator::separated1(Type::parse, winnow::ascii::space1); let types = winnow::combinator::separated1(Type::parse, winnow::ascii::space1);
let sep = (":", winnow::ascii::space0); let sep = (":", winnow::ascii::space0);
let (input, (types, word)) = let (input, (types, word)) =
winnow::combinator::separated_pair(types, sep, word).parse_next(input)?; winnow::combinator::separated_pair(types, sep, word).parse_next(input)?;
let v = Self { types, word }; let v = Self { types, word };
Ok((input, v)) Ok((input, v))
})
.parse_next(input)
} }
} }
fn word(input: &str) -> IResult<&str, String> { fn word(input: &str) -> IResult<&str, String> {
trace("word", move |input| {
winnow::token::take_till1(|item: char| item.is_ascii_whitespace()) winnow::token::take_till1(|item: char| item.is_ascii_whitespace())
.map(|s: &str| s.to_owned().replace('_', " ")) .map(|s: &str| s.to_owned().replace('_', " "))
.parse_next(input) .parse_next(input)
})
.parse_next(input)
} }
#[cfg(test)] #[cfg(test)]
@ -382,12 +399,15 @@ mod test_variant {
impl Type { impl Type {
pub fn parse(input: &str) -> IResult<&str, Type> { pub fn parse(input: &str) -> IResult<&str, Type> {
trace("type", move |input| {
let (input, category) = Category::parse(input)?; let (input, category) = Category::parse(input)?;
let (input, tag) = winnow::combinator::opt(Tag::parse).parse_next(input)?; let (input, tag) = winnow::combinator::opt(Tag::parse).parse_next(input)?;
let (input, num) = winnow::combinator::opt(winnow::ascii::digit1).parse_next(input)?; let (input, num) = winnow::combinator::opt(winnow::ascii::digit1).parse_next(input)?;
let num = num.map(|s| s.parse().expect("parser ensured its a number")); let num = num.map(|s| s.parse().expect("parser ensured its a number"));
let t = Type { category, tag, num }; let t = Type { category, tag, num };
Ok((input, t)) Ok((input, t))
})
.parse_next(input)
} }
} }
@ -439,6 +459,7 @@ mod test_type {
impl Category { impl Category {
pub fn parse(input: &str) -> IResult<&str, Category> { pub fn parse(input: &str) -> IResult<&str, Category> {
trace("category", move |input| {
let symbols = winnow::token::one_of(['A', 'B', 'Z', 'C', 'D', '_']); let symbols = winnow::token::one_of(['A', 'B', 'Z', 'C', 'D', '_']);
symbols symbols
.map(|c| match c { .map(|c| match c {
@ -451,6 +472,8 @@ impl Category {
_ => unreachable!("parser won't select this option"), _ => unreachable!("parser won't select this option"),
}) })
.parse_next(input) .parse_next(input)
})
.parse_next(input)
} }
} }
@ -475,6 +498,7 @@ mod test_category {
impl Tag { impl Tag {
pub fn parse(input: &str) -> IResult<&str, Tag> { pub fn parse(input: &str) -> IResult<&str, Tag> {
trace("tag", move |input| {
let symbols = winnow::token::one_of(['.', 'v', 'V', '-', 'x']); let symbols = winnow::token::one_of(['.', 'v', 'V', '-', 'x']);
symbols symbols
.map(|c| match c { .map(|c| match c {
@ -486,6 +510,8 @@ impl Tag {
_ => unreachable!("parser won't select this option"), _ => unreachable!("parser won't select this option"),
}) })
.parse_next(input) .parse_next(input)
})
.parse_next(input)
} }
} }
@ -510,6 +536,7 @@ mod test_tag {
impl Pos { impl Pos {
pub fn parse(input: &str) -> IResult<&str, Pos> { pub fn parse(input: &str) -> IResult<&str, Pos> {
trace("pos", move |input| {
winnow::branch::alt(( winnow::branch::alt((
"<N>".value(Pos::Noun), "<N>".value(Pos::Noun),
"<V>".value(Pos::Verb), "<V>".value(Pos::Verb),
@ -517,6 +544,8 @@ impl Pos {
"<Adv>".value(Pos::Adverb), "<Adv>".value(Pos::Adverb),
)) ))
.parse_next(input) .parse_next(input)
})
.parse_next(input)
} }
} }