New macro system and stdlib additions

This commit is contained in:
2025-11-21 14:25:03 +01:00
parent b77653f841
commit 603efef28e
230 changed files with 3033 additions and 16640 deletions

View File

@@ -12,38 +12,48 @@ use orchid_base::parse::ParseCtx;
use orchid_base::reqnot::Requester;
use crate::api;
use crate::context::{ctx, i};
use crate::expr::BorrowedExprStore;
use crate::parser::PTokTree;
use crate::system::SysCtx;
use crate::tree::GenTokTree;
pub async fn ekey_cascade(i: &Interner) -> Tok<String> {
i.i("An error cascading from a recursive call").await
pub async fn ekey_cascade() -> Tok<String> {
i().i("An error cascading from a recursive call").await
}
pub async fn ekey_not_applicable(i: &Interner) -> Tok<String> {
i.i("Pseudo-error to communicate that the current branch in a dispatch doesn't apply").await
pub async fn ekey_not_applicable() -> Tok<String> {
i().i("Pseudo-error to communicate that the current branch in a dispatch doesn't apply").await
}
const MSG_INTERNAL_ERROR: &str = "This error is a sentinel for the extension library.\
it should not be emitted by the extension.";
pub async fn err_cascade(i: &Interner) -> OrcErrv {
mk_errv(ekey_cascade(i).await, MSG_INTERNAL_ERROR, [Pos::None])
pub async fn err_cascade() -> OrcErrv {
mk_errv(ekey_cascade().await, MSG_INTERNAL_ERROR, [Pos::None])
}
pub async fn err_not_applicable(i: &Interner) -> OrcErrv {
mk_errv(ekey_not_applicable(i).await, MSG_INTERNAL_ERROR, [Pos::None])
pub async fn err_not_applicable() -> OrcErrv {
mk_errv(ekey_not_applicable().await, MSG_INTERNAL_ERROR, [Pos::None])
}
pub struct LexContext<'a> {
pub(crate) exprs: &'a BorrowedExprStore,
pub ctx: SysCtx,
pub text: &'a Tok<String>,
pub id: api::ParsId,
pub pos: u32,
i: Interner,
pub(crate) src: Sym,
pub(crate) rep: &'a Reporter,
}
impl<'a> LexContext<'a> {
pub fn new(
exprs: &'a BorrowedExprStore,
text: &'a Tok<String>,
id: api::ParsId,
pos: u32,
src: Sym,
rep: &'a Reporter,
) -> Self {
Self { exprs, i: i(), id, pos, rep, src, text }
}
pub fn src(&self) -> &Sym { &self.src }
/// This function returns [PTokTree] because it can never return
/// [orchid_base::tree::Token::NewExpr]. You can use
@@ -51,17 +61,10 @@ impl<'a> LexContext<'a> {
/// for embedding in the return value.
pub async fn recurse(&self, tail: &'a str) -> OrcRes<(&'a str, PTokTree)> {
let start = self.pos(tail);
let Some(lx) = self.ctx.reqnot().request(api::SubLex { pos: start, id: self.id }).await else {
return Err(err_cascade(self.ctx.i()).await);
let Some(lx) = ctx().reqnot().request(api::SubLex { pos: start, id: self.id }).await else {
return Err(err_cascade().await);
};
let tree = PTokTree::from_api(
&lx.tree,
&mut (self.ctx.clone(), self.exprs),
&mut (),
&self.src,
self.ctx.i(),
)
.await;
let tree = PTokTree::from_api(&lx.tree, &mut { self.exprs }, &mut (), &self.src, &i()).await;
Ok((&self.text[lx.pos as usize..], tree))
}
@@ -75,7 +78,7 @@ impl<'a> LexContext<'a> {
}
}
impl ParseCtx for LexContext<'_> {
fn i(&self) -> &Interner { self.ctx.i() }
fn i(&self) -> &Interner { &self.i }
fn rep(&self) -> &Reporter { self.rep }
}
@@ -83,7 +86,7 @@ pub trait Lexer: Send + Sync + Sized + Default + 'static {
const CHAR_FILTER: &'static [RangeInclusive<char>];
fn lex<'a>(
tail: &'a str,
ctx: &'a LexContext<'a>,
lctx: &'a LexContext<'a>,
) -> impl Future<Output = OrcRes<(&'a str, GenTokTree)>>;
}