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
use std::error;
use std::fmt;
use std::fmt::Display;
use std::result;
use crate::span::Span;
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
span: Span,
pub(crate) kind: ErrorKind,
}
impl Error {
pub fn new(span: Span, kind: ErrorKind) -> Error {
Error { span, kind }
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
pub fn span(&self) -> Span {
self.span
}
}
impl error::Error for Error {}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.kind().message())
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
Eof(WithinContext),
UnsupportedDispatch,
UnsupportedChar,
InvalidCodePoint,
UnsupportedStringEscape,
IntegerOverflow,
InvalidFloat,
UnexpectedChar(char, WithinContext),
UnevenMap,
InvalidArgLiteral,
}
impl ErrorKind {
pub fn message(&self) -> String {
match self {
ErrorKind::Eof(ref within) => format!(
"unexpected end of file while parsing {}",
within.description()
),
ErrorKind::UnsupportedDispatch => "unsupported dispatch".to_owned(),
ErrorKind::UnsupportedChar => "unsupported character".to_owned(),
ErrorKind::InvalidCodePoint => "invalid code point".to_owned(),
ErrorKind::UnsupportedStringEscape => "unsupported string escape".to_owned(),
ErrorKind::IntegerOverflow => "integer literal does not fit in i64".to_owned(),
ErrorKind::InvalidFloat => "unable to parse float".to_owned(),
ErrorKind::UnexpectedChar(c, within) => {
format!("unexpected `{}` while parsing {}", c, within.description())
}
ErrorKind::UnevenMap => "map literal must have an even number of values".to_owned(),
ErrorKind::InvalidArgLiteral => {
"arg literal must be `%`, `%{integer}` or `%&`".to_owned()
}
}
}
pub fn within_context(&self) -> Option<WithinContext> {
match self {
ErrorKind::Eof(within) | ErrorKind::UnexpectedChar(_, within) => Some(*within),
_ => None,
}
}
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum WithinContext {
List(Span),
Vector(Span),
Set(Span),
Map(Span),
String(Span),
Identifier,
Datum,
Dispatch,
QuoteEscape,
CodePoint,
}
impl WithinContext {
pub fn description(&self) -> &'static str {
match self {
WithinContext::List(_) => "list",
WithinContext::Vector(_) => "vector",
WithinContext::Set(_) => "set",
WithinContext::Map(_) => "map",
WithinContext::String(_) => "string literal",
WithinContext::Identifier => "identifier",
WithinContext::Datum => "datum",
WithinContext::Dispatch => "dispatch",
WithinContext::QuoteEscape => "quote escape",
WithinContext::CodePoint => "code point",
}
}
pub fn expected_next(&self) -> Option<ExpectedNext> {
match self {
WithinContext::List(_) => Some(ExpectedNext::List),
WithinContext::Vector(_) => Some(ExpectedNext::Vector),
WithinContext::Set(_) => Some(ExpectedNext::Set),
WithinContext::Map(_) => Some(ExpectedNext::Map),
WithinContext::String(_) => Some(ExpectedNext::String),
_ => None,
}
}
pub fn open_char_span(&self) -> Option<Span> {
match self {
WithinContext::List(span)
| WithinContext::Vector(span)
| WithinContext::Set(span)
| WithinContext::Map(span)
| WithinContext::String(span) => Some(*span),
_ => None,
}
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ExpectedNext {
List,
Vector,
Set,
Map,
String,
}
impl ExpectedNext {
pub fn close_char(self) -> char {
match self {
ExpectedNext::List => ')',
ExpectedNext::Vector => ']',
ExpectedNext::Set => '}',
ExpectedNext::Map => '}',
ExpectedNext::String => '"',
}
}
pub fn description(self) -> String {
match self {
ExpectedNext::String => "expected `\"`".to_owned(),
other => format!("expected datum or `{}`", other.close_char()),
}
}
}