@odoo/o-spreadsheet 14.0.1 → 14.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/dist/o-spreadsheet.cjs.js +17880 -0
  2. package/dist/o-spreadsheet.d.ts +4119 -0
  3. package/dist/o-spreadsheet.esm.js +17845 -0
  4. package/dist/o-spreadsheet.iife.js +17882 -0
  5. package/dist/o-spreadsheet.iife.min.js +1446 -0
  6. package/package.json +32 -9
  7. package/readme.md +10 -0
  8. package/.eslintrc.json +0 -20
  9. package/.husky/post-checkout +0 -9
  10. package/.husky/pre-commit +0 -39
  11. package/.prettierignore +0 -1
  12. package/astToFormula.patch +0 -0
  13. package/demo/data.js +0 -255
  14. package/demo/index.html +0 -9
  15. package/demo/libs/Chart.bundle.js +0 -20755
  16. package/demo/main.js +0 -101
  17. package/doc/readme.md +0 -46
  18. package/rollup.config.js +0 -23
  19. package/src/base_plugin.ts +0 -102
  20. package/src/components/autofill.ts +0 -146
  21. package/src/components/bottom_bar.ts +0 -199
  22. package/src/components/color_picker.ts +0 -150
  23. package/src/components/composer/autocomplete_dropdown.ts +0 -135
  24. package/src/components/composer/composer.ts +0 -487
  25. package/src/components/composer/content_editable_helper.ts +0 -146
  26. package/src/components/figures/chart.ts +0 -70
  27. package/src/components/figures/container.ts +0 -268
  28. package/src/components/figures/text_figure.ts +0 -36
  29. package/src/components/grid.ts +0 -635
  30. package/src/components/helpers/dom_helpers.ts +0 -7
  31. package/src/components/helpers/drag_and_drop.ts +0 -17
  32. package/src/components/icons.ts +0 -36
  33. package/src/components/index.ts +0 -1
  34. package/src/components/menu.ts +0 -288
  35. package/src/components/overlay.ts +0 -457
  36. package/src/components/scrollbar.ts +0 -22
  37. package/src/components/selection_input.ts +0 -168
  38. package/src/components/side_panel/cell_is_rule_editor.ts +0 -169
  39. package/src/components/side_panel/chart_panel.ts +0 -104
  40. package/src/components/side_panel/color_scale_rule_editor.ts +0 -145
  41. package/src/components/side_panel/conditional_formatting.ts +0 -396
  42. package/src/components/side_panel/side_panel.ts +0 -127
  43. package/src/components/side_panel/translations_terms.ts +0 -45
  44. package/src/components/spreadsheet.ts +0 -198
  45. package/src/components/top_bar.ts +0 -535
  46. package/src/constants.ts +0 -26
  47. package/src/data.ts +0 -149
  48. package/src/fonts.ts +0 -41
  49. package/src/formulas/compiler.ts +0 -258
  50. package/src/formulas/composer_tokenizer.ts +0 -70
  51. package/src/formulas/index.ts +0 -15
  52. package/src/formulas/normalize.ts +0 -52
  53. package/src/formulas/parser.ts +0 -280
  54. package/src/formulas/range_tokenizer.ts +0 -97
  55. package/src/formulas/tokenizer.ts +0 -242
  56. package/src/functions/arguments.ts +0 -114
  57. package/src/functions/dates.ts +0 -302
  58. package/src/functions/helpers.ts +0 -592
  59. package/src/functions/index.ts +0 -52
  60. package/src/functions/module_database.ts +0 -282
  61. package/src/functions/module_date.ts +0 -743
  62. package/src/functions/module_info.ts +0 -69
  63. package/src/functions/module_logical.ts +0 -208
  64. package/src/functions/module_lookup.ts +0 -185
  65. package/src/functions/module_math.ts +0 -1252
  66. package/src/functions/module_operators.ts +0 -291
  67. package/src/functions/module_statistical.ts +0 -882
  68. package/src/functions/module_text.ts +0 -370
  69. package/src/helpers/color.ts +0 -30
  70. package/src/helpers/coordinates.ts +0 -71
  71. package/src/helpers/grid_manipulation.ts +0 -68
  72. package/src/helpers/index.ts +0 -7
  73. package/src/helpers/misc.ts +0 -76
  74. package/src/helpers/numbers.ts +0 -117
  75. package/src/helpers/uuid.ts +0 -19
  76. package/src/helpers/zones.ts +0 -263
  77. package/src/history.ts +0 -186
  78. package/src/index.ts +0 -84
  79. package/src/model.ts +0 -261
  80. package/src/plugins/autofill.ts +0 -412
  81. package/src/plugins/chart.ts +0 -404
  82. package/src/plugins/clipboard.ts +0 -461
  83. package/src/plugins/conditional_format.ts +0 -445
  84. package/src/plugins/core.ts +0 -1502
  85. package/src/plugins/edition.ts +0 -157
  86. package/src/plugins/evaluation.ts +0 -428
  87. package/src/plugins/figures.ts +0 -123
  88. package/src/plugins/formatting.ts +0 -630
  89. package/src/plugins/highlight.ts +0 -187
  90. package/src/plugins/index.ts +0 -32
  91. package/src/plugins/merge.ts +0 -524
  92. package/src/plugins/renderer.ts +0 -571
  93. package/src/plugins/selection.ts +0 -562
  94. package/src/plugins/selection_inputs.ts +0 -326
  95. package/src/registries/autofill_modifiers.ts +0 -76
  96. package/src/registries/autofill_rules.ts +0 -98
  97. package/src/registries/figure_registry.ts +0 -30
  98. package/src/registries/index.ts +0 -7
  99. package/src/registries/menu_items_registry.ts +0 -110
  100. package/src/registries/menus/cell_menu_registry.ts +0 -79
  101. package/src/registries/menus/col_menu_registry.ts +0 -63
  102. package/src/registries/menus/index.ts +0 -6
  103. package/src/registries/menus/menu_items_actions.ts +0 -425
  104. package/src/registries/menus/row_menu_registry.ts +0 -63
  105. package/src/registries/menus/sheet_menu_registry.ts +0 -75
  106. package/src/registries/menus/topbar_menu_registry.ts +0 -204
  107. package/src/registries/side_panel_registry.ts +0 -26
  108. package/src/registries/topbar_component_registry.ts +0 -12
  109. package/src/registry.ts +0 -51
  110. package/src/translation.ts +0 -44
  111. package/src/types/autofill.ts +0 -48
  112. package/src/types/chart.ts +0 -26
  113. package/src/types/commands.ts +0 -716
  114. package/src/types/conditional_formatting.ts +0 -124
  115. package/src/types/env.ts +0 -13
  116. package/src/types/functions.ts +0 -35
  117. package/src/types/getters.ts +0 -103
  118. package/src/types/index.ts +0 -20
  119. package/src/types/misc.ts +0 -143
  120. package/src/types/rendering.ts +0 -41
  121. package/src/types/workbook_data.ts +0 -62
  122. package/tests/__mocks__/uuid.ts +0 -7
  123. package/tests/canvas.mock.ts +0 -34
  124. package/tests/components/__mocks__/content_editable_helper.ts +0 -79
  125. package/tests/components/__mocks__/scrollbar.ts +0 -18
  126. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +0 -29
  127. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +0 -75
  128. package/tests/components/__snapshots__/context_menu_test.ts.snap +0 -88
  129. package/tests/components/__snapshots__/grid_test.ts.snap +0 -60
  130. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +0 -455
  131. package/tests/components/__snapshots__/top_bar_test.ts.snap +0 -705
  132. package/tests/components/autocomplete_dropdown_test.ts +0 -315
  133. package/tests/components/autofill_test.ts +0 -167
  134. package/tests/components/bottom_bar_test.ts +0 -262
  135. package/tests/components/chart_side_panel_test.ts +0 -123
  136. package/tests/components/composer_test.ts +0 -400
  137. package/tests/components/context_menu_test.ts +0 -825
  138. package/tests/components/grid_manipulation_test.ts +0 -163
  139. package/tests/components/grid_test.ts +0 -581
  140. package/tests/components/overlay_test.ts +0 -541
  141. package/tests/components/selection_input_test.ts +0 -259
  142. package/tests/components/side_panel_test.ts +0 -148
  143. package/tests/components/spreadsheet_test.ts +0 -194
  144. package/tests/components/top_bar_test.ts +0 -427
  145. package/tests/data_test.ts +0 -175
  146. package/tests/dom_helper.ts +0 -42
  147. package/tests/formulas/__snapshots__/compiler_test.ts.snap +0 -302
  148. package/tests/formulas/compiler_test.ts +0 -266
  149. package/tests/formulas/composer_tokenizer_test.ts +0 -137
  150. package/tests/formulas/expressions_test.ts +0 -57
  151. package/tests/formulas/formulas_test.ts +0 -80
  152. package/tests/formulas/normalize_test.ts +0 -82
  153. package/tests/formulas/parser_test.ts +0 -224
  154. package/tests/formulas/range_tokenizer_test.ts +0 -187
  155. package/tests/formulas/tokenizer_test.ts +0 -164
  156. package/tests/functions/arguments_test.ts +0 -141
  157. package/tests/functions/dates_test.ts +0 -1081
  158. package/tests/functions/functions_test.ts +0 -53
  159. package/tests/functions/helper_test.ts +0 -98
  160. package/tests/functions/module_database_test.ts +0 -311
  161. package/tests/functions/module_date_test.ts +0 -941
  162. package/tests/functions/module_info_test.ts +0 -139
  163. package/tests/functions/module_logical_test.ts +0 -369
  164. package/tests/functions/module_lookup_test.ts +0 -541
  165. package/tests/functions/module_math_test.ts +0 -2482
  166. package/tests/functions/module_operators_test.ts +0 -955
  167. package/tests/functions/module_statistical_test.ts +0 -2014
  168. package/tests/functions/module_text_test.ts +0 -667
  169. package/tests/helpers/coordinates_test.ts +0 -25
  170. package/tests/helpers/misc_test.ts +0 -95
  171. package/tests/helpers/numbers_test.ts +0 -196
  172. package/tests/helpers/zones_test.ts +0 -97
  173. package/tests/helpers.ts +0 -384
  174. package/tests/menu_items_registry_test.ts +0 -540
  175. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +0 -90
  176. package/tests/model/plugins/chart_test.ts +0 -562
  177. package/tests/model_test.ts +0 -82
  178. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +0 -131
  179. package/tests/plugins/__snapshots__/renderer_test.ts.snap +0 -371
  180. package/tests/plugins/autofill_test.ts +0 -457
  181. package/tests/plugins/borders_test.ts +0 -295
  182. package/tests/plugins/clipboard_test.ts +0 -1180
  183. package/tests/plugins/conditional_formatting_test.ts +0 -1253
  184. package/tests/plugins/core_test.ts +0 -614
  185. package/tests/plugins/edition_test.ts +0 -100
  186. package/tests/plugins/evaluation_async_test.ts +0 -332
  187. package/tests/plugins/evaluation_dates_test.ts +0 -79
  188. package/tests/plugins/evaluation_test.ts +0 -901
  189. package/tests/plugins/figures_test.ts +0 -327
  190. package/tests/plugins/formatting_test.ts +0 -391
  191. package/tests/plugins/grid_manipulation_test.ts +0 -1366
  192. package/tests/plugins/highlight_test.ts +0 -413
  193. package/tests/plugins/history_test.ts +0 -142
  194. package/tests/plugins/import_export_test.ts +0 -276
  195. package/tests/plugins/merges_test.ts +0 -490
  196. package/tests/plugins/navigation_test.ts +0 -181
  197. package/tests/plugins/plugins/evaluation_test.ts +0 -111
  198. package/tests/plugins/renderer_test.ts +0 -598
  199. package/tests/plugins/resizing_test.ts +0 -152
  200. package/tests/plugins/selection_input_test.ts +0 -409
  201. package/tests/plugins/selection_test.ts +0 -442
  202. package/tests/plugins/sheets_test.ts +0 -643
  203. package/tests/plugins/style_test.ts +0 -90
  204. package/tsconfig.json +0 -22
