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
use crate::sources::character_class::CharacterClass;
use crate::sources::span::Span;
use itertools::Itertools;
use miette::{Diagnostic, LabeledSpan, Severity, SourceCode};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use thiserror::Error;
#[derive(Debug, Clone, Error)]
#[error("A parse error occured!")]
pub struct PEGParseError {
pub span: Span,
pub expected: Vec<Expect>,
pub fail_left_rec: bool,
pub fail_loop: bool,
}
impl Diagnostic for PEGParseError {
fn severity(&self) -> Option<Severity> {
Some(Severity::Error)
}
fn source_code(&self) -> Option<&dyn SourceCode> {
Some(&self.span)
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
let expect_str = self.expected.iter().map(|exp| exp.to_string()).join(", ");
let mut labels = vec![];
if self.fail_left_rec {
labels.push(LabeledSpan::new_with_span(Some("Encountered left recursion here. This is a problem with the grammar, and may hide other errors.".to_string()), self.span.clone()));
}
if self.fail_loop {
labels.push(LabeledSpan::new_with_span(Some("Encountered an infinite loop here. This is a problem with the grammar, and may hide other errors.".to_string()), self.span.clone()));
}
match self.expected.len() {
0 => {}
1 => labels.push(LabeledSpan::new_with_span(
Some(format!("Expected {} here", expect_str)),
self.span.clone(),
)),
_ => labels.push(LabeledSpan::new_with_span(
Some(format!("Expected one of {} here", expect_str)),
self.span.clone(),
)),
}
Some(Box::new(labels.into_iter()))
}
}
impl PEGParseError {
pub fn expect(span: Span, expect: Expect) -> Self {
PEGParseError {
span,
expected: vec![expect],
fail_left_rec: false,
fail_loop: false,
}
}
pub fn fail_left_recursion(span: Span) -> Self {
PEGParseError {
span,
expected: vec![],
fail_left_rec: true,
fail_loop: false,
}
}
pub fn fail_loop(span: Span) -> Self {
PEGParseError {
span,
expected: vec![],
fail_left_rec: false,
fail_loop: true,
}
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Expect {
ExpectCharClass(CharacterClass),
ExpectString(String),
ExpectSort(String),
NotEntireInput(),
}
impl Display for Expect {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expect::ExpectCharClass(cc) => {
write!(f, "{}", cc)
}
Expect::ExpectString(s) => {
write!(f, "\'{}\'", s)
}
Expect::ExpectSort(s) => {
write!(f, "{}", s)
}
Expect::NotEntireInput() => {
write!(f, "more input")
}
}
}
}
impl PEGParseError {
pub fn combine(mut self, mut other: PEGParseError) -> PEGParseError {
assert_eq!(self.span.source.name(), other.span.source.name());
match self.span.position.cmp(&other.span.position) {
Ordering::Less => other,
Ordering::Greater => self,
Ordering::Equal => {
self.span.length = self.span.length.max(other.span.length);
self.expected.append(&mut other.expected);
self.fail_left_rec |= other.fail_left_rec;
self
}
}
}
pub fn combine_option_parse_error(
a: Option<PEGParseError>,
b: Option<PEGParseError>,
) -> Option<PEGParseError> {
match (a, b) {
(None, None) => None,
(None, Some(e)) => Some(e),
(Some(e), None) => Some(e),
(Some(e1), Some(e2)) => Some(e1.combine(e2)),
}
}
}