-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Failure.js
93 lines (76 loc) · 1.87 KB
/
Failure.js
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
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
/*
`Failure`s represent expressions that weren't matched while parsing. They are used to generate
error messages automatically. The interface of `Failure`s includes the collowing methods:
- getText() : String
- getType() : String (one of {"description", "string", "code"})
- isDescription() : bool
- isStringTerminal() : bool
- isCode() : bool
- isFluffy() : bool
- makeFluffy() : void
- subsumes(Failure) : bool
*/
function isValidType(type) {
return type === 'description' || type === 'string' || type === 'code';
}
export class Failure {
constructor(pexpr, text, type) {
if (!isValidType(type)) {
throw new Error('invalid Failure type: ' + type);
}
this.pexpr = pexpr;
this.text = text;
this.type = type;
this.fluffy = false;
}
getPExpr() {
return this.pexpr;
}
getText() {
return this.text;
}
getType() {
return this.type;
}
isDescription() {
return this.type === 'description';
}
isStringTerminal() {
return this.type === 'string';
}
isCode() {
return this.type === 'code';
}
isFluffy() {
return this.fluffy;
}
makeFluffy() {
this.fluffy = true;
}
clearFluffy() {
this.fluffy = false;
}
subsumes(that) {
return (
this.getText() === that.getText() &&
this.type === that.type &&
(!this.isFluffy() || (this.isFluffy() && that.isFluffy()))
);
}
toString() {
return this.type === 'string' ? JSON.stringify(this.getText()) : this.getText();
}
clone() {
const failure = new Failure(this.pexpr, this.text, this.type);
if (this.isFluffy()) {
failure.makeFluffy();
}
return failure;
}
toKey() {
return this.toString() + '#' + this.type;
}
}