forked from Orchid/orchid
Parsing!
This commit is contained in:
@@ -102,11 +102,17 @@ pub struct Variant {
|
||||
pub elements: Vec<FmtElement>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variants_parse_test() {
|
||||
let vars = Variants::default().bounded("({0})");
|
||||
println!("final: {vars:?}")
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, Default)]
|
||||
pub struct Variants(pub Vec<Variant>);
|
||||
impl Variants {
|
||||
fn parse_phs(s: &'_ str) -> Vec<FmtElement> {
|
||||
let re = Regex::new(r"(?<tpl>\{\d+?[bl]?\})| (\{\{)|(\}\})").unwrap();
|
||||
let re = Regex::new(r"(?<tpl>\{\d+?[bl]?\})|(\{\{)|(\}\})").unwrap();
|
||||
let matches = re.captures_iter(s);
|
||||
let slots = matches.into_iter().filter_map(|m| m.name("tpl")).map(|tpl| {
|
||||
let no_opencurly = tpl.as_str().strip_prefix("{").expect("required by regex");
|
||||
@@ -126,7 +132,7 @@ impl Variants {
|
||||
let idx = num.parse::<u32>().expect("Decimal digits required by regex");
|
||||
(tpl.range(), idx, bounded)
|
||||
});
|
||||
(iter::once(None).chain(slots.into_iter().map(Some)).chain(None).tuple_windows())
|
||||
(iter::once(None).chain(slots.into_iter().map(Some)).chain([None]).tuple_windows())
|
||||
.flat_map(|(l, r)| {
|
||||
let string = match (l, &r) {
|
||||
(None, Some((r, ..))) => &s[..r.start],
|
||||
|
||||
@@ -52,7 +52,7 @@ impl<'a, A> MTree<'a, A> {
|
||||
}
|
||||
impl<A: Format> Format for MTree<'_, A> {
|
||||
async fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
|
||||
self.tok.print(c).await
|
||||
self.tok.print(c).boxed_local().await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ impl<A: Format> Format for MTok<'_, A> {
|
||||
Self::Atom(a) => a.print(c).await,
|
||||
Self::Done(d) =>
|
||||
FmtUnit::new(tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("(Done){0l}"))), [
|
||||
d.print(c).await,
|
||||
d.print(c).boxed_local().await,
|
||||
]),
|
||||
Self::Lambda(arg, b) => FmtUnit::new(
|
||||
tl_cache!(Rc<Variants>: Rc::new(Variants::default()
|
||||
@@ -120,7 +120,7 @@ impl<A: Format> Format for MTok<'_, A> {
|
||||
Self::Ph(ph) => format!("{ph}").into(),
|
||||
Self::Ref(r) =>
|
||||
FmtUnit::new(tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("(ref){0l}"))), [
|
||||
r.print(c).await,
|
||||
r.print(c).boxed_local().await,
|
||||
]),
|
||||
Self::S(p, body) => FmtUnit::new(
|
||||
match *p {
|
||||
|
||||
@@ -262,9 +262,9 @@ impl<A: AtomRepr, X: ExtraTok> Format for Token<'_, A, X> {
|
||||
Self::Ph(ph) => format!("{ph}").into(),
|
||||
Self::S(p, b) => FmtUnit::new(
|
||||
match *p {
|
||||
Paren::Round => Rc::new(Variants::default().bounded("({0b})")),
|
||||
Paren::Curly => Rc::new(Variants::default().bounded("{{{0b}}}")),
|
||||
Paren::Square => Rc::new(Variants::default().bounded("[{0b}]")),
|
||||
Paren::Round => tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("({0b})"))),
|
||||
Paren::Curly => tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("{{{0b}}}"))),
|
||||
Paren::Square => tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("[{0b}]"))),
|
||||
},
|
||||
[ttv_fmt(b, c).await],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user