New plans for macros

About to move them completely to stdlib
This commit is contained in:
2024-08-18 22:57:06 +02:00
parent 11951ede43
commit 3a63894de2
78 changed files with 2557 additions and 1980 deletions

View File

@@ -1,46 +1,44 @@
use std::ops::{Range, RangeInclusive};
use orchid_api::parser::{ParsId, SubLex};
use orchid_api::proto::ExtMsgSet;
use orchid_api::system::SysId;
use orchid_base::error::{mk_err, OrcErr, OrcRes};
use orchid_base::intern;
use orchid_base::interner::Tok;
use orchid_base::location::Pos;
use orchid_base::reqnot::{ReqNot, Requester};
use orchid_base::tree::TreeHandle;
use crate::error::{
ProjectError, ProjectResult
};
use crate::api;
use crate::tree::{GenTok, GenTokTree};
pub struct CascadingError;
impl ProjectError for CascadingError {
const DESCRIPTION: &'static str = "An error cascading from a recursive sublexer";
fn message(&self) -> String {
"This error should not surface. If you are seeing it, something is wrong".to_string()
}
fn one_position(&self) -> Pos { Pos::None }
pub fn err_cascade() -> OrcErr {
mk_err(
intern!(str: "An error cascading from a recursive sublexer"),
"This error should not surface. If you are seeing it, something is wrong",
[Pos::None.into()],
)
}
pub struct NotApplicableLexerError;
impl ProjectError for NotApplicableLexerError {
const DESCRIPTION: &'static str = "Pseudo-error to communicate that the lexer doesn't apply";
fn message(&self) -> String { CascadingError.message() }
fn one_position(&self) -> Pos { Pos::None }
pub fn err_lexer_na() -> OrcErr {
mk_err(
intern!(str: "Pseudo-error to communicate that the lexer doesn't apply"),
&*err_cascade().message,
[Pos::None.into()],
)
}
pub struct LexContext<'a> {
pub text: &'a Tok<String>,
pub sys: SysId,
pub id: ParsId,
pub sys: api::SysId,
pub id: api::ParsId,
pub pos: u32,
pub reqnot: ReqNot<ExtMsgSet>,
pub reqnot: ReqNot<api::ExtMsgSet>,
}
impl<'a> LexContext<'a> {
pub fn recurse(&self, tail: &'a str) -> ProjectResult<(&'a str, GenTokTree)> {
pub fn recurse(&self, tail: &'a str) -> OrcRes<(&'a str, GenTokTree<'a>)> {
let start = self.pos(tail);
let lx = (self.reqnot.request(SubLex { pos: start, id: self.id }))
.ok_or_else(|| CascadingError.pack())?;
Ok((&self.text[lx.pos as usize..], GenTok::Slot(lx.ticket).at(start..lx.pos)))
let lx =
self.reqnot.request(api::SubLex { pos: start, id: self.id }).ok_or_else(err_cascade)?;
Ok((&self.text[lx.pos as usize..], GenTok::Slot(TreeHandle::new(lx.ticket)).at(start..lx.pos)))
}
pub fn pos(&self, tail: &'a str) -> u32 { (self.text.len() - tail.len()) as u32 }
@@ -52,19 +50,13 @@ impl<'a> LexContext<'a> {
pub trait Lexer: Send + Sync + Sized + Default + 'static {
const CHAR_FILTER: &'static [RangeInclusive<char>];
fn lex<'a>(
tail: &'a str,
ctx: &'a LexContext<'a>,
) -> ProjectResult<(&'a str, GenTokTree)>;
fn lex<'a>(tail: &'a str, ctx: &'a LexContext<'a>) -> OrcRes<(&'a str, GenTokTree<'a>)>;
}
pub trait DynLexer: Send + Sync + 'static {
fn char_filter(&self) -> &'static [RangeInclusive<char>];
fn lex<'a>(
&self,
tail: &'a str,
ctx: &'a LexContext<'a>,
) -> ProjectResult<(&'a str, GenTokTree)>;
fn lex<'a>(&self, tail: &'a str, ctx: &'a LexContext<'a>)
-> OrcRes<(&'a str, GenTokTree<'a>)>;
}
impl<T: Lexer> DynLexer for T {
@@ -73,7 +65,7 @@ impl<T: Lexer> DynLexer for T {
&self,
tail: &'a str,
ctx: &'a LexContext<'a>,
) -> ProjectResult<(&'a str, GenTokTree)> {
) -> OrcRes<(&'a str, GenTokTree<'a>)> {
T::lex(tail, ctx)
}
}