1use 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::map_res::MapRes;
11use crate::combinator::optional::Optional;
12use crate::combinator::or::Or;
13use crate::combinator::sep_by::{SepBy0, SepBy1};
14use crate::combinator::zip::Zip;
15use crate::combinator::zip_left::ZipLeft;
16use crate::combinator::zip_right::ZipRight;
17use crate::input::Input;
18use crate::parser::Parser;
19
20pub trait ParserExt<I: Input>: Parser<I> + Sized {
21 fn map<F, O2>(self, f: F) -> Map<Self, F>
22 where
23 F: FnMut(Self::Output) -> O2, {
24 Map { parser: self, f }
25 }
26
27 fn zip<P2>(self, rhs: P2) -> Zip<Self, P2>
28 where
29 P2: Parser<I, Error = Self::Error>, {
30 Zip {
31 first: self,
32 second: rhs,
33 }
34 }
35
36 fn zip_left<P2>(self, rhs: P2) -> ZipLeft<Self, P2>
38 where
39 P2: Parser<I, Error = Self::Error>, {
40 ZipLeft {
41 first: self,
42 second: rhs,
43 }
44 }
45
46 fn zip_right<P2>(self, rhs: P2) -> ZipRight<Self, P2>
48 where
49 P2: Parser<I, Error = Self::Error>, {
50 ZipRight {
51 first: self,
52 second: rhs,
53 }
54 }
55
56 fn or<P2>(self, rhs: P2) -> Or<Self, P2>
57 where
58 Self::Error: crate::error::MergeError,
59 P2: Parser<I, Output = Self::Output, Error = Self::Error>, {
60 Or { left: self, right: rhs }
61 }
62
63 fn attempt(self) -> Attempt<Self> {
64 Attempt { parser: self }
65 }
66
67 fn cut(self) -> Cut<Self> {
68 Cut { parser: self }
69 }
70
71 fn optional(self) -> Optional<Self> {
72 Optional { parser: self }
73 }
74
75 fn many0(self) -> Many<Self> {
76 Many { parser: self }
77 }
78
79 fn many1(self) -> Many1<Self> {
80 Many1 { parser: self }
81 }
82
83 fn context(self, label: &'static str) -> Context<Self>
85 where
86 Self::Error: crate::error::ContextError, {
87 Context { parser: self, label }
88 }
89
90 fn sep_by0<S>(self, sep: S) -> SepBy0<Self, S>
92 where
93 S: Parser<I, Error = Self::Error>, {
94 SepBy0 { parser: self, sep }
95 }
96
97 fn sep_by1<S>(self, sep: S) -> SepBy1<Self, S>
99 where
100 S: Parser<I, Error = Self::Error>, {
101 SepBy1 { parser: self, sep }
102 }
103
104 fn chainl1<Op, F>(self, op: Op) -> ChainL1<Self, Op>
106 where
107 Op: Parser<I, Output = F, Error = Self::Error>,
108 F: FnMut(Self::Output, Self::Output) -> Self::Output, {
109 ChainL1 {
110 operand: self,
111 operator: op,
112 }
113 }
114
115 fn chainr1<Op, F>(self, op: Op) -> ChainR1<Self, Op>
117 where
118 Op: Parser<I, Output = F, Error = Self::Error>,
119 F: FnMut(Self::Output, Self::Output) -> Self::Output, {
120 ChainR1 {
121 operand: self,
122 operator: op,
123 }
124 }
125
126 fn map_res<F, O2, E2>(self, f: F, label: &'static str) -> MapRes<Self, F>
130 where
131 Self: Parser<I, Error = crate::error::ParseError>,
132 F: FnMut(Self::Output) -> Result<O2, E2>, {
133 MapRes { parser: self, f, label }
134 }
135
136 fn flat_map<F, P2>(self, f: F) -> FlatMap<Self, F>
137 where
138 P2: Parser<I, Error = Self::Error>,
139 F: FnMut(Self::Output) -> P2, {
140 FlatMap { parser: self, f }
141 }
142
143 fn and_then<F, P2>(self, f: F) -> FlatMap<Self, F>
144 where
145 P2: Parser<I, Error = Self::Error>,
146 F: FnMut(Self::Output) -> P2, {
147 FlatMap { parser: self, f }
148 }
149}
150
151impl<I: Input, P: Parser<I> + Sized> ParserExt<I> for P {}