work
This commit is contained in:
+19
-19
@@ -2,7 +2,7 @@ use crate::{
|
||||
io::{CompilerMsg, Span},
|
||||
parser::{
|
||||
Id, Ident, Node, Nodes,
|
||||
cursor::{Cursor, Lit},
|
||||
cursor::{Cursor, Lit, Token},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -10,16 +10,6 @@ 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>,
|
||||
@@ -36,16 +26,9 @@ impl<'a> ParseCtx<'a> {
|
||||
}
|
||||
|
||||
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_with(self, input).map(|r| self.push(r));
|
||||
let res = P::parse(self).map(|r| self.push(r));
|
||||
self.start = old_start;
|
||||
res
|
||||
}
|
||||
@@ -54,15 +37,18 @@ impl<'a> ParseCtx<'a> {
|
||||
let span = self.cursor.span;
|
||||
self.nodes.idents.add(Ident { inner: s }, span)
|
||||
}
|
||||
|
||||
pub fn lit(&mut self, lit: Lit) -> Id<Lit> {
|
||||
let span = self.cursor.span;
|
||||
self.nodes.lits.add(lit, span)
|
||||
}
|
||||
|
||||
pub fn push_adv<N: Node>(&mut self, node: N) -> Id<N> {
|
||||
let res = self.push(node);
|
||||
self.cursor.next();
|
||||
res
|
||||
}
|
||||
|
||||
pub fn push<N: Node>(&mut self, node: N) -> Id<N> {
|
||||
let end = self.cursor.cur_end();
|
||||
N::vec_mut(&mut self.nodes).add(
|
||||
@@ -74,6 +60,20 @@ impl<'a> ParseCtx<'a> {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn list<N: Parsable>(&mut self, sep: Token, end: Token) -> Result<Vec<Id<N>>, CompilerMsg> {
|
||||
let mut list = Vec::new();
|
||||
if self.next_if(&end) {
|
||||
return Ok(list);
|
||||
}
|
||||
list.push(self.parse()?);
|
||||
while self.next_if(&sep) {
|
||||
list.push(self.parse()?);
|
||||
}
|
||||
self.expect(end)?;
|
||||
Ok(list)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Nodes {
|
||||
self.nodes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user