From 9d744550c15582db972ee4c5fc9cdaaefdd012b4 Mon Sep 17 00:00:00 2001 From: Lawrence Bethlenfalvy Date: Mon, 10 Feb 2025 20:54:32 +0100 Subject: [PATCH] Parsing! --- orchid-base/src/format.rs | 10 ++++++++-- orchid-base/src/macros.rs | 6 +++--- orchid-base/src/tree.rs | 6 +++--- orchid-host/src/expr.rs | 15 ++++++++++----- orchid-host/src/tree.rs | 18 ++++++++++++++++++ orcx/src/main.rs | 4 ++-- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/orchid-base/src/format.rs b/orchid-base/src/format.rs index 86e5c69..9fafd23 100644 --- a/orchid-base/src/format.rs +++ b/orchid-base/src/format.rs @@ -102,11 +102,17 @@ pub struct Variant { pub elements: Vec, } +#[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); impl Variants { fn parse_phs(s: &'_ str) -> Vec { - let re = Regex::new(r"(?\{\d+?[bl]?\})| (\{\{)|(\}\})").unwrap(); + let re = Regex::new(r"(?\{\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::().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], diff --git a/orchid-base/src/macros.rs b/orchid-base/src/macros.rs index 6366206..c1fd1fc 100644 --- a/orchid-base/src/macros.rs +++ b/orchid-base/src/macros.rs @@ -52,7 +52,7 @@ impl<'a, A> MTree<'a, A> { } impl 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 Format for MTok<'_, A> { Self::Atom(a) => a.print(c).await, Self::Done(d) => FmtUnit::new(tl_cache!(Rc: 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: Rc::new(Variants::default() @@ -120,7 +120,7 @@ impl Format for MTok<'_, A> { Self::Ph(ph) => format!("{ph}").into(), Self::Ref(r) => FmtUnit::new(tl_cache!(Rc: 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 { diff --git a/orchid-base/src/tree.rs b/orchid-base/src/tree.rs index 258ef01..3111459 100644 --- a/orchid-base/src/tree.rs +++ b/orchid-base/src/tree.rs @@ -262,9 +262,9 @@ impl 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: Rc::new(Variants::default().bounded("({0b})"))), + Paren::Curly => tl_cache!(Rc: Rc::new(Variants::default().bounded("{{{0b}}}"))), + Paren::Square => tl_cache!(Rc: Rc::new(Variants::default().bounded("[{0b}]"))), }, [ttv_fmt(b, c).await], ), diff --git a/orchid-host/src/expr.rs b/orchid-host/src/expr.rs index a5b0444..46a27d4 100644 --- a/orchid-host/src/expr.rs +++ b/orchid-host/src/expr.rs @@ -76,19 +76,24 @@ impl Format for Expr { ExprKind::Call(f, x) => tl_cache!(Rc: Rc::new(Variants::default() .unbounded("{0} {1l}") .bounded("({0} {1b})"))) - .units([f.print(c).await, x.print(c).await]), + .units([ + print_expr(f, c, visited).boxed_local().await, + print_expr(x, c, visited).boxed_local().await, + ]), ExprKind::Const(c) => format!("{c}").into(), ExprKind::Lambda(None, body) => tl_cache!(Rc: Rc::new(Variants::default() .unbounded("\\.{0l}") .bounded("(\\.{0b})"))) - .units([body.print(c).await]), + .units([print_expr(body, c, visited).boxed_local().await]), ExprKind::Lambda(Some(path), body) => tl_cache!(Rc: Rc::new(Variants::default() .unbounded("\\{0b}. {1l}") .bounded("(\\{0b}. {1b})"))) - .units([format!("{path}").into(), body.print(c).await]), + .units([format!("{path}").into(), print_expr(body, c, visited).boxed_local().await]), ExprKind::Seq(l, r) => - tl_cache!(Rc: Rc::new(Variants::default().bounded("[{0b}]{1l}"))) - .units([l.print(c).await, r.print(c).await]), + tl_cache!(Rc: Rc::new(Variants::default().bounded("[{0b}]{1l}"))).units([ + print_expr(l, c, visited).boxed_local().await, + print_expr(r, c, visited).boxed_local().await, + ]), } } } diff --git a/orchid-host/src/tree.rs b/orchid-host/src/tree.rs index c974408..3b4916e 100644 --- a/orchid-host/src/tree.rs +++ b/orchid-host/src/tree.rs @@ -91,6 +91,15 @@ impl Format for Item { tl_cache!(Rc: Rc::new(Variants::default().bounded("macro {{\n\t{0}\n}}"))) .units([Variants::sequence(rules.len(), "\n", None) .units(join_all(rules.iter().map(|r| r.print(c))).await)]), + ItemKind::Member(mem) => match mem.kind.get() { + None => format!("lazy {}", mem.name).into(), + Some(MemberKind::Const(val)) => + tl_cache!(Rc: Rc::new(Variants::default().bounded("const {0} = {1}"))) + .units([mem.name.rc().into(), val.print(c).await]), + Some(MemberKind::Mod(module)) => + tl_cache!(Rc: Rc::new(Variants::default().bounded("module {0} {{\n\t{1}\n}}"))) + .units([mem.name.rc().into(), module.print(c).boxed_local().await]), + }, _ => panic!(), }; tl_cache!(Rc: Rc::new(Variants::default().bounded("{0}\n{1}"))) @@ -164,6 +173,15 @@ impl Module { ) } } +impl Format for Module { + async fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit { + let import_str = self.imports.iter().map(|i| format!("import {i}")).join("\n"); + let head_str = format!("{import_str}\nexport ::({})\n", self.exports.iter().join(", ")); + Variants::sequence(self.items.len() + 1, "\n", None).units( + [head_str.into()].into_iter().chain(join_all(self.items.iter().map(|i| i.print(c))).await), + ) + } +} pub struct LazyMemberHandle(api::TreeId, System, Vec>); impl LazyMemberHandle { diff --git a/orcx/src/main.rs b/orcx/src/main.rs index 08e112d..dbb38f8 100644 --- a/orcx/src/main.rs +++ b/orcx/src/main.rs @@ -10,7 +10,7 @@ use clap::{Parser, Subcommand}; use futures::{Stream, TryStreamExt, io}; use orchid_base::clone; use orchid_base::error::ReporterImpl; -use orchid_base::format::{FmtCtxImpl, take_first}; +use orchid_base::format::{FmtCtxImpl, Format, take_first}; use orchid_base::logging::{LogStrategy, Logger}; use orchid_base::parse::Snippet; use orchid_base::tree::ttv_fmt; @@ -111,7 +111,7 @@ async fn main() -> io::Result { return; } for item in ptree { - println!("{item:?}") + println!("{}", take_first(&item.print(&FmtCtxImpl { i: &ctx.i }).await, true)) } }, }