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