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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
use std::{
error::Error,
fmt::{self, Display},
};
use proc_macro2::Span;
use super::{
ast::{Alt, Call, Cat, Fix, Parameter, Variable},
check::Spanning,
typed::Type,
visit::Visitable,
};
/// A type error when using a fix point variable.
#[derive(Debug)]
pub enum VariableError {
/// Usage of a free variable.
FreeVariable(Variable),
/// Usage of a guarded variable.
GuardedVariable(Variable),
}
impl From<VariableError> for syn::Error {
fn from(other: VariableError) -> Self {
match other {
VariableError::FreeVariable(_) => todo!(),
VariableError::GuardedVariable(_) => todo!(),
}
}
}
impl Display for VariableError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FreeVariable(var) => {
let start = var.name().span().start();
write!(
f,
"{}:{}: unbound variable '{}'",
start.line,
start.column,
var.name()
)
}
Self::GuardedVariable(var) => {
let start = var.name().span().start();
write!(
f,
"{}:{}: variable '{}' is guarded",
start.line,
start.column,
var.name()
)
}
}
}
}
impl Error for VariableError {}
/// A type error when concatenating two terms.
#[derive(Debug)]
pub enum CatError {
/// The first term was unexpectedly nullable.
FirstNullable(Cat),
/// The flast set of the first term intersects the first set of the second.
FirstFlastOverlap(Cat),
}
impl From<CatError> for syn::Error {
fn from(other: CatError) -> Self {
match other {
CatError::FirstNullable(cat) => {
let mut err = Self::new(
cat.punct().map_or_else(Span::call_site, |p| p.span),
"first item in sequence cannot accept the empty string",
);
err.combine(Self::new(
cat.first()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"this can accept empty string",
));
err
}
CatError::FirstFlastOverlap(cat) => {
let mut err = Self::new(
cat.punct().map_or_else(Span::call_site, |p| p.span),
"cannot decide whether to repeat first or start accepting second",
);
err.combine(Self::new(
cat.first()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"a repetition of this",
));
err.combine(Self::new(
cat.second()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"collides with the start of this",
));
err
}
}
}
}
impl Display for CatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FirstNullable(cat) => {
let start = cat.punct().map_or_else(Span::call_site, |p| p.span).start();
let fst_start = cat
.first()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
write!(
f,
"{}:{}: term `{}' ({}:{}) accepts the empty string",
start.line,
start.column,
cat.first(),
fst_start.line,
fst_start.column
)
}
Self::FirstFlastOverlap(cat) => {
let start = cat.punct().map_or_else(Span::call_site, |p| p.span).start();
let fst_start = cat
.first()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
let snd_start = cat
.second()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
write!(
f,
"{}:{}: cannot decide whether to repeat `{}' ({}:{}) or start accepting `{}' ({}:{}).",
start.line,
start.column,
cat.first(),
fst_start.line,
fst_start.column,
cat.second(),
snd_start.line,
snd_start.column
)
}
}
}
}
impl Error for CatError {}
/// A type error when alternating two terms.
#[derive(Debug)]
pub enum AltError {
/// Both terms are nullable.
BothNullable(Alt),
/// The first sets of the two terms intersect.
FirstOverlap(Alt),
}
impl From<AltError> for syn::Error {
fn from(other: AltError) -> Self {
match other {
AltError::BothNullable(alt) => {
let mut err = Self::new(
alt.punct().map_or_else(Span::call_site, |p| p.span),
"both branches accept the empty string",
);
err.combine(Self::new(
alt.left()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"left branch accepts the empty string",
));
err.combine(Self::new(
alt.right()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"right branch accepts the empty string",
));
err
}
AltError::FirstOverlap(alt) => {
let mut err = Self::new(
alt.punct().map_or_else(Span::call_site, |p| p.span),
"cannot decide whether to accept the left or right branch",
);
err.combine(Self::new(
alt.left()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"left branch starts with a character",
));
err.combine(Self::new(
alt.right()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site),
"right branch starts with the same character",
));
err
}
}
}
}
impl Display for AltError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BothNullable(alt) => {
let start = alt.punct().map_or_else(Span::call_site, |p| p.span).start();
let left_start = alt
.left()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
let right_start = alt
.right()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
write!(
f,
"{}:{}: both `{}' ({}:{}) and `{}' ({}:{}) accept the empty string.",
start.line,
start.column,
alt.left(),
left_start.line,
left_start.column,
alt.right(),
right_start.line,
right_start.column,
)
}
Self::FirstOverlap(alt) => {
let start = alt.punct().map_or_else(Span::call_site, |p| p.span).start();
let left_start = alt
.left()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
let right_start = alt
.right()
.visit(&mut Spanning)
.unwrap_or_else(Span::call_site)
.start();
write!(
f,
"{}:{}: cannot decide whether to accept `{}' ({}:{}) or `{}' ({}:{}).",
start.line,
start.column,
alt.left(),
left_start.line,
left_start.column,
alt.right(),
right_start.line,
right_start.column,
)
}
}
}
}
impl Error for AltError {}
#[derive(Debug)]
pub struct FixError(pub Fix, pub Type, pub Box<TypeError>);
impl From<FixError> for syn::Error {
fn from(e: FixError) -> Self {
let mut error = Self::from(*e.2);
error.combine(Self::new(
e.0.span().unwrap_or_else(Span::call_site),
format!("assuming `{}' has type {:?}.", e.0.arg(), e.1),
));
error
}
}
impl Display for FixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.2.fmt(f)?;
let start = self.0.span().unwrap_or_else(Span::call_site).start();
write!(
f,
"\n({}:{}) assuming `{}' has type {:?}.",
start.line,
start.column,
self.0.arg(),
self.1
)
}
}
impl Error for FixError {}
#[derive(Debug)]
pub enum TypeError {
Cat(CatError),
Alt(AltError),
Variable(VariableError),
Fix(FixError),
}
impl From<CatError> for TypeError {
fn from(other: CatError) -> Self {
Self::Cat(other)
}
}
impl From<AltError> for TypeError {
fn from(other: AltError) -> Self {
Self::Alt(other)
}
}
impl From<VariableError> for TypeError {
fn from(other: VariableError) -> Self {
Self::Variable(other)
}
}
impl From<TypeError> for syn::Error {
fn from(other: TypeError) -> Self {
match other {
TypeError::Cat(e) => e.into(),
TypeError::Alt(e) => e.into(),
TypeError::Variable(e) => e.into(),
TypeError::Fix(e) => e.into(),
}
}
}
impl Display for TypeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cat(e) => e.fmt(f),
Self::Alt(e) => e.fmt(f),
Self::Variable(e) => e.fmt(f),
Self::Fix(e) => e.fmt(f),
}
}
}
impl Error for TypeError {}
#[derive(Debug)]
pub enum SubstituteError {
FreeParameter(Parameter),
WrongArgCount { call: Call, expected: usize },
}
impl Display for SubstituteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FreeParameter(param) => {
let start = param.name().span().start();
write!(
f,
"{}:{}: undeclared variable `{}'",
start.line,
start.column,
param.name()
)
}
SubstituteError::WrongArgCount { call, expected } => {
let start = call.span().unwrap_or_else(Span::call_site).start();
write!(
f,
"{}:{}: wrong number of arguments. Expected {}, got {}",
start.line,
start.column,
call.args().len(),
expected
)
}
}
}
}
impl Error for SubstituteError {}
|