diff options
Diffstat (limited to 'src/parser/parser.rs')
-rw-r--r-- | src/parser/parser.rs | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 752d532d0..7c8e47cb6 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs | |||
@@ -129,8 +129,8 @@ impl<'t> Parser<'t> { | |||
129 | m | 129 | m |
130 | } | 130 | } |
131 | 131 | ||
132 | pub(crate) fn error<'p>(&'p mut self) -> ErrorBuilder<'p, 't> { | 132 | pub(crate) fn error<'p, T: Into<String>>(&'p mut self, msg: T) -> ErrorBuilder<'p, 't> { |
133 | ErrorBuilder::new(self) | 133 | ErrorBuilder::new(self, msg.into()) |
134 | } | 134 | } |
135 | 135 | ||
136 | pub(crate) fn bump(&mut self) { | 136 | pub(crate) fn bump(&mut self) { |
@@ -175,25 +175,19 @@ impl<'t> Parser<'t> { | |||
175 | } | 175 | } |
176 | 176 | ||
177 | pub(crate) struct ErrorBuilder<'p, 't: 'p> { | 177 | pub(crate) struct ErrorBuilder<'p, 't: 'p> { |
178 | message: Option<String>, | 178 | message: String, |
179 | parser: &'p mut Parser<'t>, | 179 | parser: &'p mut Parser<'t>, |
180 | } | 180 | } |
181 | 181 | ||
182 | impl<'t, 'p> ErrorBuilder<'p, 't> { | 182 | impl<'p, 't: 'p> Drop for ErrorBuilder<'p, 't> { |
183 | fn new(parser: &'p mut Parser<'t>) -> Self { | 183 | fn drop(&mut self) { |
184 | ErrorBuilder { | 184 | let message = ::std::mem::replace(&mut self.message, String::new()); |
185 | message: None, | 185 | self.parser.event(Event::Error { message }); |
186 | parser, | ||
187 | } | ||
188 | } | ||
189 | |||
190 | pub fn message<M: Into<String>>(mut self, m: M) -> Self { | ||
191 | self.message = Some(m.into()); | ||
192 | self | ||
193 | } | 186 | } |
187 | } | ||
194 | 188 | ||
195 | pub fn emit(self) { | 189 | impl<'t, 'p> ErrorBuilder<'p, 't> { |
196 | let message = self.message.expect("Error message not set"); | 190 | fn new(parser: &'p mut Parser<'t>, message: String) -> Self { |
197 | self.parser.event(Event::Error { message }); | 191 | ErrorBuilder { message, parser } |
198 | } | 192 | } |
199 | } | 193 | } |