2023-02-21 12:11:24 -05:00
|
|
|
use winnow::prelude::*;
|
2023-07-14 13:32:16 -04:00
|
|
|
use winnow::trace::trace;
|
2020-04-07 20:50:06 -04:00
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct ClusterIter<'i> {
|
|
|
|
input: &'i str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i> ClusterIter<'i> {
|
|
|
|
pub fn new(input: &'i str) -> Self {
|
|
|
|
Self { input }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i> Iterator for ClusterIter<'i> {
|
|
|
|
type Item = Cluster;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Cluster> {
|
2023-07-14 13:44:39 -04:00
|
|
|
self.input = self.input.trim_start();
|
2023-07-14 13:48:41 -04:00
|
|
|
Cluster::parse_.parse_next(&mut self.input).ok()
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_cluster_iter {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
|
|
|
let iter = ClusterIter::new(
|
|
|
|
"# acknowledgment <verified> (level 35)
|
|
|
|
A Cv: acknowledgment / Av B C: acknowledgement
|
|
|
|
A Cv: acknowledgments / Av B C: acknowledgements
|
|
|
|
A Cv: acknowledgment's / Av B C: acknowledgement's
|
|
|
|
|
|
|
|
",
|
|
|
|
);
|
2021-11-08 12:36:05 -05:00
|
|
|
assert_eq!(iter.count(), 1);
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
|
|
|
let iter = ClusterIter::new(
|
|
|
|
"# acknowledgment <verified> (level 35)
|
|
|
|
A Cv: acknowledgment / Av B C: acknowledgement
|
|
|
|
A Cv: acknowledgments / Av B C: acknowledgements
|
|
|
|
A Cv: acknowledgment's / Av B C: acknowledgement's
|
|
|
|
|
|
|
|
# acknowledgment <verified> (level 35)
|
|
|
|
A Cv: acknowledgment / Av B C: acknowledgement
|
|
|
|
A Cv: acknowledgments / Av B C: acknowledgements
|
|
|
|
A Cv: acknowledgment's / Av B C: acknowledgement's
|
|
|
|
|
|
|
|
",
|
|
|
|
);
|
2021-11-08 12:36:05 -05:00
|
|
|
assert_eq!(iter.count(), 2);
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cluster {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("cluster", move |input: &mut &str| {
|
2023-07-14 13:32:16 -04:00
|
|
|
let header = (
|
|
|
|
"#",
|
|
|
|
winnow::ascii::space0,
|
2023-07-14 13:23:13 -04:00
|
|
|
winnow::ascii::not_line_ending,
|
|
|
|
winnow::ascii::line_ending,
|
2023-07-14 13:32:16 -04:00
|
|
|
);
|
|
|
|
let note = winnow::combinator::preceded(
|
|
|
|
("##", winnow::ascii::space0),
|
|
|
|
winnow::combinator::terminated(
|
|
|
|
winnow::ascii::not_line_ending,
|
|
|
|
winnow::ascii::line_ending,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
let mut cluster = (
|
|
|
|
winnow::combinator::opt(header),
|
|
|
|
winnow::combinator::repeat(
|
|
|
|
1..,
|
2023-07-14 13:48:41 -04:00
|
|
|
winnow::combinator::terminated(Entry::parse_, winnow::ascii::line_ending),
|
2023-07-14 13:32:16 -04:00
|
|
|
),
|
|
|
|
winnow::combinator::repeat(0.., note),
|
|
|
|
);
|
2023-07-14 13:44:39 -04:00
|
|
|
let (header, entries, notes): (_, _, Vec<_>) = cluster.parse_next(input)?;
|
2023-07-14 13:32:16 -04:00
|
|
|
|
|
|
|
let header = header.map(|s| s.2.to_owned());
|
|
|
|
let notes = notes.into_iter().map(|s| s.to_owned()).collect();
|
|
|
|
let c = Self {
|
|
|
|
header,
|
|
|
|
entries,
|
|
|
|
notes,
|
|
|
|
};
|
2023-07-14 13:44:39 -04:00
|
|
|
Ok(c)
|
2023-07-14 13:32:16 -04:00
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_cluster {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Cluster::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek(
|
|
|
|
"# acknowledgment <verified> (level 35)
|
2020-04-07 20:50:06 -04:00
|
|
|
A Cv: acknowledgment / Av B C: acknowledgement
|
|
|
|
A Cv: acknowledgments / Av B C: acknowledgements
|
|
|
|
A Cv: acknowledgment's / Av B C: acknowledgement's
|
|
|
|
|
|
|
|
",
|
2023-07-14 13:44:39 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(
|
|
|
|
actual.header,
|
|
|
|
Some("acknowledgment <verified> (level 35)".to_owned())
|
|
|
|
);
|
|
|
|
assert_eq!(actual.entries.len(), 3);
|
|
|
|
assert_eq!(actual.notes.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_notes() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Cluster::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek(
|
|
|
|
"# coloration <verified> (level 50)
|
2020-04-07 20:50:06 -04:00
|
|
|
A B C: coloration / B. Cv: colouration
|
|
|
|
A B C: colorations / B. Cv: colourations
|
|
|
|
A B C: coloration's / B. Cv: colouration's
|
|
|
|
## OED has coloration as the preferred spelling and discolouration as a
|
|
|
|
## variant for British Engl or some reason
|
|
|
|
|
|
|
|
",
|
2023-07-14 13:44:39 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(
|
|
|
|
actual.header,
|
|
|
|
Some("coloration <verified> (level 50)".to_owned())
|
|
|
|
);
|
|
|
|
assert_eq!(actual.entries.len(), 3);
|
|
|
|
assert_eq!(actual.notes.len(), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("entry", move |input: &mut &str| {
|
2023-07-14 13:32:16 -04:00
|
|
|
let var_sep = (winnow::ascii::space0, '/', winnow::ascii::space0);
|
2023-07-14 13:44:39 -04:00
|
|
|
let variants =
|
2023-12-04 13:15:09 -05:00
|
|
|
winnow::combinator::separated(1.., Variant::parse_, var_sep).parse_next(input)?;
|
2023-07-14 13:32:16 -04:00
|
|
|
|
|
|
|
let desc_sep = (winnow::ascii::space0, '|');
|
2023-07-14 13:44:39 -04:00
|
|
|
let description =
|
2023-07-14 13:32:16 -04:00
|
|
|
winnow::combinator::opt((desc_sep, Self::parse_description)).parse_next(input)?;
|
|
|
|
|
|
|
|
let comment_sep = (winnow::ascii::space0, '#');
|
2023-07-14 13:44:39 -04:00
|
|
|
let comment = winnow::combinator::opt((
|
2023-07-14 13:32:16 -04:00
|
|
|
comment_sep,
|
2023-07-14 13:23:13 -04:00
|
|
|
winnow::ascii::space1,
|
2023-07-14 13:32:16 -04:00
|
|
|
winnow::ascii::not_line_ending,
|
|
|
|
))
|
2023-02-21 12:11:24 -05:00
|
|
|
.parse_next(input)?;
|
2020-04-07 20:50:06 -04:00
|
|
|
|
2023-07-14 13:32:16 -04:00
|
|
|
let mut e = match description {
|
|
|
|
Some((_, description)) => description,
|
|
|
|
None => Self {
|
|
|
|
variants: Vec::new(),
|
|
|
|
pos: None,
|
|
|
|
archaic: false,
|
|
|
|
note: false,
|
|
|
|
description: None,
|
|
|
|
comment: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
e.variants = variants;
|
|
|
|
e.comment = comment.map(|c| c.2.to_owned());
|
2023-07-14 13:44:39 -04:00
|
|
|
Ok(e)
|
2023-07-14 13:32:16 -04:00
|
|
|
})
|
|
|
|
.parse_next(input)
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:44:39 -04:00
|
|
|
fn parse_description(input: &mut &str) -> PResult<Self, ()> {
|
|
|
|
trace("description", move |input: &mut &str| {
|
|
|
|
let (pos, archaic, note, description) = (
|
2023-07-14 13:48:41 -04:00
|
|
|
winnow::combinator::opt((winnow::ascii::space1, Pos::parse_)),
|
2023-07-14 13:32:16 -04:00
|
|
|
winnow::combinator::opt((winnow::ascii::space1, "(-)")),
|
|
|
|
winnow::combinator::opt((winnow::ascii::space1, "--")),
|
|
|
|
winnow::combinator::opt((
|
|
|
|
winnow::ascii::space1,
|
|
|
|
winnow::token::take_till0(('\n', '\r', '#')),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
.parse_next(input)?;
|
|
|
|
|
|
|
|
let variants = Vec::new();
|
|
|
|
let pos = pos.map(|(_, p)| p);
|
|
|
|
let archaic = archaic.is_some();
|
|
|
|
let note = note.is_some();
|
|
|
|
let description = description.map(|(_, d)| d.to_owned());
|
|
|
|
let e = Self {
|
|
|
|
variants,
|
|
|
|
pos,
|
|
|
|
archaic,
|
|
|
|
note,
|
|
|
|
description,
|
|
|
|
comment: None,
|
|
|
|
};
|
2023-07-14 13:44:39 -04:00
|
|
|
Ok(e)
|
2023-07-14 13:32:16 -04:00
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_entry {
|
2021-06-28 14:50:59 -04:00
|
|
|
#![allow(clippy::bool_assert_comparison)]
|
2020-04-07 20:50:06 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_variant_only() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek("A Cv: acknowledgment's / Av B C: acknowledgement's\n")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 2);
|
|
|
|
assert_eq!(actual.pos, None);
|
|
|
|
assert_eq!(actual.archaic, false);
|
|
|
|
assert_eq!(actual.note, false);
|
|
|
|
assert_eq!(actual.description, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_description() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek("A C: prize / B: prise | otherwise\n")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 2);
|
|
|
|
assert_eq!(actual.pos, None);
|
|
|
|
assert_eq!(actual.archaic, false);
|
|
|
|
assert_eq!(actual.note, false);
|
|
|
|
assert_eq!(actual.description, Some("otherwise".to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pos() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek("A B C: practice / AV Cv: practise | <N>\n")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 2);
|
|
|
|
assert_eq!(actual.pos, Some(Pos::Noun));
|
|
|
|
assert_eq!(actual.archaic, false);
|
|
|
|
assert_eq!(actual.note, false);
|
|
|
|
assert_eq!(actual.description, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_archaic() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek("A: bark / Av B: barque | (-) ship\n")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 2);
|
|
|
|
assert_eq!(actual.pos, None);
|
|
|
|
assert_eq!(actual.archaic, true);
|
|
|
|
assert_eq!(actual.note, false);
|
|
|
|
assert_eq!(actual.description, Some("ship".to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_note() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_
|
|
|
|
.parse_peek("_: cabbies | -- plural\n")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 1);
|
|
|
|
assert_eq!(actual.pos, None);
|
|
|
|
assert_eq!(actual.archaic, false);
|
|
|
|
assert_eq!(actual.note, true);
|
|
|
|
assert_eq!(actual.description, Some("plural".to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trailing_comment() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Entry::parse_.parse_peek(
|
2020-04-07 20:50:06 -04:00
|
|
|
"A B: accursed / AV B-: accurst # ODE: archaic, M-W: 'or' but can find little evidence of use\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(actual.variants.len(), 2);
|
|
|
|
assert_eq!(actual.pos, None);
|
|
|
|
assert_eq!(actual.archaic, false);
|
|
|
|
assert_eq!(actual.note, false);
|
|
|
|
assert_eq!(actual.description, None);
|
|
|
|
assert_eq!(
|
|
|
|
actual.comment,
|
|
|
|
Some("ODE: archaic, M-W: 'or' but can find little evidence of use".to_owned())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variant {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("variant", move |input: &mut &str| {
|
2023-12-04 13:15:09 -05:00
|
|
|
let types = winnow::combinator::separated(1.., Type::parse_, winnow::ascii::space1);
|
2023-07-14 13:32:16 -04:00
|
|
|
let sep = (":", winnow::ascii::space0);
|
2023-07-14 13:44:39 -04:00
|
|
|
let (types, word) =
|
2023-07-14 13:32:16 -04:00
|
|
|
winnow::combinator::separated_pair(types, sep, word).parse_next(input)?;
|
|
|
|
let v = Self { types, word };
|
2023-07-14 13:44:39 -04:00
|
|
|
Ok(v)
|
2023-07-14 13:32:16 -04:00
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:44:39 -04:00
|
|
|
fn word(input: &mut &str) -> PResult<String, ()> {
|
|
|
|
trace("word", move |input: &mut &str| {
|
2023-07-14 13:32:16 -04:00
|
|
|
winnow::token::take_till1(|item: char| item.is_ascii_whitespace())
|
|
|
|
.map(|s: &str| s.to_owned().replace('_', " "))
|
|
|
|
.parse_next(input)
|
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_variant {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Variant::parse_.parse_peek("A Cv: acknowledgment ").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " ");
|
|
|
|
assert_eq!(
|
|
|
|
actual.types,
|
|
|
|
vec![
|
|
|
|
Type {
|
|
|
|
category: Category::American,
|
|
|
|
tag: None,
|
|
|
|
num: None,
|
|
|
|
},
|
|
|
|
Type {
|
|
|
|
category: Category::Canadian,
|
|
|
|
tag: Some(Tag::Variant),
|
|
|
|
num: None,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(actual.word, "acknowledgment");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extra() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Variant::parse_
|
2023-07-14 13:44:39 -04:00
|
|
|
.parse_peek("A Cv: acknowledgment's / Av B C: acknowledgement's")
|
|
|
|
.unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " / Av B C: acknowledgement's");
|
|
|
|
assert_eq!(
|
|
|
|
actual.types,
|
|
|
|
vec![
|
|
|
|
Type {
|
|
|
|
category: Category::American,
|
|
|
|
tag: None,
|
|
|
|
num: None,
|
|
|
|
},
|
|
|
|
Type {
|
|
|
|
category: Category::Canadian,
|
|
|
|
tag: Some(Tag::Variant),
|
|
|
|
num: None,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(actual.word, "acknowledgment's");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_underscore() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Variant::parse_.parse_peek("_: air_gun\n").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "\n");
|
|
|
|
assert_eq!(
|
|
|
|
actual.types,
|
|
|
|
vec![Type {
|
|
|
|
category: Category::Other,
|
|
|
|
tag: None,
|
|
|
|
num: None,
|
|
|
|
},]
|
|
|
|
);
|
|
|
|
assert_eq!(actual.word, "air gun");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Type {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Type, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("type", move |input: &mut &str| {
|
2023-07-14 13:48:41 -04:00
|
|
|
let category = Category::parse_(input)?;
|
|
|
|
let tag = winnow::combinator::opt(Tag::parse_).parse_next(input)?;
|
2023-07-14 13:44:39 -04:00
|
|
|
let num = winnow::combinator::opt(winnow::ascii::digit1).parse_next(input)?;
|
2023-07-14 13:32:16 -04:00
|
|
|
let num = num.map(|s| s.parse().expect("parser ensured its a number"));
|
|
|
|
let t = Type { category, tag, num };
|
2023-07-14 13:44:39 -04:00
|
|
|
Ok(t)
|
2023-07-14 13:32:16 -04:00
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_type {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid() {
|
|
|
|
// Having nothing after `A` causes an incomplete parse. Shouldn't be a problem for my use
|
|
|
|
// cases.
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Type::parse_.parse_peek("A ").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " ");
|
|
|
|
assert_eq!(actual.category, Category::American);
|
|
|
|
assert_eq!(actual.tag, None);
|
|
|
|
assert_eq!(actual.num, None);
|
|
|
|
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Type::parse_.parse_peek("Bv ").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " ");
|
|
|
|
assert_eq!(actual.category, Category::BritishIse);
|
|
|
|
assert_eq!(actual.tag, Some(Tag::Variant));
|
|
|
|
assert_eq!(actual.num, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extra() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Type::parse_.parse_peek("Z foobar").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " foobar");
|
|
|
|
assert_eq!(actual.category, Category::BritishIze);
|
|
|
|
assert_eq!(actual.tag, None);
|
|
|
|
assert_eq!(actual.num, None);
|
|
|
|
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Type::parse_.parse_peek("C- foobar").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " foobar");
|
|
|
|
assert_eq!(actual.category, Category::Canadian);
|
|
|
|
assert_eq!(actual.tag, Some(Tag::Possible));
|
|
|
|
assert_eq!(actual.num, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_num() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Type::parse_.parse_peek("Av1 ").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " ");
|
|
|
|
assert_eq!(actual.category, Category::American);
|
|
|
|
assert_eq!(actual.tag, Some(Tag::Variant));
|
|
|
|
assert_eq!(actual.num, Some(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Category {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("category", move |input: &mut &str| {
|
2023-07-14 13:32:16 -04:00
|
|
|
let symbols = winnow::token::one_of(['A', 'B', 'Z', 'C', 'D', '_']);
|
|
|
|
symbols
|
|
|
|
.map(|c| match c {
|
|
|
|
'A' => Category::American,
|
|
|
|
'B' => Category::BritishIse,
|
|
|
|
'Z' => Category::BritishIze,
|
|
|
|
'C' => Category::Canadian,
|
|
|
|
'D' => Category::Australian,
|
|
|
|
'_' => Category::Other,
|
|
|
|
_ => unreachable!("parser won't select this option"),
|
|
|
|
})
|
|
|
|
.parse_next(input)
|
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_category {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Category::parse_.parse_peek("A").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "");
|
|
|
|
assert_eq!(actual, Category::American);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extra() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Category::parse_.parse_peek("_ foobar").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " foobar");
|
|
|
|
assert_eq!(actual, Category::Other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tag {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("tag", move |input: &mut &str| {
|
2023-07-14 13:32:16 -04:00
|
|
|
let symbols = winnow::token::one_of(['.', 'v', 'V', '-', 'x']);
|
|
|
|
symbols
|
|
|
|
.map(|c| match c {
|
|
|
|
'.' => Tag::Eq,
|
|
|
|
'v' => Tag::Variant,
|
|
|
|
'V' => Tag::Seldom,
|
|
|
|
'-' => Tag::Possible,
|
|
|
|
'x' => Tag::Improper,
|
|
|
|
_ => unreachable!("parser won't select this option"),
|
|
|
|
})
|
|
|
|
.parse_next(input)
|
|
|
|
})
|
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_tag {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Tag::parse_.parse_peek(".").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "");
|
|
|
|
assert_eq!(actual, Tag::Eq);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extra() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Tag::parse_.parse_peek("x foobar").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " foobar");
|
|
|
|
assert_eq!(actual, Tag::Improper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pos {
|
2023-07-14 13:48:41 -04:00
|
|
|
pub fn parse(input: &str) -> Result<Self, ParseError> {
|
|
|
|
Self::parse_.parse(input).map_err(|_err| ParseError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_(input: &mut &str) -> PResult<Self, ()> {
|
2023-07-14 13:44:39 -04:00
|
|
|
trace("pos", move |input: &mut &str| {
|
|
|
|
winnow::combinator::alt((
|
2023-07-14 13:32:16 -04:00
|
|
|
"<N>".value(Pos::Noun),
|
|
|
|
"<V>".value(Pos::Verb),
|
|
|
|
"<Adj>".value(Pos::Adjective),
|
|
|
|
"<Adv>".value(Pos::Adverb),
|
|
|
|
))
|
|
|
|
.parse_next(input)
|
|
|
|
})
|
2023-03-18 05:11:55 -04:00
|
|
|
.parse_next(input)
|
2020-04-07 20:50:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_pos {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Pos::parse_.parse_peek("<N>").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, "");
|
|
|
|
assert_eq!(actual, Pos::Noun);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extra() {
|
2023-07-14 13:48:41 -04:00
|
|
|
let (input, actual) = Pos::parse_.parse_peek("<Adj> foobar").unwrap();
|
2020-04-07 20:50:06 -04:00
|
|
|
assert_eq!(input, " foobar");
|
|
|
|
assert_eq!(actual, Pos::Adjective);
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 13:48:41 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ParseError;
|
|
|
|
|
|
|
|
impl std::fmt::Display for ParseError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "invalid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for ParseError {}
|