2017-12-19 09:06:54 -06:00
|
|
|
import { Lexer } from "./lexer";
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
export function Parser(expression) {
|
|
|
|
this.expression = expression;
|
|
|
|
this.lexer = new Lexer(expression);
|
|
|
|
this.tokens = this.lexer.tokenize();
|
|
|
|
this.index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser.prototype = {
|
2017-12-19 09:06:54 -06:00
|
|
|
getAst: function() {
|
2016-02-02 05:52:43 -06:00
|
|
|
return this.start();
|
|
|
|
},
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
start: function() {
|
2016-02-02 05:52:43 -06:00
|
|
|
try {
|
|
|
|
return this.functionCall() || this.metricExpression();
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "error",
|
2016-02-02 05:52:43 -06:00
|
|
|
message: e.message,
|
|
|
|
pos: e.pos
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
curlyBraceSegment: function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (this.match("identifier", "{") || this.match("{")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
var curlySegment = "";
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
while (!this.match("") && !this.match("}")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
curlySegment += this.consumeToken().value;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("}")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
this.errorMark("Expected closing '}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
curlySegment += this.consumeToken().value;
|
|
|
|
|
|
|
|
// if curly segment is directly followed by identifier
|
|
|
|
// include it in the segment
|
2017-12-19 09:06:54 -06:00
|
|
|
if (this.match("identifier")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
curlySegment += this.consumeToken().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "segment",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: curlySegment
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
metricSegment: function() {
|
|
|
|
var curly = this.curlyBraceSegment();
|
|
|
|
if (curly) {
|
|
|
|
return curly;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (this.match("identifier") || this.match("number")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
// hack to handle float numbers in metric segments
|
2017-12-19 09:06:54 -06:00
|
|
|
var parts = this.consumeToken().value.split(".");
|
2016-02-02 05:52:43 -06:00
|
|
|
if (parts.length === 2) {
|
2017-12-19 09:06:54 -06:00
|
|
|
this.tokens.splice(this.index, 0, { type: "." });
|
|
|
|
this.tokens.splice(this.index + 1, 0, {
|
|
|
|
type: "number",
|
|
|
|
value: parts[1]
|
|
|
|
});
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "segment",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: parts[0]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("templateStart")) {
|
|
|
|
this.errorMark("Expected metric identifier");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
this.consumeToken();
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("identifier")) {
|
|
|
|
this.errorMark("Expected identifier after templateStart");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var node = {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "template",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: this.consumeToken().value
|
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("templateEnd")) {
|
|
|
|
this.errorMark("Expected templateEnd");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
this.consumeToken();
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
|
|
|
|
metricExpression: function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (
|
|
|
|
!this.match("templateStart") &&
|
|
|
|
!this.match("identifier") &&
|
|
|
|
!this.match("number") &&
|
|
|
|
!this.match("{")
|
|
|
|
) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var node = {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "metric",
|
2016-02-02 05:52:43 -06:00
|
|
|
segments: []
|
|
|
|
};
|
|
|
|
|
|
|
|
node.segments.push(this.metricSegment());
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
while (this.match(".")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
this.consumeToken();
|
|
|
|
|
|
|
|
var segment = this.metricSegment();
|
|
|
|
if (!segment) {
|
2017-12-19 09:06:54 -06:00
|
|
|
this.errorMark("Expected metric identifier");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
node.segments.push(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
|
|
|
|
functionCall: function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("identifier", "(")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var node: any = {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "function",
|
|
|
|
name: this.consumeToken().value
|
2016-02-02 05:52:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// consume left parenthesis
|
|
|
|
this.consumeToken();
|
|
|
|
|
|
|
|
node.params = this.functionParameters();
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match(")")) {
|
|
|
|
this.errorMark("Expected closing parenthesis");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
this.consumeToken();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
|
|
|
|
boolExpression: function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("bool")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "bool",
|
|
|
|
value: this.consumeToken().value === "true"
|
2016-02-02 05:52:43 -06:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
functionParameters: function() {
|
|
|
|
if (this.match(")") || this.match("")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var param =
|
|
|
|
this.functionCall() ||
|
|
|
|
this.numericLiteral() ||
|
|
|
|
this.seriesRefExpression() ||
|
|
|
|
this.boolExpression() ||
|
|
|
|
this.metricExpression() ||
|
|
|
|
this.stringLiteral();
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match(",")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return [param];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.consumeToken();
|
|
|
|
return [param].concat(this.functionParameters());
|
|
|
|
},
|
|
|
|
|
|
|
|
seriesRefExpression: function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.match("identifier")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = this.tokens[this.index].value;
|
|
|
|
if (!value.match(/\#[A-Z]/)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var token = this.consumeToken();
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "series-ref",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: token.value
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
numericLiteral: function() {
|
|
|
|
if (!this.match("number")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "number",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: parseFloat(this.consumeToken().value)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
stringLiteral: function() {
|
|
|
|
if (!this.match("string")) {
|
2016-02-02 05:52:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var token = this.consumeToken();
|
|
|
|
if (token.isUnclosed) {
|
2017-12-19 09:06:54 -06:00
|
|
|
throw { message: "Unclosed string parameter", pos: token.pos };
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
type: "string",
|
2016-02-02 05:52:43 -06:00
|
|
|
value: token.value
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
errorMark: function(text) {
|
|
|
|
var currentToken = this.tokens[this.index];
|
2017-12-19 09:06:54 -06:00
|
|
|
var type = currentToken ? currentToken.type : "end of string";
|
2016-02-02 05:52:43 -06:00
|
|
|
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];
|
2017-12-19 09:06:54 -06:00
|
|
|
return (
|
|
|
|
(token === undefined && type === "") || (token && token.type === type)
|
|
|
|
);
|
2016-02-02 05:52:43 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
match: function(token1, token2) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return (
|
|
|
|
this.matchToken(token1, 0) && (!token2 || this.matchToken(token2, 1))
|
|
|
|
);
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
};
|