extra-parser 0.5.0 → 0.6.1

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 (41) hide show
  1. package/README.md +176 -13
  2. package/lib/consume-node.d.ts +3 -0
  3. package/lib/consume-node.js +13 -0
  4. package/lib/consume-node.js.map +1 -0
  5. package/lib/consume-token.d.ts +3 -0
  6. package/lib/consume-token.js +12 -0
  7. package/lib/consume-token.js.map +1 -0
  8. package/lib/create-binary-operator-expression-node-pattern.d.ts +12 -0
  9. package/lib/create-binary-operator-expression-node-pattern.js +27 -0
  10. package/lib/create-binary-operator-expression-node-pattern.js.map +1 -0
  11. package/lib/create-grouped-expression-node-pattern.d.ts +6 -0
  12. package/lib/create-grouped-expression-node-pattern.js +23 -0
  13. package/lib/create-grouped-expression-node-pattern.js.map +1 -0
  14. package/lib/create-token-pattern-from-regexp.js +2 -2
  15. package/lib/create-token-pattern-from-regexp.js.map +1 -1
  16. package/lib/create-unary-operator-expression-node-pattern.d.ts +10 -0
  17. package/lib/create-unary-operator-expression-node-pattern.js +25 -0
  18. package/lib/create-unary-operator-expression-node-pattern.js.map +1 -0
  19. package/lib/create-value-expression-node-pattern.d.ts +10 -0
  20. package/lib/create-value-expression-node-pattern.js +22 -0
  21. package/lib/create-value-expression-node-pattern.js.map +1 -0
  22. package/lib/index.d.ts +10 -1
  23. package/lib/index.js +11 -1
  24. package/lib/index.js.map +1 -1
  25. package/lib/match-any-of.d.ts +3 -0
  26. package/lib/match-any-of.js +14 -0
  27. package/lib/match-any-of.js.map +1 -0
  28. package/lib/match-repetitions.d.ts +7 -0
  29. package/lib/match-repetitions.js +36 -0
  30. package/lib/match-repetitions.js.map +1 -0
  31. package/lib/match-sequence.d.ts +6 -0
  32. package/lib/match-sequence.js +120 -0
  33. package/lib/match-sequence.js.map +1 -0
  34. package/lib/parse.d.ts +1 -1
  35. package/lib/parse.js +1 -1
  36. package/lib/parse.js.map +1 -1
  37. package/lib/tokenize.d.ts +1 -1
  38. package/lib/tokenize.js +1 -1
  39. package/lib/tokenize.js.map +1 -1
  40. package/lib/types.d.ts +10 -0
  41. package/package.json +4 -4
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # extra-parser
2
+ A functional parser toolkit.
3
+
2
4
  ## Install
3
5
  ```sh
4
6
  npm install --save extra-parser
@@ -6,6 +8,17 @@ npm install --save extra-parser
6
8
  yarn add extra-parser
7
9
  ```
8
10
 
11
+ ## FAQ
12
+ ### Why are functions asynchronous?
13
+ Some parsers make heavy use of recursion,
14
+ and most JavaScript engines do not support tail-call optimization,
15
+ which leads to the possibility of stack overflow in programs.
16
+
17
+ Asynchronous functions are an escape route:
18
+ developers can change recursive functions to asynchronous recursive functions
19
+ to get their programs out of stack overflow problems
20
+ without significantly reducing readability.
21
+
9
22
  ## API
10
23
  ```ts
11
24
  interface IToken {
@@ -34,24 +47,100 @@ interface ITokenPattern<Token extends IToken = IToken> {
34
47
  interface INodePattern<Token extends IToken = IToken, Node extends INode = INode> {
35
48
  (tokens: ReadonlyArray<Token>): Awaitable<INodePatternMatch<Node> | Falsy>
36
49
  }
50
+
51
+ type MapSequenceToPatterns<Sequence extends ReadonlyArray<IToken | INode>> = {
52
+ [Index in keyof Sequence]:
53
+ [Sequence[Index]] extends [infer Element]
54
+ ? (
55
+ Element extends IToken
56
+ ? string
57
+ : Element extends INode
58
+ ? INodePattern<IToken, Element>
59
+ : never
60
+ )
61
+ : never
62
+ }
63
+
64
+ type MapSequenceToMatches<Sequence extends ReadonlyArray<IToken | INode>> = {
65
+ [Index in keyof Sequence]:
66
+ [Sequence[Index]] extends [infer Element]
67
+ ? (
68
+ Element extends IToken
69
+ ? IToken
70
+ : Element extends INode
71
+ ? INodePatternMatch<Element>
72
+ : never
73
+ )
74
+ : never
75
+ }
37
76
  ```
38
77
 
39
78
  ### tokenize
40
79
  ```ts
41
80
  function tokenize<Token extends IToken = IToken>(
42
- text: string
43
- , patterns: Array<ITokenPattern<Token>>
81
+ patterns: Array<ITokenPattern<Token>>
82
+ , text: string
44
83
  ): AsyncIterableIterator<Token>
45
84
  ```
46
85
 
47
86
  ### parse
48
87
  ```ts
49
88
  function parse<Token extends IToken = IToken, Node extends INode = INode>(
50
- tokens: Token[]
51
- , patterns: Array<INodePattern<Token, Node>>
89
+ patterns: Array<INodePattern<Token, Node>>
90
+ , tokens: Token[]
52
91
  ): AsyncIterableIterator<Node>
53
92
  ```
54
93
 
