-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast-navigator.js
129 lines (105 loc) · 3.36 KB
/
ast-navigator.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
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
function createASTNavigator(ast) {
return new ExpressionNavigator(ast);
}
function ExpressionNavigator(ast) {
this.ast = ast;
}
ExpressionNavigator.prototype.str = function() {
return this.ast.str;
}
ExpressionNavigator.prototype.type = function() {
return this.ast.type;
}
ExpressionNavigator.prototype.alternativeIndex = function() {
return this.ast.alternativeIndex;
}
ExpressionNavigator.prototype.nthItem = function(n) {
return createASTNavigator(this.ast.value.sequence[n].result);
}
ExpressionNavigator.prototype.nthItemName = function(n) {
return this.ast.value.sequence[n].name;
}
ExpressionNavigator.prototype.alternativeName = function() {
return this.ast.name;
}
ExpressionNavigator.prototype.nthNamedItemOrThrow = function(n,name) {
if(!name || name === this.nthItemName(n))
return this.nthItem(n);
else throw new Error('named item does not exist here: ' + name);
}
ExpressionNavigator.prototype.descriptors = function(dict, depth) {
var ret = dict || {};
var depth = depth || 0;
if(depth == 0 && this._descriptorCache)
return this._descriptorCache;
else if(depth == 0)
this._descriptorCache = ret;
if(this.ast.explicitExpression && depth++ == 1) return ret;
for(var i=0; i<this.ast.value.sequence.length; ++i) {
var item = this.nthItem(i);
item._descriptors(ret, depth);
}
return ret;
}
ExpressionNavigator.prototype._descriptors = function(dict, depth) {
var ret = dict || {};
var depth = depth || 0;
switch(this.ast.type) {
case 'alternative':
this.descriptors(ret, depth);
break;
case 'group':
if(this.ignored()) return;
else if(this.ast.descriptor) ret[this.ast.descriptor] = this.groupContent();
else this.groupContent().descriptors(ret, depth);
break;
case 'repeated-token':
var tmp = {};
for(var i=0; i<this.count(); ++i) {
var single = this.nthRepetition(i)._descriptors(null, depth);
var keys = Object.keys(single);
for(var j=0; j<keys.length; ++j) {
tmp[keys[j]] = tmp[keys[j]] || [];
tmp[keys[j]][i] = single[keys[j]];
}
}
var keys = Object.keys(tmp);
for(var i=0; i<keys.length; ++i) {
ret[keys[i]] = tmp[keys[i]];
}
break;
}
return ret;
}
ExpressionNavigator.prototype.singleItem = function() {
if(this.ast.value.sequence.length == 1) return this.nthItem(0);
else throw new Error('single item expression expected');
}
//Group
ExpressionNavigator.prototype.groupContent = function() {
if(this.ignored()) throw new Error('no group content');
return createASTNavigator(this.ast.value);
}
ExpressionNavigator.prototype.groupContentName = function() {
return this.groupContent().alternativeName();
}
ExpressionNavigator.prototype.namedGroupContentOrThrow = function(name) {
if(!name || name === this.groupContentName())
return this.groupContent();
else throw new Error('named group content does not exist here: ' + name);
}
ExpressionNavigator.prototype.ignored = function() {
return this.ast.option === 'ignore';
}
//String
ExpressionNavigator.prototype.stringPattern = function() {
return this.ast.pattern;
}
//RepeatedToken
ExpressionNavigator.prototype.count = function() {
return this.ast.count;
}
ExpressionNavigator.prototype.nthRepetition = function(n) {
return createASTNavigator(this.ast.items[n]);
}
module.exports = createASTNavigator;