-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith_pos.rs
More file actions
46 lines (40 loc) · 995 Bytes
/
with_pos.rs
File metadata and controls
46 lines (40 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use rusty_common::{AtPos, HasPos, Positioned};
use rusty_pc::{InputTrait, Parser};
pub trait WithPos<I, C>: Parser<I, C>
where
Self: Sized,
I: HasPos + InputTrait,
{
fn with_pos(self) -> impl Parser<I, C, Output = Positioned<Self::Output>, Error = Self::Error> {
WithPosMapper::new(self)
}
}
impl<I, C, P> WithPos<I, C> for P
where
P: Parser<I, C>,
I: HasPos + InputTrait,
{
}
struct WithPosMapper<P> {
parser: P,
}
impl<P> WithPosMapper<P> {
pub fn new(parser: P) -> Self {
Self { parser }
}
}
impl<I, C, P> Parser<I, C> for WithPosMapper<P>
where
P: Parser<I, C>,
I: HasPos + InputTrait,
{
type Output = Positioned<P::Output>;
type Error = P::Error;
fn parse(&mut self, tokenizer: &mut I) -> Result<Self::Output, Self::Error> {
let pos = tokenizer.pos();
self.parser.parse(tokenizer).map(|x| x.at_pos(pos))
}
fn set_context(&mut self, ctx: &C) {
self.parser.set_context(ctx)
}
}