| 1 |
|
| 2 |
|
| 3 |
use crate::{Parse, RuleResult}; |
| 4 |
use std::fmt::{self, Debug, Display}; |
| 5 |
|
| 6 |
#[cfg(feature = "std")] |
| 7 |
use std::collections::BTreeSet; |
| 8 |
|
| 9 |
#[cfg(not(feature = "std"))] |
| 10 |
use {alloc::collections::BTreeSet, alloc::vec::Vec}; |
| 11 |
|
| 12 |
|
| 13 |
#[derive(PartialEq, Eq, Debug, Clone)] |
| 14 |
pub struct ExpectedSet { |
| 15 |
expected: BTreeSet<&'static str>, |
| 16 |
} |
| 17 |
|
| 18 |
impl ExpectedSet { |
| 19 |
|
| 20 |
pub fn tokens<'a>(&'a self) -> impl Iterator<Item = &'static str> + 'a { |
| 21 |
self.expected.iter().map(|x| *x) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl Display for ExpectedSet { |
| 26 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 27 |
if self.expected.is_empty() { |
| 28 |
write!(fmt, "<unreported>")?; |
| 29 |
} else if self.expected.len() == 1 { |
| 30 |
write!(fmt, "{}", self.expected.iter().next().unwrap())?; |
| 31 |
} else { |
| 32 |
let mut errors = self.tokens().collect::<Vec<_>>(); |
| 33 |
errors.sort(); |
| 34 |
let mut iter = errors.into_iter(); |
| 35 |
|
| 36 |
write!(fmt, "one of {}", iter.next().unwrap())?; |
| 37 |
for elem in iter { |
| 38 |
write!(fmt, ", {}", elem)?; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
Ok(()) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
#[derive(PartialEq, Eq, Debug, Clone)] |
| 48 |
pub struct ParseError<L> { |
| 49 |
|
| 50 |
pub location: L, |
| 51 |
|
| 52 |
|
| 53 |
pub expected: ExpectedSet, |
| 54 |
} |
| 55 |
|
| 56 |
impl<L: Display> Display for ParseError<L> { |
| 57 |
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> { |
| 58 |
write!( |
| 59 |
fmt, |
| 60 |
"error at {}: expected {}", |
| 61 |
self.location, self.expected |
| 62 |
) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg(any(feature = "std", feature = "unstable"))] |
| 67 |
impl<L: Display + Debug> ::std::error::Error for ParseError<L> { |
| 68 |
fn description(&self) -> &str { |
| 69 |
"parse error" |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[doc(hidden)] |
| 74 |
pub struct ErrorState { |
| 75 |
|
| 76 |
pub max_err_pos: usize, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pub suppress_fail: usize, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pub reparsing_on_error: bool, |
| 85 |
|
| 86 |
|
| 87 |
pub expected: ExpectedSet, |
| 88 |
} |
| 89 |
|
| 90 |
impl ErrorState { |
| 91 |
pub fn new(initial_pos: usize) -> Self { |
| 92 |
ErrorState { |
| 93 |
max_err_pos: initial_pos, |
| 94 |
suppress_fail: 0, |
| 95 |
reparsing_on_error: false, |
| 96 |
expected: ExpectedSet { |
| 97 |
expected: BTreeSet::new(), |
| 98 |
}, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
pub fn reparse_for_error(&mut self) { |
| 104 |
self.suppress_fail = 0; |
| 105 |
self.reparsing_on_error = true; |
| 106 |
} |
| 107 |
|
| 108 |
#[inline(never)] |
| 109 |
pub fn mark_failure_slow_path(&mut self, pos: usize, expected: &'static str) { |
| 110 |
if pos == self.max_err_pos { |
| 111 |
self.expected.expected.insert(expected); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
#[inline(always)] |
| 117 |
pub fn mark_failure(&mut self, pos: usize, expected: &'static str) -> RuleResult<()> { |
| 118 |
if self.suppress_fail == 0 { |
| 119 |
if self.reparsing_on_error { |
| 120 |
self.mark_failure_slow_path(pos, expected); |
| 121 |
} else if pos > self.max_err_pos { |
| 122 |
self.max_err_pos = pos; |
| 123 |
} |
| 124 |
} |
| 125 |
RuleResult::Failed |
| 126 |
} |
| 127 |
|
| 128 |
pub fn into_parse_error<I: Parse + ?Sized>(self, input: &I) -> ParseError<I::PositionRepr> { |
| 129 |
ParseError { |
| 130 |
location: Parse::position_repr(input, self.max_err_pos.into()), |
| 131 |
expected: self.expected, |
| 132 |
} |
| 133 |
} |
| 134 |
} |