-
Notifications
You must be signed in to change notification settings - Fork 73
/
parser.js
216 lines (216 loc) · 8.68 KB
/
parser.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
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
System.register(['./lexer'], function(exports_1) {
var lexer_1;
function Parser(expression) {
this.expression = expression;
this.lexer = new lexer_1.Lexer(expression);
this.tokens = this.lexer.tokenize();
this.index = 0;
}
exports_1("Parser", Parser);
return {
setters:[
function (lexer_1_1) {
lexer_1 = lexer_1_1;
}],
execute: function() {
Parser.prototype = {
getAst: function () {
return this.start();
},
start: function () {
try {
return this.functionCall() || this.metricExpression();
}
catch (e) {
return {
type: 'error',
message: e.message,
pos: e.pos
};
}
},
curlyBraceSegment: function () {
if (this.match('identifier', '{') || this.match('{')) {
var curlySegment = "";
while (!this.match('') && !this.match('}')) {
curlySegment += this.consumeToken().value;
}
if (!this.match('}')) {
this.errorMark("Expected closing '}'");
}
curlySegment += this.consumeToken().value;
// if curly segment is directly followed by identifier
// include it in the segment
if (this.match('identifier')) {
curlySegment += this.consumeToken().value;
}
return {
type: 'segment',
value: curlySegment
};
}
else {
return null;
}
},
metricSegment: function () {
var curly = this.curlyBraceSegment();
if (curly) {
return curly;
}
if (this.match('identifier') || this.match('number')) {
// hack to handle float numbers in metric segments
var parts = this.consumeToken().value.split('.');
if (parts.length === 2) {
this.tokens.splice(this.index, 0, { type: '.' });
this.tokens.splice(this.index + 1, 0, { type: 'number', value: parts[1] });
}
return {
type: 'segment',
value: parts[0]
};
}
if (!this.match('templateStart')) {
this.errorMark('Expected metric identifier');
}
this.consumeToken();
if (!this.match('identifier')) {
this.errorMark('Expected identifier after templateStart');
}
var node = {
type: 'template',
value: this.consumeToken().value
};
if (!this.match('templateEnd')) {
this.errorMark('Expected templateEnd');
}
this.consumeToken();
return node;
},
metricExpression: function () {
if (!this.match('templateStart') &&
!this.match('identifier') &&
!this.match('number') &&
!this.match('{')) {
return null;
}
var node = {
type: 'metric',
segments: []
};
node.segments.push(this.metricSegment());
while (this.match('.')) {
this.consumeToken();
var segment = this.metricSegment();
if (!segment) {
this.errorMark('Expected metric identifier');
}
node.segments.push(segment);
}
return node;
},
functionCall: function () {
if (!this.match('identifier', '(')) {
return null;
}
var node = {
type: 'function',
name: this.consumeToken().value,
};
// consume left parenthesis
this.consumeToken();
node.params = this.functionParameters();
if (!this.match(')')) {
this.errorMark('Expected closing parenthesis');
}
this.consumeToken();
return node;
},
boolExpression: function () {
if (!this.match('bool')) {
return null;
}
return {
type: 'bool',
value: this.consumeToken().value === 'true',
};
},
functionParameters: function () {
if (this.match(')') || this.match('')) {
return [];
}
var param = this.functionCall() ||
this.numericLiteral() ||
this.seriesRefExpression() ||
this.boolExpression() ||
this.metricExpression() ||
this.stringLiteral();
if (!this.match(',')) {
return [param];
}
this.consumeToken();
return [param].concat(this.functionParameters());
},
seriesRefExpression: function () {
if (!this.match('identifier')) {
return null;
}
var value = this.tokens[this.index].value;
if (!value.match(/\#[A-Z]/)) {
return null;
}
var token = this.consumeToken();
return {
type: 'series-ref',
value: token.value
};
},
numericLiteral: function () {
if (!this.match('number')) {
return null;
}
return {
type: 'number',
value: parseFloat(this.consumeToken().value)
};
},
stringLiteral: function () {
if (!this.match('string')) {
return null;
}
var token = this.consumeToken();
if (token.isUnclosed) {
throw { message: 'Unclosed string parameter', pos: token.pos };
}
return {
type: 'string',
value: token.value
};
},
errorMark: function (text) {
var currentToken = this.tokens[this.index];
var type = currentToken ? currentToken.type : 'end of string';
throw {
message: text + " instead found " + type,
pos: currentToken ? currentToken.pos : this.lexer.char
};
},
// returns token value and incre
consumeToken: function () {
this.index++;
return this.tokens[this.index - 1];
},
matchToken: function (type, index) {
var token = this.tokens[this.index + index];
return (token === undefined && type === '') ||
token && token.type === type;
},
match: function (token1, token2) {
return this.matchToken(token1, 0) &&
(!token2 || this.matchToken(token2, 1));
},
};
}
}
});
//# sourceMappingURL=parser.js.map