94
+ ### consumeNode
95
+ ```ts
96
+ function consumeNode<
97
+ Token extends IToken = IToken
98
+ , Node extends INode = INode
99
+ >(
100
+ nodePattern: INodePattern<Token, Node>
101
+ , tokens: Token[]
102
+ ): Promise<INodePatternMatch<Node> | Falsy>
103
+ ```
104
+
105
+ ### consumeToken
106
+ ```ts
107
+ function consumeToken<Token extends IToken = IToken>(
108
+ tokenType: string
109
+ , tokens: Token[]
110
+ ): Token | Falsy
111
+ ```
112
+
113
+ #### matchAnyOf
114
+ ```ts
115
+ function matchAnyOf<
116
+ Token extends IToken = IToken
117
+ , Node extends INode = INode
118
+ >(
119
+ nodePatterns: ReadonlyArray<INodePattern<Token, Node>>
120
+ , tokens: ReadonlyArray<Token>
121
+ ): Promise<INodePatternMatch<Node> | Falsy>
122
+ ```
123
+
124
+ #### matchSequence
125
+ ```ts
126
+ function matchSequence<Sequence extends ReadonlyArray<IToken | INode>>(
127
+ patterns: MapSequenceToPatterns<Sequence>
128
+ , tokens: ReadonlyArray<IToken>
129
+ ): Promise<MapSequenceToMatches<Sequence> | Falsy>
130
+ ```
131
+
132
+ #### matchRepetitions
133
+ ```ts
134
+ function matchRepetitions<Sequence extends ReadonlyArray<IToken | INode>>(
135
+ patterns: MapSequenceToPatterns<Sequence>
136
+ , tokens: ReadonlyArray<IToken>
137
+ , options?: {
138
+ minimumRepetitions?: number = 1
139
+ maximumRepetitions?: number = Infinity
140
+ }
141
+ ): Promise<Flatten<Array<MapSequenceToMatches<Sequence>>> | Falsy>
142
+ ```
143
+
55
144
  ### createTokenPatternFromRegExp
56
145
  ```ts
57
146
  function createTokenPatternFromRegExp<Token extends IToken>(
@@ -60,13 +149,87 @@ function createTokenPatternFromRegExp<Token extends IToken>(
60
149
  ): ITokenPattern<IToken>
61
150
  ```
62
151
 
