64 lines
1.4 KiB
Rust
64 lines
1.4 KiB
Rust
use super::*;
|
|
|
|
pub struct Node<T> {
|
|
pub data: Option<T>,
|
|
}
|
|
|
|
pub type BNode<T> = Box<Node<T>>;
|
|
|
|
pub enum ParseResult<T> {
|
|
Ok(T),
|
|
Node(Node<T>),
|
|
Continue(CompilerMsg),
|
|
Break(CompilerMsg),
|
|
SubErr,
|
|
}
|
|
|
|
pub trait Parsable: Sized {
|
|
type Data = ();
|
|
fn parse(ctx: &mut ParserCtx, data: Self::Data) -> ParseResult<Self>;
|
|
}
|
|
|
|
impl<T> Node<T> {
|
|
pub fn bx(self) -> Box<Self> {
|
|
Box::new(self)
|
|
}
|
|
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Node<U> {
|
|
Node {
|
|
data: self.data.map(f),
|
|
}
|
|
}
|
|
}
|
|
|
|
use std::convert::Infallible;
|
|
impl<T> std::ops::FromResidual<Option<Infallible>> for ParseResult<T> {
|
|
fn from_residual(residual: Option<Infallible>) -> Self {
|
|
match residual {
|
|
None => ParseResult::SubErr,
|
|
}
|
|
}
|
|
}
|
|
impl<T> std::ops::FromResidual<Result<Infallible, CompilerMsg>> for ParseResult<T> {
|
|
fn from_residual(residual: Result<Infallible, CompilerMsg>) -> Self {
|
|
match residual {
|
|
Err(msg) => ParseResult::Break(msg),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl CompilerMsg {
|
|
pub fn res<T>(self) -> ParseResult<T> {
|
|
ParseResult::Break(self)
|
|
}
|
|
}
|
|
|
|
impl<T: std::fmt::Debug> std::fmt::Debug for Node<T> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
if let Some(d) = &self.data {
|
|
d.fmt(f)
|
|
} else {
|
|
f.write_str("{error}")
|
|
}
|
|
}
|
|
}
|