@renovatebot/good-enough-parser 1.1.24 → 1.2.0

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 (166) hide show
  1. package/README.md +6 -6
  2. package/dist/cjs/index.d.ts +4 -4
  3. package/dist/cjs/index.js +7 -7
  4. package/dist/cjs/lang/groovy.d.ts +6 -6
  5. package/dist/cjs/lang/groovy.js +83 -83
  6. package/dist/cjs/lang/index.d.ts +20 -20
  7. package/dist/cjs/lang/index.js +56 -56
  8. package/dist/cjs/lang/python.d.ts +6 -6
  9. package/dist/cjs/lang/python.js +73 -73
  10. package/dist/cjs/lang/scala.d.ts +6 -6
  11. package/dist/cjs/lang/scala.js +71 -71
  12. package/dist/cjs/lang/starlark.d.ts +6 -6
  13. package/dist/cjs/lang/starlark.js +58 -58
  14. package/dist/cjs/lang/types.d.ts +6 -6
  15. package/dist/cjs/lang/types.js +2 -2
  16. package/dist/cjs/lexer/bracket.d.ts +6 -6
  17. package/dist/cjs/lexer/bracket.js +57 -57
  18. package/dist/cjs/lexer/comment.d.ts +3 -3
  19. package/dist/cjs/lexer/comment.js +44 -44
  20. package/dist/cjs/lexer/index.d.ts +5 -5
  21. package/dist/cjs/lexer/index.js +155 -155
  22. package/dist/cjs/lexer/number.d.ts +2 -2
  23. package/dist/cjs/lexer/number.js +18 -18
  24. package/dist/cjs/lexer/operator.d.ts +2 -2
  25. package/dist/cjs/lexer/operator.js +24 -24
  26. package/dist/cjs/lexer/rules.d.ts +4 -4
  27. package/dist/cjs/lexer/rules.js +74 -74
  28. package/dist/cjs/lexer/string.d.ts +2 -2
  29. package/dist/cjs/lexer/string.js +181 -181
  30. package/dist/cjs/lexer/symbol.d.ts +2 -2
  31. package/dist/cjs/lexer/symbol.js +18 -18
  32. package/dist/cjs/lexer/token.d.ts +3 -3
  33. package/dist/cjs/lexer/token.js +64 -64
  34. package/dist/cjs/lexer/types.d.ts +142 -142
  35. package/dist/cjs/lexer/types.js +2 -2
  36. package/dist/cjs/parser/cursor.d.ts +2 -2
  37. package/dist/cjs/parser/cursor.js +13 -13
  38. package/dist/cjs/parser/index.d.ts +3 -3
  39. package/dist/cjs/parser/index.js +19 -19
  40. package/dist/cjs/parser/tree.d.ts +4 -4
  41. package/dist/cjs/parser/tree.js +246 -246
  42. package/dist/cjs/parser/types.d.ts +33 -33
  43. package/dist/cjs/parser/types.js +2 -2
  44. package/dist/cjs/query/builder.d.ts +144 -144
  45. package/dist/cjs/query/builder.js +332 -332
  46. package/dist/cjs/query/handler.d.ts +2 -2
  47. package/dist/cjs/query/handler.js +10 -10
  48. package/dist/cjs/query/index.d.ts +2 -2
  49. package/dist/cjs/query/index.js +32 -32
  50. package/dist/cjs/query/matchers/abstract-matcher.d.ts +9 -9
  51. package/dist/cjs/query/matchers/abstract-matcher.js +36 -36
  52. package/dist/cjs/query/matchers/alt-matcher.d.ts +10 -10
  53. package/dist/cjs/query/matchers/alt-matcher.js +33 -33
  54. package/dist/cjs/query/matchers/anchor-matcher.d.ts +17 -17
  55. package/dist/cjs/query/matchers/anchor-matcher.js +50 -50
  56. package/dist/cjs/query/matchers/comment-matcher.d.ts +10 -10
  57. package/dist/cjs/query/matchers/comment-matcher.js +39 -39
  58. package/dist/cjs/query/matchers/index.d.ts +6 -6
  59. package/dist/cjs/query/matchers/index.js +22 -22
  60. package/dist/cjs/query/matchers/many-matcher.d.ts +13 -13
  61. package/dist/cjs/query/matchers/many-matcher.js +61 -61
  62. package/dist/cjs/query/matchers/num-matcher.d.ts +8 -8
  63. package/dist/cjs/query/matchers/num-matcher.js +34 -34
  64. package/dist/cjs/query/matchers/op-matcher.d.ts +8 -8
  65. package/dist/cjs/query/matchers/op-matcher.js +34 -34
  66. package/dist/cjs/query/matchers/seq-matcher.d.ts +14 -14
  67. package/dist/cjs/query/matchers/seq-matcher.js +76 -76
  68. package/dist/cjs/query/matchers/str-matcher.d.ts +38 -38
  69. package/dist/cjs/query/matchers/str-matcher.js +108 -108
  70. package/dist/cjs/query/matchers/sym-matcher.d.ts +8 -8
  71. package/dist/cjs/query/matchers/sym-matcher.js +34 -34
  72. package/dist/cjs/query/matchers/tree-matcher.d.ts +23 -23
  73. package/dist/cjs/query/matchers/tree-matcher.js +121 -121
  74. package/dist/cjs/query/options.d.ts +10 -10
  75. package/dist/cjs/query/options.js +149 -149
  76. package/dist/cjs/query/regex.d.ts +1 -1
  77. package/dist/cjs/query/regex.js +13 -13
  78. package/dist/cjs/query/types.d.ts +100 -100
  79. package/dist/cjs/query/types.js +2 -2
  80. package/dist/cjs/util/clone.d.ts +1 -1
  81. package/dist/cjs/util/clone.js +5 -5
  82. package/dist/cjs/util/regex.d.ts +1 -1
  83. package/dist/cjs/util/regex.js +7 -7
  84. package/dist/esm/index.d.ts +4 -4
  85. package/dist/esm/index.js +8 -8
  86. package/dist/esm/lang/groovy.d.ts +6 -6
  87. package/dist/esm/lang/groovy.js +80 -80
  88. package/dist/esm/lang/index.d.ts +20 -20
  89. package/dist/esm/lang/index.js +37 -37
  90. package/dist/esm/lang/python.d.ts +6 -6
  91. package/dist/esm/lang/python.js +70 -70
  92. package/dist/esm/lang/scala.d.ts +6 -6
  93. package/dist/esm/lang/scala.js +68 -68
  94. package/dist/esm/lang/starlark.d.ts +6 -6
  95. package/dist/esm/lang/starlark.js +55 -55
  96. package/dist/esm/lang/types.d.ts +6 -6
  97. package/dist/esm/lang/types.js +1 -1
  98. package/dist/esm/lexer/bracket.d.ts +6 -6
  99. package/dist/esm/lexer/bracket.js +49 -49
  100. package/dist/esm/lexer/comment.d.ts +3 -3
  101. package/dist/esm/lexer/comment.js +39 -39
  102. package/dist/esm/lexer/index.d.ts +5 -5
  103. package/dist/esm/lexer/index.js +136 -136
  104. package/dist/esm/lexer/number.d.ts +2 -2
  105. package/dist/esm/lexer/number.js +14 -14
  106. package/dist/esm/lexer/operator.d.ts +2 -2
  107. package/dist/esm/lexer/operator.js +20 -20
  108. package/dist/esm/lexer/rules.d.ts +4 -4
  109. package/dist/esm/lexer/rules.js +69 -69
  110. package/dist/esm/lexer/string.d.ts +2 -2
  111. package/dist/esm/lexer/string.js +177 -177
  112. package/dist/esm/lexer/symbol.d.ts +2 -2
  113. package/dist/esm/lexer/symbol.js +14 -14
  114. package/dist/esm/lexer/token.d.ts +3 -3
  115. package/dist/esm/lexer/token.js +60 -60
  116. package/dist/esm/lexer/types.d.ts +142 -142
  117. package/dist/esm/lexer/types.js +1 -1
  118. package/dist/esm/parser/cursor.d.ts +2 -2
  119. package/dist/esm/parser/cursor.js +9 -9
  120. package/dist/esm/parser/index.d.ts +3 -3
  121. package/dist/esm/parser/index.js +3 -3
  122. package/dist/esm/parser/tree.d.ts +4 -4
  123. package/dist/esm/parser/tree.js +241 -241
  124. package/dist/esm/parser/types.d.ts +33 -33
  125. package/dist/esm/parser/types.js +1 -1
  126. package/dist/esm/query/builder.d.ts +144 -144
  127. package/dist/esm/query/builder.js +304 -304
  128. package/dist/esm/query/handler.d.ts +2 -2
  129. package/dist/esm/query/handler.js +6 -6
  130. package/dist/esm/query/index.d.ts +2 -2
  131. package/dist/esm/query/index.js +2 -2
  132. package/dist/esm/query/matchers/abstract-matcher.d.ts +9 -9
  133. package/dist/esm/query/matchers/abstract-matcher.js +32 -32
  134. package/dist/esm/query/matchers/alt-matcher.d.ts +10 -10
  135. package/dist/esm/query/matchers/alt-matcher.js +29 -29
  136. package/dist/esm/query/matchers/anchor-matcher.d.ts +17 -17
  137. package/dist/esm/query/matchers/anchor-matcher.js +44 -44
  138. package/dist/esm/query/matchers/comment-matcher.d.ts +10 -10
  139. package/dist/esm/query/matchers/comment-matcher.js +35 -35
  140. package/dist/esm/query/matchers/index.d.ts +6 -6
  141. package/dist/esm/query/matchers/index.js +6 -6
  142. package/dist/esm/query/matchers/many-matcher.d.ts +13 -13
  143. package/dist/esm/query/matchers/many-matcher.js +57 -57
  144. package/dist/esm/query/matchers/num-matcher.d.ts +8 -8
  145. package/dist/esm/query/matchers/num-matcher.js +30 -30
  146. package/dist/esm/query/matchers/op-matcher.d.ts +8 -8
  147. package/dist/esm/query/matchers/op-matcher.js +30 -30
  148. package/dist/esm/query/matchers/seq-matcher.d.ts +14 -14
  149. package/dist/esm/query/matchers/seq-matcher.js +72 -72
  150. package/dist/esm/query/matchers/str-matcher.d.ts +38 -38
  151. package/dist/esm/query/matchers/str-matcher.js +102 -102
  152. package/dist/esm/query/matchers/sym-matcher.d.ts +8 -8
  153. package/dist/esm/query/matchers/sym-matcher.js +30 -30
  154. package/dist/esm/query/matchers/tree-matcher.d.ts +23 -23
  155. package/dist/esm/query/matchers/tree-matcher.js +117 -117
  156. package/dist/esm/query/options.d.ts +10 -10
  157. package/dist/esm/query/options.js +139 -139
  158. package/dist/esm/query/regex.d.ts +1 -1
  159. package/dist/esm/query/regex.js +9 -9
  160. package/dist/esm/query/types.d.ts +100 -100
  161. package/dist/esm/query/types.js +1 -1
  162. package/dist/esm/util/clone.d.ts +1 -1
  163. package/dist/esm/util/clone.js +1 -1
  164. package/dist/esm/util/regex.d.ts +1 -1
  165. package/dist/esm/util/regex.js +3 -3
  166. package/package.json +3 -3