63
- ## FAQ
64
- ### Why are functions asynchronous?
65
- Some parsers make heavy use of recursion,
66
- and most JavaScript engines do not support tail-call optimization,
67
- which leads to the possibility of stack overflow in programs.
152
+ ### createUnaryOperatorExpressionNodePattern
153
+ ```ts
154
+ interface IUnaryOperatorExpressionNode<
155
+ NodeType extends string
156
+ , RightNode extends INode
157
+ > extends INode {
158
+ nodeType: NodeType
159
+ right: RightNode
160
+ }
68
161
 
69
- Asynchronous functions are an escape route:
70
- developers can change recursive functions to asynchronous recursive functions
71
- to get their programs out of stack overflow problems
72
- without significantly reducing readability.
162
+ function createUnaryOperatorExpressionNodePattern<
163
+ Token extends IToken
164
+ , Node extends IUnaryOperatorExpressionNode<string, RightNode>
165
+ , RightNode extends INode
166
+ >(params: {
167
+ nodeType: Node['nodeType']
168
+ leftTokenType: string
169
+ rightNodePattern: INodePattern<Token, RightNode>
170
+ }): INodePattern<
171
+ Token
172
+ , IUnaryOperatorExpressionNode<Node['nodeType'], Node['right']>
173
+ >
174
+ ```
175
+
176
+ ### createBinaryOperatorExpressionNodePattern
177
+ ```ts
178
+ interface IBinaryOperatorExpressionNode<
179
+ NodeType extends string
180
+ , LeftNode extends INode
181
+ , RightNode extends INode
182
+ > extends INode {
183
+ nodeType: NodeType
184
+ left: LeftNode
185
+ right: RightNode
186
+ }
187
+
188
+ function createBinaryOperatorExpressionNodePattern<
189
+ Token extends IToken
190
+ , Node extends IBinaryOperatorExpressionNode<string, LeftNode, RightNode>
191
+ , LeftNode extends INode
192
+ , RightNode extends INode
193
+ >(params: {
194
+ nodeType: Node['nodeType']
195
+ centerTokenType: string
196
+ leftNodePattern: INodePattern<Token, LeftNode>
197
+ rightNodePattern: INodePattern<Token, RightNode>
198
+ }): INodePattern<
199
+ Token
200
+ , IBinaryOperatorExpressionNode<Node['nodeType'], Node['left'], Node['right']>
201
+ >
202
+ ```
203
+
204
+ ### createGroupedExpressionNodePattern
205
+ ```ts
206
+ function createGroupedExpressionNodePattern<
207
+ Token extends IToken
208
+ , CenterNode extends INode
209
+ >(params: {
210
+ leftTokenType: string
211
+ rightTokenType: string
212
+ centerNodePattern: INodePattern<Token, CenterNode>
213
+ }): INodePattern<Token, CenterNode>
214
+ ```
215
+
216
+ ### createValueExpressionNodePattern
217
+ ```ts
218
+ interface IValueExpressionNode<
219
+ NodeType extends string
220
+ , Value
221
+ > extends INode {
222
+ nodeType: NodeType
223
+ value: Value
224
+ }
225
+
226
+ function createValueExpressionNodePattern<
227
+ Token extends IToken
228
+ , Node extends IValueExpressionNode<string, Value>
229
+ , Value
230
+ >(params: {
231
+ nodeType: Node['nodeType']
232
+ valueTokenType: string
233
+ transformValue: (value: string) => Value
234
+ }): INodePattern<Token, IValueExpressionNode<Node['nodeType'], Node['value']>>
235
+ ```
@@ -0,0 +1,3 @@
1
+ import { Falsy } from '@blackglory/prelude';
2
+ import { IToken, INode, INodePattern, INodePatternMatch } from './types';
3
+ export declare function consumeNode<Token extends IToken = IToken, Node extends INode = INode>(nodePattern: INodePattern<Token, Node>, tokens: Token[]): Promise<INodePatternMatch<Node> | Falsy>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.consumeNode = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ async function consumeNode(nodePattern, tokens) {
6
+ const match = await nodePattern(tokens);
7
+ if ((0, prelude_1.isntFalsy)(match)) {
8
+ tokens.splice(0, match.consumed);
9
+ return match;
10
+ }
11
+ }
12
+ exports.consumeNode = consumeNode;
13
+ //# sourceMappingURL=consume-node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consume-node.js","sourceRoot":"","sources":["../src/consume-node.ts"],"names":[],"mappings":";;;AAAA,iDAAsD;AAQ/C,KAAK,UAAU,WAAW,CAI/B,WAAsC,EACtC,MAAe;IAEf,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,CAAA;IAEvC,IAAI,IAAA,mBAAS,EAAC,KAAK,CAAC,EAAE;QACpB,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAA;QAChC,OAAO,KAAK,CAAA;KACb;AACH,CAAC;AAbD,kCAaC"}
@@ -0,0 +1,3 @@
1
+ import { Falsy } from '@blackglory/prelude';
2
+ import { IToken } from './types';
3
+ export declare function consumeToken<Token extends IToken = IToken>(tokenType: string, tokens: Token[]): Token | Falsy;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.consumeToken = void 0;
4
+ function consumeToken(tokenType, tokens) {
5
+ const firstToken = tokens[0];
6
+ if (firstToken && firstToken.tokenType === tokenType) {
7
+ tokens.shift();
8
+ return firstToken;
9
+ }
10
+ }
11
+ exports.consumeToken = consumeToken;
12
+ //# sourceMappingURL=consume-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consume-token.js","sourceRoot":"","sources":["../src/consume-token.ts"],"names":[],"mappings":";;;AAQA,SAAgB,YAAY,CAC1B,SAAiB,EACjB,MAAe;IAEf,MAAM,UAAU,GAAuB,MAAM,CAAC,CAAC,CAAC,CAAA;IAEhD,IAAI,UAAU,IAAI,UAAU,CAAC,SAAS,KAAK,SAAS,EAAE;QACpD,MAAM,CAAC,KAAK,EAAE,CAAA;QACd,OAAO,UAAmB,CAAA;KAC3B;AACH,CAAC;AAVD,oCAUC"}
@@ -0,0 +1,12 @@
1
+ import { IToken, INode, INodePattern } from './types';
2
+ export interface IBinaryOperatorExpressionNode<NodeType extends string, LeftNode extends INode, RightNode extends INode> extends INode {
3
+ nodeType: NodeType;
4
+ left: LeftNode;
5
+ right: RightNode;
6
+ }
7
+ export declare function createBinaryOperatorExpressionNodePattern<Token extends IToken, Node extends IBinaryOperatorExpressionNode<string, LeftNode, RightNode>, LeftNode extends INode, RightNode extends INode>({ nodeType, centerTokenType, rightNodePattern, leftNodePattern }: {
8
+ nodeType: Node['nodeType'];
9
+ centerTokenType: string;
10
+ leftNodePattern: INodePattern<Token, LeftNode>;
11
+ rightNodePattern: INodePattern<Token, RightNode>;
12
+ }): INodePattern<Token, IBinaryOperatorExpressionNode<Node['nodeType'], Node['left'], Node['right']>>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBinaryOperatorExpressionNodePattern = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const match_sequence_1 = require("./match-sequence");
6
+ function createBinaryOperatorExpressionNodePattern({ nodeType, centerTokenType, rightNodePattern, leftNodePattern }) {
7
+ return async (tokens) => {
8
+ const matches = await (0, match_sequence_1.matchSequence)([
9
+ leftNodePattern,
10
+ centerTokenType,
11
+ rightNodePattern
12
+ ], tokens);
13
+ if ((0, prelude_1.isntFalsy)(matches)) {
14
+ const [leftMatch, token, rightMatch] = matches;
15
+ return {
16
+ consumed: leftMatch.consumed + 1 + rightMatch.consumed,
17
+ node: {
18
+ nodeType,
19
+ left: leftMatch.node,
20
+ right: rightMatch.node
21
+ }
22
+ };
23
+ }
24
+ };
25
+ }
26
+ exports.createBinaryOperatorExpressionNodePattern = createBinaryOperatorExpressionNodePattern;
27
+ //# sourceMappingURL=create-binary-operator-expression-node-pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-binary-operator-expression-node-pattern.js","sourceRoot":"","sources":["../src/create-binary-operator-expression-node-pattern.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAE/C,qDAAgD;AAYhD,SAAgB,yCAAyC,CAKvD,EAAE,QAAQ,EAAE,eAAe,EAAE,gBAAgB,EAAE,eAAe,EAK/D;IAIC,OAAO,KAAK,EAAC,MAAM,EAAC,EAAE;QACpB,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAa,EACjC;YACE,eAAiD;YACjD,eAAe;YACf,gBAAmD;SACpD,EACD,MAAM,CACP,CAAA;QACD,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,UAAU,CAAC,GAAG,OAAO,CAAA;YAC9C,OAAO;gBACL,QAAQ,EAAE,SAAS,CAAC,QAAQ,GAAG,CAAC,GAAG,UAAU,CAAC,QAAQ;gBACtD,IAAI,EAAE;oBACJ,QAAQ;oBACR,IAAI,EAAE,SAAS,CAAC,IAAoB;oBACpC,KAAK,EAAE,UAAU,CAAC,IAAqB;iBACxC;aACF,CAAA;SACF;IACH,CAAC,CAAA;AACH,CAAC;AAnCD,8FAmCC"}
@@ -0,0 +1,6 @@
1
+ import { IToken, INode, INodePattern } from './types';
2
+ export declare function createGroupedExpressionNodePattern<Token extends IToken, CenterNode extends INode>({ leftTokenType, rightTokenType, centerNodePattern }: {
3
+ leftTokenType: string;
4
+ rightTokenType: string;
5
+ centerNodePattern: INodePattern<Token, CenterNode>;
6
+ }): INodePattern<Token, CenterNode>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createGroupedExpressionNodePattern = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const match_sequence_1 = require("./match-sequence");
6
+ function createGroupedExpressionNodePattern({ leftTokenType, rightTokenType, centerNodePattern }) {
7
+ return async (tokens) => {
8
+ const matches = await (0, match_sequence_1.matchSequence)([
9
+ leftTokenType,
10
+ centerNodePattern,
11
+ rightTokenType
12
+ ], tokens);
13
+ if ((0, prelude_1.isntFalsy)(matches)) {
14
+ const [leftToken, nodeMatch, rightToken] = matches;
15
+ return {
16
+ consumed: 1 + nodeMatch.consumed + 1,
17
+ node: nodeMatch.node
18
+ };
19
+ }
20
+ };
21
+ }
22
+ exports.createGroupedExpressionNodePattern = createGroupedExpressionNodePattern;
23
+ //# sourceMappingURL=create-grouped-expression-node-pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-grouped-expression-node-pattern.js","sourceRoot":"","sources":["../src/create-grouped-expression-node-pattern.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAE/C,qDAAgD;AAEhD,SAAgB,kCAAkC,CAGhD,EAAE,aAAa,EAAE,cAAc,EAAE,iBAAiB,EAInD;IACC,OAAO,KAAK,EAAC,MAAM,EAAC,EAAE;QACpB,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAa,EACjC;YACE,aAAa;YACb,iBAAqD;YACrD,cAAc;SACf,EACD,MAAM,CACP,CAAA;QACD,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,UAAU,CAAC,GAAG,OAAO,CAAA;YAClD,OAAO;gBACL,QAAQ,EAAE,CAAC,GAAG,SAAS,CAAC,QAAQ,GAAG,CAAC;gBACpC,IAAI,EAAE,SAAS,CAAC,IAAkB;aACnC,CAAA;SACF;IACH,CAAC,CAAA;AACH,CAAC;AAzBD,gFAyBC"}
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTokenPatternFromRegExp = void 0;
4
- const types_1 = require("@blackglory/types");
4
+ const prelude_1 = require("@blackglory/prelude");
5
5
  function createTokenPatternFromRegExp(tokenType, regExp) {
6
6
  const startsWithRegExp = convertToStartsWithRegExp(regExp);
7
7
  return (text) => {
8
8
  const result = startsWithRegExp.exec(text);
9
- if ((0, types_1.isntNull)(result)) {
9
+ if ((0, prelude_1.isntNull)(result)) {
10
10
  const [matchedText] = result;
11
11
  return {
12
12
  consumed: matchedText.length,
@@ -1 +1 @@
1
- {"version":3,"file":"create-token-pattern-from-regexp.js","sourceRoot":"","sources":["../src/create-token-pattern-from-regexp.ts"],"names":[],"mappings":";;;AACA,6CAA4C;AAG5C,SAAgB,4BAA4B,CAC1C,SAA6B,E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AE;oBACL,SAAS,EAAE,SAAS;oBACpB,KAAK,EAAE,WAAW;iBACnB;aACF,CAAA;SACF;aAAM;YACL,OAAO,KAAK,CAAA;SACb;IACH,CAAC,CAAA;AACH,CAAC;AArBD,oEAqBC;AAED,SAAS,yBAAyB,CAAC,EAAU;IAC3C,OAAO,IAAI,MAAM,CACf,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,EACvD,EAAE,CAAC,KAAK,CACT,CAAA;AACH,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { IToken, INode, INodePattern } from './types';
2
+ export interface IUnaryOperatorExpressionNode<NodeType extends string, RightNode extends INode> extends INode {
3
+ nodeType: NodeType;
4
+ right: RightNode;
5
+ }
6
+ export declare function createUnaryOperatorExpressionNodePattern<Token extends IToken, Node extends IUnaryOperatorExpressionNode<string, RightNode>, RightNode extends INode>({ leftTokenType, nodeType, rightNodePattern }: {
7
+ nodeType: Node['nodeType'];
8
+ leftTokenType: string;
9
+ rightNodePattern: INodePattern<Token, RightNode>;
10
+ }): INodePattern<Token, IUnaryOperatorExpressionNode<Node['nodeType'], Node['right']>>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createUnaryOperatorExpressionNodePattern = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const match_sequence_1 = require("./match-sequence");
6
+ function createUnaryOperatorExpressionNodePattern({ leftTokenType, nodeType, rightNodePattern }) {
7
+ return async (tokens) => {
8
+ const matches = await (0, match_sequence_1.matchSequence)([
9
+ leftTokenType,
10
+ rightNodePattern
11
+ ], tokens);
12
+ if ((0, prelude_1.isntFalsy)(matches)) {
13
+ const [leftToken, rightMatch] = matches;
14
+ return {
15
+ consumed: 1 + rightMatch.consumed,
16
+ node: {
17
+ nodeType: nodeType,
18
+ right: rightMatch.node
19
+ }
20
+ };
21
+ }
22
+ };
23
+ }
24
+ exports.createUnaryOperatorExpressionNodePattern = createUnaryOperatorExpressionNodePattern;
25
+ //# sourceMappingURL=create-unary-operator-expression-node-pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-unary-operator-expression-node-pattern.js","sourceRoot":"","sources":["../src/create-unary-operator-expression-node-pattern.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAE/C,qDAAgD;AAUhD,SAAgB,wCAAwC,CAItD,EAAE,aAAa,EAAE,QAAQ,EAAE,gBAAgB,EAI5C;IAIC,OAAO,KAAK,EAAC,MAAM,EAAC,EAAE;QACpB,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAa,EACjC;YACE,aAAa;YACb,gBAAmD;SACpD,EACD,MAAM,CACP,CAAA;QACD,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,OAAO,CAAA;YACvC,OAAO;gBACL,QAAQ,EAAE,CAAC,GAAG,UAAU,CAAC,QAAQ;gBACjC,IAAI,EAAE;oBACJ,QAAQ,EAAE,QAAQ;oBAClB,KAAK,EAAE,UAAU,CAAC,IAAiB;iBACpC;aACF,CAAA;SACF;IACH,CAAC,CAAA;AACH,CAAC;AA/BD,4FA+BC"}
@@ -0,0 +1,10 @@
1
+ import { IToken, INode, INodePattern } from './types';
2
+ export interface IValueExpressionNode<NodeType extends string, Value> extends INode {
3
+ nodeType: NodeType;
4
+ value: Value;
5
+ }
6
+ export declare function createValueExpressionNodePattern<Token extends IToken, Node extends IValueExpressionNode<string, Value>, Value>({ valueTokenType, nodeType, transformValue }: {
7
+ nodeType: Node['nodeType'];
8
+ valueTokenType: string;
9
+ transformValue: (value: string) => Value;
10
+ }): INodePattern<Token, IValueExpressionNode<Node['nodeType'], Node['value']>>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createValueExpressionNodePattern = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const consume_token_1 = require("./consume-token");
6
+ function createValueExpressionNodePattern({ valueTokenType, nodeType, transformValue }) {
7
+ return tokens => {
8
+ const mutableTokens = (0, prelude_1.toArray)(tokens);
9
+ const token = (0, consume_token_1.consumeToken)(valueTokenType, mutableTokens);
10
+ if ((0, prelude_1.isntFalsy)(token)) {
11
+ return {
12
+ consumed: 1,
13
+ node: {
14
+ nodeType,
15
+ value: transformValue(token.value)
16
+ }
17
+ };
18
+ }
19
+ };
20
+ }
21
+ exports.createValueExpressionNodePattern = createValueExpressionNodePattern;
22
+ //# sourceMappingURL=create-value-expression-node-pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-value-expression-node-pattern.js","sourceRoot":"","sources":["../src/create-value-expression-node-pattern.ts"],"names":[],"mappings":";;;AAAA,iDAAwD;AACxD,mDAA8C;AAW9C,SAAgB,gCAAgC,CAI9C,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,EAI3C;IACC,OAAO,MAAM,CAAC,EAAE;QACd,MAAM,aAAa,GAAG,IAAA,iBAAO,EAAC,MAAM,CAAC,CAAA;QAErC,MAAM,KAAK,GAAG,IAAA,4BAAY,EAAC,cAAc,EAAE,aAAa,CAAC,CAAA;QACzD,IAAI,IAAA,mBAAS,EAAC,KAAK,CAAC,EAAE;YACpB,OAAO;gBACL,QAAQ,EAAE,CAAC;gBACX,IAAI,EAAE;oBACJ,QAAQ;oBACR,KAAK,EAAE,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC;iBACnC;aACF,CAAA;SACF;IACH,CAAC,CAAA;AACH,CAAC;AAvBD,4EAuBC"}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,13 @@
1
- export * from './types';
1
+ export { IToken, ITokenPattern, ITokenPatternMatch, INode, INodePattern, INodePatternMatch } from './types';
2
2
  export * from './tokenize';
3
3
  export * from './parse';
4
+ export * from './consume-token';
5
+ export * from './consume-node';
6
+ export { matchSequence } from './match-sequence';
7
+ export * from './match-repetitions';
8
+ export * from './match-any-of';
4
9
  export * from './create-token-pattern-from-regexp';
10
+ export * from './create-unary-operator-expression-node-pattern';
11
+ export * from './create-binary-operator-expression-node-pattern';
12
+ export * from './create-grouped-expression-node-pattern';
13
+ export * from './create-value-expression-node-pattern';
package/lib/index.js CHANGED
@@ -14,8 +14,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./types"), exports);
17
+ exports.matchSequence = void 0;
18
18
  __exportStar(require("./tokenize"), exports);
19
19
  __exportStar(require("./parse"), exports);
20
+ __exportStar(require("./consume-token"), exports);
21
+ __exportStar(require("./consume-node"), exports);
22
+ var match_sequence_1 = require("./match-sequence");
23
+ Object.defineProperty(exports, "matchSequence", { enumerable: true, get: function () { return match_sequence_1.matchSequence; } });
24
+ __exportStar(require("./match-repetitions"), exports);
25
+ __exportStar(require("./match-any-of"), exports);
20
26
  __exportStar(require("./create-token-pattern-from-regexp"), exports);
27
+ __exportStar(require("./create-unary-operator-expression-node-pattern"), exports);
28
+ __exportStar(require("./create-binary-operator-expression-node-pattern"), exports);
29
+ __exportStar(require("./create-grouped-expression-node-pattern"), exports);
30
+ __exportStar(require("./create-value-expression-node-pattern"), exports);
21
31
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,6CAA0B;AAC1B,0CAAuB;AACvB,qEAAkD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAQA,6CAA0B;AAC1B,0CAAuB;AACvB,kDAA+B;AAC/B,iDAA8B;AAC9B,mDAAgD;AAAvC,+GAAA,aAAa,OAAA;AACtB,sDAAmC;AACnC,iDAA8B;AAC9B,qEAAkD;AAClD,kFAA+D;AAC/D,mFAAgE;AAChE,2EAAwD;AACxD,yEAAsD"}
@@ -0,0 +1,3 @@
1
+ import { Falsy } from '@blackglory/prelude';
2
+ import { IToken, INode, INodePattern, INodePatternMatch } from './types';
3
+ export declare function matchAnyOf<Token extends IToken = IToken, Node extends INode = INode>(nodePatterns: ReadonlyArray<INodePattern<Token, Node>>, tokens: ReadonlyArray<Token>): Promise<INodePatternMatch<Node> | Falsy>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchAnyOf = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ async function matchAnyOf(nodePatterns, tokens) {
6
+ for (const pattern of nodePatterns) {
7
+ const match = await pattern(tokens);
8
+ if ((0, prelude_1.isntFalsy)(match)) {
9
+ return match;
10
+ }
11
+ }
12
+ }
13
+ exports.matchAnyOf = matchAnyOf;
14
+ //# sourceMappingURL=match-any-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match-any-of.js","sourceRoot":"","sources":["../src/match-any-of.ts"],"names":[],"mappings":";;;AAAA,iDAAsD;AAM/C,KAAK,UAAU,UAAU,CAI9B,YAAsD,EACtD,MAA4B;IAE5B,KAAK,MAAM,OAAO,IAAI,YAAY,EAAE;QAClC,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;QACnC,IAAI,IAAA,mBAAS,EAAC,KAAK,CAAC,EAAE;YACpB,OAAO,KAAK,CAAA;SACb;KACF;AACH,CAAC;AAbD,gCAaC"}
@@ -0,0 +1,7 @@
1
+ import { Falsy } from '@blackglory/prelude';
2
+ import { Flatten } from 'hotypes';
3
+ import { IToken, INode, MapSequenceToPatterns, MapSequenceToMatches } from './types';
4
+ export declare function matchRepetitions<Sequence extends ReadonlyArray<IToken | INode>>(patterns: MapSequenceToPatterns<Sequence>, tokens: ReadonlyArray<IToken>, { minimumRepetitions, maximumRepetitions }?: {
5
+ minimumRepetitions?: number;
6
+ maximumRepetitions?: number;
7
+ }): Promise<Flatten<Array<MapSequenceToMatches<Sequence>>> | Falsy>;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchRepetitions = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const match_sequence_1 = require("./match-sequence");
6
+ async function matchRepetitions(patterns, tokens, { minimumRepetitions = 1, maximumRepetitions = Infinity } = {}) {
7
+ (0, prelude_1.assert)(Number.isInteger(minimumRepetitions), 'The minimum repetiions must be an integer');
8
+ (0, prelude_1.assert)(minimumRepetitions >= 0, 'The minimum repetitions must be greater than or equal to 0');
9
+ (0, prelude_1.assert)(Number.isInteger(maximumRepetitions) || maximumRepetitions === Infinity, 'The maxmium repetiions must be an integer or an Infinity');
10
+ (0, prelude_1.assert)(maximumRepetitions >= minimumRepetitions, 'The maximum repetitions must be greater than or equal to the minimum repetitions');
11
+ const results = [];
12
+ const mutableTokens = (0, prelude_1.toArray)(tokens);
13
+ for (let i = 0; i < minimumRepetitions; i++) {
14
+ const matches = await (0, match_sequence_1.matchSequence)(patterns, mutableTokens);
15
+ if ((0, prelude_1.isntFalsy)(matches)) {
16
+ results.push(matches);
17
+ mutableTokens.splice(0, matches.length);
18
+ }
19
+ else {
20
+ return;
21
+ }
22
+ }
23
+ for (let i = minimumRepetitions; i < maximumRepetitions; i++) {
24
+ const matches = await (0, match_sequence_1.matchSequence)(patterns, mutableTokens);
25
+ if ((0, prelude_1.isntFalsy)(matches)) {
26
+ results.push(matches);
27
+ mutableTokens.splice(0, matches.length);
28
+ }
29
+ else {
30
+ break;
31
+ }
32
+ }
33
+ return results.flat();
34
+ }
35
+ exports.matchRepetitions = matchRepetitions;
36
+ //# sourceMappingURL=match-repetitions.js.map
@@ -0,0 +1 @@
1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,IAAI,IAAA,mBAAS,EAAC,OAAO,CAAC,EAAE;YACtB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YACrB,aAAa,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;SACxC;aAAM;YACL,MAAK;SACN;KACF;IAED,OAAO,OAAO,CAAC,IAAI,EAAoD,CAAA;AACzE,CAAC;AAjDD,4CAiDC"}
@@ -0,0 +1,6 @@
1
+ import { NonEmptyArray, Falsy } from '@blackglory/prelude';
2
+ import { IToken, INode, INodePattern, MapSequenceToPatterns, MapSequenceToMatches } from './types';
3
+ export declare function matchSequence<Sequence extends ReadonlyArray<IToken | INode>>(patterns: MapSequenceToPatterns<Sequence>, tokens: ReadonlyArray<IToken>): Promise<MapSequenceToMatches<Sequence> | Falsy>;
4
+ declare type SubPatterns = [INodePattern<IToken, INode>, string] | NonEmptyArray<string> | NonEmptyArray<INodePattern<IToken, INode>>;
5
+ export declare function splitPatterns<Sequence extends ReadonlyArray<IToken | INode>>(patterns: MapSequenceToPatterns<Sequence>): IterableIterator<SubPatterns>;
6
+ export {};
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitPatterns = exports.matchSequence = void 0;
4
+ const prelude_1 = require("@blackglory/prelude");
5
+ const iterable_operator_1 = require("iterable-operator");
6
+ const consume_token_1 = require("./consume-token");
7
+ const consume_node_1 = require("./consume-node");
8
+ async function matchSequence(patterns, tokens) {
9
+ if (isTokenTypes(patterns)) {
10
+ const matches = [];
11
+ const mutableTokens = (0, prelude_1.toArray)(tokens);
12
+ for (const pattern of patterns) {
13
+ const match = (0, consume_token_1.consumeToken)(pattern, mutableTokens);
14
+ if ((0, prelude_1.isntFalsy)(match)) {
15
+ matches.push(match);
16
+ }
17
+ else {
18
+ return;
19
+ }
20
+ }
21
+ return matches;
22
+ }
23
+ else if (isNodePatterns(patterns)) {
24
+ const matches = [];
25
+ const mutableTokens = (0, prelude_1.toArray)(tokens);
26
+ for (const pattern of patterns) {
27
+ const match = await (0, consume_node_1.consumeNode)(pattern, mutableTokens);
28
+ if ((0, prelude_1.isntFalsy)(match)) {
29
+ matches.push(match);
30
+ }
31
+ else {
32
+ return;
33
+ }
34
+ }
35
+ return matches;
36
+ }
37
+ else if (isNodePatternNodeType(patterns)) {
38
+ const [nodePattern, tokenType] = patterns;
39
+ for (const indexOfToken of (0, iterable_operator_1.findAllIndexes)(tokens, x => x.tokenType === tokenType)) {
40
+ const leftTokens = tokens.slice(0, indexOfToken);
41
+ const leftMatch = await nodePattern(leftTokens);
42
+ if ((0, prelude_1.isntFalsy)(leftMatch) &&
43
+ leftMatch.consumed === indexOfToken) {
44
+ const matches = [
45
+ leftMatch,
46
+ tokens[indexOfToken]
47
+ ];
48
+ return matches;
49
+ }
50
+ }
51
+ }
52
+ else {
53
+ const matches = [];
54
+ const remainingTokens = (0, prelude_1.toArray)(tokens);
55
+ for (const subPatterns of splitPatterns(patterns)) {
56
+ const subMatches = await matchSequence(subPatterns, remainingTokens);
57
+ if ((0, prelude_1.isntFalsy)(subMatches)) {
58
+ const consumed = subMatches
59
+ .map(match => {
60
+ return 'consumed' in match
61
+ ? match.consumed
62
+ : 1;
63
+ })
64
+ .reduce((acc, cur) => acc + cur, 0);
65
+ remainingTokens.splice(0, consumed);
66
+ matches.push(...subMatches);
67
+ }
68
+ else {
69
+ return;
70
+ }
71
+ }
72
+ return matches;
73
+ }
74
+ }
75
+ exports.matchSequence = matchSequence;
76
+ function* splitPatterns(patterns) {
77
+ const mutablePatterns = (0, prelude_1.toArray)(patterns);
78
+ while (mutablePatterns.length > 0) {
79
+ if (isTokenType(mutablePatterns[0])) {
80
+ const indexOfNodePattern = mutablePatterns.findIndex(x => isNodePattern(x));
81
+ if (indexOfNodePattern === -1) {
82
+ yield mutablePatterns.splice(0);
83
+ }
84
+ else {
85
+ yield mutablePatterns.splice(0, indexOfNodePattern);
86
+ }
87
+ }
88
+ else if (isNodePattern(mutablePatterns[0])) {
89
+ const indexOfToken = mutablePatterns.findIndex(x => isTokenType(x));
90
+ if (indexOfToken === -1) {
91
+ yield mutablePatterns.splice(0);
92
+ }
93
+ else {
94
+ yield mutablePatterns.splice(0, indexOfToken + 1);
95
+ }
96
+ }
97
+ else {
98
+ throw new Error('Unknown patterns');
99
+ }
100
+ }
101
+ }
102
+ exports.splitPatterns = splitPatterns;
103
+ function isTokenTypes(arr) {
104
+ return arr.every(isTokenType);
105
+ }
106
+ function isTokenType(val) {
107
+ return (0, prelude_1.isString)(val);
108
+ }
109
+ function isNodePatterns(arr) {
110
+ return arr.every(isNodePattern);
111
+ }
112
+ function isNodePattern(val) {
113
+ return (0, prelude_1.isFunction)(val);
114
+ }
115
+ function isNodePatternNodeType(arr) {
116
+ return arr.length === 2
117
+ && isNodePattern(arr[0])
118
+ && isTokenType(arr[1]);
119
+ }
120
+ //# sourceMappingURL=match-sequence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match-sequence.js","sourceRoot":"","sources":["../src/match-sequence.ts"],"names":[],"mappings":";;;AAAA,iDAO4B;AAC5B,yDAAkD;AASlD,mDAA8C;AAC9C,iDAA4C;AAWrC,KAAK,UAAU,aAAa,CACjC,QAAyC,EACzC,MAA6B;IAE7B,IAAI,YAAY,CAAC,QAAQ,CAAC,EAAE;QAC1B,MAAM,OAAO,GAAkB,EAAE,CAAA;QAEjC,MAAM,aAAa,GAAG,IAAA,iBAAO,EAAC,MAAM,CAAC,CAAA;QACrC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,KAAK,GAAG,IAAA,4BAAY,EAAC,OAAO,EAAE,aAAa,CAAC,CAAA;YAClD,IAAI,IAAA,mBAAS,EAAC,KAAK,CAAC,EAAE;gBACpB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;aACpB;iBAAM;gBACL,OAAM;aACP;SACF;QAED,OAAO,OAAyC,CAAA;KACjD;SAAM,IAAI,cAAc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AAC,CAAC,CAAA;YACnE,IAAI,YAAY,KAAK,CAAC,CAAC,EAAE;gBACvB,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,CAE7B,CAAA;aACF;iBAAM;gBACL,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,EAAE,YAAY,GAAG,CAAC,CAG/C,CAAA;aACF;SACF;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;SACpC;KACF;AACH,CAAC;AA7BD,sCA6BC;AAED,SAAS,YAAY,CAAC,GAA2B;IAC/C,OAAO,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;AAC/B,CAAC;AAED,SAAS,WAAW,CAAC,GAAY;IAC/B,OAAO,IAAA,kBAAQ,EAAC,GAAG,CAAC,CAAA;AACtB,CAAC;AAED,SAAS,cAAc,CACrB,GAA2B;IAE3B,OAAO,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;AACjC,CAAC;AAED,SAAS,aAAa,CACpB,GAAY;IAEZ,OAAO,IAAA,oBAAU,EAAC,GAAG,CAAC,CAAA;AACxB,CAAC;AAED,SAAS,qBAAqB,CAC5B,GAA2B;IAE3B,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC;WAChB,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;WACrB,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;AAC5B,CAAC"}
package/lib/parse.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { IToken, INodePattern, INode } from './types';
2
- export declare function parse<Token extends IToken = IToken, Node extends INode = INode>(tokens: Token[], patterns: Array<INodePattern<Token, Node>>): AsyncIterableIterator<Node>;
2
+ export declare function parse<Token extends IToken = IToken, Node extends INode = INode>(patterns: Array<INodePattern<Token, Node>>, tokens: Token[]): AsyncIterableIterator<Node>;
package/lib/parse.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parse = void 0;
4
4
  const prelude_1 = require("@blackglory/prelude");
5
- async function* parse(tokens, patterns) {
5
+ async function* parse(patterns, tokens) {
6
6
  let i = 0;
7
7
  loop: while (i < tokens.length) {
8
8
  const remainingTokens = tokens.slice(i);
package/lib/parse.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAGxC,KAAK,SAAS,CAAC,CAAC,KAAK,CAC1B,MAAe,EACf,QAA0C;IAE1C,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE;QAC9B,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAEvC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,eAAe,CAAC,CAAA;YAC7C,IAAI,IAAA,mBAAS,EAAC,MAAM,CAAC,EAAE;gBACrB,MAAM,MAAM,CAAC,IAAI,CAAA;gBACjB,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAA;gBACpB,SAAS,IAAI,CAAA;aACd;SACF;QAED,MAAM,IAAI,KAAK,CAAC,uBAAuB,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;KAC1E;AACH,CAAC;AAnBD,sBAmBC"}
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAGxC,KAAK,SAAS,CAAC,CAAC,KAAK,CAI1B,QAA0C,EAC1C,MAAe;IAEf,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE;QAC9B,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAEvC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,eAAe,CAAC,CAAA;YAC7C,IAAI,IAAA,mBAAS,EAAC,MAAM,CAAC,EAAE;gBACrB,MAAM,MAAM,CAAC,IAAI,CAAA;gBACjB,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAA;gBACpB,SAAS,IAAI,CAAA;aACd;SACF;QAED,MAAM,IAAI,KAAK,CAAC,uBAAuB,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;KAC1E;AACH,CAAC;AAtBD,sBAsBC"}
package/lib/tokenize.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { ITokenPattern, IToken } from './types';
2
- export declare function tokenize<Token extends IToken = IToken>(text: string, patterns: Array<ITokenPattern<Token>>): AsyncIterableIterator<Token>;
2
+ export declare function tokenize<Token extends IToken = IToken>(patterns: Array<ITokenPattern<Token>>, text: string): AsyncIterableIterator<Token>;
package/lib/tokenize.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tokenize = void 0;
4
4
  const prelude_1 = require("@blackglory/prelude");
5
- async function* tokenize(text, patterns) {
5
+ async function* tokenize(patterns, text) {
6
6
  let i = 0;
7
7
  loop: while (i < text.length) {
8
8
  const remainingText = text.slice(i);
@@ -1 +1 @@
1
- {"version":3,"file":"tokenize.js","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAGxC,KAAK,SAAS,CAAC,CAAC,QAAQ,CAC7B,IAAY,EACZ,QAAqC;IAErC,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE;QAC5B,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAEnC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,CAAA;YAC3C,IAAI,IAAA,mBAAS,EAAC,MAAM,CAAC,EAAE;gBACrB,MAAM,MAAM,CAAC,KAAK,CAAA;gBAClB,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAA;gBACpB,SAAS,IAAI,CAAA;aACd;SACF;QAED,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,CAAC,CAAA;KAClE;AACH,CAAC;AAnBD,4BAmBC"}
1
+ {"version":3,"file":"tokenize.js","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAGxC,KAAK,SAAS,CAAC,CAAC,QAAQ,CAC7B,QAAqC,EACrC,IAAY;IAEZ,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE;QAC5B,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAEnC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,CAAA;YAC3C,IAAI,IAAA,mBAAS,EAAC,MAAM,CAAC,EAAE;gBACrB,MAAM,MAAM,CAAC,KAAK,CAAA;gBAClB,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAA;gBACpB,SAAS,IAAI,CAAA;aACd;SACF;QAED,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,CAAC,CAAA;KAClE;AACH,CAAC;AAnBD,4BAmBC"}
package/lib/types.d.ts CHANGED
@@ -20,3 +20,13 @@ export interface ITokenPattern<Token extends IToken = IToken> {
20
20
  export interface INodePattern<Token extends IToken = IToken, Node extends INode = INode> {
21
21
  (tokens: ReadonlyArray<Token>): Awaitable<INodePatternMatch<Node> | Falsy>;
22
22
  }
23
+ export declare type MapSequenceToPatterns<Sequence extends ReadonlyArray<IToken | INode>> = {
24
+ [Index in keyof Sequence]: [
25
+ Sequence[Index]
26
+ ] extends [infer Element] ? (Element extends IToken ? string : Element extends INode ? INodePattern<IToken, Element> : never) : never;
27
+ };
28
+ export declare type MapSequenceToMatches<Sequence extends ReadonlyArray<IToken | INode>> = {
29
+ [Index in keyof Sequence]: [
30
+ Sequence[Index]
31
+ ] extends [infer Element] ? (Element extends IToken ? IToken : Element extends INode ? INodePatternMatch<Element> : never) : never;
32
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "extra-parser",
3
- "version": "0.5.0",
4
- "description": "",
3
+ "version": "0.6.1",
4
+ "description": "A functional parser toolkit",
5
5
  "keywords": [],
6
6
  "files": [
7
7
  "lib"
@@ -39,7 +39,6 @@
39
39
  "@typescript-eslint/parser": "^5.42.1",
40
40
  "eslint": "^8.27.0",
41
41
  "husky": "^4.3.8",
42
- "iterable-operator": "^2.2.0",
43
42
  "jest": "^29.3.0",
44
43
  "npm-run-all": "^4.1.5",
45
44
  "return-style": "^1.0.0",
@@ -51,6 +50,7 @@
51
50
  "typescript": "^4.8.4"
52
51
  },
53
52
  "dependencies": {
54
- "@blackglory/prelude": "^0.1.8"
53
+ "@blackglory/prelude": "^0.1.8",
54
+ "iterable-operator": "^2.3.0"
55
55
  }
56
56
  }