This commit is contained in:
2026-04-12 17:26:39 -04:00
parent 2582e8c87e
commit f702f47714
9 changed files with 121 additions and 49 deletions
+19 -2
View File
@@ -1,3 +1,5 @@
use std::ops::Index;
use crate::{
io::{CompilerMsg, Span},
parser::{
@@ -25,10 +27,17 @@ impl<'a> ParseCtx<'a> {
}
}
pub fn parse<P: Parsable>(&mut self) -> Result<Id<P>, CompilerMsg> {
pub fn parse<N: Parsable>(&mut self) -> Result<Id<N>, CompilerMsg> {
self.parse_with(N::parse)
}
pub fn parse_with<N: Node>(
&mut self,
f: impl FnOnce(&mut Self) -> Result<N, CompilerMsg>,
) -> Result<Id<N>, CompilerMsg> {
let old_start = self.start;
self.start = self.cursor.peek_start();
let res = P::parse(self).map(|r| self.push(r));
let res = f(self).map(|r| self.push(r));
self.start = old_start;
res
}
@@ -92,3 +101,11 @@ impl<'a> std::ops::DerefMut for ParseCtx<'a> {
&mut self.cursor
}
}
impl<N: Node> Index<Id<N>> for ParseCtx<'_> {
type Output = N;
fn index(&self, index: Id<N>) -> &Self::Output {
&self.nodes[index]
}
}