commit
message
[Parser,ParserConstants] expanding comparison operator tests
author
Ben Vogt <[email protected]>
date
2017-12-11 00:03:39
stats
3 file(s) changed,
56 insertions(+),
14 deletions(-)
files
src/Parser/Parser.ts
src/Parser/ParserConstants.ts
tests/Parser/ParseEngineTest.ts
1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
2index 7002e00..f97abd3 100644
3--- a/src/Parser/Parser.ts
4+++ b/src/Parser/Parser.ts
5@@ -354,16 +354,16 @@ let Parser = (function () {
6 action = ACTION_TABLE[state] && ACTION_TABLE[state][symbol];
7 }
8
9- console.log({
10- text: lexer.match,
11- token: SYMBOL_INDEX_TO_NAME[symbol] || symbol,
12- tokenIndex: symbol,
13- line: lexer.yylineno,
14- loc: yyloc,
15- state: state,
16- stack: stack,
17- semanticValueStack: semanticValueStack
18- });
19+ // console.log({
20+ // text: lexer.match,
21+ // token: SYMBOL_INDEX_TO_NAME[symbol] || symbol,
22+ // tokenIndex: symbol,
23+ // line: lexer.yylineno,
24+ // loc: yyloc,
25+ // state: state,
26+ // stack: stack,
27+ // semanticValueStack: semanticValueStack
28+ // });
29
30 // handle parse error
31 if (typeof action === 'undefined' || !action.length || !action[0]) {
32diff --git a/src/Parser/ParserConstants.ts b/src/Parser/ParserConstants.ts
33index 87e3e28..1145f3b 100644
34--- a/src/Parser/ParserConstants.ts
35+++ b/src/Parser/ParserConstants.ts
36@@ -6,7 +6,7 @@ import {
37 const WHITE_SPACE_RULE = /^(?:\s+)/;
38 const DOUBLE_QUOTES_RULE = /^(?:"(\\["]|[^"])*")/;
39 const SINGLE_QUOTES_RULE = /^(?:'(\\[']|[^'])*')/;
40-const FORMULA_NAME_RULE = /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/; // Changed from /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/
41+const FORMULA_NAME_RULE = /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/;
42 const $_A1_CELL_RULE = /^(?:\$[A-Za-z]+\$[0-9]+)/;
43 const A1_CELL_RULE = /^(?:[A-Za-z]+[0-9]+)/;
44 const FORMULA_NAME_SIMPLE_RULE = /^(?:[A-Za-z.]+(?=[(]))/;
45@@ -366,6 +366,7 @@ const enum State {
46 NUMBER_FOLLOWED_BY_CARROT = 29,
47 ADD_TWO_NUMBERS = 45,
48 LESS_THAN_EQUALS = 46,
49+ GREATER_THAN_EQUALS = 49,
50 SUBTRACT_TWO_NUMBERS = 52,
51 MULTIPLY_TWO_NUMBERS = 53,
52 DIVIDE_TWO_NUMBERS = 54,
53@@ -704,7 +705,7 @@ table[State.GREATER_THAN] = ObjectBuilder
54 .add(Symbol.VARIABLE_SEQUENCE, 3)
55 .add(Symbol.NUMBER, State.START_NUMBER)
56 .add(Symbol.STRING, [SHIFT, State.START_STRING])
57- .add(Symbol.EQUALS, [SHIFT, 49])
58+ .add(Symbol.EQUALS, [SHIFT, State.GREATER_THAN_EQUALS])
59 .add(Symbol.PLUS, [SHIFT, 10])
60 .add(Symbol.LEFT_PAREN, [SHIFT, State.LEFT_PAREN])
61 .add(Symbol.MINUS, [SHIFT, 9])
62@@ -1010,7 +1011,7 @@ table[48] = ObjectBuilder
63 .add(Symbol.SEMI_COLON, [REDUCE, ReduceActions.LT])
64 .add(Symbol.COMMA, [REDUCE, ReduceActions.LT])
65 .build();
66-table[49] = ObjectBuilder
67+table[State.GREATER_THAN_EQUALS] = ObjectBuilder
68 .add(Symbol.ERROR, 13)
69 .add(Symbol.EXPRESSION, State.COMPARE_TWO_EXPRESSIONS_WITH_GTE)
70 .add(Symbol.VARIABLE_SEQUENCE, 3)
71diff --git a/tests/Parser/ParseEngineTest.ts b/tests/Parser/ParseEngineTest.ts
72index d9246d2..8382045 100644
73--- a/tests/Parser/ParseEngineTest.ts
74+++ b/tests/Parser/ParseEngineTest.ts
75@@ -370,13 +370,15 @@ test("Parse ^ operator", function(){
76 assertEquals(parser.parse('2 ^ 10'), 1024);
77 });
78
79-test("Parse equality operators", function(){
80+test("Parse comparison operators on numbers", function(){
81 assertEquals(parser.parse('1 = 1'), true);
82 assertEquals(parser.parse('1 = 0'), false);
83 assertEquals(parser.parse('1 < 2'), true);
84 assertEquals(parser.parse('1 < 0'), false);
85+ assertEquals(parser.parse('1 < 1'), false);
86+ assertEquals(parser.parse('1 <= 0'), false);
87 assertEquals(parser.parse('1 <= 1'), true);
88- assertEquals(parser.parse('1 <= 2'), true);// fails...
89+ assertEquals(parser.parse('1 <= 2'), true);
90 assertEquals(parser.parse('1 >= 1'), true);
91 assertEquals(parser.parse('2 >= 1'), true);
92 assertEquals(parser.parse('1 >= 0'), true);
93@@ -385,6 +387,40 @@ test("Parse equality operators", function(){
94 assertEquals(parser.parse('1 <> 2'), true);
95 });
96
97+test("Parse comparison operators on strings", function(){
98+ assertEquals(parser.parse('"abc" = "abc"'), true);
99+ assertEquals(parser.parse('"abc" = "xyz"'), false);
100+ assertEquals(parser.parse('"abc" < "abc"'), false);
101+ assertEquals(parser.parse('"abc" < "xyz"'), true);
102+ assertEquals(parser.parse('"abc" <= "abc"'), true);
103+ assertEquals(parser.parse('"abc" <= "xyz"'), true);
104+ assertEquals(parser.parse('"xyz" <= "abc"'), false);
105+ assertEquals(parser.parse('"abc" >= "abc"'), true);
106+ assertEquals(parser.parse('"abc" >= "zyx"'), false);
107+ assertEquals(parser.parse('"xyz" >= "abc"'), true);
108+ assertEquals(parser.parse('"abc" <> "abc"'), false);
109+ assertEquals(parser.parse('"abc" <> "zyz"'), true);
110+});
111+
112+test("Parse comparison operators on boolean", function(){
113+ assertEquals(parser.parse('TRUE = TRUE'), true);
114+ assertEquals(parser.parse('TRUE = FALSE'), false);
115+ assertEquals(parser.parse('FALSE = FALSE'), true);
116+ assertEquals(parser.parse('TRUE > TRUE'), false);
117+ assertEquals(parser.parse('TRUE > FALSE'), true);
118+ assertEquals(parser.parse('FALSE > FALSE'), false);
119+ assertEquals(parser.parse('TRUE <= TRUE'), true);
120+ assertEquals(parser.parse('TRUE <= FALSE'), false);
121+ assertEquals(parser.parse('FALSE <= TRUE'), true);
122+ assertEquals(parser.parse('TRUE >= TRUE'), true);
123+ assertEquals(parser.parse('TRUE >= FALSE'), true);
124+ assertEquals(parser.parse('FALSE >= TRUE'), false);
125+ assertEquals(parser.parse('TRUE <> TRUE'), false);
126+ assertEquals(parser.parse('FALSE <> FALSE'), false);
127+ assertEquals(parser.parse('TRUE <> FALSE'), true);
128+ assertEquals(parser.parse('FALSE <> TRUE'), true);
129+});
130+
131 test("Parse operators, order of operations", function(){
132 assertEquals(parser.parse('10 + -10'), 0);
133 assertEquals(parser.parse('10 + -10 = 0'), true);
134@@ -521,4 +557,4 @@ test("Parse range following comma", function(){
135
136
137
138-assertEquals(parser.parse('1 <= 2'), true);
139+assertEquals(parser.parse('"one" = "one"'), true);