@markuplint/types 1.0.0-dev.20211213.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 (177) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +117 -0
  3. package/extends.json +12 -0
  4. package/gen/specific-schema.json +98 -0
  5. package/gen/types.ts +66 -0
  6. package/lib/check-multi-types.d.ts +2 -0
  7. package/lib/check-multi-types.js +29 -0
  8. package/lib/check.d.ts +8 -0
  9. package/lib/check.js +49 -0
  10. package/lib/check.spec.d.ts +1 -0
  11. package/lib/check.spec.js +57 -0
  12. package/lib/css-syntax.d.ts +2 -0
  13. package/lib/css-syntax.js +141 -0
  14. package/lib/css-syntax.spec.d.ts +1 -0
  15. package/lib/css-syntax.spec.js +157 -0
  16. package/lib/debug.d.ts +2 -0
  17. package/lib/debug.js +6 -0
  18. package/lib/defs.d.ts +3 -0
  19. package/lib/defs.js +752 -0
  20. package/lib/enum.d.ts +3 -0
  21. package/lib/enum.js +26 -0
  22. package/lib/get-candicate.d.ts +3 -0
  23. package/lib/get-candicate.js +26 -0
  24. package/lib/index.d.ts +3 -0
  25. package/lib/index.js +6 -0
  26. package/lib/keyword-type.d.ts +3 -0
  27. package/lib/keyword-type.js +46 -0
  28. package/lib/list.d.ts +3 -0
  29. package/lib/list.js +29 -0
  30. package/lib/list.spec.d.ts +1 -0
  31. package/lib/list.spec.js +129 -0
  32. package/lib/match-result.d.ts +9 -0
  33. package/lib/match-result.js +33 -0
  34. package/lib/number.d.ts +3 -0
  35. package/lib/number.js +41 -0
  36. package/lib/primitive/index.d.ts +7 -0
  37. package/lib/primitive/index.js +17 -0
  38. package/lib/primitive/index.spec.d.ts +1 -0
  39. package/lib/primitive/index.spec.js +62 -0
  40. package/lib/primitive/is-float.d.ts +6 -0
  41. package/lib/primitive/is-float.js +12 -0
  42. package/lib/primitive/is-int.d.ts +6 -0
  43. package/lib/primitive/is-int.js +12 -0
  44. package/lib/primitive/is-non-zero-uint.d.ts +6 -0
  45. package/lib/primitive/is-non-zero-uint.js +12 -0
  46. package/lib/primitive/is-quantity.d.ts +7 -0
  47. package/lib/primitive/is-quantity.js +41 -0
  48. package/lib/primitive/is-uint.d.ts +8 -0
  49. package/lib/primitive/is-uint.js +19 -0
  50. package/lib/primitive/range.d.ts +8 -0
  51. package/lib/primitive/range.js +18 -0
  52. package/lib/primitive/split-unit.d.ts +9 -0
  53. package/lib/primitive/split-unit.js +24 -0
  54. package/lib/rfc/is-bcp-47.d.ts +5 -0
  55. package/lib/rfc/is-bcp-47.js +14 -0
  56. package/lib/rfc/is-bcp-47.spec.d.ts +1 -0
  57. package/lib/rfc/is-bcp-47.spec.js +19 -0
  58. package/lib/token/index.d.ts +2 -0
  59. package/lib/token/index.js +7 -0
  60. package/lib/token/token-collection.d.ts +62 -0
  61. package/lib/token/token-collection.js +383 -0
  62. package/lib/token/token-collection.spec.d.ts +1 -0
  63. package/lib/token/token-collection.spec.js +144 -0
  64. package/lib/token/token.d.ts +55 -0
  65. package/lib/token/token.js +128 -0
  66. package/lib/types.d.ts +78 -0
  67. package/lib/types.js +2 -0
  68. package/lib/types.schema.d.ts +54 -0
  69. package/lib/types.schema.js +8 -0
  70. package/lib/w3c/check-serialized-permissions-policy.d.ts +15 -0
  71. package/lib/w3c/check-serialized-permissions-policy.js +251 -0
  72. package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +1 -0
  73. package/lib/w3c/check-serialized-permissions-policy.spec.js +26 -0
  74. package/lib/whatwg/check-autocomplete.d.ts +5 -0
  75. package/lib/whatwg/check-autocomplete.js +282 -0
  76. package/lib/whatwg/check-autocomplete.spec.d.ts +1 -0
  77. package/lib/whatwg/check-autocomplete.spec.js +370 -0
  78. package/lib/whatwg/check-datetime/date-string.d.ts +5 -0
  79. package/lib/whatwg/check-datetime/date-string.js +31 -0
  80. package/lib/whatwg/check-datetime/datetime-tokens.d.ts +3 -0
  81. package/lib/whatwg/check-datetime/datetime-tokens.js +509 -0
  82. package/lib/whatwg/check-datetime/duration-string.d.ts +6 -0
  83. package/lib/whatwg/check-datetime/duration-string.js +322 -0
  84. package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +5 -0
  85. package/lib/whatwg/check-datetime/global-date-and-time-string.js +66 -0
  86. package/lib/whatwg/check-datetime/index.d.ts +5 -0
  87. package/lib/whatwg/check-datetime/index.js +35 -0
  88. package/lib/whatwg/check-datetime/index.spec.d.ts +1 -0
  89. package/lib/whatwg/check-datetime/index.spec.js +318 -0
  90. package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +9 -0
  91. package/lib/whatwg/check-datetime/local-date-and-time-string.js +116 -0
  92. package/lib/whatwg/check-datetime/month-string.d.ts +5 -0
  93. package/lib/whatwg/check-datetime/month-string.js +20 -0
  94. package/lib/whatwg/check-datetime/time-string.d.ts +5 -0
  95. package/lib/whatwg/check-datetime/time-string.js +35 -0
  96. package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +7 -0
  97. package/lib/whatwg/check-datetime/time-zone-offset-string.js +75 -0
  98. package/lib/whatwg/check-datetime/week-string.d.ts +5 -0
  99. package/lib/whatwg/check-datetime/week-string.js +29 -0
  100. package/lib/whatwg/check-datetime/year-string.d.ts +5 -0
  101. package/lib/whatwg/check-datetime/year-string.js +20 -0
  102. package/lib/whatwg/check-datetime/yearless-date-string.d.ts +5 -0
  103. package/lib/whatwg/check-datetime/yearless-date-string.js +27 -0
  104. package/lib/whatwg/check-mime-type.d.ts +11 -0
  105. package/lib/whatwg/check-mime-type.js +42 -0
  106. package/lib/whatwg/check-mime-type.spec.d.ts +1 -0
  107. package/lib/whatwg/check-mime-type.spec.js +58 -0
  108. package/lib/whatwg/is-abs-url.d.ts +15 -0
  109. package/lib/whatwg/is-abs-url.js +34 -0
  110. package/lib/whatwg/is-abs-url.spec.d.ts +1 -0
  111. package/lib/whatwg/is-abs-url.spec.js +8 -0
  112. package/lib/whatwg/is-browser-context-name.d.ts +11 -0
  113. package/lib/whatwg/is-browser-context-name.js +18 -0
  114. package/lib/whatwg/is-custom-element-name.d.ts +22 -0
  115. package/lib/whatwg/is-custom-element-name.js +77 -0
  116. package/lib/whatwg/is-itemprop-name.d.ts +12 -0
  117. package/lib/whatwg/is-itemprop-name.js +19 -0
  118. package/package.json +33 -0
  119. package/src/check-multi-types.ts +35 -0
  120. package/src/check.spec.ts +62 -0
  121. package/src/check.ts +48 -0
  122. package/src/css-syntax.spec.ts +167 -0
  123. package/src/css-syntax.ts +177 -0
  124. package/src/debug.ts +3 -0
  125. package/src/defs.ts +810 -0
  126. package/src/enum.ts +25 -0
  127. package/src/get-candicate.ts +24 -0
  128. package/src/index.ts +3 -0
  129. package/src/keyword-type.ts +52 -0
  130. package/src/list.spec.ts +133 -0
  131. package/src/list.ts +34 -0
  132. package/src/match-result.ts +49 -0
  133. package/src/number.ts +46 -0
  134. package/src/primitive/index.spec.ts +65 -0
  135. package/src/primitive/index.ts +7 -0
  136. package/src/primitive/is-float.ts +8 -0
  137. package/src/primitive/is-int.ts +8 -0
  138. package/src/primitive/is-non-zero-uint.ts +8 -0
  139. package/src/primitive/is-quantity.ts +38 -0
  140. package/src/primitive/is-uint.ts +15 -0
  141. package/src/primitive/range.ts +14 -0
  142. package/src/primitive/split-unit.ts +20 -0
  143. package/src/rfc/is-bcp-47.spec.ts +23 -0
  144. package/src/rfc/is-bcp-47.ts +13 -0
  145. package/src/token/index.ts +2 -0
  146. package/src/token/token-collection.spec.ts +147 -0
  147. package/src/token/token-collection.ts +444 -0
  148. package/src/token/token.ts +144 -0
  149. package/src/types.schema.ts +1059 -0
  150. package/src/types.ts +118 -0
  151. package/src/w3c/check-serialized-permissions-policy.spec.ts +27 -0
  152. package/src/w3c/check-serialized-permissions-policy.ts +287 -0
  153. package/src/whatwg/check-autocomplete.spec.ts +378 -0
  154. package/src/whatwg/check-autocomplete.ts +324 -0
  155. package/src/whatwg/check-datetime/date-string.ts +44 -0
  156. package/src/whatwg/check-datetime/datetime-tokens.ts +600 -0
  157. package/src/whatwg/check-datetime/duration-string.ts +399 -0
  158. package/src/whatwg/check-datetime/global-date-and-time-string.ts +96 -0
  159. package/src/whatwg/check-datetime/index.spec.ts +333 -0
  160. package/src/whatwg/check-datetime/index.ts +36 -0
  161. package/src/whatwg/check-datetime/local-date-and-time-string.ts +163 -0
  162. package/src/whatwg/check-datetime/month-string.ts +31 -0
  163. package/src/whatwg/check-datetime/time-string.ts +49 -0
  164. package/src/whatwg/check-datetime/time-zone-offset-string.ts +93 -0
  165. package/src/whatwg/check-datetime/week-string.ts +41 -0
  166. package/src/whatwg/check-datetime/year-string.ts +26 -0
  167. package/src/whatwg/check-datetime/yearless-date-string.ts +38 -0
  168. package/src/whatwg/check-mime-type.spec.ts +59 -0
  169. package/src/whatwg/check-mime-type.ts +46 -0
  170. package/src/whatwg/is-abs-url.spec.ts +8 -0
  171. package/src/whatwg/is-abs-url.ts +31 -0
  172. package/src/whatwg/is-browser-context-name.ts +16 -0
  173. package/src/whatwg/is-custom-element-name.ts +76 -0
  174. package/src/whatwg/is-itemprop-name.ts +17 -0
  175. package/tsconfig.test.json +3 -0
  176. package/tsconfig.tsbuildinfo +1 -0
  177. package/types.schema.json +1092 -0
