extra-parser 0.6.4 → 0.6.6
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/README.md +9 -9
- package/lib/match-repetitions.d.ts +2 -3
- package/lib/match-repetitions.js +12 -5
- package/lib/match-repetitions.js.map +1 -1
- package/lib/types.d.ts +2 -2
- package/package.json +1 -2
- package/src/match-repetitions.ts +22 -9
- package/src/types.ts +8 -8
package/README.md
CHANGED
|
@@ -54,12 +54,12 @@ type MapSequenceToPatterns<
|
|
|
54
54
|
, Node extends INode = INode
|
|
55
55
|
> = {
|
|
56
56
|
[Index in keyof Sequence]:
|
|
57
|
-
[Sequence[Index]] extends [infer
|
|
57
|
+
[Sequence[Index]] extends [infer TokenOrNode]
|
|
58
58
|
? (
|
|
59
|
-
|
|
59
|
+
TokenOrNode extends Token
|
|
60
60
|
? string
|
|
61
|
-
:
|
|
62
|
-
? INodePattern<Token,
|
|
61
|
+
: TokenOrNode extends Node
|
|
62
|
+
? INodePattern<Token, TokenOrNode>
|
|
63
63
|
: never
|
|
64
64
|
)
|
|
65
65
|
: never
|
|
@@ -71,12 +71,12 @@ type MapSequenceToMatches<
|
|
|
71
71
|
, Node extends INode = INode
|
|
72
72
|
> = {
|
|
73
73
|
[Index in keyof Sequence]:
|
|
74
|
-
[Sequence[Index]] extends [infer
|
|
74
|
+
[Sequence[Index]] extends [infer TokenOrNode]
|
|
75
75
|
? (
|
|
76
|
-
|
|
76
|
+
TokenOrNode extends IToken
|
|
77
77
|
? Token
|
|
78
|
-
:
|
|
79
|
-
? INodePatternMatch<
|
|
78
|
+
: TokenOrNode extends INode
|
|
79
|
+
? INodePatternMatch<TokenOrNode>
|
|
80
80
|
: never
|
|
81
81
|
)
|
|
82
82
|
: never
|
|
@@ -154,7 +154,7 @@ function matchRepetitions<
|
|
|
154
154
|
minimumRepetitions?: number = 1
|
|
155
155
|
maximumRepetitions?: number = Infinity
|
|
156
156
|
}
|
|
157
|
-
): Promise<
|
|
157
|
+
): Promise<Array<Token | INodePatternMatch<Node>> | Falsy>
|
|
158
158
|
```
|
|
159
159
|
|
|
160
160
|
### createTokenPatternFromRegExp
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Falsy } from '@blackglory/prelude';
|
|
2
|
-
import {
|
|
3
|
-
import { IToken, INode, MapSequenceToPatterns, MapSequenceToMatches } from './types';
|
|
2
|
+
import { IToken, INode, MapSequenceToPatterns, INodePatternMatch } from './types';
|
|
4
3
|
export declare function matchRepetitions<Sequence extends ReadonlyArray<Token | Node>, Token extends IToken = IToken, Node extends INode = INode>(patterns: MapSequenceToPatterns<Sequence, Token, Node>, tokens: ReadonlyArray<Token>, { minimumRepetitions, maximumRepetitions }?: {
|
|
5
4
|
minimumRepetitions?: number;
|
|
6
5
|
maximumRepetitions?: number;
|
|
7
|
-
}): Promise<
|
|
6
|
+
}): Promise<Array<Token | INodePatternMatch<Node>> | Falsy>;
|
package/lib/match-repetitions.js
CHANGED
|
@@ -13,8 +13,7 @@ async function matchRepetitions(patterns, tokens, { minimumRepetitions = 1, maxi
|
|
|
13
13
|
for (let i = 0; i < minimumRepetitions; i++) {
|
|
14
14
|
const matches = await (0, match_sequence_1.matchSequence)(patterns, mutableTokens);
|
|
15
15
|
if ((0, prelude_1.isntFalsy)(matches)) {
|
|
16
|
-
|
|
17
|
-
mutableTokens.splice(0, matches.length);
|
|
16
|
+
handleMatches(matches);
|
|
18
17
|
}
|
|
19
18
|
else {
|
|
20
19
|
return;
|
|
@@ -23,14 +22,22 @@ async function matchRepetitions(patterns, tokens, { minimumRepetitions = 1, maxi
|
|
|
23
22
|
for (let i = minimumRepetitions; i < maximumRepetitions; i++) {
|
|
24
23
|
const matches = await (0, match_sequence_1.matchSequence)(patterns, mutableTokens);
|
|
25
24
|
if ((0, prelude_1.isntFalsy)(matches)) {
|
|
26
|
-
|
|
27
|
-
mutableTokens.splice(0, matches.length);
|
|
25
|
+
handleMatches(matches);
|
|
28
26
|
}
|
|
29
27
|
else {
|
|
30
28
|
break;
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
return results
|
|
31
|
+
return results;
|
|
32
|
+
function handleMatches(matches) {
|
|
33
|
+
results.push(...matches);
|
|
34
|
+
mutableTokens.splice(0, getConsumed(matches));
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
exports.matchRepetitions = matchRepetitions;
|
|
38
|
+
function getConsumed(matches) {
|
|
39
|
+
return matches
|
|
40
|
+
.map(match => 'consumed' in match ? match.consumed : 1)
|
|
41
|
+
.reduce((acc, cur) => acc + cur);
|
|
42
|
+
}
|
|
36
43
|
//# sourceMappingURL=match-repetitions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match-repetitions.js","sourceRoot":"","sources":["../src/match-repetitions.ts"],"names":[],"mappings":";;;AAAA,iDAAuE;
|
|
1
|
+
{"version":3,"file":"match-repetitions.js","sourceRoot":"","sources":["../src/match-repetitions.ts"],"names":[],"mappings":";;;AAAA,iDAAuE;AAEvE,qDAAgD;AAEzC,KAAK,UAAU,gBAAgB,CAKpC,QAAsD,EACtD,MAA4B,EAC5B,EACE,kBAAkB,GAAG,CAAC,EACtB,kBAAkB,GAAG,QAAQ,KAI3B,EAAE;IAEN,IAAA,gBAAM,EAAC,MAAM,CAAC,SAAS,CAAC,kBAAkB,CAAC,EAAE,2CAA2C,CAAC,CAAA;IACzF,IAAA,gBAAM,EACJ,kBAAkB,IAAI,CAAC,EACvB,4DAA4D,CAC7D,CAAA;IACD,IAAA,gBAAM,EACJ,MAAM,CAAC,SAAS,CAAC,kBAAkB,CAAC,IAAI,kBAAkB,KAAK,QAAQ,EACvE,0DAA0D,CAC3D,CAAA;IACD,IAAA,gBAAM,EACJ,kBAAkB,IAAI,kBAAkB,EACxC,kFAAkF,CACnF,CAAA;IAED,MAAM,OAAO,GAA2C,EAAE,CAAA;IAC1D,MAAM,aAAa,GAAG,IAAA,iBAAO,EAAC,MAAM,CAAC,CAAA;IAErC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,EAAE,CAAC,EAAE,EAAE;QAC3C,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAa,EAAC,QAAQ,EAAE,aAAa,CAAC,CAAA;QAC5D,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,aAAa,CAAC,OAAO,CAAC,CAAA;SACvB;aAAM;YACL,OAAM;SACP;KACF;IAED,KAAK,IAAI,CAAC,GAAG,kBAAkB,EAAE,CAAC,GAAG,kBAAkB,EAAE,CAAC,EAAE,EAAE;QAC5D,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAa,EAAC,QAAQ,EAAE,aAAa,CAAC,CAAA;QAC5D,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,aAAa,CAAC,OAAO,CAAC,CAAA;SACvB;aAAM;YACL,MAAK;SACN;KACF;IAED,OAAO,OAAO,CAAA;IAEd,SAAS,aAAa,CACpB,OAAoD;QAEpD,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAA;QACxB,aAAa,CAAC,MAAM,CAClB,CAAC,EACD,WAAW,CAAC,OAAiD,CAAC,CAC/D,CAAA;IACH,CAAC;AACH,CAAC;AA7DD,4CA6DC;AAED,SAAS,WAAW,CAAC,OAAiD;IACpE,OAAO,OAAO;SACX,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,UAAU,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;SACtD,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,GAAG,CAAC,CAAA;AACpC,CAAC"}
|
package/lib/types.d.ts
CHANGED
|
@@ -23,10 +23,10 @@ export interface INodePattern<Token extends IToken = IToken, Node extends INode
|
|
|
23
23
|
export declare type MapSequenceToPatterns<Sequence extends ReadonlyArray<Token | Node>, Token extends IToken = IToken, Node extends INode = INode> = {
|
|
24
24
|
[Index in keyof Sequence]: [
|
|
25
25
|
Sequence[Index]
|
|
26
|
-
] extends [infer
|
|
26
|
+
] extends [infer TokenOrNode] ? (TokenOrNode extends Token ? string : TokenOrNode extends Node ? INodePattern<Token, TokenOrNode> : never) : never;
|
|
27
27
|
};
|
|
28
28
|
export declare type MapSequenceToMatches<Sequence extends ReadonlyArray<Token | Node>, Token extends IToken = IToken, Node extends INode = INode> = {
|
|
29
29
|
[Index in keyof Sequence]: [
|
|
30
30
|
Sequence[Index]
|
|
31
|
-
] extends [infer
|
|
31
|
+
] extends [infer TokenOrNode] ? (TokenOrNode extends IToken ? Token : TokenOrNode extends INode ? INodePatternMatch<TokenOrNode> : never) : never;
|
|
32
32
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-parser",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"description": "A functional parser toolkit",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"files": [
|
|
@@ -51,7 +51,6 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@blackglory/prelude": "^0.1.8",
|
|
54
|
-
"hotypes": "^0.5.1",
|
|
55
54
|
"iterable-operator": "^2.3.0"
|
|
56
55
|
}
|
|
57
56
|
}
|
package/src/match-repetitions.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { assert, Falsy, isntFalsy, toArray } from '@blackglory/prelude'
|
|
2
|
-
import {
|
|
3
|
-
import { IToken, INode, MapSequenceToPatterns, MapSequenceToMatches } from './types'
|
|
2
|
+
import { IToken, INode, MapSequenceToPatterns, MapSequenceToMatches, INodePatternMatch } from './types'
|
|
4
3
|
import { matchSequence } from './match-sequence'
|
|
5
4
|
|
|
6
5
|
export async function matchRepetitions<
|
|
@@ -17,7 +16,7 @@ export async function matchRepetitions<
|
|
|
17
16
|
minimumRepetitions?: number
|
|
18
17
|
maximumRepetitions?: number
|
|
19
18
|
} = {}
|
|
20
|
-
): Promise<
|
|
19
|
+
): Promise<Array<Token | INodePatternMatch<Node>> | Falsy> {
|
|
21
20
|
assert(Number.isInteger(minimumRepetitions), 'The minimum repetiions must be an integer')
|
|
22
21
|
assert(
|
|
23
22
|
minimumRepetitions >= 0
|
|
@@ -32,14 +31,13 @@ export async function matchRepetitions<
|
|
|
32
31
|
, 'The maximum repetitions must be greater than or equal to the minimum repetitions'
|
|
33
32
|
)
|
|
34
33
|
|
|
35
|
-
const results: Array<
|
|
34
|
+
const results: Array<Token | INodePatternMatch<Node>> = []
|
|
36
35
|
const mutableTokens = toArray(tokens)
|
|
37
36
|
|
|
38
37
|
for (let i = 0; i < minimumRepetitions; i++) {
|
|
39
38
|
const matches = await matchSequence(patterns, mutableTokens)
|
|
40
39
|
if (isntFalsy(matches)) {
|
|
41
|
-
|
|
42
|
-
mutableTokens.splice(0, matches.length)
|
|
40
|
+
handleMatches(matches)
|
|
43
41
|
} else {
|
|
44
42
|
return
|
|
45
43
|
}
|
|
@@ -48,12 +46,27 @@ export async function matchRepetitions<
|
|
|
48
46
|
for (let i = minimumRepetitions; i < maximumRepetitions; i++) {
|
|
49
47
|
const matches = await matchSequence(patterns, mutableTokens)
|
|
50
48
|
if (isntFalsy(matches)) {
|
|
51
|
-
|
|
52
|
-
mutableTokens.splice(0, matches.length)
|
|
49
|
+
handleMatches(matches)
|
|
53
50
|
} else {
|
|
54
51
|
break
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
return results
|
|
55
|
+
return results
|
|
56
|
+
|
|
57
|
+
function handleMatches(
|
|
58
|
+
matches: MapSequenceToMatches<Sequence, Token, Node>
|
|
59
|
+
): void {
|
|
60
|
+
results.push(...matches)
|
|
61
|
+
mutableTokens.splice(
|
|
62
|
+
0
|
|
63
|
+
, getConsumed(matches as Array<Token | INodePatternMatch<Node>>)
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function getConsumed(matches: Array<IToken | INodePatternMatch<INode>>): number {
|
|
69
|
+
return matches
|
|
70
|
+
.map(match => 'consumed' in match ? match.consumed : 1)
|
|
71
|
+
.reduce((acc, cur) => acc + cur)
|
|
59
72
|
}
|
package/src/types.ts
CHANGED
|
@@ -36,12 +36,12 @@ export type MapSequenceToPatterns<
|
|
|
36
36
|
, Node extends INode = INode
|
|
37
37
|
> = {
|
|
38
38
|
[Index in keyof Sequence]:
|
|
39
|
-
[Sequence[Index]] extends [infer
|
|
39
|
+
[Sequence[Index]] extends [infer TokenOrNode]
|
|
40
40
|
? (
|
|
41
|
-
|
|
41
|
+
TokenOrNode extends Token
|
|
42
42
|
? string
|
|
43
|
-
:
|
|
44
|
-
? INodePattern<Token,
|
|
43
|
+
: TokenOrNode extends Node
|
|
44
|
+
? INodePattern<Token, TokenOrNode>
|
|
45
45
|
: never
|
|
46
46
|
)
|
|
47
47
|
: never
|
|
@@ -53,12 +53,12 @@ export type MapSequenceToMatches<
|
|
|
53
53
|
, Node extends INode = INode
|
|
54
54
|
> = {
|
|
55
55
|
[Index in keyof Sequence]:
|
|
56
|
-
[Sequence[Index]] extends [infer
|
|
56
|
+
[Sequence[Index]] extends [infer TokenOrNode]
|
|
57
57
|
? (
|
|
58
|
-
|
|
58
|
+
TokenOrNode extends IToken
|
|
59
59
|
? Token
|
|
60
|
-
:
|
|
61
|
-
? INodePatternMatch<
|
|
60
|
+
: TokenOrNode extends INode
|
|
61
|
+
? INodePatternMatch<TokenOrNode>
|
|
62
62
|
: never
|
|
63
63
|
)
|
|
64
64
|
: never
|