/
pexprs-toFailure.js
72 lines (57 loc) · 2.39 KB
/
pexprs-toFailure.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
'use strict';
// --------------------------------------------------------------------
// Imports
// --------------------------------------------------------------------
const Failure = require('./Failure');
const common = require('./common');
const pexprs = require('./pexprs-main');
// --------------------------------------------------------------------
// Operations
// --------------------------------------------------------------------
pexprs.PExpr.prototype.toFailure = common.abstract('toFailure');
pexprs.any.toFailure = function(grammar) {
return new Failure(this, 'any object', 'description');
};
pexprs.end.toFailure = function(grammar) {
return new Failure(this, 'end of input', 'description');
};
pexprs.Terminal.prototype.toFailure = function(grammar) {
return new Failure(this, this.obj, 'string');
};
pexprs.Range.prototype.toFailure = function(grammar) {
// TODO: come up with something better
return new Failure(this, JSON.stringify(this.from) + '..' + JSON.stringify(this.to), 'code');
};
pexprs.Not.prototype.toFailure = function(grammar) {
const description =
this.expr === pexprs.any ? 'nothing' : 'not ' + this.expr.toFailure(grammar);
return new Failure(this, description, 'description');
};
pexprs.Lookahead.prototype.toFailure = function(grammar) {
return this.expr.toFailure(grammar);
};
pexprs.Apply.prototype.toFailure = function(grammar) {
let {description} = grammar.rules[this.ruleName];
if (!description) {
const article = /^[aeiouAEIOU]/.test(this.ruleName) ? 'an' : 'a';
description = article + ' ' + this.ruleName;
}
return new Failure(this, description, 'description');
};
pexprs.UnicodeChar.prototype.toFailure = function(grammar) {
return new Failure(this, 'a Unicode [' + this.category + '] character', 'description');
};
pexprs.Alt.prototype.toFailure = function(grammar) {
const fs = this.terms.map(t => t.toFailure(grammar));
const description = '(' + fs.join(' or ') + ')';
return new Failure(this, description, 'description');
};
pexprs.Seq.prototype.toFailure = function(grammar) {
const fs = this.factors.map(f => f.toFailure(grammar));
const description = '(' + fs.join(' ') + ')';
return new Failure(this, description, 'description');
};
pexprs.Iter.prototype.toFailure = function(grammar) {
const description = '(' + this.expr.toFailure(grammar) + this.operator + ')';
return new Failure(this, description, 'description');
};