type checking !?!?

This commit is contained in:
2025-03-22 14:40:32 -04:00
parent 606cb30c6b
commit 7f809d797c
44 changed files with 664 additions and 314 deletions

View File

@@ -3,14 +3,12 @@ use std::{
ops::{ControlFlow, FromResidual, Try},
};
use crate::ir::FilePos;
use super::{Node, ParserCtx, ParserMsg};
use super::{CompilerMsg, FilePos, Node, ParserCtx};
pub enum ParseResult<T> {
Ok(T),
Recover(T),
Err(ParserMsg),
Err(CompilerMsg),
SubErr,
}
@@ -26,7 +24,7 @@ impl<T> ParseResult<T> {
impl<T> Try for ParseResult<T> {
type Output = T;
type Residual = Option<ParserMsg>;
type Residual = Option<CompilerMsg>;
fn from_output(output: Self::Output) -> Self {
Self::Ok(output)
}
@@ -50,8 +48,8 @@ impl<T> FromResidual for ParseResult<T> {
}
}
impl<T> FromResidual<Result<Infallible, ParserMsg>> for ParseResult<T> {
fn from_residual(residual: Result<Infallible, ParserMsg>) -> Self {
impl<T> FromResidual<Result<Infallible, CompilerMsg>> for ParseResult<T> {
fn from_residual(residual: Result<Infallible, CompilerMsg>) -> Self {
match residual {
Err(e) => Self::Err(e),
}
@@ -115,14 +113,28 @@ pub trait Parsable: Sized {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self>;
}
pub trait MaybeParsable: Sized {
fn maybe_parse(ctx: &mut ParserCtx) -> Result<Option<Self>, ParserMsg>;
pub trait ParsableWith: Sized {
type Data;
fn parse(ctx: &mut ParserCtx, data: Self::Data) -> ParseResult<Self>;
}
impl<T: Parsable> Node<T> {
pub fn parse(ctx: &mut ParserCtx) -> NodeParseResult<T> {
pub trait MaybeParsable: Sized {
fn maybe_parse(ctx: &mut ParserCtx) -> Result<Option<Self>, CompilerMsg>;
}
impl<T: Parsable> ParsableWith for T {
type Data = ();
fn parse(ctx: &mut ParserCtx, _: Self::Data) -> ParseResult<Self> {
T::parse(ctx)
}
}
impl<T: ParsableWith> Node<T> {
pub fn parse_with(ctx: &mut ParserCtx, data: T::Data) -> NodeParseResult<T> {
let start = ctx.peek().map(|t| t.span.start).unwrap_or(FilePos::start());
let (inner, recover) = match T::parse(ctx) {
let (inner, recover) = match T::parse(ctx, data) {
ParseResult::Ok(v) => (Some(v), false),
ParseResult::Recover(v) => (Some(v), true),
ParseResult::Err(e) => {
@@ -142,6 +154,12 @@ impl<T: Parsable> Node<T> {
}
}
impl<T: Parsable> Node<T> {
pub fn parse(ctx: &mut ParserCtx) -> NodeParseResult<T> {
Node::parse_with(ctx, ())
}
}
impl<T: MaybeParsable> Node<T> {
pub fn maybe_parse(ctx: &mut ParserCtx) -> Option<Self> {
let start = ctx.next_start();