oni_comb_parser/
parser_ext.rs1use crate::combinator::attempt::Attempt;
2use crate::combinator::chainl1::ChainL1;
3use crate::combinator::chainr1::ChainR1;
4use crate::combinator::context::Context;
5use crate::combinator::cut::Cut;
6use crate::combinator::flat_map::FlatMap;
7use crate::combinator::many::Many;
8use crate::combinator::many1::Many1;
9use crate::combinator::map::Map;
10use crate::combinator::optional::Optional;
11use crate::combinator::or::Or;
12use crate::combinator::sep_by::{SepBy0, SepBy1};
13use crate::combinator::zip::Zip;
14use crate::combinator::zip_left::ZipLeft;
15use crate::combinator::zip_right::ZipRight;
16use crate::input::Input;
17use crate::parser::Parser;
18
19pub trait ParserExt<I: Input>: Parser<I> + Sized {
20 fn map<F, O2>(self, f: F) -> Map<Self, F>
21 where
22 F: FnMut(Self::Output) -> O2, {
23 Map { parser: self, f }
24 }
25
26 fn zip<P2>(self, rhs: P2) -> Zip<Self, P2>
27 where
28 P2: Parser<I, Error = Self::Error>, {
29 Zip {
30 first: self,
31 second: rhs,
32 }
33 }
34
35 fn zip_left<P2>(self, rhs: P2) -> ZipLeft<Self, P2>
37 where
38 P2: Parser<I, Error = Self::Error>, {
39 ZipLeft {
40 first: self,
41 second: rhs,
42 }
43 }
44
45 fn zip_right<P2>(self, rhs: P2) -> ZipRight<Self, P2>
47 where
48 P2: Parser<I, Error = Self::Error>, {
49 ZipRight {
50 first: self,
51 second: rhs,
52 }
53 }
54
55 fn or<P2>(self, rhs: P2) -> Or<Self, P2>
56 where
57 Self::Error: crate::error::MergeError,
58 P2: Parser<I, Output = Self::Output, Error = Self::Error>, {
59 Or { left: self, right: rhs }
60 }
61
62 fn attempt(self) -> Attempt<Self> {
63 Attempt { parser: self }
64 }
65
66 fn cut(self) -> Cut<Self> {
67 Cut { parser: self }
68 }
69
70 fn optional(self) -> Optional<Self> {
71 Optional { parser: self }
72 }
73
74 fn many0(self) -> Many<Self> {
75 Many { parser: self }
76 }
77
78 fn many1(self) -> Many1<Self> {
79 Many1 { parser: self }
80 }
81
82 fn context(self, label: &'static str) -> Context<Self>
84 where
85 Self::Error: crate::error::ContextError, {
86 Context { parser: self, label }
87 }
88
89 fn sep_by0<S>(self, sep: S) -> SepBy0<Self, S>
91 where
92 S: Parser<I, Error = Self::Error>, {
93 SepBy0 { parser: self, sep }
94 }
95
96 fn sep_by1<S>(self, sep: S) -> SepBy1<Self, S>
98 where
99 S: Parser<I, Error = Self::Error>, {
100 SepBy1 { parser: self, sep }
101 }
102
103 fn chainl1<Op, F>(self, op: Op) -> ChainL1<Self, Op>
105 where
106 Op: Parser<I, Output = F, Error = Self::Error>,
107 F: FnMut(Self::Output, Self::Output) -> Self::Output, {
108 ChainL1 {
109 operand: self,
110 operator: op,
111 }
112 }
113
114 fn chainr1<Op, F>(self, op: Op) -> ChainR1<Self, Op>
116 where
117 Op: Parser<I, Output = F, Error = Self::Error>,
118 F: FnMut(Self::Output, Self::Output) -> Self::Output, {
119 ChainR1 {
120 operand: self,
121 operator: op,
122 }
123 }
124
125 fn flat_map<F, P2>(self, f: F) -> FlatMap<Self, F>
126 where
127 P2: Parser<I, Error = Self::Error>,
128 F: FnMut(Self::Output) -> P2, {
129 FlatMap { parser: self, f }
130 }
131
132 fn and_then<F, P2>(self, f: F) -> FlatMap<Self, F>
133 where
134 P2: Parser<I, Error = Self::Error>,
135 F: FnMut(Self::Output) -> P2, {
136 FlatMap { parser: self, f }
137 }
138}
139
140impl<I: Input, P: Parser<I> + Sized> ParserExt<I> for P {}