impl Parseable trait

This commit is contained in:
John Turner
2025-10-28 09:44:32 +00:00
parent 92a8e46082
commit b54616a6dd
6 changed files with 367 additions and 272 deletions

View File

@@ -334,14 +334,14 @@ impl fmt::Display for Atom {
mod test { mod test {
use mon::{Parser, input::InputIter}; use mon::{Parser, input::InputIter};
use crate::atom::parsers; use super::*;
use crate::Parseable;
#[test] #[test]
fn test_version_display() { fn test_version_display() {
let s = "1.0.0_alpha1_beta1-r1"; let s = "1.0.0_alpha1_beta1-r1";
let version = parsers::version() let version = Version::parser().parse_finished(InputIter::new(s)).unwrap();
.parse_finished(InputIter::new(s))
.unwrap();
assert_eq!(version.to_string().as_str(), s); assert_eq!(version.to_string().as_str(), s);
} }
@@ -349,7 +349,7 @@ mod test {
#[test] #[test]
fn test_display_atom() { fn test_display_atom() {
let s = "!!>=foo/bar-1.0.0v_alpha1_beta1-r1:slot/sub=[a,b,c]"; let s = "!!>=foo/bar-1.0.0v_alpha1_beta1-r1:slot/sub=[a,b,c]";
let atom = parsers::atom().parse_finished(InputIter::new(s)).unwrap(); let atom = Atom::parser().parse_finished(InputIter::new(s)).unwrap();
assert_eq!(atom.to_string().as_str(), s); assert_eq!(atom.to_string().as_str(), s);
} }

View File

@@ -3,232 +3,291 @@ use core::option::Option::None;
use mon::{Parser, r#if, numeric1, one_of, tag}; use mon::{Parser, r#if, numeric1, one_of, tag};
use crate::{ use crate::{
Parseable,
atom::{ atom::{
Atom, Blocker, Category, Name, Slot, SlotName, SlotOperator, UseDep, UseDepCondition, Atom, Blocker, Category, Name, Slot, SlotName, SlotOperator, UseDep, UseDepCondition,
UseDepNegate, UseDepSign, Version, VersionNumber, VersionOperator, VersionSuffix, UseDepNegate, UseDepSign, Version, VersionNumber, VersionOperator, VersionSuffix,
VersionSuffixKind, VersionSuffixKind,
}, },
useflag::parsers::useflag, useflag::UseFlag,
}; };
pub fn blocker<'a>() -> impl Parser<&'a str, Output = Blocker> { impl<'a> Parseable<'a, &'a str> for Blocker {
tag("!!") type Parser = impl Parser<&'a str, Output = Self>;
.map(|_| Blocker::Strong)
.or(tag("!").map(|_| Blocker::Weak)) fn parser() -> Self::Parser {
tag("!!")
.map(|_| Blocker::Strong)
.or(tag("!").map(|_| Blocker::Weak))
}
} }
pub fn version_operator<'a>() -> impl Parser<&'a str, Output = VersionOperator> { impl<'a> Parseable<'a, &'a str> for VersionOperator {
tag("<=") type Parser = impl Parser<&'a str, Output = Self>;
.map(|_| VersionOperator::LtEq)
.or(tag(">=").map(|_| VersionOperator::GtEq)) fn parser() -> Self::Parser {
.or(tag("<").map(|_| VersionOperator::Lt)) tag("<=")
.or(tag(">").map(|_| VersionOperator::Gt)) .map(|_| VersionOperator::LtEq)
.or(tag("=").map(|_| VersionOperator::Eq)) .or(tag(">=").map(|_| VersionOperator::GtEq))
.or(tag("~").map(|_| VersionOperator::Roughly)) .or(tag("<").map(|_| VersionOperator::Lt))
.or(tag(">").map(|_| VersionOperator::Gt))
.or(tag("=").map(|_| VersionOperator::Eq))
.or(tag("~").map(|_| VersionOperator::Roughly))
}
} }
pub fn version_number<'a>() -> impl Parser<&'a str, Output = VersionNumber> { impl<'a> Parseable<'a, &'a str> for VersionNumber {
numeric1() type Parser = impl Parser<&'a str, Output = Self>;
.followed_by(tag("*").opt())
.recognize() fn parser() -> Self::Parser {
.map(|output: &str| VersionNumber(output.to_string())) numeric1()
.followed_by(tag("*").opt())
.recognize()
.map(|output: &str| VersionNumber(output.to_string()))
}
} }
pub fn version_suffix_kind<'a>() -> impl Parser<&'a str, Output = VersionSuffixKind> { impl<'a> Parseable<'a, &'a str> for VersionSuffixKind {
tag("alpha") type Parser = impl Parser<&'a str, Output = Self>;
.map(|_| VersionSuffixKind::Alpha)
.or(tag("beta").map(|_| VersionSuffixKind::Beta)) fn parser() -> Self::Parser {
.or(tag("pre").map(|_| VersionSuffixKind::Pre)) tag("alpha")
.or(tag("rc").map(|_| VersionSuffixKind::Rc)) .map(|_| VersionSuffixKind::Alpha)
.or(tag("p").map(|_| VersionSuffixKind::P)) .or(tag("beta").map(|_| VersionSuffixKind::Beta))
.or(tag("pre").map(|_| VersionSuffixKind::Pre))
.or(tag("rc").map(|_| VersionSuffixKind::Rc))
.or(tag("p").map(|_| VersionSuffixKind::P))
}
} }
pub fn version_suffix<'a>() -> impl Parser<&'a str, Output = VersionSuffix> { impl<'a> Parseable<'a, &'a str> for VersionSuffix {
version_suffix_kind() type Parser = impl Parser<&'a str, Output = Self>;
.and(version_number().opt())
.map(|(kind, number)| VersionSuffix { kind, number }) fn parser() -> Self::Parser {
VersionSuffixKind::parser()
.and(VersionNumber::parser().opt())
.map(|(kind, number)| VersionSuffix { kind, number })
}
} }
pub fn version<'a>() -> impl Parser<&'a str, Output = Version> { impl<'a> Parseable<'a, &'a str> for Version {
let numbers = version_number().separated_list(tag("."), 1..); type Parser = impl Parser<&'a str, Output = Self>;
let suffixes = version_suffix().separated_list(tag("_"), 0..);
let rev = version_number().preceded_by(tag("-r"));
numbers fn parser() -> Self::Parser {
.and(r#if(|c: &char| c.is_ascii_alphabetic() && c.is_ascii_lowercase()).opt()) let numbers = VersionNumber::parser().separated_list(tag("."), 1..);
.and(suffixes.preceded_by(tag("_")).opt()) let suffixes = VersionSuffix::parser().separated_list(tag("_"), 0..);
.and(rev.opt()) let rev = VersionNumber::parser().preceded_by(tag("-r"));
.map(|(((numbers, letter), suffixes), rev)| Version {
numbers, numbers
letter, .and(r#if(|c: &char| c.is_ascii_alphabetic() && c.is_ascii_lowercase()).opt())
suffixes: suffixes.unwrap_or(Vec::new()), .and(suffixes.preceded_by(tag("_")).opt())
rev, .and(rev.opt())
}) .map(|(((numbers, letter), suffixes), rev)| Version {
numbers,
letter,
suffixes: suffixes.unwrap_or(Vec::new()),
rev,
})
}
} }
pub fn category<'a>() -> impl Parser<&'a str, Output = Category> { impl<'a> Parseable<'a, &'a str> for Category {
let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_'); type Parser = impl Parser<&'a str, Output = Self>;
let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_.-".contains(*c)).list(0..);
start fn parser() -> Self::Parser {
.and(rest) let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_');
.recognize() let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_.-".contains(*c)).list(0..);
.map(|output: &str| Category(output.to_string()))
start
.and(rest)
.recognize()
.map(|output: &str| Category(output.to_string()))
}
} }
pub fn name<'a>() -> impl Parser<&'a str, Output = Name> { impl<'a> Parseable<'a, &'a str> for Name {
let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_'); type Parser = impl Parser<&'a str, Output = Self>;
let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "_+".contains(*c))
.or(one_of("-".chars()).and_not(
version().preceded_by(tag("-")).followed_by(
r#if(|c: &char| c.is_ascii_alphanumeric() || "_+-".contains(*c)).not(),
),
))
.list(0..);
start fn parser() -> Self::Parser {
.and(rest) let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_');
.recognize() let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "_+".contains(*c))
.map(|output: &str| Name(output.to_string())) .or(
one_of("-".chars()).and_not(Version::parser().preceded_by(tag("-")).followed_by(
r#if(|c: &char| c.is_ascii_alphanumeric() || "_+-".contains(*c)).not(),
)),
)
.list(0..);
start
.and(rest)
.recognize()
.map(|output: &str| Name(output.to_string()))
}
} }
pub fn slot_operator<'a>() -> impl Parser<&'a str, Output = SlotOperator> { impl<'a> Parseable<'a, &'a str> for SlotOperator {
tag("=") type Parser = impl Parser<&'a str, Output = Self>;
.map(|_| SlotOperator::Eq)
.or(tag("*").map(|_| SlotOperator::Star)) fn parser() -> Self::Parser {
tag("=")
.map(|_| SlotOperator::Eq)
.or(tag("*").map(|_| SlotOperator::Star))
}
} }
pub fn slotname<'a>() -> impl Parser<&'a str, Output = SlotName> { impl<'a> Parseable<'a, &'a str> for SlotName {
let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_'); type Parser = impl Parser<&'a str, Output = Self>;
let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_.-".contains(*c)).list(0..);
start fn parser() -> Self::Parser {
.and(rest) let start = r#if(|c: &char| c.is_ascii_alphanumeric() || *c == '_');
.recognize() let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_.-".contains(*c)).list(0..);
.map(|output: &str| SlotName(output.to_string()))
start
.and(rest)
.recognize()
.map(|output: &str| SlotName(output.to_string()))
}
} }
pub fn slot<'a>() -> impl Parser<&'a str, Output = Slot> { impl<'a> Parseable<'a, &'a str> for Slot {
slotname() type Parser = impl Parser<&'a str, Output = Self>;
.opt()
.and(slotname().preceded_by(tag("/")).opt())
.and(slot_operator().opt())
.map(|((slot, sub), operator)| Slot {
slot,
sub,
operator,
})
}
pub fn usedep_sign<'a>() -> impl Parser<&'a str, Output = UseDepSign> { fn parser() -> Self::Parser {
tag("(-)") SlotName::parser()
.map(|_| UseDepSign::Disabled) .opt()
.or(tag("(+)").map(|_| UseDepSign::Enabled)) .and(SlotName::parser().preceded_by(tag("/")).opt())
} .and(SlotOperator::parser().opt())
.map(|((slot, sub), operator)| Slot {
pub fn usedep<'a>() -> impl Parser<&'a str, Output = UseDep> {
let a = useflag()
.and(usedep_sign().opt())
.preceded_by(tag("-"))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Minus),
flag,
sign,
condition: None,
});
let b = useflag()
.and(usedep_sign().opt())
.preceded_by(tag("!"))
.followed_by(tag("?"))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Exclamation),
flag,
sign,
condition: Some(UseDepCondition::Question),
});
let c = useflag()
.and(usedep_sign().opt())
.followed_by(tag("?"))
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: Some(UseDepCondition::Question),
});
let d = useflag()
.and(usedep_sign().opt())
.preceded_by(tag("!"))
.followed_by(tag("="))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Exclamation),
flag,
sign,
condition: Some(UseDepCondition::Eq),
});
let e = useflag()
.and(usedep_sign().opt())
.followed_by(tag("="))
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: Some(UseDepCondition::Eq),
});
let f = useflag()
.and(usedep_sign().opt())
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: None,
});
a.or(b).or(c).or(d).or(e).or(f)
}
pub fn atom<'a>() -> impl Parser<&'a str, Output = Atom> {
blocker()
.opt()
.and(version_operator().opt())
.and(category())
.and(name().preceded_by(tag("/")))
.and(version().preceded_by(tag("-")).opt())
.and(slot().preceded_by(tag(":")).opt())
.and(
usedep()
.separated_list(tag(","), 0..)
.delimited_by(tag("["), tag("]"))
.opt(),
)
.map(
|((((((blocker, version_operator), category), name), version), slot), usedeps)| Atom {
blocker,
version_operator,
category,
name,
version,
slot, slot,
usedeps: usedeps.unwrap_or(Vec::new()), sub,
}, operator,
) })
.verify_output(|atom| match (&atom.version_operator, &atom.version) { }
(Some(VersionOperator::Eq), Some(_)) => true, }
(Some(_), Some(version))
if !version impl<'a> Parseable<'a, &'a str> for UseDepSign {
.numbers() type Parser = impl Parser<&'a str, Output = Self>;
.iter()
.any(|number| number.get().contains("*")) => fn parser() -> Self::Parser {
{ tag("(-)")
true .map(|_| UseDepSign::Disabled)
} .or(tag("(+)").map(|_| UseDepSign::Enabled))
(None, None) => true, }
_ => false, }
})
impl<'a> Parseable<'a, &'a str> for UseDep {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
let a = UseFlag::parser()
.and(UseDepSign::parser().opt())
.preceded_by(tag("-"))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Minus),
flag,
sign,
condition: None,
});
let b = UseFlag::parser()
.and(UseDepSign::parser().opt())
.preceded_by(tag("!"))
.followed_by(tag("?"))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Exclamation),
flag,
sign,
condition: Some(UseDepCondition::Question),
});
let c = UseFlag::parser()
.and(UseDepSign::parser().opt())
.followed_by(tag("?"))
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: Some(UseDepCondition::Question),
});
let d = UseFlag::parser()
.and(UseDepSign::parser().opt())
.preceded_by(tag("!"))
.followed_by(tag("="))
.map(|(flag, sign)| UseDep {
negate: Some(UseDepNegate::Exclamation),
flag,
sign,
condition: Some(UseDepCondition::Eq),
});
let e = UseFlag::parser()
.and(UseDepSign::parser().opt())
.followed_by(tag("="))
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: Some(UseDepCondition::Eq),
});
let f = UseFlag::parser()
.and(UseDepSign::parser().opt())
.map(|(flag, sign)| UseDep {
negate: None,
flag,
sign,
condition: None,
});
a.or(b).or(c).or(d).or(e).or(f)
}
}
impl<'a> Parseable<'a, &'a str> for Atom {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
Blocker::parser()
.opt()
.and(VersionOperator::parser().opt())
.and(Category::parser())
.and(Name::parser().preceded_by(tag("/")))
.and(Version::parser().preceded_by(tag("-")).opt())
.and(Slot::parser().preceded_by(tag(":")).opt())
.and(
UseDep::parser()
.separated_list(tag(","), 0..)
.delimited_by(tag("["), tag("]"))
.opt(),
)
.map(
|((((((blocker, version_operator), category), name), version), slot), usedeps)| {
Atom {
blocker,
version_operator,
category,
name,
version,
slot,
usedeps: usedeps.unwrap_or(Vec::new()),
}
},
)
.verify_output(|atom| match (&atom.version_operator, &atom.version) {
(Some(VersionOperator::Eq), Some(_)) => true,
(Some(_), Some(version))
if !version
.numbers()
.iter()
.any(|number| number.get().contains("*")) =>
{
true
}
(None, None) => true,
_ => false,
})
}
} }
#[cfg(test)] #[cfg(test)]
@@ -242,14 +301,14 @@ mod test {
fn test_version() { fn test_version() {
let it = InputIter::new("1.0.0v_alpha1_beta1-r1"); let it = InputIter::new("1.0.0v_alpha1_beta1-r1");
version().check_finished(it).unwrap(); Version::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_name() { fn test_name() {
let it = InputIter::new("foo-1-bar-1.0.0"); let it = InputIter::new("foo-1-bar-1.0.0");
match name().parse(it) { match Name::parser().parse(it) {
Ok((_, output)) => { Ok((_, output)) => {
assert_eq!(output.0.as_str(), "foo-1-bar"); assert_eq!(output.0.as_str(), "foo-1-bar");
} }
@@ -263,7 +322,7 @@ mod test {
"!!>=cat/pkg-1-foo-1.0.0v_alpha1_p20250326-r1:primary/sub=[use,use=,!use=,use?,!use?,-use,use(+),use(-)]", "!!>=cat/pkg-1-foo-1.0.0v_alpha1_p20250326-r1:primary/sub=[use,use=,!use=,use?,!use?,-use,use(+),use(-)]",
); );
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
@@ -272,83 +331,83 @@ mod test {
"!!>=_.+-0-/_-test-T-123_beta1_-4a-6+-_p--1.00.02b_alpha3_pre_p4-r5:slot/_-+6-9=[test(+),test(-)]", "!!>=_.+-0-/_-test-T-123_beta1_-4a-6+-_p--1.00.02b_alpha3_pre_p4-r5:slot/_-+6-9=[test(+),test(-)]",
); );
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_atom_with_star_in_non_empty_slot() { fn test_atom_with_star_in_non_empty_slot() {
let it = InputIter::new("foo/bar:*/subslot"); let it = InputIter::new("foo/bar:*/subslot");
assert!(atom().check_finished(it).is_err()); assert!(Atom::parser().check_finished(it).is_err());
} }
#[test] #[test]
fn test_invalid_usedep() { fn test_invalid_usedep() {
let it = InputIter::new("foo-bar:slot/sub=[!use]"); let it = InputIter::new("foo-bar:slot/sub=[!use]");
assert!(atom().check_finished(it).is_err()) assert!(Atom::parser().check_finished(it).is_err())
} }
#[test] #[test]
fn test_empty_slot() { fn test_empty_slot() {
let it = InputIter::new("foo/bar:="); let it = InputIter::new("foo/bar:=");
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_usedep_with_underscore() { fn test_usedep_with_underscore() {
let it = InputIter::new("foo/bar[use_dep]"); let it = InputIter::new("foo/bar[use_dep]");
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_version_with_uppercase_letter() { fn test_version_with_uppercase_letter() {
let it = InputIter::new("=foo/bar-1.0.0V"); let it = InputIter::new("=foo/bar-1.0.0V");
assert!(atom().check_finished(it).is_err()); assert!(Atom::parser().check_finished(it).is_err());
} }
#[test] #[test]
fn test_version_with_version_operator_without_version() { fn test_version_with_version_operator_without_version() {
let it = InputIter::new("=foo/bar"); let it = InputIter::new("=foo/bar");
assert!(atom().check_finished(it).is_err()); assert!(Atom::parser().check_finished(it).is_err());
} }
#[test] #[test]
fn test_version_with_version_without_version_operator() { fn test_version_with_version_without_version_operator() {
let it = InputIter::new("foo/bar-1.0.0"); let it = InputIter::new("foo/bar-1.0.0");
assert!(atom().check_finished(it).is_err()); assert!(Atom::parser().check_finished(it).is_err());
} }
#[test] #[test]
fn test_atom_with_eq_version_operator() { fn test_atom_with_eq_version_operator() {
let it = InputIter::new("=foo/bar-1.0.0"); let it = InputIter::new("=foo/bar-1.0.0");
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_atom_with_star_in_version() { fn test_atom_with_star_in_version() {
let it = InputIter::new("=foo/bar-1.2*"); let it = InputIter::new("=foo/bar-1.2*");
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
#[test] #[test]
fn test_atom_with_star_in_version_without_eq_version_operator() { fn test_atom_with_star_in_version_without_eq_version_operator() {
let it = InputIter::new(">=foo/bar-1.2*"); let it = InputIter::new(">=foo/bar-1.2*");
assert!(atom().check_finished(it).is_err()); assert!(Atom::parser().check_finished(it).is_err());
} }
#[test] #[test]
fn test_atom_with_trailing_dash_and_letter() { fn test_atom_with_trailing_dash_and_letter() {
let it = InputIter::new("dev-db/mysql-connector-c"); let it = InputIter::new("dev-db/mysql-connector-c");
atom().check_finished(it).unwrap(); Atom::parser().check_finished(it).unwrap();
} }
} }

View File

@@ -1,61 +1,73 @@
use mon::{Parser, ParserResult, input::InputIter, tag, whitespace1}; use mon::{Parser, tag, whitespace1};
use crate::{ use crate::{
atom, Parseable,
atom::Atom,
depend::{Conditional, Expr}, depend::{Conditional, Expr},
useflag, useflag::UseFlag,
}; };
fn expr(it: InputIter<&str>) -> ParserResult<&str, Expr> { impl<'a> Parseable<'a, &'a str> for Expr {
let all_of = expr type Parser = impl Parser<&'a str, Output = Self>;
.separated_list(whitespace1(), 1..)
.delimited_by(tag("(").followed_by(whitespace1()), tag(")"))
.map(|exprs| Expr::AllOf(exprs));
let any_of = expr fn parser() -> Self::Parser {
.separated_list(whitespace1(), 1..) |it| {
.delimited_by(tag("(").followed_by(whitespace1()), tag(")")) let all_of = Expr::parser()
.preceded_by(tag("||").followed_by(whitespace1())) .separated_list(whitespace1(), 1..)
.map(|exprs| Expr::AnyOf(exprs)); .delimited_by(tag("(").followed_by(whitespace1()), tag(")"))
.map(|exprs| Expr::AllOf(exprs));
let one_of = expr let any_of = Expr::parser()
.separated_list(whitespace1(), 1..) .separated_list(whitespace1(), 1..)
.delimited_by(tag("(").followed_by(whitespace1()), tag(")")) .delimited_by(tag("(").followed_by(whitespace1()), tag(")"))
.preceded_by(tag("^^").followed_by(whitespace1())) .preceded_by(tag("||").followed_by(whitespace1()))
.map(|exprs| Expr::OneOf(exprs)); .map(|exprs| Expr::AnyOf(exprs));
atom::parsers::atom() let one_of = Expr::parser()
.map(|atom| Expr::Atom(atom)) .separated_list(whitespace1(), 1..)
.or(conditional().map(|conditional| Expr::Conditional(conditional))) .delimited_by(tag("(").followed_by(whitespace1()), tag(")"))
.or(any_of) .preceded_by(tag("^^").followed_by(whitespace1()))
.or(all_of) .map(|exprs| Expr::OneOf(exprs));
.or(one_of)
.parse(it) Atom::parser()
.map(|atom| Expr::Atom(atom))
.or(Conditional::parser().map(|conditional| Expr::Conditional(conditional)))
.or(any_of)
.or(all_of)
.or(one_of)
.parse(it)
}
}
} }
fn conditional<'a>() -> impl Parser<&'a str, Output = Conditional> { impl<'a> Parseable<'a, &'a str> for Conditional {
useflag::parsers::useflag() type Parser = impl Parser<&'a str, Output = Self>;
.preceded_by(tag("!"))
.followed_by(tag("?")) fn parser() -> Self::Parser {
.map(|flag| Conditional::Negative(flag)) UseFlag::parser()
.or(useflag::parsers::useflag() .preceded_by(tag("!"))
.followed_by(tag("?")) .followed_by(tag("?"))
.map(|flag| Conditional::Positive(flag))) .map(|flag| Conditional::Negative(flag))
} .or(UseFlag::parser()
.followed_by(tag("?"))
pub fn exprs<'a>() -> impl Parser<&'a str, Output = Vec<Expr>> { .map(|flag| Conditional::Positive(flag)))
expr.separated_list(whitespace1(), 0..) }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use mon::input::InputIter;
use super::*; use super::*;
#[test] #[test]
fn test_expr() { fn test_expr() {
let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )"); let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )");
exprs().check_finished(it).unwrap(); Expr::parser()
.separated_list(whitespace1(), 0..)
.check_finished(it)
.unwrap();
} }
} }

View File

@@ -1,5 +1,14 @@
#![deny(clippy::pedantic)] #![deny(clippy::pedantic)]
#![allow(dead_code, unstable_name_collisions)] #![allow(dead_code, unstable_name_collisions)]
#![feature(impl_trait_in_assoc_type)]
use mon::{Parser, input::Input};
pub trait Parseable<'a, I: Input + 'a> {
type Parser: Parser<I, Output = Self>;
fn parser() -> Self::Parser;
}
pub mod atom; pub mod atom;
pub mod depend; pub mod depend;

View File

@@ -1,26 +1,37 @@
use mon::{Parser, r#if, tag}; use mon::{Parser, r#if, tag};
use crate::useflag::{IUseFlag, UseFlag}; use crate::{
Parseable,
useflag::{IUseFlag, UseFlag},
};
pub fn useflag<'a>() -> impl Parser<&'a str, Output = UseFlag> { impl<'a> Parseable<'a, &'a str> for UseFlag {
let start = r#if(|c: &char| c.is_ascii_alphanumeric()); type Parser = impl Parser<&'a str, Output = Self>;
let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_@-".contains(*c)).list(0..);
start fn parser() -> Self::Parser {
.and(rest) let start = r#if(|c: &char| c.is_ascii_alphanumeric());
.recognize() let rest = r#if(|c: &char| c.is_ascii_alphanumeric() || "+_@-".contains(*c)).list(0..);
.map(|output: &str| UseFlag(output.to_string()))
start
.and(rest)
.recognize()
.map(|output: &str| UseFlag(output.to_string()))
}
} }
pub fn iuseflag<'a>() -> impl Parser<&'a str, Output = IUseFlag> { impl<'a> Parseable<'a, &'a str> for IUseFlag {
useflag() type Parser = impl Parser<&'a str, Output = Self>;
.preceded_by(tag("+"))
.map(|flag| IUseFlag { fn parser() -> Self::Parser {
default: true, UseFlag::parser()
flag, .preceded_by(tag("+"))
}) .map(|flag| IUseFlag {
.or(useflag().map(|flag| IUseFlag { default: true,
default: false, flag,
flag, })
})) .or(UseFlag::parser().map(|flag| IUseFlag {
default: false,
flag,
}))
}
} }

View File

@@ -1,5 +1,8 @@
use gentoo_utils::depend; use gentoo_utils::{
use mon::{Parser, eof, input::InputIter, tag}; Parseable,
depend::{self, Expr},
};
use mon::{Parser, eof, input::InputIter, tag, whitespace1};
use std::fs; use std::fs;
#[test] #[test]
@@ -14,7 +17,8 @@ fn parse_md5_cache() {
if line.starts_with("DEPEND=") { if line.starts_with("DEPEND=") {
eprintln!("{line}"); eprintln!("{line}");
eprintln!(); eprintln!();
depend::parsers::exprs() Expr::parser()
.separated_list(whitespace1(), 0..)
.ignore() .ignore()
.or(eof()) .or(eof())
.preceded_by(tag("DEPEND=")) .preceded_by(tag("DEPEND="))