Commit 8f77a821 by Torkel Ödegaard

began work on parser, lexer

parent 4f0042be
define([
'./lexer'
], function (Lexer) {
var NodeTypes = {
MetricExpression = 1,
MetricNode: 2,
FunctionCall: 4
};
function Node(type, value) {
this.type = type;
this.value = value;
}
function Parser(expression) {
this.expression = expression;
this.lexer = new Lexer(expression);
this.state = "start";
this.error = null;
}
Parser.prototype = {
getAst: function () {
return parse('start');
},
checkToken: function (token, expected) {
if (token === null) {
this.error = "Expected token: " + expected + " instead found end of string";
return;
}
if (token.type === expected) {
return true;
}
this.error = "Expected token "
+ expected + " instead found + "
found + " at position: " + lexer.char;
return false;
},
parse: function (state) {
var node = { children: [] };
var token = lexer.next();
var nextToken = lexer.next();
if (checkToken(token, Lexer.Token.Identifier) {
return null;
}
if (nextToken == null) {
return {
type: NodeTypes.MetricExpression,
nodes: [
{
type: NodeTypes.MetricNode,
value: token.value
}
]
}
}
if (checkToken(nextToken, Lexer.Token.Punctuator)) {
return null;
}
if (nextToken.value === '.') {
return parseMetricExpression(token);
}
},
parseMetricExpression: function(firstToken) {
var node = {
type: NodeTypes.MetricExpression,
nodes: [
{
type: NodeTypes.MetricNode,
value: firstToken.value
}
]
};
var token;
while(true) {
token = lexer.nextToken();
if (checkToken(token, Lexer.Token.Identifier)) {
return null;
}
}
}
/*while(true) {
token = lexer.next();
switch(state) {
case "start":
if (checkToken(token, Lexer.Token.Identifier) {
return;
}
state = "identifier";
prevToken = token;
break;
case "identifier":
if (token == null) {
node.type = NodeTypes.MetricExpression;
node.children.push([
type: NodeTypes.MetricNode,
value: prevToken.value;
]);
return node;
}
if (checkToken(token, Lexer.Token.Punctuator)) {
return;
}
if (token.value === '.') {
state = "metricNode";
node.type = NodeTypes.MetricExpression;
node.children.push({
type: NodeTypes.MetricNode,
value: prevToken.value
});
}
if (token.value === '(') {
state = 'function';
}
break;
case 'metricEnd':
if (token === null) {
return node;
}
if (checkToken(token, Lexer.Token.Punctuator)) {
return null;
}
case 'metricNode':
if (checkToken(token, Lexer.Token.Identifier)) {
return null;
}
node.children.push([
type: NodeTypes.MetricNode,
value: token.value
]);
state = 'metricEnd';
break;
}
}
}*/
};
return Parser;
});
\ No newline at end of file
@charset "utf-8";
body {
margin:0;
}
#mocha {
font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 60px 50px;
}
#mocha ul,
#mocha li {
margin: 0;
padding: 0;
}
#mocha ul {
list-style: none;
}
#mocha h1,
#mocha h2 {
margin: 0;
}
#mocha h1 {
margin-top: 15px;
font-size: 1em;
font-weight: 200;
}
#mocha h1 a {
text-decoration: none;
color: inherit;
}
#mocha h1 a:hover {
text-decoration: underline;
}
#mocha .suite .suite h1 {
margin-top: 0;
font-size: .8em;
}
#mocha .hidden {
display: none;
}
#mocha h2 {
font-size: 12px;
font-weight: normal;
cursor: pointer;
}
#mocha .suite {
margin-left: 15px;
}
#mocha .test {
margin-left: 15px;
overflow: hidden;
}
#mocha .test.pending:hover h2::after {
content: '(pending)';
font-family: arial, sans-serif;
}
#mocha .test.pass.medium .duration {
background: #c09853;
}
#mocha .test.pass.slow .duration {
background: #b94a48;
}
#mocha .test.pass::before {
content: '✓';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #00d6b2;
}
#mocha .test.pass .duration {
font-size: 9px;
margin-left: 5px;
padding: 2px 5px;
color: #fff;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
}
#mocha .test.pass.fast .duration {
display: none;
}
#mocha .test.pending {
color: #0b97c4;
}
#mocha .test.pending::before {
content: '◦';
color: #0b97c4;
}
#mocha .test.fail {
color: #c00;
}
#mocha .test.fail pre {
color: black;
}
#mocha .test.fail::before {
content: '✖';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #c00;
}
#mocha .test pre.error {
color: #c00;
max-height: 300px;
overflow: auto;
}
/**
* (1): approximate for browsers not supporting calc
* (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border)
* ^^ seriously
*/
#mocha .test pre {
display: block;
float: left;
clear: left;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
max-width: 85%; /*(1)*/
max-width: calc(100% - 42px); /*(2)*/
word-wrap: break-word;
border-bottom-color: #ddd;
-webkit-border-radius: 3px;
-webkit-box-shadow: 0 1px 3px #eee;
-moz-border-radius: 3px;
-moz-box-shadow: 0 1px 3px #eee;
border-radius: 3px;
}
#mocha .test h2 {
position: relative;
}
#mocha .test a.replay {
position: absolute;
top: 3px;
right: 0;
text-decoration: none;
vertical-align: middle;
display: block;
width: 15px;
height: 15px;
line-height: 15px;
text-align: center;
background: #eee;
font-size: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-transition: opacity 200ms;
-moz-transition: opacity 200ms;
transition: opacity 200ms;
opacity: 0.3;
color: #888;
}
#mocha .test:hover a.replay {
opacity: 1;
}
#mocha-report.pass .test.fail {
display: none;
}
#mocha-report.fail .test.pass {
display: none;
}
#mocha-report.pending .test.pass,
#mocha-report.pending .test.fail {
display: none;
}
#mocha-report.pending .test.pass.pending {
display: block;
}
#mocha-error {
color: #c00;
font-size: 1.5em;
font-weight: 100;
letter-spacing: 1px;
}
#mocha-stats {
position: fixed;
top: 15px;
right: 10px;
font-size: 12px;
margin: 0;
color: #888;
z-index: 1;
}
#mocha-stats .progress {
float: right;
padding-top: 0;
}
#mocha-stats em {
color: black;
}
#mocha-stats a {
text-decoration: none;
color: inherit;
}
#mocha-stats a:hover {
border-bottom: 1px solid #eee;
}
#mocha-stats li {
display: inline-block;
margin: 0 5px;
list-style: none;
padding-top: 11px;
}
#mocha-stats canvas {
width: 40px;
height: 40px;
}
#mocha code .comment { color: #ddd; }
#mocha code .init { color: #2f6fad; }
#mocha code .string { color: #5890ad; }
#mocha code .keyword { color: #8a6343; }
#mocha code .number { color: #2f6fad; }
@media screen and (max-device-width: 480px) {
#mocha {
margin: 60px 0px;
}
#mocha #stats {
position: absolute;
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
define([
'../../app/services/graphite/lexer'
], function(Lexer) {
describe('when lexing graphite expression', function() {
it('should tokenize metric expression', function() {
var lexer = new Lexer('metric.test.*.asd.count');
var tokens = lexer.tokenize();
expect(tokens[0].value).to.be('metric');
expect(tokens[1].value).to.be('.');
expect(tokens[2].type).to.be(Lexer.Token.Identifier);
expect(tokens[3].type).to.be(Lexer.Token.Punctuator);
});
it('should tokenize functions and args', function() {
var lexer = new Lexer("sum(metric.test, 12, 'test')");
var tokens = lexer.tokenize();
expect(tokens[0].value).to.be('sum');
expect(tokens[0].type).to.be(Lexer.Token.Identifier);
expect(tokens[1].value).to.be('(');
expect(tokens[1].type).to.be(Lexer.Token.Punctuator);
expect(tokens[5].type).to.be(Lexer.Token.Punctuator);
expect(tokens[5].value).to.be(',');
expect(tokens[6].type).to.be(Lexer.Token.NumericLiteral);
expect(tokens[6].value).to.be('12');
expect(tokens[8].type).to.be(Lexer.Token.StringLiteral);
expect(tokens[8].value).to.be('test');
});
});
});
define([
'../../app/services/graphite/Parser'
], function(Parser) {
describe('when parsing graphite expression', function() {
it('should return ast', function() {
var parser = new Parser('metric.test.*.asd.count');
var ast = parser.getAst();
expect(ast[0].type).to.be(Parser.Nodes.MetricExpression);
expect(ast[0].nodes.length).to.be(5);
expect(ast[0].nodes[0].value).to.be('metric');
});
});
});
<html>
<head>
<meta charset="utf-8"/>
<title>Tests</title>
<link rel="stylesheet" href="assets/mocha.css"/>
</head>
<body>
<div id="mocha"></div>
<script src="../vendor/require/require.js"></script>
<script src="../app/components/require.config.js"></script>
<script src="assets/mocha.js"></script>
<script src="assets/expect.js"></script>
<script>
/*globals mocha */
mocha.setup('bdd');
require([
'../specs/lexer-specs',
'../specs/parser-specs',
], function () {
mocha.run();
});
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment