commit
message
[Parser,Rules,ObjectFromPairs] work-in-progress on using index constants in parser so it's easier to read.
author
Ben Vogt <[email protected]>
date
2017-08-14 00:49:13
stats
5 file(s) changed,
124 insertions(+),
20 deletions(-)
files
src/Parser/Parser.ts
src/Parser/Rules.ts
src/Utilities/ObjectFromPairs.ts
tests/Utilities/ObjectFromPairsTest.ts
tests/Utils/Asserts.ts
1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
2index 36dd299..7f906db 100644
3--- a/src/Parser/Parser.ts
4+++ b/src/Parser/Parser.ts
5@@ -1,6 +1,47 @@
6 import {
7- RULES
8+ RULES,
9+
10+ WHITE_SPACE_RULE_INDEX,
11+ DOUBLE_QUOTES_RULE_INDEX,
12+ SINGLE_QUOTES_RULE_INDEX,
13+ FORMULA_NAME_RULE_INDEX,
14+ DATE_RULE_INDEX,
15+ TIME_RULE_INDEX,
16+ $_A1_CELL_RULE_INDEX,
17+ A1_CELL_RULE_INDEX,
18+ FORMULA_NAME_SIMPLE_RULE_INDEX,
19+ VARIABLE_RULE_INDEX,
20+ SIMPLE_VARIABLE_RILE_INDEX,
21+ INTEGER_RULE_INDEX,
22+ OPEN_AND_CLOSE_OF_ARRAY_RULE_INDEX,
23+ DOLLAR_SIGN_RULE_INDEX,
24+ AMPERSAND_SIGN_RULE_INDEX,
25+ SINGLE_WHITESPACE_RULE_INDEX,
26+ PERIOD_RULE_INDEX,
27+ COLON_RULE_INDEX,
28+ SEMI_COLON_RULE_INDEX,
29+ COMMA_RULE_INDEX,
30+ ASTERISK_RULE_INDEX,
31+ FORWARD_SLASH_RULE_INDEX,
32+ MINUS_SIGN_RULE_INDEX,
33+ PLUS_SIGN_RULE_INDEX,
34+ CARET_SIGN_RULE_INDEX,
35+ OPEN_PAREN_RULE_INDEX,
36+ CLOSE_PAREN_RULE_INDEX,
37+ GREATER_THAN_SIGN_RULE_INDEX,
38+ LESS_THAN_SIGN_RULE_INDEX,
39+ NOT_RULE_INDEX,
40+ OPEN_DOUBLE_QUOTE_INDEX,
41+ OPEN_SINGLE_QUITE_INDEX,
42+ EXCLAMATION_POINT_RULE_INDEX,
43+ EQUALS_SIGN_RULE_INDEX,
44+ PERCENT_SIGN_RULE_INDEX,
45+ HASH_SIGN_RULE_INDEX,
46+ END_OF_STRING_RULE_INDEX
47 } from "./Rules";
48+import {
49+ ObjectFromPairs
50+} from "../Utilities/ObjectFromPairs";
51
52
53 /* parser generated by jison 0.4.15 */
54@@ -375,26 +416,27 @@ var Parser = (function () {
55 break;
56 }
57 },
58- table: [{
59- 2: 13,
60- 3: 1,
61- 4: 2,
62- 6: 3,
63- 7: $V0,
64- 8: $V1,
65- 9: 6,
66- 10: $V2,
67- 13: $V3,
68- 14: $V4,
69- 19: $V5,
70- 23: $V6,
71- 25: 12,
72- 26: $V7,
73- 28: $V8,
74- 32: $V9,
75- 34: $Va,
76- 36: $Vb
77- }, {1: [3]}, {
78+ table: [
79+ ObjectFromPairs.of([
80+ SINGLE_QUOTES_RULE_INDEX, 13,
81+ FORMULA_NAME_RULE_INDEX, 1,
82+ DATE_RULE_INDEX, 2,
83+ 6, 3,
84+ 7, $V0,
85+ 8, $V1,
86+ 9, 6,
87+ 10, $V2,
88+ 13, $V3,
89+ 14, $V4,
90+ 19, $V5,
91+ 23, $V6,
92+ 25, 12,
93+ 26, $V7,
94+ 28, $V8,
95+ 32, $V9,
96+ 34, $Va,
97+ 36, $Vb
98+ ]),{1: [3]}, {
99 5: [1, 19],
100 11: $Vc,
101 12: $Vd,
102diff --git a/src/Parser/Rules.ts b/src/Parser/Rules.ts
103index d1dba0e..f0c3d51 100644
104--- a/src/Parser/Rules.ts
105+++ b/src/Parser/Rules.ts
106@@ -2,8 +2,8 @@
107 const WHITE_SPACE_RULE = /^(?:\s+)/; // rule 0
108 const DOUBLE_QUOTES_RULE = /^(?:"(\\["]|[^"])*")/; // rule 1
109 const SINGLE_QUOTES_RULE = /^(?:'(\\[']|[^'])*')/; // rule 2
110-const FORMULA_NAME_RULE = /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/; // Changed from /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/
111-const DATE_RULE= /^(?:([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm))/; // rule 4
112+const FORMULA_NAME_RULE = /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/; // Changed from /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/ // rule 3
113+const DATE_RULE = /^(?:([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm))/; // rule 4
114 const TIME_RULE = /^(?:([0]?[0-9]|1[0-9]|2[0-3])[:][0-5][0-9]([:][0-5][0-9])?)/; // rule 5
115 const $_A1_CELL_RULE = /^(?:\$[A-Za-z]+\$[0-9]+)/; // rule 6
116 const A1_CELL_RULE = /^(?:[A-Za-z]+[0-9]+)/; // rules 7
117@@ -38,6 +38,7 @@ const HASH_SIGN_RULE = /^(?:[#])/; // rule 35
118 const END_OF_STRING_RULE = /^(?:$)/; // rule 36
119
120
121+
122 // Sequential rules to use when parsing a given input.
123 const RULES = [
124 WHITE_SPACE_RULE,
125diff --git a/src/Utilities/ObjectFromPairs.ts b/src/Utilities/ObjectFromPairs.ts
126new file mode 100644
127index 0000000..ad11876
128--- /dev/null
129+++ b/src/Utilities/ObjectFromPairs.ts
130@@ -0,0 +1,38 @@
131+/**
132+ * Static class for building objects using variables as keys. Mostly used in situations where we want to build objects
133+ * with similar key structures inside of arrays.
134+ * ```
135+ * var m = "key";
136+ * var n = "another"
137+ * var x = [
138+ * ObjectFromPairs.of([
139+ * m, 1,
140+ * n, 2
141+ * ]);
142+ * ]
143+ * ```
144+ * The example above would result in: `{"key": 1, "another": 2}`.
145+ * NOTE: This code does not perform well, and should be used sparingly.
146+ */
147+class ObjectFromPairs {
148+ /**
149+ * Creates an object from pairs.
150+ * @param values - Even number of values, where odd-indexed values will be used as keys, and even-indexed values will
151+ * be used as values in the object. If function is given odd number of values the last value will still be used as a
152+ * key for which the value will be undefined. Use at your own risk.
153+ * @returns {{}}
154+ */
155+ static of(values : Array<any>) {
156+ var o = {};
157+ for (var i = 0; i < values.length - 1; i = i + 2) {
158+ if (i % 2 === 0) {
159+ o[values[i]] = values[i+1];
160+ }
161+ }
162+ return o;
163+ }
164+}
165+
166+export {
167+ ObjectFromPairs
168+}
169\ No newline at end of file
170diff --git a/tests/Utilities/ObjectFromPairsTest.ts b/tests/Utilities/ObjectFromPairsTest.ts
171new file mode 100644
172index 0000000..f1cc8d2
173--- /dev/null
174+++ b/tests/Utilities/ObjectFromPairsTest.ts
175@@ -0,0 +1,13 @@
176+import {
177+ assertEquals,
178+ test
179+} from "../Utils/Asserts";
180+import {
181+ ObjectFromPairs
182+} from "../../src/Utilities/ObjectFromPairs";
183+
184+test("ObjectFromPairs.of", function () {
185+ assertEquals(ObjectFromPairs.of(["a", 1]), {a:1});
186+ var key = "key";
187+ assertEquals(ObjectFromPairs.of([key, 1]), {"key":1});
188+});
189diff --git a/tests/Utils/Asserts.ts b/tests/Utils/Asserts.ts
190index 4270deb..0de1f76 100644
191--- a/tests/Utils/Asserts.ts
192+++ b/tests/Utils/Asserts.ts
193@@ -10,6 +10,11 @@ function assertEquals(actual, expected) {
194 console.log("expected:", expected, " actual:", actual);
195 console.trace();
196 }
197+ } else if (actual instanceof Object && expected instanceof Object) {
198+ if (JSON.stringify(actual) !== JSON.stringify(expected)) {
199+ console.log("expected:", expected, " actual:", actual);
200+ console.trace();
201+ }
202 } else {
203 if (expected !== actual) {
204 console.log("expected:", expected, " actual:", actual);