@@ -1,280 +0,0 @@
1
- import { functionRegistry } from "../functions/index";
2
- import { parseNumber } from "../helpers/index";
3
- import { _lt } from "../translation";
4
- import { Dependencies, NormalizedFormulaString } from "../types";
5
- import { Token, tokenize } from "./tokenizer";
6
-
7
- const functions = functionRegistry.content;
8
-
9
- const UNARY_OPERATORS = ["-", "+"];
10
-
11
- // -----------------------------------------------------------------------------
12
- // PARSER
13
- // -----------------------------------------------------------------------------
14
- interface ASTBase {
15
- debug?: boolean;
16
- }
17
-
18
- interface ASTNumber extends ASTBase {
19
- type: "NUMBER";
20
- value: number;
21
- }
22
-
23
- interface ASTNormalizedNumber extends ASTBase {
24
- type: "NORMALIZED_NUMBER";
25
- value: string;
26
- }
27
-
28
- interface ASTReference extends ASTBase {
29
- type: "REFERENCE";
30
- value: number;
31
- }
32
- interface ASTString extends ASTBase {
33
- type: "STRING";
34
- value: string;
35
- }
36
-
37
- interface ASTNormalizedString extends ASTBase {
38
- type: "NORMALIZED_STRING";
39
- value: string;
40
- }
41
-
42
- interface ASTBoolean extends ASTBase {
43
- type: "BOOLEAN";
44
- value: boolean;
45
- }
46
-
47
- interface ASTUnaryOperation extends ASTBase {
48
- type: "UNARY_OPERATION";
49
- value: any;
50
- right: AST;
51
- }
52
-
53
- interface ASTOperation extends ASTBase {
54
- type: "BIN_OPERATION";
55
- value: any;
56
- left: AST;
57
- right: AST;
58
- }
59
-
60
- export interface ASTFuncall extends ASTBase {
61
- type: "FUNCALL";
62
- value: string;
63
- args: AST[];
64
- }
65
-
66
- export interface ASTAsyncFuncall extends ASTBase {
67
- type: "ASYNC_FUNCALL";
68
- value: string;
69
- args: AST[];
70
- }
71
-
72
- interface ASTUnknown extends ASTBase {
73
- type: "UNKNOWN";
74
- value: string;
75
- }
76
-
77
- export type AST =
78
- | ASTOperation
79
- | ASTUnaryOperation
80
- | ASTFuncall
81
- | ASTAsyncFuncall
82
- | ASTNumber
83
- | ASTBoolean
84
- | ASTString
85
- | ASTReference
86
- | ASTNormalizedNumber
87
- | ASTNormalizedString
88
- | ASTUnknown;
89
-
90
- const OP_PRIORITY = {
91
- "^": 30,
92
- "*": 20,
93
- "/": 20,
94
- "+": 15,
95
- "-": 15,
96
- "&": 13,
97
- ">": 10,
98
- "<>": 10,
99
- ">=": 10,
100
- "<": 10,
101
- "<=": 10,
102
- "=": 10,
103
- };
104
-
105
- const FUNCTION_BP = 6;
106
-
107
- function bindingPower(token: Token): number {
108
- switch (token.type) {
109
- case "NUMBER":
110
- case "SYMBOL":
111
- return 0;
112
- case "COMMA":
113
- return 3;
114
- case "LEFT_PAREN":
115
- return 5;
116
- case "RIGHT_PAREN":
117
- return 5;
118
- case "OPERATOR":
119
- return OP_PRIORITY[token.value] || 15;
120
- }
121
- throw new Error("?");
122
- }
123
-
124
- export const cellReference = new RegExp(/\$?[A-Z]+\$?[0-9]+/, "i");
125
- export const rangeReference = new RegExp(
126
- /^\s*\$?[A-Z]+\$?[0-9]+\s*(\s*:\s*\$?[A-Z]+\$?[0-9]+\s*)?$/,
127
- "i"
128
- );
129
-
130
- function parsePrefix(current: Token, tokens: Token[]): AST {
131
- switch (current.type) {
132
- case "DEBUGGER":
133
- const next = parseExpression(tokens, 1000);
134
- next.debug = true;
135
- return next;
136
- case "NUMBER":
137
- return { type: "NUMBER", value: parseNumber(current.value) };
138
- case "NORMALIZED_NUMBER":
139
- return { type: "NORMALIZED_NUMBER", value: current.value };
140
- case "STRING":
141
- return { type: "STRING", value: current.value };
142
- case "NORMALIZED_STRING":
143
- return { type: "NORMALIZED_STRING", value: current.value };
144
- case "FUNCTION":
145
- if (tokens.shift()!.type !== "LEFT_PAREN") {
146
- throw new Error(_lt("wrong function call"));
147
- } else {
148
- const args: AST[] = [];
149
- if (tokens[0] && tokens[0].type !== "RIGHT_PAREN") {
150
- if (tokens[0].type === "COMMA") {
151
- args.push({ type: "UNKNOWN", value: "" });
152
- } else {
153
- args.push(parseExpression(tokens, FUNCTION_BP));
154
- }
155
- while (tokens[0]?.type === "COMMA") {
156
- tokens.shift();
157
- const token = tokens[0] as Token | undefined;
158
- if (token?.type === "RIGHT_PAREN") {
159
- args.push({ type: "UNKNOWN", value: "" });
160
- break;
161
- } else if (token?.type === "COMMA") {
162
- args.push({ type: "UNKNOWN", value: "" });
163
- } else {
164
- args.push(parseExpression(tokens, FUNCTION_BP));
165
- }
166
- }
167
- }
168
- const closingToken = tokens.shift();
169
- if (!closingToken || closingToken.type !== "RIGHT_PAREN") {
170
- throw new Error(_lt("wrong function call"));
171
- }
172
- const isAsync = functions[current.value.toUpperCase()].async;
173
- const type = isAsync ? "ASYNC_FUNCALL" : "FUNCALL";
174
- return { type, value: current.value, args };
175
- }
176
- case "REFERENCE":
177
- return {
178
- type: "REFERENCE",
179
- value: parseInt(current.value, 10),
180
- };
181
- case "SYMBOL":
182
- if (["TRUE", "FALSE"].includes(current.value.toUpperCase())) {
183
- return { type: "BOOLEAN", value: current.value.toUpperCase() === "TRUE" } as AST;
184
- } else {
185
- if (current.value) {
186
- throw new Error(_lt("Invalid formula"));
187
- }
188
- return { type: "UNKNOWN", value: current.value };
189
- }
190
- case "LEFT_PAREN":
191
- const result = parseExpression(tokens, 5);
192
- if (!tokens.length || tokens[0].type !== "RIGHT_PAREN") {
193
- throw new Error(_lt("unmatched left parenthesis"));
194
- }
195
- tokens.shift();
196
- return result;
197
- default:
198
- if (current.type === "OPERATOR" && UNARY_OPERATORS.includes(current.value)) {
199
- return {
200
- type: "UNARY_OPERATION",
201
- value: current.value,
202
- right: parseExpression(tokens, OP_PRIORITY[current.value]),
203
- };
204
- }
205
- throw new Error(_lt("nope")); //todo: provide explicit error
206
- }
207
- }
208
-
209
- function parseInfix(left: AST, current: Token, tokens: Token[]): AST {
210
- if (current.type === "OPERATOR") {
211
- const bp = bindingPower(current);
212
- const right = parseExpression(tokens, bp);
213
-
214
- return {
215
- type: "BIN_OPERATION",
216
- value: current.value,
217
- left,
218
- right,
219
- };
220
- }
221
- throw new Error(_lt("nope")); //todo: provide explicit error
222
- }
223
-
224
- function parseExpression(tokens: Token[], bp: number): AST {
225
- const token = tokens.shift();
226
- if (!token) {
227
- throw new Error(_lt("invalid expression"));
228
- }
229
- let expr = parsePrefix(token, tokens);
230
- while (tokens[0] && bindingPower(tokens[0]) > bp) {
231
- expr = parseInfix(expr, tokens.shift()!, tokens);
232
- }
233
- return expr;
234
- }
235
-
236
- /**
237
- * Parse an expression (as a string) into an AST.
238
- */
239
- export function parse(str: NormalizedFormulaString): AST {
240
- const tokens = tokenize(str).filter((x) => x.type !== "SPACE");
241
- if (tokens[0].type === "OPERATOR" && tokens[0].value === "=") {
242
- tokens.splice(0, 1);
243
- }
244
- const result = parseExpression(tokens, 0);
245
- if (tokens.length) {
246
- throw new Error(_lt("invalid expression"));
247
- }
248
- return result;
249
- }
250
-
251
- /**
252
- * Converts an ast formula to the corresponding string
253
- */
254
- export function astToFormula(ast: AST, dependencies: Dependencies): string {
255
- switch (ast.type) {
256
- case "FUNCALL":
257
- case "ASYNC_FUNCALL":
258
- const args = ast.args.map((arg) => astToFormula(arg, dependencies));
259
- return `${ast.value}(${args.join(",")})`;
260
- case "NUMBER":
261
- return ast.value.toString();
262
- case "NORMALIZED_STRING":
263
- return `"${dependencies[ast.value]}"`;
264
- case "NORMALIZED_NUMBER":
265
- case "REFERENCE":
266
- return dependencies[ast.value].toString();
267
- case "STRING":
268
- return ast.value;
269
- case "BOOLEAN":
270
- return ast.value ? "TRUE" : "FALSE";
271
- case "UNARY_OPERATION":
272
- return ast.value + astToFormula(ast.right, dependencies);
273
- case "BIN_OPERATION":
274
- return (
275
- astToFormula(ast.left, dependencies) + ast.value + astToFormula(ast.right, dependencies)
276
- );
277
- default:
278
- return ast.value;
279
- }
280
- }
@@ -1,97 +0,0 @@
1
- import { Token, tokenize, TokenType } from "./tokenizer";
2
-
3
- /**
4
- * finds a sequence of token that represent a range and replace them with a single token
5
- * The range can be
6
- * ?spaces symbol ?spaces operator: ?spaces symbol ?spaces
7
- */
8
- export function mergeSymbolsIntoRanges(result: Token[], removeSpace = false): Token[] {
9
- let operator: number | void = undefined;
10
- let refStart: number | void = undefined;
11
- let refEnd: number | void = undefined;
12
- let startIncludingSpaces: number | void = undefined;
13
-
14
- const reset = () => {
15
- startIncludingSpaces = undefined;
16
- refStart = undefined;
17
- operator = undefined;
18
- refEnd = undefined;
19
- };
20
-
21
- for (let i = 0; i < result.length; i++) {
22
- const token = result[i];
23
-
24
- if (startIncludingSpaces) {
25
- if (refStart) {
26
- if (token.type === "SPACE") {
27
- continue;
28
- } else if (token.type === "OPERATOR" && token.value === ":") {
29
- operator = i;
30
- } else if (operator && token.type === "SYMBOL") {
31
- refEnd = i;
32
- } else {
33
- if (startIncludingSpaces && refStart && operator && refEnd) {
34
- const newToken = {
35
- type: <TokenType>"SYMBOL",
36
- value: result
37
- .slice(startIncludingSpaces, i)
38
- .filter((x) => !removeSpace || x.type !== "SPACE")
39
- .map((x) => x.value)
40
- .join(""),
41
- };
42
- result.splice(startIncludingSpaces, i - startIncludingSpaces, newToken);
43
- i = startIncludingSpaces + 1;
44
- reset();
45
- } else {
46
- if (token.type === "SYMBOL") {
47
- startIncludingSpaces = i;
48
- refStart = i;
49
- operator = undefined;
50
- } else {
51
- reset();
52
- }
53
- }
54
- }
55
- } else {
56
- if (token.type === "SYMBOL") {
57
- refStart = i;
58
- operator = refEnd = undefined;
59
- } else {
60
- reset();
61
- }
62
- }
63
- } else {
64
- if (["SPACE", "SYMBOL"].includes(token.type)) {
65
- startIncludingSpaces = i;
66
- refStart = token.type === "SYMBOL" ? i : undefined;
67
- operator = refEnd = undefined;
68
- } else {
69
- reset();
70
- }
71
- }
72
- }
73
- const i = result.length - 1;
74
- if (startIncludingSpaces && refStart && operator && refEnd) {
75
- const newToken = {
76
- type: <TokenType>"SYMBOL",
77
- value: result
78
- .slice(startIncludingSpaces, i + 1)
79
- .filter((x) => !removeSpace || x.type !== "SPACE")
80
- .map((x) => x.value)
81
- .join(""),
82
- };
83
- result.splice(startIncludingSpaces, i - startIncludingSpaces + 1, newToken);
84
- }
85
- return result;
86
- }
87
-
88
- /**
89
- * Take the result of the tokenizer and transform it to be usable in the
90
- * manipulations of range
91
- *
92
- * @param formula
93
- */
94
- export function rangeTokenize(formula: string): Token[] {
95
- const tokens = tokenize(formula);
96
- return mergeSymbolsIntoRanges(tokens, true);
97
- }
@@ -1,242 +0,0 @@
1
- import { functionRegistry } from "../functions/index";
2
- import { formulaNumberRegexp } from "../helpers/index";
3
- import { _lt } from "../translation";
4
-
5
- /**
6
- * Tokenizer
7
- *
8
- * A tokenizer is a piece of code whose job is to transform a string into a list
9
- * of "tokens". For example, "(12+" is converted into:
10
- * [{type: "LEFT_PAREN", value: "("},
11
- * {type: "NUMBER", value: "12"},
12
- * {type: "OPERATOR", value: "+"}]
13
- *
14
- * As the example shows, a tokenizer does not care about the meaning behind those
15
- * tokens. It only cares about the structure.
16
- *
17
- * The tokenizer is usually the first step in a compilation pipeline. Also, it
18
- * is useful for the composer, which needs to be able to work with incomplete
19
- * formulas.
20
- */
21
-
22
- const dependencyIdentifierRegex = /^[S|N]?\d+/;
23
- const functions = functionRegistry.content;
24
- const OPERATORS = "+,-,*,/,:,=,<>,>=,>,<=,<,%,^,&".split(",");
25
-
26
- export type TokenType =
27
- | "OPERATOR"
28
- | "NUMBER"
29
- | "STRING"
30
- | "BOOLEAN"
31
- | "FUNCTION"
32
- | "SYMBOL"
33
- | "SPACE"
34
- | "DEBUGGER"
35
- | "COMMA"
36
- | "LEFT_PAREN"
37
- | "RIGHT_PAREN"
38
- | "REFERENCE"
39
- | "NORMALIZED_NUMBER"
40
- | "NORMALIZED_STRING"
41
- | "UNKNOWN";
42
-
43
- export interface Token {
44
- type: TokenType;
45
- value: string;
46
- }
47
-
48
- export function tokenize(str: string): Token[] {
49
- const chars = str.split("");
50
- const result: Token[] = [];
51
- let tokenCount = 0;
52
-
53
- while (chars.length) {
54
- tokenCount++;
55
- if (tokenCount > 100) {
56
- throw new Error(
57
- _lt(
58
- "This formula has over 100 parts. It can't be processed properly, consider splitting it into multiple cells"
59
- )
60
- );
61
- }
62
- let token =
63
- tokenizeSpace(chars) ||
64
- tokenizeMisc(chars) ||
65
- tokenizeOperator(chars) ||
66
- tokenizeString(chars) ||
67
- tokenizeDebugger(chars) ||
68
- tokenizeNormalizedReferences(chars) ||
69
- tokenizeNumber(chars) ||
70
- tokenizeSymbol(chars);
71
-
72
- if (!token) {
73
- token = { type: "UNKNOWN", value: chars.shift()! };
74
- }
75
-
76
- result.push(token);
77
- }
78
- return result;
79
- }
80
-
81
- export const FORMULA_REF_IDENTIFIER = "|";
82
-
83
- function tokenizeNormalizedReferences(chars: string[]): Token | null {
84
- if (chars[0] === FORMULA_REF_IDENTIFIER) {
85
- chars.shift(); // consume the | even if it is incorrect
86
- const match = chars.join("").match(dependencyIdentifierRegex);
87
- if (match) {
88
- chars.splice(0, match[0].length);
89
- } else {
90
- return null;
91
- }
92
- if (chars[0] === FORMULA_REF_IDENTIFIER) {
93
- chars.shift();
94
- }
95
- const value = match[0];
96
- switch (value[0]) {
97
- case "S":
98
- return { type: "NORMALIZED_STRING", value: value.substring(1) };
99
- case "N":
100
- return { type: "NORMALIZED_NUMBER", value: value.substring(1) };
101
- default:
102
- return { type: "REFERENCE", value };
103
- }
104
- }
105
- return null;
106
- }
107
-
108
- function tokenizeDebugger(chars: string[]): Token | null {
109
- if (chars[0] === "?") {
110
- chars.shift();
111
- return { type: "DEBUGGER", value: "?" };
112
- }
113
- return null;
114
- }
115
-
116
- const misc = {
117
- ",": "COMMA",
118
- "(": "LEFT_PAREN",
119
- ")": "RIGHT_PAREN",
120
- } as const;
121
-
122
- function tokenizeMisc(chars: string[]): Token | null {
123
- if (chars[0] in misc) {
124
- const value = chars.shift()!;
125
- const type = misc[value];
126
- return { type, value };
127
- }
128
- return null;
129
- }
130
-
131
- function startsWith(chars: string[], op: string): boolean {
132
- for (let i = 0; i < op.length; i++) {
133
- if (op[i] !== chars[i]) {
134
- return false;
135
- }
136
- }
137
- return true;
138
- }
139
- function tokenizeOperator(chars: string[]): Token | null {
140
- for (let op of OPERATORS) {
141
- if (startsWith(chars, op)) {
142
- chars.splice(0, op.length);
143
- return { type: "OPERATOR", value: op };
144
- }
145
- }
146
- return null;
147
- }
148
-
149
- function tokenizeNumber(chars: string[]): Token | null {
150
- const match = chars.join("").match(formulaNumberRegexp);
151
- if (match) {
152
- chars.splice(0, match[0].length);
153
- return { type: "NUMBER", value: match[0] };
154
- }
155
- return null;
156
- }
157
-
158
- function tokenizeString(chars: string[]): Token | null {
159
- if (chars[0] === '"') {
160
- const startChar = chars.shift();
161
- const letters: any[] = [startChar];
162
- while (chars[0] && (chars[0] !== startChar || letters[letters.length - 1] === "\\")) {
163
- letters.push(chars.shift());
164
- }
165
- if (chars[0] === '"') {
166
- letters.push(chars.shift());
167
- }
168
- return {
169
- type: "STRING",
170
- value: letters.join(""),
171
- };
172
- }
173
- return null;
174
- }
175
-
176
- const separatorRegexp = /\w|\.|!|\$/;
177
-
178
- /**
179
- * A "Symbol" is just basically any word-like element that can appear in a
180
- * formula, which is not a string. So:
181
- * A1
182
- * SUM
183
- * CEILING.MATH
184
- * A$1
185
- * Sheet2!A2
186
- * 'Sheet 2'!A2
187
- *
188
- * are examples of symbols
189
- */
190
- function tokenizeSymbol(chars: string[]): Token | null {
191
- const result: any[] = [];
192
- // there are two main cases to manage: either something which starts with
193
- // a ', like 'Sheet 2'A2, or a word-like element.
194
- if (chars[0] === "'") {
195
- let lastChar = chars.shift();
196
- result.push(lastChar);
197
- while (chars[0]) {
198
- lastChar = chars.shift();
199
- result.push(lastChar);
200
- if (lastChar === "'") {
201
- if (chars[0] && chars[0] === "'") {
202
- lastChar = chars.shift();
203
- result.push(lastChar);
204
- } else {
205
- break;
206
- }
207
- }
208
- }
209
-
210
- if (lastChar !== "'") {
211
- return {
212
- type: "UNKNOWN",
213
- value: result.join(""),
214
- };
215
- }
216
- }
217
- while (chars[0] && chars[0].match(separatorRegexp)) {
218
- result.push(chars.shift());
219
- }
220
- if (result.length) {
221
- const value = result.join("");
222
- const isFunction = value.toUpperCase() in functions;
223
- const type = isFunction ? "FUNCTION" : "SYMBOL";
224
- return { type, value };
225
- }
226
- return null;
227
- }
228
-
229
- const whiteSpaceRegexp = /\s/;
230
-
231
- function tokenizeSpace(chars: string[]): Token | null {
232
- let length = 0;
233
- while (chars[0] && chars[0].match(whiteSpaceRegexp)) {
234
- length++;
235
- chars.shift();
236
- }
237
-
238
- if (length) {
239
- return { type: "SPACE", value: " ".repeat(length) };
240
- }
241
- return null;
242
- }