@@ -1,15 +1,15 @@
1
- export function configNumbers(states, { match }) {
2
- return {
3
- ...states,
4
- $: {
5
- ...states.$,
6
- number: {
7
- t: 'regex',
8
- type: 'number',
9
- match,
10
- chunk: null,
11
- },
12
- },
13
- };
14
- }
1
+ export function configNumbers(states, { match }) {
2
+ return {
3
+ ...states,
4
+ $: {
5
+ ...states.$,
6
+ number: {
7
+ t: 'regex',
8
+ type: 'number',
9
+ match,
10
+ chunk: null,
11
+ },
12
+ },
13
+ };
14
+ }
15
15
  //# sourceMappingURL=number.js.map
@@ -1,3 +1,3 @@
1
- import type { OperatorOption, StatesMap } from './types';
2
- export declare function configOperators(states: StatesMap, operators: OperatorOption[]): StatesMap;
1
+ import type { OperatorOption, StatesMap } from './types';
2
+ export declare function configOperators(states: StatesMap, operators: OperatorOption[]): StatesMap;
3
3
  //# sourceMappingURL=operator.d.ts.map
@@ -1,21 +1,21 @@
1
- export function configOperators(states, operators) {
2
- const operatorRules = {};
3
- operators.map((op, idx) => {
4
- const tokenName = `op$${idx}`;
5
- const tokenRule = {
6
- t: 'string',
7
- type: tokenName,
8
- match: op,
9
- chunk: op,
10
- };
11
- operatorRules[tokenName] = tokenRule;
12
- });
13
- return {
14
- ...states,
15
- $: {
16
- ...states.$,
17
- ...operatorRules,
18
- },
19
- };
20
- }
1
+ export function configOperators(states, operators) {
2
+ const operatorRules = {};
3
+ operators.map((op, idx) => {
4
+ const tokenName = `op$${idx}`;
5
+ const tokenRule = {
6
+ t: 'string',
7
+ type: tokenName,
8
+ match: op,
9
+ chunk: op,
10
+ };
11
+ operatorRules[tokenName] = tokenRule;
12
+ });
13
+ return {
14
+ ...states,
15
+ $: {
16
+ ...states.$,
17
+ ...operatorRules,
18
+ },
19
+ };
20
+ }
21
21
  //# sourceMappingURL=operator.js.map
