Skip to content

Commit e19d3a5

Browse files
committed
parser and lexer is working ok
1 parent 2ded42e commit e19d3a5

File tree

5 files changed

+68
-53
lines changed

5 files changed

+68
-53
lines changed

grafana.sublime-project

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
2-
"folders":
3-
[
4-
{
5-
"follow_symlinks": true,
6-
"path": ".",
7-
"folder_exclude_patterns": [
8-
"node_modules"
9-
]
10-
}
11-
],
12-
"settings":
13-
{
14-
"tab_size": 2,
15-
"translate_tabs_to_spaces": true,
16-
"trim_trailing_white_space_on_save": true
17-
}
2+
"folders":
3+
[
4+
{
5+
"follow_symlinks": true,
6+
"path": ".",
7+
"folder_exclude_patterns": [
8+
"node_modules"
9+
]
10+
}
11+
],
12+
"settings":
13+
{
14+
"tab_size": 2,
15+
"translate_tabs_to_spaces": true,
16+
"trim_trailing_white_space_on_save": true
17+
}
1818
}

src/app/services/graphite/lexer.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ define([
339339

340340
return {
341341
type: type,
342-
value: id
342+
value: id,
343+
pos: this.char
343344
};
344345

345346
},
@@ -407,7 +408,8 @@ define([
407408
return {
408409
type: 'number',
409410
value: value,
410-
isMalformed: true
411+
isMalformed: true,
412+
pos: this.char
411413
};
412414
}
413415

@@ -422,7 +424,8 @@ define([
422424
type: 'number',
423425
value: value,
424426
base: 16,
425-
isMalformed: false
427+
isMalformed: false,
428+
pos: this.char
426429
};
427430
}
428431

@@ -538,6 +541,7 @@ define([
538541
type: 'number',
539542
value: value,
540543
base: 10,
544+
pos: this.char,
541545
isMalformed: !isFinite(value)
542546
};
543547
},
@@ -554,7 +558,8 @@ define([
554558
case "}":
555559
return {
556560
type: ch1,
557-
value: ch1
561+
value: ch1,
562+
pos: this.char
558563
};
559564
}
560565

@@ -658,7 +663,8 @@ define([
658663
type: 'string',
659664
value: value,
660665
isUnclosed: false,
661-
quote: quote
666+
quote: quote,
667+
pos: this.char
662668
};
663669
},
664670

src/app/services/graphite/parser.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,17 @@ define([
5454
if (this.match('.')) {
5555
this.index++;
5656
var rest = this.metricExpression();
57+
if (!rest) {
58+
this.errorMark('Expected metric identifier');
59+
return null;
60+
}
61+
5762
node.segments = node.segments.concat(rest.segments)
5863
}
5964

6065
return node;
6166
},
6267

63-
matchToken: function(type, index) {
64-
var token = this.tokens[this.index + index];
65-
return (token === undefined && type === '') ||
66-
token && token.type === type;
67-
},
68-
69-
match: function(token1, token2) {
70-
return this.matchToken(token1, 0) &&
71-
(!token2 || this.matchToken(token2, 1))
72-
},
73-
7468
functionCall: function() {
7569
if (!this.match('identifier', '(')) {
7670
return null;
@@ -86,7 +80,7 @@ define([
8680
node.params = this.functionParameters();
8781

8882
if (!this.match(')')) {
89-
this.error = 'missing closing paranthesis';
83+
this.errorMark('Expected closing paranthesis');
9084
return null;
9185
}
9286

@@ -140,27 +134,26 @@ define([
140134
};
141135
},
142136

143-
isUnexpectedToken: function (expected, value) {
144-
if (this.token === null) {
145-
this.error = "Expected token: " + expected + " instead found end of string";
146-
return true;
147-
}
148-
149-
if (this.token.type === expected) {
150-
return false;
151-
}
152-
153-
if (value && this.token.value === value) {
154-
return false;
155-
}
137+
errorMark: function(text) {
138+
var currentToken = this.tokens[this.index];
139+
var type = currentToken ? currentToken.type : 'end of string';
140+
this.error = {
141+
text: text + " instead found " + type,
142+
pos: currentToken ? currentToken.pos : this.lexer.char
143+
};
144+
},
156145

157-
this.error = "Expected token " + expected +
158-
' instead found token ' + this.token.type +
159-
' ("' + this.token.value + '")' +
160-
" at position: " + this.lexer.char;
146+
matchToken: function(type, index) {
147+
var token = this.tokens[this.index + index];
148+
return (token === undefined && type === '') ||
149+
token && token.type === type;
150+
},
161151

162-
return true;
152+
match: function(token1, token2) {
153+
return this.matchToken(token1, 0) &&
154+
(!token2 || this.matchToken(token2, 1))
163155
},
156+
164157
};
165158

166159
return Parser;

src/test/specs/lexer-specs.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ define([
1111
expect(tokens[1].value).to.be('.');
1212
expect(tokens[2].type).to.be('identifier');
1313
expect(tokens[4].type).to.be('identifier');
14+
expect(tokens[4].pos).to.be(13);
1415
});
1516

1617
it('should tokenize functions and args', function() {

src/test/specs/parser-specs.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ define([
22
'../../app/services/graphite/Parser'
33
], function(Parser) {
44

5-
describe('when parsing graphite expression', function() {
5+
describe('when parsing', function() {
66

77
it('simple metric expression', function() {
88
var parser = new Parser('metric.test.*.asd.count');
@@ -12,7 +12,6 @@ define([
1212
expect(rootNode.type).to.be('metric');
1313
expect(rootNode.segments.length).to.be(5);
1414
expect(rootNode.segments[0].value).to.be('metric');
15-
1615
});
1716

1817
it('simple function', function() {
@@ -60,6 +59,22 @@ define([
6059
expect(rootNode.params[1].type).to.be('metric');
6160
});
6261

62+
it('invalid metric expression', function() {
63+
var parser = new Parser('metric.test.*.asd.');
64+
var rootNode = parser.getAst();
65+
66+
expect(parser.error.text).to.be('Expected metric identifier instead found end of string');
67+
expect(parser.error.pos).to.be(19);
68+
});
69+
70+
it('invalid function expression missing closing paranthesis', function() {
71+
var parser = new Parser('sum(test');
72+
var rootNode = parser.getAst();
73+
74+
expect(parser.error.text).to.be('Expected closing paranthesis instead found end of string');
75+
expect(parser.error.pos).to.be(9);
76+
});
77+
6378
});
6479

6580
});

0 commit comments

Comments
 (0)