aboutsummaryrefslogtreecommitdiff
path: root/src/parser/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/parser.rs')
-rw-r--r--src/parser/parser.rs33
1 files changed, 15 insertions, 18 deletions
diff --git a/src/parser/parser.rs b/src/parser/parser.rs
index 752d532d0..069701483 100644
--- a/src/parser/parser.rs
+++ b/src/parser/parser.rs
@@ -27,9 +27,9 @@ impl Marker {
27 if idx == p.events.len() - 1 { 27 if idx == p.events.len() - 1 {
28 match p.events.pop() { 28 match p.events.pop() {
29 Some(Event::Start { 29 Some(Event::Start {
30 kind: TOMBSTONE, 30 kind: TOMBSTONE,
31 forward_parent: None, 31 forward_parent: None,
32 }) => (), 32 }) => (),
33 _ => unreachable!(), 33 _ => unreachable!(),
34 } 34 }
35 } 35 }
@@ -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,22 @@ impl<'t> Parser<'t> {
175} 175}
176 176
177pub(crate) struct ErrorBuilder<'p, 't: 'p> { 177pub(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
182impl<'p, 't: 'p> Drop for ErrorBuilder<'p, 't> {
183 fn drop(&mut self) {
184 let message = ::std::mem::replace(&mut self.message, String::new());
185 self.parser.event(Event::Error { message });
186 }
187}
188
182impl<'t, 'p> ErrorBuilder<'p, 't> { 189impl<'t, 'p> ErrorBuilder<'p, 't> {
183 fn new(parser: &'p mut Parser<'t>) -> Self { 190 fn new(parser: &'p mut Parser<'t>, message: String) -> Self {
184 ErrorBuilder { 191 ErrorBuilder {
185 message: None, 192 message,
186 parser, 193 parser,
187 } 194 }
188 } 195 }
189
190 pub fn message<M: Into<String>>(mut self, m: M) -> Self {
191 self.message = Some(m.into());
192 self
193 }
194
195 pub fn emit(self) {
196 let message = self.message.expect("Error message not set");
197 self.parser.event(Event::Error { message });
198 }
199} 196}