This commit is contained in:
2026-04-11 03:50:43 -04:00
parent 29316e6353
commit 229b026573
16 changed files with 266 additions and 136 deletions
+19 -1
View File
@@ -10,6 +10,16 @@ pub trait Parsable: Sized + Node {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
}
pub trait ParsableWith<Input>: Sized + Node {
fn parse_with(ctx: &mut ParseCtx, input: Input) -> Result<Self, CompilerMsg>;
}
impl<P: Parsable> ParsableWith<()> for P {
fn parse_with(ctx: &mut ParseCtx, _: ()) -> Result<Self, CompilerMsg> {
P::parse(ctx)
}
}
pub struct ParseCtx<'a> {
start: usize,
cursor: Cursor<'a>,
@@ -24,10 +34,18 @@ impl<'a> ParseCtx<'a> {
cursor,
}
}
pub fn parse<P: Parsable>(&mut self) -> Result<Id<P>, CompilerMsg> {
self.parse_with(())
}
pub fn parse_with<P: ParsableWith<Input>, Input>(
&mut self,
input: Input,
) -> Result<Id<P>, CompilerMsg> {
let old_start = self.start;
self.start = self.cursor.peek_start();
let res = P::parse(self).map(|r| self.push(r));
let res = P::parse_with(self, input).map(|r| self.push(r));
self.start = old_start;
res
}