@@ -1,5 +1,5 @@
1
- import type { FallbackRule, LexerRule, OrderedStatesMap, StatesMap } from './types';
2
- export declare const fallbackRule: Omit<FallbackRule, 'type'>;
3
- export declare function sortLexerRules(state: LexerRule[]): LexerRule[];
4
- export declare function createOrderedStateMap(statesMap: StatesMap): OrderedStatesMap;
1
+ import type { FallbackRule, LexerRule, OrderedStatesMap, StatesMap } from './types';
2
+ export declare const fallbackRule: Omit<FallbackRule, 'type'>;
3
+ export declare function sortLexerRules(state: LexerRule[]): LexerRule[];
4
+ export declare function createOrderedStateMap(statesMap: StatesMap): OrderedStatesMap;
5
5
  //# sourceMappingURL=rules.d.ts.map
@@ -1,70 +1,70 @@
1
- import { fallback as mooFallback } from 'moo';
2
- export const fallbackRule = {
3
- ...mooFallback,
4
- t: 'fallback',
5
- chunk: null,
6
- };
7
- function compareChunksByValue(x, y) {
8
- if (typeof x.chunk === 'string' && typeof y.chunk === 'string') {
9
- const xChunk = x.chunk;
10
- const yChunk = y.chunk;
11
- if (yChunk < xChunk) {
12
- return 1;
13
- }
14
- else if (xChunk < yChunk) {
15
- return -1;
16
- }
17
- }
18
- return 0;
19
- }
20
- function compareChunksByInclusion(x, y) {
21
- if (typeof x.chunk === 'string' && typeof y.chunk === 'string') {
22
- const xChunk = x.chunk;
23
- const yChunk = y.chunk;
24
- if (yChunk.startsWith(xChunk)) {
25
- return 1;
26
- }
27
- else if (xChunk.startsWith(yChunk)) {
28
- return -1;
29
- }
30
- }
31
- return 0;
32
- }
33
- export function sortLexerRules(state) {
34
- const values = Object.values(state);
35
- if (values.length < 2) {
36
- return state;
37
- }
38
- const fallbackRules = [];
39
- const numberRules = [];
40
- const chunkRules = [];
41
- const otherRules = [];
42
- for (const rule of values) {
43
- if (rule.t === 'fallback') {
44
- fallbackRules.push(rule);
45
- }
46
- else if (rule.type === 'number') {
47
- numberRules.push(rule);
48
- }
49
- else if (typeof rule.chunk === 'string') {
50
- chunkRules.push(rule);
51
- }
52
- else {
53
- otherRules.push(rule);
54
- }
55
- }
56
- chunkRules.sort((x, y) => compareChunksByValue(x, y));
57
- chunkRules.sort((x, y) => compareChunksByInclusion(x, y));
58
- const res = [...numberRules, ...chunkRules, ...otherRules, ...fallbackRules];
59
- return res;
60
- }
61
- export function createOrderedStateMap(statesMap) {
62
- const result = { $: [] };
63
- for (const [stateName, state] of Object.entries(statesMap)) {
64
- const stateValues = Object.values(state);
65
- const orderedStateValues = sortLexerRules(stateValues);
66
- result[stateName] = orderedStateValues;
67
- }
68
- return result;
69
- }
1
+ import { fallback as mooFallback } from 'moo';
2
+ export const fallbackRule = {
3
+ ...mooFallback,
4
+ t: 'fallback',
5
+ chunk: null,
6
+ };
7
+ function compareChunksByValue(x, y) {
8
+ if (typeof x.chunk === 'string' && typeof y.chunk === 'string') {
9
+ const xChunk = x.chunk;
10
+ const yChunk = y.chunk;
11
+ if (yChunk < xChunk) {
12
+ return 1;
13
+ }
14
+ else if (xChunk < yChunk) {
15
+ return -1;
16
+ }
17
+ }
18
+ return 0;
19
+ }
20
+ function compareChunksByInclusion(x, y) {
21
+ if (typeof x.chunk === 'string' && typeof y.chunk === 'string') {
22
+ const xChunk = x.chunk;
23
+ const yChunk = y.chunk;
24
+ if (yChunk.startsWith(xChunk)) {
25
+ return 1;
26
+ }
27
+ else if (xChunk.startsWith(yChunk)) {
28
+ return -1;
29
+ }
30
+ }
31
+ return 0;
32
+ }
33
+ export function sortLexerRules(state) {
34
+ const values = Object.values(state);
35
+ if (values.length < 2) {
36
+ return state;
37
+ }
38
+ const fallbackRules = [];
39
+ const numberRules = [];
40
+ const chunkRules = [];
41
+ const otherRules = [];
42
+ for (const rule of values) {
43
+ if (rule.t === 'fallback') {
44
+ fallbackRules.push(rule);
45
+ }
46
+ else if (rule.type === 'number') {
47
+ numberRules.push(rule);
48
+ }
49
+ else if (typeof rule.chunk === 'string') {
50
+ chunkRules.push(rule);
51
+ }
52
+ else {
53
+ otherRules.push(rule);
54
+ }
55
+ }
56
+ chunkRules.sort((x, y) => compareChunksByValue(x, y));
57
+ chunkRules.sort((x, y) => compareChunksByInclusion(x, y));
58
+ const res = [...numberRules, ...chunkRules, ...otherRules, ...fallbackRules];
59
+ return res;
60
+ }
61
+ export function createOrderedStateMap(statesMap) {
62
+ const result = { $: [] };
63
+ for (const [stateName, state] of Object.entries(statesMap)) {
64
+ const stateValues = Object.values(state);
65
+ const orderedStateValues = sortLexerRules(stateValues);
66
+ result[stateName] = orderedStateValues;
67
+ }
68
+ return result;
69
+ }
70
70
  //# sourceMappingURL=rules.js.map
