1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.parse = void 0;
|
5 |
|
6 | var whitespace = new Set([9, 10, 12, 13, 32]);
|
7 | var ZERO = "0".charCodeAt(0);
|
8 | var NINE = "9".charCodeAt(0);
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function parse(formula) {
|
17 | formula = formula.trim().toLowerCase();
|
18 | if (formula === "even") {
|
19 | return [2, 0];
|
20 | }
|
21 | else if (formula === "odd") {
|
22 | return [2, 1];
|
23 | }
|
24 |
|
25 | var idx = 0;
|
26 | var a = 0;
|
27 | var sign = readSign();
|
28 | var number = readNumber();
|
29 | if (idx < formula.length && formula.charAt(idx) === "n") {
|
30 | idx++;
|
31 | a = sign * (number !== null && number !== void 0 ? number : 1);
|
32 | skipWhitespace();
|
33 | if (idx < formula.length) {
|
34 | sign = readSign();
|
35 | skipWhitespace();
|
36 | number = readNumber();
|
37 | }
|
38 | else {
|
39 | sign = number = 0;
|
40 | }
|
41 | }
|
42 |
|
43 | if (number === null || idx < formula.length) {
|
44 | throw new Error("n-th rule couldn't be parsed ('".concat(formula, "')"));
|
45 | }
|
46 | return [a, sign * number];
|
47 | function readSign() {
|
48 | if (formula.charAt(idx) === "-") {
|
49 | idx++;
|
50 | return -1;
|
51 | }
|
52 | if (formula.charAt(idx) === "+") {
|
53 | idx++;
|
54 | }
|
55 | return 1;
|
56 | }
|
57 | function readNumber() {
|
58 | var start = idx;
|
59 | var value = 0;
|
60 | while (idx < formula.length &&
|
61 | formula.charCodeAt(idx) >= ZERO &&
|
62 | formula.charCodeAt(idx) <= NINE) {
|
63 | value = value * 10 + (formula.charCodeAt(idx) - ZERO);
|
64 | idx++;
|
65 | }
|
66 |
|
67 | return idx === start ? null : value;
|
68 | }
|
69 | function skipWhitespace() {
|
70 | while (idx < formula.length &&
|
71 | whitespace.has(formula.charCodeAt(idx))) {
|
72 | idx++;
|
73 | }
|
74 | }
|
75 | }
|
76 | exports.parse = parse;
|
77 |
|
\ | No newline at end of file |