fixed sorting of functions, and lexing parsing fix for unclosed string params

This commit is contained in:
Torkel Ödegaard
2013-12-28 11:40:58 +01:00
parent 38609fb964
commit 2acfa83b76
6 changed files with 58 additions and 29 deletions
+18 -18
View File
@@ -3,25 +3,13 @@ define([
], function (Lexer) {
'use strict';
var NodeTypes = {
MetricExpression: 1,
MetricNode: 2,
FunctionCall: 4,
NumericLiteral: 5,
StringLiteral: 6
};
function Parser(expression) {
this.expression = expression;
this.lexer = new Lexer(expression);
this.state = "start";
this.error = null;
this.tokens = this.lexer.tokenize();
this.index = 0;
}
Parser.Nodes = NodeTypes;
Parser.prototype = {
getAst: function () {
@@ -29,7 +17,16 @@ define([
},
start: function () {
return this.functionCall() || this.metricExpression();
try {
return this.functionCall() || this.metricExpression();
}
catch(e) {
return {
type: 'error',
message: e.message,
pos: e.pos
};
}
},
metricExpression: function() {
@@ -52,7 +49,6 @@ define([
var rest = this.metricExpression();
if (!rest) {
this.errorMark('Expected metric identifier');
return null;
}
node.segments = node.segments.concat(rest.segments);
@@ -77,7 +73,6 @@ define([
if (!this.match(')')) {
this.errorMark('Expected closing paranthesis');
return null;
}
this.index++;
@@ -122,19 +117,24 @@ define([
return null;
}
var token = this.tokens[this.index];
if (token.isUnclosed) {
throw { message: 'Unclosed string parameter', pos: token.pos };
}
this.index++;
return {
type: 'string',
value: this.tokens[this.index-1].value
value: token.value
};
},
errorMark: function(text) {
var currentToken = this.tokens[this.index];
var type = currentToken ? currentToken.type : 'end of string';
this.error = {
text: text + " instead found " + type,
throw {
message: text + " instead found " + type,
pos: currentToken ? currentToken.pos : this.lexer.char
};
},