@@ -1,3 +1,3 @@
1
- import type { StatesMap, StringOption } from './types';
2
- export declare function configStrings(states: StatesMap, opts: StringOption[]): StatesMap;
1
+ import type { StatesMap, StringOption } from './types';
2
+ export declare function configStrings(states: StatesMap, opts: StringOption[]): StatesMap;
3
3
  //# sourceMappingURL=string.d.ts.map
@@ -1,178 +1,178 @@
1
- import { clone } from '../util/clone';
2
- import { escape } from '../util/regex';
3
- import { getCounterpartBracketKey, isBracketKey, isRightKey } from './bracket';
4
- import { fallbackRule } from './rules';
5
- function copyStateDefinition(state) {
6
- const result = clone(state);
7
- Object.entries(result).forEach(([key, val]) => {
8
- result[key] = clone(val);
9
- });
10
- return result;
11
- }
12
- function exprTplStatesMap($, { tplEndToken, tplEnd }) {
13
- const rootState = {};
14
- const tplState = copyStateDefinition($);
15
- for (const [bracketKey, bracketRule] of Object.entries(tplState)) {
16
- if (bracketRule.t === 'string' &&
17
- isBracketKey(bracketKey) &&
18
- isRightKey(bracketKey) &&
19
- (bracketRule.match.startsWith(tplEnd) ||
20
- tplEnd.startsWith(bracketRule.match))) {
21
- const counterpartKey = getCounterpartBracketKey(bracketKey);
22
- if (!counterpartKey) {
23
- throw new Error('String template definition conflicts with brackets');
24
- }
25
- const counterpartRule = tplState[counterpartKey];
26
- if (!counterpartRule || counterpartRule.t !== 'string') {
27
- throw new Error('String template definition conflicts with brackets');
28
- }
29
- delete tplState[bracketKey];
30
- counterpartRule.push = '$';
31
- rootState[counterpartKey] = clone(counterpartRule);
32
- rootState[bracketKey] = { ...bracketRule, pop: 1 };
33
- }
34
- }
35
- const tplEndRule = {
36
- t: 'string',
37
- type: tplEndToken,
38
- match: tplEnd,
39
- chunk: tplEnd,
40
- pop: 1,
41
- };
42
- tplState[tplEndToken] = tplEndRule;
43
- return { $: rootState, tplState };
44
- }
45
- export function configStrings(states, opts) {
46
- if (!opts.length) {
47
- return states;
48
- }
49
- const $ = copyStateDefinition(states.$);
50
- const strStates = {};
51
- const exprTplPreStates = [];
52
- const tplStates = {};
53
- opts.forEach((strOpt, strIdx) => {
54
- const { startsWith: strStart, endsWith: strEnd = strStart, escapeChar, templates: tplOpts, } = strOpt;
55
- const strToken = `str$${strIdx}`;
56
- const strStartToken = `${strToken}$start`;
57
- const strEndToken = `${strToken}$end`;
58
- const strValueToken = `${strToken}$value`;
59
- const strStateName = `${strToken}$state`;
60
- const strState = {
61
- ...(escapeChar && strEnd[0]
62
- ? {
63
- // 1. Escape the escape character (e.g. \\) to prevent it from escaping the delimiter
64
- [`${strToken}$escape_bs`]: {
65
- t: 'string',
66
- type: strValueToken,
67
- match: `${escapeChar}${escapeChar}`,
68
- chunk: `${escapeChar}${escapeChar}`,
69
- },
70
- // 2. Escape the delimiter (e.g. \")
71
- [`${strToken}$escape`]: {
72
- t: 'string',
73
- type: strValueToken,
74
- match: `${escapeChar}${strEnd[0]}`,
75
- chunk: `${escapeChar}${strEnd[0]}`,
76
- },
77
- }
78
- : {}),
79
- [strEndToken]: {
80
- t: 'string',
81
- type: strEndToken,
82
- match: strEnd,
83
- chunk: strEnd,
84
- pop: 1,
85
- },
86
- [strValueToken]: { ...fallbackRule, type: strValueToken },
87
- };
88
- tplOpts === null || tplOpts === void 0 ? void 0 : tplOpts.forEach((tplOpt, tplIdx) => {
89
- var _a;
90
- const { startsWith: tplStart } = tplOpt;
91
- if (tplOpt.type === 'expr') {
92
- const tplToken = `${strToken}$tpl$${tplIdx}`;
93
- const tplStartToken = `${tplToken}$start`;
94
- const tplEndToken = `${tplToken}$end`;
95
- const tplStateName = `${tplToken}$state`;
96
- strState[tplStartToken] = {
97
- t: 'string',
98
- type: tplStartToken,
99
- match: tplStart,
100
- chunk: tplStart,
101
- push: tplStateName,
102
- };
103
- const { endsWith: tplEnd } = tplOpt;
104
- exprTplPreStates.push({ tplStateName, tplEndToken, tplEnd });
105
- }
106
- if (tplOpt.type === 'var') {
107
- const tplToken = `${strToken}$tpl$${tplIdx}`;
108
- const tplStartToken = `${tplToken}$start`;
109
- const tplTokenName = `${tplToken}$token`;
110
- const { operators = [], symbols } = tplOpt;
111
- let symRegex = symbols;
112
- if (!symRegex && ((_a = $.symbol) === null || _a === void 0 ? void 0 : _a.t) === 'regex') {
113
- symRegex = $.symbol.match;
114
- }
115
- if (!symRegex) {
116
- throw new Error(`String definition isn't found for template definition`);
117
- }
118
- const start = escape(tplStart);
119
- const symSource = symRegex.source;
120
- let varTplSource = `${start}${symSource}`;
121
- const opRules = {};
122
- if (operators.length) {
123
- const opSource = `(?:${operators.map(escape).join('|')})`;
124
- varTplSource += `(?:${opSource}${symSource})*`;
125
- operators.forEach((match, idx) => {
126
- const type = `op$${idx}`;
127
- opRules[type] = {
128
- t: 'string',
129
- type,
130
- match,
131
- chunk: match,
132
- };
133
- });
134
- }
135
- const match = new RegExp(varTplSource);
136
- strState[tplTokenName] = {
137
- t: 'regex',
138
- type: tplTokenName,
139
- match,
140
- chunk: tplStart,
141
- };
142
- const tplStartRule = {
143
- t: 'string',
144
- type: tplStartToken,
145
- match: tplStart,
146
- chunk: tplStart,
147
- };
148
- const symbolRule = {
149
- t: 'regex',
150
- type: 'symbol',
151
- match: symRegex,
152
- chunk: null,
153
- };
154
- tplStates[tplTokenName] = {
155
- [tplStartToken]: tplStartRule,
156
- symbol: symbolRule,
157
- ...opRules,
158
- };
159
- }
160
- });
161
- strStates[strStateName] = strState;
162
- $[strStartToken] = {
163
- t: 'string',
164
- type: strStartToken,
165
- match: strStart,
166
- chunk: strStart,
167
- push: strStateName,
168
- };
169
- });
170
- for (const exprTplStateInput of exprTplPreStates) {
171
- const { tplStateName } = exprTplStateInput;
172
- const exprTplStates = exprTplStatesMap($, exprTplStateInput);
173
- Object.assign($, exprTplStates.$);
174
- tplStates[tplStateName] = exprTplStates.tplState;
175
- }
176
- return { $, ...strStates, ...tplStates };
177
- }
1
+ import { clone } from '../util/clone';
2
+ import { escape } from '../util/regex';
3
+ import { getCounterpartBracketKey, isBracketKey, isRightKey } from './bracket';
4
+ import { fallbackRule } from './rules';
5
+ function copyStateDefinition(state) {
6
+ const result = clone(state);
7
+ Object.entries(result).forEach(([key, val]) => {
8
+ result[key] = clone(val);
9
+ });
10
+ return result;
11
+ }
12
+ function exprTplStatesMap($, { tplEndToken, tplEnd }) {
13
+ const rootState = {};
14
+ const tplState = copyStateDefinition($);
15
+ for (const [bracketKey, bracketRule] of Object.entries(tplState)) {
16
+ if (bracketRule.t === 'string' &&
17
+ isBracketKey(bracketKey) &&
18
+ isRightKey(bracketKey) &&
19
+ (bracketRule.match.startsWith(tplEnd) ||
20
+ tplEnd.startsWith(bracketRule.match))) {
21
+ const counterpartKey = getCounterpartBracketKey(bracketKey);
22
+ if (!counterpartKey) {
23
+ throw new Error('String template definition conflicts with brackets');
24
+ }
25
+ const counterpartRule = tplState[counterpartKey];
26
+ if (!counterpartRule || counterpartRule.t !== 'string') {
27
+ throw new Error('String template definition conflicts with brackets');
28
+ }
29
+ delete tplState[bracketKey];
30
+ counterpartRule.push = '$';
31
+ rootState[counterpartKey] = clone(counterpartRule);
32
+ rootState[bracketKey] = { ...bracketRule, pop: 1 };
33
+ }
34
+ }
35
+ const tplEndRule = {
36
+ t: 'string',
37
+ type: tplEndToken,
38
+ match: tplEnd,
39
+ chunk: tplEnd,
40
+ pop: 1,
41
+ };
42
+ tplState[tplEndToken] = tplEndRule;
43
+ return { $: rootState, tplState };
44
+ }
45
+ export function configStrings(states, opts) {
46
+ if (!opts.length) {
47
+ return states;
48
+ }
49
+ const $ = copyStateDefinition(states.$);
50
+ const strStates = {};
51
+ const exprTplPreStates = [];
52
+ const tplStates = {};
53
+ opts.forEach((strOpt, strIdx) => {
54
+ const { startsWith: strStart, endsWith: strEnd = strStart, escapeChar, templates: tplOpts, } = strOpt;
55
+ const strToken = `str$${strIdx}`;
56
+ const strStartToken = `${strToken}$start`;
57
+ const strEndToken = `${strToken}$end`;
58
+ const strValueToken = `${strToken}$value`;
59
+ const strStateName = `${strToken}$state`;
60
+ const strState = {
61
+ ...(escapeChar && strEnd[0]
62
+ ? {
63
+ // 1. Escape the escape character (e.g. \\) to prevent it from escaping the delimiter
64
+ [`${strToken}$escape_bs`]: {
65
+ t: 'string',
66
+ type: strValueToken,
67
+ match: `${escapeChar}${escapeChar}`,
68
+ chunk: `${escapeChar}${escapeChar}`,
69
+ },
70
+ // 2. Escape the delimiter (e.g. \")
71
+ [`${strToken}$escape`]: {
72
+ t: 'string',
73
+ type: strValueToken,
74
+ match: `${escapeChar}${strEnd[0]}`,
75
+ chunk: `${escapeChar}${strEnd[0]}`,
76
+ },
77
+ }
78
+ : {}),
79
+ [strEndToken]: {
80
+ t: 'string',
81
+ type: strEndToken,
82
+ match: strEnd,
83
+ chunk: strEnd,
84
+ pop: 1,
85
+ },
86
+ [strValueToken]: { ...fallbackRule, type: strValueToken },
87
+ };
88
+ tplOpts === null || tplOpts === void 0 ? void 0 : tplOpts.forEach((tplOpt, tplIdx) => {
89
+ var _a;
90
+ const { startsWith: tplStart } = tplOpt;
91
+ if (tplOpt.type === 'expr') {
92
+ const tplToken = `${strToken}$tpl$${tplIdx}`;
93
+ const tplStartToken = `${tplToken}$start`;
94
+ const tplEndToken = `${tplToken}$end`;
95
+ const tplStateName = `${tplToken}$state`;
96
+ strState[tplStartToken] = {
97
+ t: 'string',
98
+ type: tplStartToken,
99
+ match: tplStart,
100
+ chunk: tplStart,
101
+ push: tplStateName,
102
+ };
103
+ const { endsWith: tplEnd } = tplOpt;
104
+ exprTplPreStates.push({ tplStateName, tplEndToken, tplEnd });
105
+ }
106
+ if (tplOpt.type === 'var') {
107
+ const tplToken = `${strToken}$tpl$${tplIdx}`;
108
+ const tplStartToken = `${tplToken}$start`;
109
+ const tplTokenName = `${tplToken}$token`;
110
+ const { operators = [], symbols } = tplOpt;
111
+ let symRegex = symbols;
112
+ if (!symRegex && ((_a = $.symbol) === null || _a === void 0 ? void 0 : _a.t) === 'regex') {
113
+ symRegex = $.symbol.match;
114
+ }
115
+ if (!symRegex) {
116
+ throw new Error(`String definition isn't found for template definition`);
117
+ }
118
+ const start = escape(tplStart);
119
+ const symSource = symRegex.source;
120
+ let varTplSource = `${start}${symSource}`;
121
+ const opRules = {};
122
+ if (operators.length) {
123
+ const opSource = `(?:${operators.map(escape).join('|')})`;
124
+ varTplSource += `(?:${opSource}${symSource})*`;
125
+ operators.forEach((match, idx) => {
126
+ const type = `op$${idx}`;
127
+ opRules[type] = {
128
+ t: 'string',
129
+ type,
130
+ match,
131
+ chunk: match,
132
+ };
133
+ });
134
+ }
135
+ const match = new RegExp(varTplSource);
136
+ strState[tplTokenName] = {
137
+ t: 'regex',
138
+ type: tplTokenName,
139
+ match,
140
+ chunk: tplStart,
141
+ };
142
+ const tplStartRule = {
143
+ t: 'string',
144
+ type: tplStartToken,
145
+ match: tplStart,
146
+ chunk: tplStart,
147
+ };
148
+ const symbolRule = {
149
+ t: 'regex',
150
+ type: 'symbol',
151
+ match: symRegex,
152
+ chunk: null,
153
+ };
154
+ tplStates[tplTokenName] = {
155
+ [tplStartToken]: tplStartRule,
156
+ symbol: symbolRule,
157
+ ...opRules,
158
+ };
159
+ }
160
+ });
161
+ strStates[strStateName] = strState;
162
+ $[strStartToken] = {
163
+ t: 'string',
164
+ type: strStartToken,
165
+ match: strStart,
166
+ chunk: strStart,
167
+ push: strStateName,
168
+ };
169
+ });
170
+ for (const exprTplStateInput of exprTplPreStates) {
171
+ const { tplStateName } = exprTplStateInput;
172
+ const exprTplStates = exprTplStatesMap($, exprTplStateInput);
173
+ Object.assign($, exprTplStates.$);
174
+ tplStates[tplStateName] = exprTplStates.tplState;
175
+ }
176
+ return { $, ...strStates, ...tplStates };
177
+ }
178
178
  //# sourceMappingURL=string.js.map