@@ -0,0 +1,147 @@
1
+ import { TokenCollection } from './token-collection';
2
+
3
+ import { Token } from '.';
4
+
5
+ test('parse', () => {
6
+ expect(new TokenCollection(' a b c ').toJSON()).toStrictEqual([
7
+ {
8
+ offset: 0,
9
+ type: 13,
10
+ value: ' ',
11
+ },
12
+ {
13
+ offset: 1,
14
+ type: 1,
15
+ value: 'a',
16
+ },
17
+ {
18
+ offset: 2,
19
+ type: 13,
20
+ value: ' ',
21
+ },
22
+ {
23
+ offset: 4,
24
+ type: 1,
25
+ value: 'b',
26
+ },
27
+ {
28
+ offset: 5,
29
+ type: 13,
30
+ value: ' ',
31
+ },
32
+ {
33
+ offset: 8,
34
+ type: 1,
35
+ value: 'c',
36
+ },
37
+ {
38
+ offset: 9,
39
+ type: 13,
40
+ value: ' ',
41
+ },
42
+ ]);
43
+ });
44
+
45
+ test('parse camma separator', () => {
46
+ expect(new TokenCollection('a,, ,b,cde', { separator: 'comma' }).toJSON()).toStrictEqual([
47
+ {
48
+ offset: 0,
49
+ type: 1,
50
+ value: 'a',
51
+ },
52
+ {
53
+ offset: 1,
54
+ type: 18,
55
+ value: ',',
56
+ },
57
+ {
58
+ offset: 2,
59
+ type: 18,
60
+ value: ',',
61
+ },
62
+ {
63
+ offset: 3,
64
+ type: 13,
65
+ value: ' ',
66
+ },
67
+ {
68
+ offset: 4,
69
+ type: 18,
70
+ value: ',',
71
+ },
72
+ {
73
+ offset: 5,
74
+ type: 1,
75
+ value: 'b',
76
+ },
77
+ {
78
+ offset: 6,
79
+ type: 18,
80
+ value: ',',
81
+ },
82
+ {
83
+ offset: 7,
84
+ type: 1,
85
+ value: 'cde',
86
+ },
87
+ ]);
88
+ });
89
+
90
+ test('A camma is an ident if the comma is not a separator', () => {
91
+ expect(new TokenCollection('a ,cde , f').toJSON()).toStrictEqual([
92
+ {
93
+ offset: 0,
94
+ type: 1,
95
+ value: 'a',
96
+ },
97
+ {
98
+ offset: 1,
99
+ type: 13,
100
+ value: ' ',
101
+ },
102
+ {
103
+ offset: 2,
104
+ type: 1,
105
+ value: ',cde',
106
+ },
107
+ {
108
+ offset: 6,
109
+ type: 13,
110
+ value: ' ',
111
+ },
112
+ {
113
+ offset: 7,
114
+ type: 1,
115
+ value: ',',
116
+ },
117
+ {
118
+ offset: 8,
119
+ type: 13,
120
+ value: ' ',
121
+ },
122
+ {
123
+ offset: 9,
124
+ type: 1,
125
+ value: 'f',
126
+ },
127
+ ]);
128
+ });
129
+
130
+ test('getConsecutiveToken', () => {
131
+ expect(new TokenCollection('a,b,c', { separator: 'comma' }).getConsecutiveToken(Token.Comma)).toBeFalsy();
132
+ expect(new TokenCollection('a,,b', { separator: 'comma' }).getConsecutiveToken(Token.Comma)).toBeTruthy();
133
+ expect(new TokenCollection('a,,b', { separator: 'comma' }).getConsecutiveToken(Token.Comma)?.offset).toBe(2);
134
+ });
135
+
136
+ test('fromPatterns', () => {
137
+ const patterns = [/\+|-/, /[0-9]{2}/, /:?/, /[0-9]{2}/];
138
+ expect(TokenCollection.fromPatterns('+00:00', patterns).map(t => t.value)).toStrictEqual(['+', '00', ':', '00']);
139
+ expect(TokenCollection.fromPatterns('+0000', patterns).map(t => t.value)).toStrictEqual(['+', '00', '', '00']);
140
+ expect(TokenCollection.fromPatterns('+00/00', patterns).map(t => t.value)).toStrictEqual([
141
+ '+',
142
+ '00',
143
+ '',
144
+ '',
145
+ '/00',
146
+ ]);
147
+ });
@@ -0,0 +1,444 @@
1
+ import type { UnmatchedResult } from '..';
2
+ import type { Expect, Result, List } from '../types';
3
+
4
+ import { matched, unmatched } from '../match-result';
5
+
6
+ import { Token } from './token';
7
+
8
+ type TokenCollectionOptions = Partial<
9
+ Omit<List, 'token'> & {
10
+ speificSeparator: string | string[];
11
+ }
12
+ >;
13
+
14
+ export type TokenEachCheck = (head: Token | null, tail: TokenCollection) => Result | void;
15
+
16
+ export class TokenCollection extends Array<Token> {
17
+ private static _new(tokens: Token[], old?: TokenCollection) {
18
+ const newCollection = new TokenCollection('', old);
19
+ newCollection.push(...tokens);
20
+ return newCollection;
21
+ }
22
+
23
+ static fromPatterns(
24
+ value: Token | string,
25
+ patterns: RegExp[],
26
+ typeOptions?: Omit<TokenCollectionOptions, 'speificSeparator'> & { repeat?: boolean },
27
+ ) {
28
+ const origin = typeof value === 'string' ? value : value.origin;
29
+ let strings = typeof value === 'string' ? value : value.value;
30
+ let cumulativeOffset = typeof value === 'string' ? 0 : value.offset;
31
+ const tokens: Token[] = [];
32
+
33
+ function addToken(tokenValue: string) {
34
+ const token = new Token(tokenValue, cumulativeOffset, origin);
35
+ tokens.push(token);
36
+
37
+ strings = strings.slice(tokenValue.length);
38
+ cumulativeOffset += tokenValue.length;
39
+
40
+ return token;
41
+ }
42
+
43
+ let isBreaked = false;
44
+ do {
45
+ isBreaked = false;
46
+ for (const pattern of patterns) {
47
+ const res = pattern.exec(strings);
48
+ let value: string;
49
+
50
+ if (!res) {
51
+ isBreaked = true;
52
+ value = '';
53
+ } else {
54
+ if (res.index !== 0) {
55
+ value = strings.slice(res.index + res[0].length);
56
+ } else {
57
+ value = res[0] || '';
58
+ }
59
+ }
60
+
61
+ const token = addToken(value);
62
+
63
+ // @ts-ignore
64
+ token._ = pattern;
65
+ }
66
+ if (isBreaked) {
67
+ addToken(strings);
68
+ }
69
+ } while (typeOptions?.repeat && strings);
70
+
71
+ if (strings) {
72
+ addToken(strings);
73
+ }
74
+
75
+ return TokenCollection._new(tokens, new TokenCollection('', typeOptions));
76
+ }
77
+
78
+ static get [Symbol.species]() {
79
+ return Array;
80
+ }
81
+
82
+ readonly disallowToSurroundBySpaces: NonNullable<List['disallowToSurroundBySpaces']>;
83
+ readonly allowEmpty: NonNullable<List['allowEmpty']>;
84
+ readonly ordered: NonNullable<List['ordered']>;
85
+ readonly unique: NonNullable<List['unique']>;
86
+ readonly caseInsensitive: NonNullable<List['caseInsensitive']>;
87
+ readonly number: List['number'];
88
+ readonly separator: NonNullable<List['separator']>;
89
+
90
+ constructor(value?: string, typeOptions?: TokenCollectionOptions);
91
+ constructor(value?: number); // for map method etc.
92
+ constructor(value?: string | number, typeOptions?: TokenCollectionOptions) {
93
+ super();
94
+
95
+ this.disallowToSurroundBySpaces = typeOptions?.disallowToSurroundBySpaces || false;
96
+ this.allowEmpty = typeOptions?.allowEmpty || true;
97
+ this.ordered = typeOptions?.ordered || false;
98
+ this.unique = typeOptions?.unique || false;
99
+ this.caseInsensitive = typeOptions?.caseInsensitive || true;
100
+ this.number = typeOptions?.number;
101
+ this.separator = typeOptions?.separator || 'space';
102
+
103
+ if (typeof value === 'number') {
104
+ this.length = value;
105
+ return;
106
+ }
107
+
108
+ if (!value) {
109
+ return;
110
+ }
111
+
112
+ const separators: string[] = [];
113
+ if (this.separator === 'comma') {
114
+ separators.push(',');
115
+ }
116
+
117
+ if (typeOptions?.speificSeparator) {
118
+ if (Array.isArray(typeOptions.speificSeparator)) {
119
+ separators.push(...typeOptions.speificSeparator);
120
+ } else {
121
+ separators.push(typeOptions.speificSeparator);
122
+ }
123
+ }
124
+
125
+ const chars = value.split('');
126
+ const values: string[] = [];
127
+ let char: string | undefined;
128
+ while ((char = chars.shift())) {
129
+ const last = values.pop();
130
+ if (!last) {
131
+ values.push(char);
132
+ continue;
133
+ }
134
+ const lastChar = last[0];
135
+ if (
136
+ !separators.includes(char) &&
137
+ !separators.includes(last[0]) &&
138
+ ((Token.whitespace.includes(lastChar) && Token.whitespace.includes(char)) ||
139
+ (!Token.whitespace.includes(lastChar) && !Token.whitespace.includes(char)))
140
+ ) {
141
+ values.push(last + char);
142
+ } else {
143
+ values.push(last);
144
+ values.push(char);
145
+ }
146
+ }
147
+
148
+ let offset = 0;
149
+ values.forEach(v => {
150
+ const token = new Token(v, offset, value, separators);
151
+ this.push(token);
152
+ offset += v.length;
153
+ });
154
+ }
155
+
156
+ get value() {
157
+ const value = this.map(t => t.value).join('');
158
+ return value;
159
+ }
160
+
161
+ filter(callback: (value: Token, index: number, array: Token[]) => boolean) {
162
+ return TokenCollection._new(super.filter(callback), this);
163
+ }
164
+
165
+ headAndTail(): { head: Token | null; tail: TokenCollection } {
166
+ const copy = this.slice();
167
+ const head = copy.shift();
168
+ if (!head) {
169
+ return { head: head || null, tail: TokenCollection._new([], this) };
170
+ }
171
+ const tail = TokenCollection._new(copy, this);
172
+ return { head, tail };
173
+ }
174
+
175
+ getIdentTokens() {
176
+ return this.filter(token => token.type === Token.Ident);
177
+ }
178
+
179
+ compareTokens(callback: (prev: Token, current: Token) => Token | null | void) {
180
+ const _tokens = this.slice();
181
+ let prev = _tokens.shift();
182
+ while (prev) {
183
+ const current = _tokens.shift();
184
+ if (!current) {
185
+ return;
186
+ }
187
+ const result = callback(prev, current);
188
+ if (result) {
189
+ return result;
190
+ }
191
+ prev = current;
192
+ }
193
+ return null;
194
+ }
195
+
196
+ /**
197
+ *
198
+ * @param value The token value or the token type or its list
199
+ */
200
+ has(value: string | RegExp | number | (string | RegExp | number)[]) {
201
+ return this.some(t => t.match(value));
202
+ }
203
+
204
+ /**
205
+ *
206
+ * @param value The token value or the token type or its list
207
+ */
208
+ search(value: string | RegExp | number | (string | RegExp | number)[]) {
209
+ for (const token of this) {
210
+ if (token.includes(value)) {
211
+ return token;
212
+ }
213
+ }
214
+ return null;
215
+ }
216
+
217
+ check(options: { expects?: Expect[]; ref?: string; cache?: boolean } = {}) {
218
+ const { expects, ref } = options;
219
+
220
+ if (this.disallowToSurroundBySpaces) {
221
+ for (const token of this) {
222
+ if (token.type === 13) {
223
+ return token.unmatched({
224
+ reason: 'unexpected-space',
225
+ ref,
226
+ expects,
227
+ });
228
+ }
229
+ }
230
+ }
231
+
232
+ if (this.separator === 'comma') {
233
+ const tokensWithoutWP = this.filter(token => token.type !== Token.WhiteSpace);
234
+ const consecutiveComma = tokensWithoutWP.getConsecutiveToken(Token.Comma);
235
+ if (consecutiveComma) {
236
+ return consecutiveComma.unmatched({
237
+ reason: 'unexpected-comma',
238
+ ref,
239
+ expects,
240
+ });
241
+ }
242
+
243
+ if (tokensWithoutWP[0] && tokensWithoutWP[0].type === Token.Comma) {
244
+ return tokensWithoutWP[0].unmatched({
245
+ reason: 'unexpected-comma',
246
+ ref,
247
+ expects,
248
+ });
249
+ }
250
+
251
+ const takeTurnsError = tokensWithoutWP.takeTurns([Token.Ident, Token.Comma], Token.Ident);
252
+ if (takeTurnsError) {
253
+ if (takeTurnsError.unexpectedLastToken) {
254
+ return takeTurnsError.token.unmatched({
255
+ reason: 'extra-token',
256
+ ref,
257
+ expects,
258
+ });
259
+ } else if (takeTurnsError.token.type === takeTurnsError.expectedTokenNumber) {
260
+ // Consecutive cammas
261
+ return takeTurnsError.token.unmatched({
262
+ reason: 'unexpected-comma',
263
+ ref,
264
+ expects,
265
+ });
266
+ }
267
+ return takeTurnsError.token.unmatched({
268
+ reason: 'missing-comma',
269
+ ref,
270
+ expects,
271
+ candicate: `,${takeTurnsError.token.value}`,
272
+ });
273
+ }
274
+ }
275
+
276
+ const identTokens = this.getIdentTokens();
277
+
278
+ const allowEmpty = this.allowEmpty ?? true;
279
+ if (!allowEmpty && identTokens.length === 0) {
280
+ return unmatched(this.value, 'empty-token', {
281
+ ref,
282
+ expects,
283
+ });
284
+ }
285
+
286
+ if (this.unique) {
287
+ const duplicated = identTokens.getDuplicated();
288
+ if (duplicated) {
289
+ return duplicated.unmatched({
290
+ partName: 'the content of the list',
291
+ reason: 'duplicated',
292
+ ref,
293
+ expects,
294
+ });
295
+ }
296
+ }
297
+
298
+ return matched();
299
+ }
300
+
301
+ getConsecutiveToken(tokenType: number) {
302
+ const resultToken = this.compareTokens((prev, current) => {
303
+ if (prev.type === tokenType && current.type === tokenType) {
304
+ return current;
305
+ }
306
+ });
307
+ return resultToken || null;
308
+ }
309
+
310
+ takeTurns(tokenNumbers: ReadonlyArray<number>, lastTokenNumber?: number) {
311
+ const tokens = this.slice();
312
+ for (let i = 0; i < tokens.length; i++) {
313
+ const token = tokens[i];
314
+ const expectedTokenNumber = tokenNumbers[i % tokenNumbers.length];
315
+ if (token.type !== expectedTokenNumber) {
316
+ return {
317
+ unexpectedLastToken: false,
318
+ expectedTokenNumber,
319
+ token,
320
+ };
321
+ }
322
+ if (lastTokenNumber != null && i === tokens.length - 1 && token.type !== lastTokenNumber) {
323
+ return {
324
+ unexpectedLastToken: true,
325
+ expectedTokenNumber,
326
+ token,
327
+ };
328
+ }
329
+ }
330
+ return null;
331
+ }
332
+
333
+ eachCheck(...callbacks: TokenEachCheck[]): Result {
334
+ let headAndTail = this.headAndTail();
335
+ let head = headAndTail.head;
336
+ let tail = headAndTail.tail;
337
+ let prev: Token | null = null;
338
+ let cumulativeOffset = 0;
339
+ let passCount = 0;
340
+ let firstUnmatched: UnmatchedResult | null = null;
341
+ let wait = 20;
342
+ for (const callback of callbacks) {
343
+ wait--;
344
+ const result = callback.call(this, head, tail);
345
+ if (result && !result.matched) {
346
+ passCount += result.passCount || 0;
347
+ if (prev && result.offset === 0 && result.length === 0) {
348
+ const { offset, line, column } = Token.shiftLocation(prev, cumulativeOffset);
349
+ firstUnmatched = firstUnmatched || {
350
+ ...result,
351
+ offset,
352
+ line,
353
+ column,
354
+ };
355
+ }
356
+ if (!prev && this.value.length === 0) {
357
+ firstUnmatched = firstUnmatched || {
358
+ ...result,
359
+ reason: 'empty-token',
360
+ expects: undefined,
361
+ };
362
+ }
363
+ switch (result.reason) {
364
+ case 'extra-token': {
365
+ passCount += 1;
366
+ break;
367
+ }
368
+ default: {
369
+ if (typeof result.reason !== 'string') {
370
+ passCount += 2 * wait;
371
+ }
372
+ }
373
+ }
374
+ firstUnmatched = firstUnmatched || result;
375
+ } else if (result && !firstUnmatched && result.matched) {
376
+ return result;
377
+ } else {
378
+ if (head?.value && !head.match(/^\s*$/)) {
379
+ passCount += 4 * wait;
380
+ }
381
+ }
382
+ cumulativeOffset = head?.length || 0;
383
+ headAndTail = tail.headAndTail();
384
+ prev = head;
385
+ head = headAndTail.head;
386
+ tail = headAndTail.tail;
387
+ }
388
+
389
+ if (firstUnmatched) {
390
+ const res = {
391
+ ...firstUnmatched,
392
+ passCount,
393
+ };
394
+ return res;
395
+ }
396
+
397
+ return matched();
398
+ }
399
+
400
+ getDuplicated() {
401
+ const aList = this.slice();
402
+ const bList = this.slice();
403
+ for (const aToken of aList) {
404
+ for (const bToken of bList) {
405
+ if (aToken.offset === bToken.offset) {
406
+ continue;
407
+ }
408
+ let a = aToken.value;
409
+ let b = bToken.value;
410
+ if (this.caseInsensitive) {
411
+ a = a.toLowerCase();
412
+ b = b.toLowerCase();
413
+ }
414
+ if (a === b) {
415
+ return bToken;
416
+ }
417
+ }
418
+ }
419
+ return null;
420
+ }
421
+
422
+ divide(position: number) {
423
+ const _a = this.slice(0, position);
424
+ const _b = this.slice(position);
425
+ const a = TokenCollection._new(_a, this);
426
+ const b = TokenCollection._new(_b, this);
427
+ return [a, b] as const;
428
+ }
429
+
430
+ chunk(split: number) {
431
+ const chunks: TokenCollection[] = [];
432
+ const tokens = this.slice();
433
+ while (tokens.length) {
434
+ const chunkTokens = tokens.splice(0, split);
435
+ const chunk = TokenCollection._new(chunkTokens, this);
436
+ chunks.push(chunk);
437
+ }
438
+ return chunks;
439
+ }
440
+
441
+ toJSON() {
442
+ return this.map(t => t.toJSON());
443
+ }
444
+ }