1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use nom::error::{ContextError, ErrorKind};
use nom::lib::std::fmt::Formatter;
use std::borrow::Cow;
use std::error::Error;
use std::fmt::Display;
#[derive(Debug, PartialEq)]
pub struct TwigParsingErrorInformation<I> {
pub leftover: I,
pub context: Option<Cow<'static, str>>,
pub(crate) kind: ErrorKind,
}
#[derive(Debug, PartialEq)]
pub enum TwigParseError<I> {
ParsingError(TwigParsingErrorInformation<I>),
ParsingFailure(TwigParsingErrorInformation<I>),
}
impl<I: std::fmt::Debug + std::fmt::Display> Error for TwigParseError<I> {}
impl<I: std::fmt::Debug + std::fmt::Display> Display for TwigParseError<I> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
TwigParseError::ParsingError(info) => write!(
f,
"parsing error because: ({}, {:?}, {:?})",
info.leftover, info.kind, info.context
),
TwigParseError::ParsingFailure(info) => write!(
f,
"Unrecoverable parsing failure because: ({}, {:?}, {:?})",
info.leftover, info.kind, info.context
),
}
}
}
impl<I: std::fmt::Debug + std::fmt::Display> nom::error::ParseError<I>
for TwigParsingErrorInformation<I>
{
fn from_error_kind(_input: I, _kind: ErrorKind) -> Self {
TwigParsingErrorInformation {
leftover: _input,
kind: _kind,
context: None,
}
}
fn append(_input: I, _kind: ErrorKind, other: Self) -> Self {
other
}
fn from_char(input: I, _: char) -> Self {
TwigParsingErrorInformation {
leftover: input,
kind: ErrorKind::Not,
context: None,
}
}
}
impl<I: std::fmt::Debug + std::fmt::Display> ContextError<I> for TwigParsingErrorInformation<I> {
fn add_context(_input: I, _ctx: &'static str, mut other: Self) -> Self {
other.context = Some(_ctx.into());
other
}
}
pub(crate) trait DynamicParseError<I> {
fn add_dynamic_context(input: I, ctx: String, other: Self) -> Self;
}
impl<I: std::fmt::Debug + std::fmt::Display> DynamicParseError<I>
for TwigParsingErrorInformation<I>
{
fn add_dynamic_context(_input: I, ctx: String, mut other: Self) -> Self {
other.context = Some(Cow::Owned(ctx));
other
}
}
impl<I> From<nom::Err<TwigParsingErrorInformation<I>>> for TwigParseError<I> {
fn from(e: nom::Err<TwigParsingErrorInformation<I>>) -> Self {
match e {
nom::Err::Incomplete(_) => unreachable!(),
nom::Err::Error(i) => TwigParseError::ParsingError(i),
nom::Err::Failure(i) => TwigParseError::ParsingFailure(i),
}
}
}
impl TwigParseError<&str> {
pub fn pretty_helpful_error_string(&self, input: &str) -> String {
let mut output = String::with_capacity(256);
let info = match self {
TwigParseError::ParsingError(i) => i,
TwigParseError::ParsingFailure(i) => i,
};
let (line, column, last_line) = get_line_and_column_of_subslice(input, info.leftover);
output = format!(
"{}Parsing goes wrong in line {} and column {} :\n",
output, line, column
);
output = format!("{}{}\n", output, last_line);
for _ in 0..(column - 1) {
output = format!("{} ", output);
}
output = format!("{}^\n", output);
for _ in 0..(column - 1) {
output = format!("{} ", output);
}
output = format!("{}|\n", output);
output = match &info.context {
None => format!("{}{:?}", output, info.kind),
Some(c) => format!("{}{}", output, c),
};
output
}
}
trait SubsliceOffset {
fn subslice_offset(&self, inner: &Self) -> Option<usize>;
}
impl SubsliceOffset for str {
fn subslice_offset(&self, inner: &str) -> Option<usize> {
let self_beg = self.as_ptr() as usize;
let inner = inner.as_ptr() as usize;
if inner < self_beg || inner > self_beg.wrapping_add(self.len()) {
None
} else {
Some(inner.wrapping_sub(self_beg))
}
}
}
fn get_line_and_column_of_subslice<'a>(input: &'a str, slice: &'a str) -> (usize, usize, &'a str) {
let offset = input.subslice_offset(slice).unwrap();
let mut last_line_start = 0;
let mut last_line_end = 1;
let mut found = false;
let mut lines = 0;
let mut byte_number = 0;
let mut last_byte = None;
for (i, byte) in input.bytes().enumerate() {
byte_number = i;
if byte == b'\r' || byte == b'\n' {
last_line_end = i + 1;
if let Some(l_byte) = last_byte {
if l_byte != b'\r' || l_byte != b'\n' {
lines += 1;
}
}
if found {
break;
}
last_line_start = last_line_end;
}
if i == offset {
found = true;
}
last_byte = Some(byte);
}
if last_line_start == last_line_end {
last_line_end = byte_number + 1;
lines += 1;
} else {
last_line_end -= 1;
}
let last_line = &input[last_line_start..last_line_end];
let column = offset - last_line_start + 1;
(lines, column, last_line)
}