@@ -1,3 +1,3 @@
1
- import type { StatesMap, SymbolOption } from './types';
2
- export declare function configSymbols(states: StatesMap, { match }: SymbolOption): StatesMap;
1
+ import type { StatesMap, SymbolOption } from './types';
2
+ export declare function configSymbols(states: StatesMap, { match }: SymbolOption): StatesMap;
3
3
  //# sourceMappingURL=symbol.d.ts.map
@@ -1,15 +1,15 @@
1
- export function configSymbols(states, { match }) {
2
- return {
3
- ...states,
4
- $: {
5
- ...states.$,
6
- symbol: {
7
- t: 'regex',
8
- type: 'symbol',
9
- match,
10
- chunk: null,
11
- },
12
- },
13
- };
14
- }
1
+ export function configSymbols(states, { match }) {
2
+ return {
3
+ ...states,
4
+ $: {
5
+ ...states.$,
6
+ symbol: {
7
+ t: 'regex',
8
+ type: 'symbol',
9
+ match,
10
+ chunk: null,
11
+ },
12
+ },
13
+ };
14
+ }
15
15
  //# sourceMappingURL=symbol.js.map
@@ -1,4 +1,4 @@
1
- import type { Token as MooToken } from 'moo';
2
- import type { Token } from './types';
3
- export declare function coerceToken({ type, value, offset, line, col, lineBreaks }: MooToken, parent?: MooToken): Token;
1
+ import type { Token as MooToken } from 'moo';
2
+ import type { Token } from './types';
3
+ export declare function coerceToken({ type, value, offset, line, col, lineBreaks }: MooToken, parent?: MooToken): Token;
4
4
  //# sourceMappingURL=token.d.ts.map