work
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user