This commit is contained in:
2026-04-17 18:51:12 -04:00
parent 2f91e454dd
commit b3f77076d4
20 changed files with 244 additions and 192 deletions
+8 -37
View File
@@ -1,5 +1,5 @@
use crate::{
io::{CompilerMsg, CompilerOutput, Span},
io::{CompilerMsg, CompilerOutput},
parser::{Cursor, nodes::*},
};
@@ -8,17 +8,18 @@ mod dsp;
pub use ctx::*;
pub use dsp::*;
pub struct Parsed<N> {
pub node: N,
pub span: Span,
}
pub trait Node: Sized {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result;
fn dsp(&self, ctx: DisplayCtx) -> NodeDsp<'_, Self> {
NodeDsp { node: self, ctx }
}
fn new_dsp(&self) -> NodeDsp<'_, Self> {
self.dsp(DisplayCtx { indent: 0 })
}
}
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body>> {
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Body> {
let root_code = match std::fs::read_to_string(path) {
Ok(code) => code,
Err(err) => {
@@ -37,33 +38,3 @@ pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body
};
Some(root)
}
impl<N> Parsed<N> {
pub fn new(node: N, span: Span) -> Self {
Self { node, span }
}
pub fn boxed(self) -> Box<Self> {
Box::new(self)
}
}
impl<N> std::ops::Deref for Parsed<N> {
type Target = N;
fn deref(&self) -> &Self::Target {
&self.node
}
}
impl<N> std::ops::DerefMut for Parsed<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.node
}
}
impl<N: Node> std::fmt::Display for Parsed<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.node.fmt(f, DisplayCtx { indent: 0 })
}
}