Skip to content

Commit bae0548

Browse files
committed
Finalized tests
1 parent 1f2c614 commit bae0548

4 files changed

Lines changed: 106 additions & 21 deletions

File tree

src/Parser.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ class Parser {
289289
}
290290
token = this.getExpectedToken(TOKEN_TYPE_BINARY_OPERATOR);
291291
const precedenceInfo = PrecedenceTable[token.subType];
292-
const nextPrecedence = precedenceInfo.association === LEFT_ASSOCIATIVE
293-
? precedenceInfo.precedence + 1 : precedenceInfo.precedence;
292+
const nextPrecedence = precedenceInfo.precedence + 1;
293+
// there currently is no right associative binary operator
294+
// const nextPrecedence = precedenceInfo.association === LEFT_ASSOCIATIVE
295+
// ? precedenceInfo.precedence + 1 : precedenceInfo.precedence;
294296

295297
tree = {
296298
type: PARSER_TYPE_BINARY_OPERATOR,
@@ -306,6 +308,8 @@ class Parser {
306308
const token = this.lookahead(1, [
307309
TOKEN_TYPE_UNARY_OPERATOR,
308310
TOKEN_BOUNDARY_BRACKET_OPEN,
311+
TOKEN_BOUNDARY_STRING_DOUBLE,
312+
TOKEN_BOUNDARY_STRING_SINGLE,
309313
TOKEN_TYPE_VALUE
310314
]);
311315

@@ -336,25 +340,29 @@ class Parser {
336340
}
337341

338342
generateValue() {
339-
const token = this.getExpectedToken(TOKEN_TYPE_VALUE);
343+
const token = this.getExpectedToken([
344+
TOKEN_TYPE_VALUE, TOKEN_BOUNDARY_STRING_SINGLE, TOKEN_BOUNDARY_STRING_DOUBLE
345+
]);
340346
if (token.subType === TOKEN_VALUE_VARIABLE) {
341347
return {
342348
type: PARSER_TYPE_VARIABLE,
343349
name: token.value
344350
};
345351
}
346-
352+
347353
let value;
354+
if (token.subType === TOKEN_BOUNDARY_STRING_SINGLE || token.subType === TOKEN_BOUNDARY_STRING_DOUBLE) {
355+
value = this.getExpectedToken(TOKEN_VALUE_STRING).value;
356+
this.getExpectedToken(token.subType);
357+
}
358+
348359
if (token.subType === TOKEN_VALUE_FLOAT) {
349360
value = parseFloat(token.value);
350361
}
351362
else if (token.subType === TOKEN_VALUE_INTEGER) {
352363
value = parseInt(token.value, 10);
353364
}
354-
else if (token.subType === TOKEN_VALUE_STRING) {
355-
value = token.value;
356-
}
357-
365+
358366
return {
359367
type: PARSER_TYPE_VALUE,
360368
value

test/Parser/Parser.generate.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const Parser = require('../../src/Parser');
55
const Lexer = require('../../src/Lexer');
66
const nb = require('../nodeBuilder');
77

8-
98
function getParserResult(input) {
109
const inputString = Array.isArray(input) ? input.join('\n') : input;
1110
const parser = new Parser(new Lexer(inputString));

test/Parser/Parser.generateExpression.spec.js

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@ const Lexer = require('../../src/Lexer');
66
const nb = require('../nodeBuilder');
77

88
const {
9-
OPERATOR_EQUALS,
10-
OPERATOR_NOT_EQUALS,
11-
OPERATOR_STRICT_EQUALS,
12-
OPERATOR_STRICT_NOT_EQUALS,
139
OPERATOR_AND,
1410
OPERATOR_OR,
15-
OPERATOR_NOT,
1611
OPERATOR_GREATER_THAN,
17-
OPERATOR_LESS_THAN,
18-
OPERATOR_GREATER_EQUAL_THAN,
19-
OPERATOR_LESS_EQUAL_THAN,
2012
} = require('../../src/constants');
2113

22-
describe.only('Parser.generateExpression', function() {
14+
describe('Parser.generateExpression', function() {
2315
[
2416
{
2517
description: 'with base binary operator',
@@ -31,13 +23,99 @@ describe.only('Parser.generateExpression', function() {
3123
)
3224
},
3325
{
34-
description: 'with nested expression operator',
26+
description: 'with expression chain',
3527
input: 'a > b > c',
28+
expected: nb.binaryExpression(
29+
nb.binaryExpression(
30+
nb.variable('a'),
31+
nb.variable('b'),
32+
OPERATOR_GREATER_THAN
33+
),
34+
nb.variable('c'),
35+
OPERATOR_GREATER_THAN
36+
)
37+
},
38+
{
39+
description: 'with brackets on the right',
40+
input: 'a > (b > c)',
3641
expected: nb.binaryExpression(
3742
nb.variable('a'),
38-
nb.variable('b'),
43+
nb.binaryExpression(
44+
nb.variable('b'),
45+
nb.variable('c'),
46+
OPERATOR_GREATER_THAN
47+
),
48+
OPERATOR_GREATER_THAN
49+
)
50+
},
51+
{
52+
description: 'with brackets on the left',
53+
input: '(a > b) > c',
54+
expected: nb.binaryExpression(
55+
nb.binaryExpression(
56+
nb.variable('a'),
57+
nb.variable('b'),
58+
OPERATOR_GREATER_THAN
59+
),
60+
nb.variable('c'),
3961
OPERATOR_GREATER_THAN
4062
)
63+
},
64+
{
65+
description: 'with ors and ands',
66+
input: 'a || b && c',
67+
expected: nb.binaryExpression(
68+
nb.variable('a'),
69+
nb.binaryExpression(
70+
nb.variable('b'),
71+
nb.variable('c'),
72+
OPERATOR_AND
73+
),
74+
OPERATOR_OR
75+
)
76+
},
77+
{
78+
description: 'with a not on the left',
79+
input: '!a || b',
80+
expected: nb.binaryExpression(
81+
nb.notConditional(nb.variable('a')),
82+
nb.variable('b'),
83+
OPERATOR_OR
84+
)
85+
},
86+
{
87+
description: 'with a not on the right',
88+
input: 'a || !b',
89+
expected: nb.binaryExpression(
90+
nb.variable('a'),
91+
nb.notConditional(nb.variable('b')),
92+
OPERATOR_OR
93+
)
94+
},
95+
{
96+
description: 'with a variable',
97+
input: 'a',
98+
expected: nb.variable('a')
99+
},
100+
{
101+
description: 'with a integer value',
102+
input: '123',
103+
expected: nb.value(123)
104+
},
105+
{
106+
description: 'with a float value',
107+
input: '123.34',
108+
expected: nb.value(123.34)
109+
},
110+
{
111+
description: 'with a single quote string',
112+
input: '\'foo\'',
113+
expected: nb.value('foo')
114+
},
115+
{
116+
description: 'with a double quote string',
117+
input: '"foo"',
118+
expected: nb.value('foo')
41119
}
42120
].forEach((testCase) => {
43121
it(`should parse ${testCase.description}`, function() {

test/nodeBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = {
6363
};
6464
},
6565
andConditional(left, right) {
66-
module.exports.binaryExpression(left, right, OPERATOR_AND);
66+
return module.exports.binaryExpression(left, right, OPERATOR_AND);
6767
},
6868
notConditional(expression) {
6969
return {

0 commit comments

Comments
 (0)