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