Files
lang/src/parser/node/mod.rs
T
2026-04-17 01:49:43 -04:00

70 lines
1.5 KiB
Rust

use crate::{
io::{CompilerMsg, CompilerOutput, Span},
parser::{Cursor, nodes::*},
};
mod ctx;
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;
}
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body>> {
let root_code = match std::fs::read_to_string(path) {
Ok(code) => code,
Err(err) => {
output.error(format!("Failed to read input file: {err}"));
return None;
}
};
output.files.push(path.to_string());
let mut ctx = ParseCtx::new(Cursor::new(&root_code, 0));
let root = match ctx.parse() {
Ok(v) => v,
Err(msg) => {
output.error(msg);
return None;
}
};
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 })
}
}