@formvk/path 0.0.0-stage → 0.1.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/dist/index.d.ts +122 -0
- package/dist/index.js +1334 -0
- package/dist/index.js.map +1 -0
- package/package.json +19 -3
- package/src/__tests__/accessor.spec.ts +429 -0
- package/src/__tests__/basic.spec.ts +14 -0
- package/src/__tests__/match.spec.ts +280 -0
- package/src/__tests__/parser.spec.ts +689 -0
- package/src/__tests__/share.spec.ts +43 -0
- package/src/contexts.ts +23 -0
- package/src/destructor.ts +131 -0
- package/src/index.ts +583 -0
- package/src/matcher.ts +224 -0
- package/src/parser.ts +460 -0
- package/src/shared.ts +83 -0
- package/src/tokenizer.ts +306 -0
- package/src/tokens.ts +233 -0
- package/src/types.ts +133 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +22 -0
- package/README.md +0 -3
package/src/matcher.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { isEqual, toArr, isSegmentEqual } from './shared'
|
|
2
|
+
import type {
|
|
3
|
+
Segments,
|
|
4
|
+
Node,
|
|
5
|
+
IdentifierNode,
|
|
6
|
+
IgnoreExpressionNode,
|
|
7
|
+
DestructorExpressionNode,
|
|
8
|
+
ExpandOperatorNode,
|
|
9
|
+
WildcardOperatorNode,
|
|
10
|
+
GroupExpressionNode,
|
|
11
|
+
RangeExpressionNode,
|
|
12
|
+
} from './types'
|
|
13
|
+
import {
|
|
14
|
+
isIdentifier,
|
|
15
|
+
isExpandOperator,
|
|
16
|
+
isWildcardOperator,
|
|
17
|
+
isGroupExpression,
|
|
18
|
+
isRangeExpression,
|
|
19
|
+
isIgnoreExpression,
|
|
20
|
+
isDotOperator,
|
|
21
|
+
isDestructorExpression,
|
|
22
|
+
} from './types'
|
|
23
|
+
export interface IRecord {
|
|
24
|
+
score: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class Matcher {
|
|
28
|
+
private tree: Node
|
|
29
|
+
|
|
30
|
+
private stack: Node[]
|
|
31
|
+
|
|
32
|
+
private record?: IRecord
|
|
33
|
+
|
|
34
|
+
private excluding: boolean
|
|
35
|
+
|
|
36
|
+
private wildcards: WildcardOperatorNode[]
|
|
37
|
+
|
|
38
|
+
private path: Segments = []
|
|
39
|
+
|
|
40
|
+
constructor(tree: Node, record?: any) {
|
|
41
|
+
this.tree = tree
|
|
42
|
+
this.stack = []
|
|
43
|
+
this.excluding = false
|
|
44
|
+
this.wildcards = []
|
|
45
|
+
this.record = record
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
next(node: Node, pos: number): boolean {
|
|
49
|
+
// const isOverToken = pos > this.path.length
|
|
50
|
+
if (node.after) {
|
|
51
|
+
// if (isOverToken) {
|
|
52
|
+
// return false
|
|
53
|
+
// }
|
|
54
|
+
return this.matchNode(node.after, pos)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (isWildcardOperator(node) && !node.filter) {
|
|
58
|
+
if (this.excluding) {
|
|
59
|
+
return false
|
|
60
|
+
} else {
|
|
61
|
+
if (pos === 0 || node.optional) return true
|
|
62
|
+
return !!this.take(pos)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const isLastToken = pos === this.path.length - 1
|
|
67
|
+
if (isLastToken) {
|
|
68
|
+
return !!this.take(pos)
|
|
69
|
+
} else {
|
|
70
|
+
const wildcard = this.wildcards.pop()
|
|
71
|
+
if (wildcard && wildcard.after) {
|
|
72
|
+
return this.next(wildcard, pos)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
shot() {
|
|
80
|
+
if (this.record && this.record.score >= 0) {
|
|
81
|
+
this.record.score++
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
take(pos: number) {
|
|
86
|
+
return String(this.path[pos] ?? '')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
matchExcludeIdentifier(matched: boolean, node: Node, pos: number) {
|
|
90
|
+
const isLastToken = pos === this.path.length - 1
|
|
91
|
+
const isContainToken = pos < this.path.length
|
|
92
|
+
if (!node.after) {
|
|
93
|
+
this.excluding = false
|
|
94
|
+
}
|
|
95
|
+
if (matched) {
|
|
96
|
+
if (node.after) {
|
|
97
|
+
return this.next(node, pos)
|
|
98
|
+
}
|
|
99
|
+
if (isLastToken) {
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (isLastToken) {
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
return isContainToken
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
matchIdentifier(node: IdentifierNode, pos: number) {
|
|
110
|
+
const current = this.take(pos)
|
|
111
|
+
let matched = false
|
|
112
|
+
if (isExpandOperator(node.after)) {
|
|
113
|
+
if (current.indexOf(node.value) === 0) {
|
|
114
|
+
this.shot()
|
|
115
|
+
matched = true
|
|
116
|
+
}
|
|
117
|
+
if (this.excluding) {
|
|
118
|
+
return this.matchExcludeIdentifier(matched, node.after, pos)
|
|
119
|
+
} else {
|
|
120
|
+
return matched && this.next(node.after, pos)
|
|
121
|
+
}
|
|
122
|
+
} else if (current === node.value) {
|
|
123
|
+
this.shot()
|
|
124
|
+
matched = true
|
|
125
|
+
}
|
|
126
|
+
if (this.excluding) {
|
|
127
|
+
return this.matchExcludeIdentifier(matched, node, pos)
|
|
128
|
+
} else {
|
|
129
|
+
return matched && this.next(node, pos)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
matchIgnoreExpression(node: IgnoreExpressionNode, pos: number) {
|
|
134
|
+
return isEqual(node.value, this.take(pos)) && this.next(node, pos)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
matchDestructorExpression(node: DestructorExpressionNode, pos: number) {
|
|
138
|
+
return isEqual(node.source, this.take(pos)) && this.next(node, pos)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
matchExpandOperator(node: ExpandOperatorNode, pos: number) {
|
|
142
|
+
return this.next(node, pos)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
matchWildcardOperator(node: WildcardOperatorNode, pos: number): boolean {
|
|
146
|
+
let matched = false
|
|
147
|
+
if (node.filter) {
|
|
148
|
+
this.stack.push(node)
|
|
149
|
+
matched = this.matchNode(node.filter, pos)
|
|
150
|
+
this.stack.pop()
|
|
151
|
+
} else {
|
|
152
|
+
matched = this.next(node, pos)
|
|
153
|
+
}
|
|
154
|
+
return matched
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
matchGroupExpression(node: GroupExpressionNode, pos: number) {
|
|
158
|
+
let excluding = false
|
|
159
|
+
if (node.isExclude) {
|
|
160
|
+
excluding = !this.excluding
|
|
161
|
+
}
|
|
162
|
+
return toArr(node.value)[excluding ? 'every' : 'some']((item: Node) => {
|
|
163
|
+
this.wildcards = this.stack.slice() as WildcardOperatorNode[]
|
|
164
|
+
this.excluding = excluding
|
|
165
|
+
return this.matchNode(item, pos)
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
matchRangeExpression(node: RangeExpressionNode, pos: number) {
|
|
170
|
+
const current = Number(this.take(pos))
|
|
171
|
+
if (node.start) {
|
|
172
|
+
if (node.end) {
|
|
173
|
+
return current >= Number(node.start.value) && current <= Number(node.end.value)
|
|
174
|
+
} else {
|
|
175
|
+
return current >= Number(node.start.value)
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
if (node.end) {
|
|
179
|
+
return current <= Number(node.end.value)
|
|
180
|
+
} else {
|
|
181
|
+
this.wildcards = this.stack.slice() as WildcardOperatorNode[]
|
|
182
|
+
return this.next(node, pos)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
matchNode(node: Node, pos = 0): boolean {
|
|
188
|
+
if (isDotOperator(node)) {
|
|
189
|
+
return this.next(node, pos + 1)
|
|
190
|
+
} else if (isIdentifier(node)) {
|
|
191
|
+
return this.matchIdentifier(node, pos)
|
|
192
|
+
} else if (isIgnoreExpression(node)) {
|
|
193
|
+
return this.matchIgnoreExpression(node, pos)
|
|
194
|
+
} else if (isDestructorExpression(node)) {
|
|
195
|
+
return this.matchDestructorExpression(node, pos)
|
|
196
|
+
} else if (isExpandOperator(node)) {
|
|
197
|
+
return this.matchExpandOperator(node, pos)
|
|
198
|
+
} else if (isWildcardOperator(node)) {
|
|
199
|
+
return this.matchWildcardOperator(node, pos)
|
|
200
|
+
} else if (isGroupExpression(node)) {
|
|
201
|
+
return this.matchGroupExpression(node, pos)
|
|
202
|
+
} else if (isRangeExpression(node)) {
|
|
203
|
+
return this.matchRangeExpression(node, pos)
|
|
204
|
+
}
|
|
205
|
+
return false
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
match(path: Segments): { matched: boolean; record?: IRecord } {
|
|
209
|
+
this.path = path
|
|
210
|
+
return { matched: this.matchNode(this.tree), record: this.record }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static matchSegments(source: Segments, target: Segments, record?: any): { matched: boolean; record?: IRecord } {
|
|
214
|
+
if (source.length !== target.length) return { matched: false, record }
|
|
215
|
+
function match(pos = 0): boolean {
|
|
216
|
+
const current = isSegmentEqual(source[pos], target[pos])
|
|
217
|
+
if (record?.score >= 0) {
|
|
218
|
+
record.score++
|
|
219
|
+
}
|
|
220
|
+
return current && (pos < source.length - 1 ? match(pos + 1) : true)
|
|
221
|
+
}
|
|
222
|
+
return { matched: match(), record }
|
|
223
|
+
}
|
|
224
|
+
}
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { bracketArrayContext, destructorContext } from './contexts'
|
|
2
|
+
import { parseDestructorRules, setDestructor } from './destructor'
|
|
3
|
+
import type { Path } from './index'
|
|
4
|
+
import { isNumberLike } from './shared'
|
|
5
|
+
import { Tokenizer } from './tokenizer'
|
|
6
|
+
import type { Token } from './tokens'
|
|
7
|
+
import {
|
|
8
|
+
bangTok,
|
|
9
|
+
braceLTok,
|
|
10
|
+
braceRTok,
|
|
11
|
+
bracketDLTok,
|
|
12
|
+
bracketLTok,
|
|
13
|
+
bracketRTok,
|
|
14
|
+
colonTok,
|
|
15
|
+
commaTok,
|
|
16
|
+
dbStarTok,
|
|
17
|
+
dotTok,
|
|
18
|
+
eofTok,
|
|
19
|
+
expandTok,
|
|
20
|
+
nameTok,
|
|
21
|
+
parenLTok,
|
|
22
|
+
parenRTok,
|
|
23
|
+
starTok,
|
|
24
|
+
} from './tokens'
|
|
25
|
+
import type {
|
|
26
|
+
ArrayPatternNode,
|
|
27
|
+
DestructorExpressionNode,
|
|
28
|
+
DotOperatorNode,
|
|
29
|
+
ExpandOperatorNode,
|
|
30
|
+
GroupExpressionNode,
|
|
31
|
+
IdentifierNode,
|
|
32
|
+
IgnoreExpressionNode,
|
|
33
|
+
Node,
|
|
34
|
+
ObjectPatternNode,
|
|
35
|
+
ObjectPatternPropertyNode,
|
|
36
|
+
RangeExpressionNode,
|
|
37
|
+
Segments,
|
|
38
|
+
WildcardOperatorNode,
|
|
39
|
+
} from './types'
|
|
40
|
+
|
|
41
|
+
function createTreeBySegments(segments: Segments = [], afterNode?: Node) {
|
|
42
|
+
const segLen = segments.length
|
|
43
|
+
function build(start = 0): Node {
|
|
44
|
+
const after: Node | undefined = start < segLen - 1 ? build(start + 1) : afterNode
|
|
45
|
+
const dot: Node | undefined = after && {
|
|
46
|
+
type: 'DotOperator',
|
|
47
|
+
after,
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
type: 'Identifier',
|
|
51
|
+
value: segments[start] as string,
|
|
52
|
+
after: dot,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return build()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function calculate(a: string | number, b: string | number, operator: string) {
|
|
59
|
+
if (isNumberLike(a) && isNumberLike(b)) {
|
|
60
|
+
if (operator === '+') return String(Number(a) + Number(b))
|
|
61
|
+
if (operator === '-') return String(Number(a) - Number(b))
|
|
62
|
+
if (operator === '*') return String(Number(a) * Number(b))
|
|
63
|
+
if (operator === '/') return String(Number(a) / Number(b))
|
|
64
|
+
} else {
|
|
65
|
+
if (operator === '+') return String(a) + String(b)
|
|
66
|
+
if (operator === '-') return 'NaN'
|
|
67
|
+
if (operator === '*') return 'NaN'
|
|
68
|
+
if (operator === '/') return 'NaN'
|
|
69
|
+
}
|
|
70
|
+
return String(Number(b))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class Parser extends Tokenizer {
|
|
74
|
+
public isMatchPattern = false
|
|
75
|
+
|
|
76
|
+
public isWildMatchPattern = false
|
|
77
|
+
|
|
78
|
+
public haveExcludePattern = false
|
|
79
|
+
|
|
80
|
+
public haveRelativePattern = false
|
|
81
|
+
|
|
82
|
+
public base?: Path
|
|
83
|
+
|
|
84
|
+
public relative?: string | number
|
|
85
|
+
|
|
86
|
+
public data!: {
|
|
87
|
+
segments: Segments
|
|
88
|
+
tree?: Node
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
constructor(input: string, base?: Path) {
|
|
92
|
+
super(input)
|
|
93
|
+
this.base = base
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
parse() {
|
|
97
|
+
let node: Node | undefined
|
|
98
|
+
this.data = {
|
|
99
|
+
segments: [],
|
|
100
|
+
}
|
|
101
|
+
if (!this.eat(eofTok)) {
|
|
102
|
+
this.next()
|
|
103
|
+
node = this.parseAtom(this.state.type)
|
|
104
|
+
}
|
|
105
|
+
this.data.tree = node
|
|
106
|
+
|
|
107
|
+
return node
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
append(parent: Node, node?: Node) {
|
|
111
|
+
if (parent && node) {
|
|
112
|
+
parent.after = node
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
parseAtom(type?: Token): Node | undefined {
|
|
117
|
+
switch (type) {
|
|
118
|
+
case braceLTok:
|
|
119
|
+
case bracketLTok:
|
|
120
|
+
if (this.includesContext(destructorContext)) {
|
|
121
|
+
if (type === braceLTok) {
|
|
122
|
+
return this.parseObjectPattern()
|
|
123
|
+
} else {
|
|
124
|
+
return this.parseArrayPattern()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return this.parseDestructorExpression()
|
|
128
|
+
case nameTok:
|
|
129
|
+
return this.parseIdentifier()
|
|
130
|
+
case expandTok:
|
|
131
|
+
return this.parseExpandOperator()
|
|
132
|
+
case dbStarTok:
|
|
133
|
+
case starTok:
|
|
134
|
+
return this.parseWildcardOperator()
|
|
135
|
+
case bracketDLTok:
|
|
136
|
+
return this.parseIgnoreExpression()
|
|
137
|
+
case dotTok:
|
|
138
|
+
return this.parseDotOperator()
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pushSegments(key: string | number) {
|
|
143
|
+
this.data.segments.push(key)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
parseIdentifier() {
|
|
147
|
+
const node: IdentifierNode = {
|
|
148
|
+
type: 'Identifier',
|
|
149
|
+
value: this.state.value,
|
|
150
|
+
}
|
|
151
|
+
const hasNotInDestructor =
|
|
152
|
+
!this.includesContext(destructorContext) && !this.isMatchPattern && !this.isWildMatchPattern
|
|
153
|
+
|
|
154
|
+
this.next()
|
|
155
|
+
if (this.includesContext(bracketArrayContext)) {
|
|
156
|
+
if (this.state.type !== bracketRTok) {
|
|
157
|
+
throw this.unExpect()
|
|
158
|
+
} else {
|
|
159
|
+
this.state.context.pop()
|
|
160
|
+
this.next()
|
|
161
|
+
}
|
|
162
|
+
} else if (hasNotInDestructor) {
|
|
163
|
+
this.pushSegments(node.value)
|
|
164
|
+
}
|
|
165
|
+
if (this.state.type === bracketLTok) {
|
|
166
|
+
this.next()
|
|
167
|
+
if (this.state.type !== nameTok) {
|
|
168
|
+
throw this.unExpect()
|
|
169
|
+
}
|
|
170
|
+
this.state.context.push(bracketArrayContext)
|
|
171
|
+
let isNumberKey = false
|
|
172
|
+
if (/^\d+$/.test(this.state.value)) {
|
|
173
|
+
isNumberKey = true
|
|
174
|
+
}
|
|
175
|
+
const value = this.state.value
|
|
176
|
+
this.pushSegments(isNumberKey ? Number(value) : value)
|
|
177
|
+
const after = this.parseAtom(this.state.type) as IdentifierNode
|
|
178
|
+
if (isNumberKey) {
|
|
179
|
+
after.arrayIndex = true
|
|
180
|
+
}
|
|
181
|
+
this.append(node, after)
|
|
182
|
+
} else {
|
|
183
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return node
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
parseExpandOperator() {
|
|
190
|
+
const node: ExpandOperatorNode = {
|
|
191
|
+
type: 'ExpandOperator',
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.isMatchPattern = true
|
|
195
|
+
this.isWildMatchPattern = true
|
|
196
|
+
this.data.segments = []
|
|
197
|
+
|
|
198
|
+
this.next()
|
|
199
|
+
|
|
200
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
201
|
+
|
|
202
|
+
return node
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
parseWildcardOperator(): WildcardOperatorNode {
|
|
206
|
+
const node: WildcardOperatorNode = {
|
|
207
|
+
type: 'WildcardOperator',
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (this.state.type === dbStarTok) {
|
|
211
|
+
node.optional = true
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.isMatchPattern = true
|
|
215
|
+
this.isWildMatchPattern = true
|
|
216
|
+
this.data.segments = []
|
|
217
|
+
|
|
218
|
+
this.next()
|
|
219
|
+
|
|
220
|
+
if (this.state.type === parenLTok) {
|
|
221
|
+
node.filter = this.parseGroupExpression(node)
|
|
222
|
+
} else if (this.state.type === bracketLTok) {
|
|
223
|
+
node.filter = this.parseRangeExpression(node)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
227
|
+
|
|
228
|
+
return node
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
parseDestructorExpression(): DestructorExpressionNode {
|
|
232
|
+
const node: DestructorExpressionNode = {
|
|
233
|
+
type: 'DestructorExpression',
|
|
234
|
+
}
|
|
235
|
+
this.state.context.push(destructorContext)
|
|
236
|
+
const startPos = this.state.pos - 1
|
|
237
|
+
node.value = this.state.type === braceLTok ? this.parseObjectPattern() : this.parseArrayPattern()
|
|
238
|
+
const endPos = this.state.pos
|
|
239
|
+
this.state.context.pop()
|
|
240
|
+
node.source = this.input
|
|
241
|
+
.substring(startPos, endPos)
|
|
242
|
+
.replace(/\[\s*([\\+\-\\*\\/])?\s*([^,\]\s]*)\s*\]/, (match, operator, target) => {
|
|
243
|
+
if (this.relative !== undefined) {
|
|
244
|
+
if (operator) {
|
|
245
|
+
if (target) {
|
|
246
|
+
return calculate(this.relative, target, operator)
|
|
247
|
+
} else {
|
|
248
|
+
return calculate(this.relative, 1, operator)
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
if (target) {
|
|
252
|
+
return calculate(this.relative, target, '+')
|
|
253
|
+
} else {
|
|
254
|
+
return String(this.relative)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return match
|
|
259
|
+
})
|
|
260
|
+
.replace(/\s*\.\s*/g, '')
|
|
261
|
+
.replace(/\s*/g, '')
|
|
262
|
+
if (this.relative === undefined) {
|
|
263
|
+
setDestructor(node.source, parseDestructorRules(node))
|
|
264
|
+
}
|
|
265
|
+
this.relative = undefined
|
|
266
|
+
this.pushSegments(node.source)
|
|
267
|
+
this.next()
|
|
268
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
269
|
+
return node
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
parseArrayPattern(): ArrayPatternNode {
|
|
273
|
+
const node: ArrayPatternNode = {
|
|
274
|
+
type: 'ArrayPattern',
|
|
275
|
+
elements: [],
|
|
276
|
+
}
|
|
277
|
+
this.next()
|
|
278
|
+
node.elements = this.parseArrayPatternElements() as ObjectPatternNode[] | ArrayPatternNode[] | IdentifierNode[]
|
|
279
|
+
return node
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
parseArrayPatternElements() {
|
|
283
|
+
const nodes: Node[] = []
|
|
284
|
+
while (this.state.type !== bracketRTok && this.state.type !== eofTok) {
|
|
285
|
+
nodes.push(this.parseAtom(this.state.type)!)
|
|
286
|
+
if (this.state.type === bracketRTok) {
|
|
287
|
+
if (this.includesContext(destructorContext)) {
|
|
288
|
+
this.next()
|
|
289
|
+
}
|
|
290
|
+
return nodes
|
|
291
|
+
}
|
|
292
|
+
this.next()
|
|
293
|
+
}
|
|
294
|
+
return nodes
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
parseObjectPattern(): ObjectPatternNode {
|
|
298
|
+
const node: ObjectPatternNode = {
|
|
299
|
+
type: 'ObjectPattern',
|
|
300
|
+
properties: [],
|
|
301
|
+
}
|
|
302
|
+
this.next()
|
|
303
|
+
node.properties = this.parseObjectProperties()
|
|
304
|
+
return node
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
parseObjectProperties(): ObjectPatternPropertyNode[] {
|
|
308
|
+
const nodes: ObjectPatternPropertyNode[] = []
|
|
309
|
+
while (this.state.type !== braceRTok && this.state.type !== eofTok) {
|
|
310
|
+
const node: ObjectPatternPropertyNode = {
|
|
311
|
+
type: 'ObjectPatternProperty',
|
|
312
|
+
key: this.parseAtom(this.state.type) as IdentifierNode,
|
|
313
|
+
}
|
|
314
|
+
nodes.push(node)
|
|
315
|
+
if (this.state.type === colonTok) {
|
|
316
|
+
this.next()
|
|
317
|
+
node.value = this.parseAtom(this.state.type) as IdentifierNode | ObjectPatternNode[] | ArrayPatternNode[]
|
|
318
|
+
}
|
|
319
|
+
if (this.state.type === braceRTok) {
|
|
320
|
+
if (this.includesContext(destructorContext)) {
|
|
321
|
+
this.next()
|
|
322
|
+
}
|
|
323
|
+
return nodes
|
|
324
|
+
}
|
|
325
|
+
this.next()
|
|
326
|
+
}
|
|
327
|
+
return nodes
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
parseDotOperator(): Node {
|
|
331
|
+
const node: DotOperatorNode = {
|
|
332
|
+
type: 'DotOperator',
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const prevToken = this.type_
|
|
336
|
+
if (!prevToken && this.base) {
|
|
337
|
+
if (this.base.isMatchPattern) {
|
|
338
|
+
throw new Error('Base path must be an absolute path.')
|
|
339
|
+
}
|
|
340
|
+
this.data.segments = this.base.toArr()
|
|
341
|
+
while (this.state.type === dotTok) {
|
|
342
|
+
this.relative = this.data.segments.pop()
|
|
343
|
+
this.haveRelativePattern = true
|
|
344
|
+
this.next()
|
|
345
|
+
}
|
|
346
|
+
return createTreeBySegments(this.data.segments.slice(), this.parseAtom(this.state.type))
|
|
347
|
+
} else {
|
|
348
|
+
this.next()
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
352
|
+
|
|
353
|
+
return node
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
parseIgnoreExpression() {
|
|
357
|
+
this.next()
|
|
358
|
+
|
|
359
|
+
const value = String(this.state.value).replace(/\s*/g, '')
|
|
360
|
+
|
|
361
|
+
const node: IgnoreExpressionNode = {
|
|
362
|
+
type: 'IgnoreExpression',
|
|
363
|
+
value: value,
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
this.pushSegments(value)
|
|
367
|
+
|
|
368
|
+
this.next()
|
|
369
|
+
|
|
370
|
+
this.append(node, this.parseAtom(this.state.type))
|
|
371
|
+
|
|
372
|
+
this.next()
|
|
373
|
+
|
|
374
|
+
return node
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
parseGroupExpression(parent: Node) {
|
|
378
|
+
const node: GroupExpressionNode = {
|
|
379
|
+
type: 'GroupExpression',
|
|
380
|
+
value: [],
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
this.isMatchPattern = true
|
|
384
|
+
this.data.segments = []
|
|
385
|
+
|
|
386
|
+
this.next()
|
|
387
|
+
|
|
388
|
+
loop: while (true) {
|
|
389
|
+
switch (this.state.type) {
|
|
390
|
+
case commaTok:
|
|
391
|
+
this.next()
|
|
392
|
+
break
|
|
393
|
+
case bangTok:
|
|
394
|
+
node.isExclude = true
|
|
395
|
+
this.haveExcludePattern = true
|
|
396
|
+
this.next()
|
|
397
|
+
break
|
|
398
|
+
case eofTok:
|
|
399
|
+
break loop
|
|
400
|
+
case parenRTok:
|
|
401
|
+
break loop
|
|
402
|
+
default:
|
|
403
|
+
node.value.push(this.parseAtom(this.state.type)!)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
this.next()
|
|
408
|
+
|
|
409
|
+
this.append(parent, this.parseAtom(this.state.type))
|
|
410
|
+
|
|
411
|
+
return node
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
parseRangeExpression(parent: Node) {
|
|
415
|
+
const node: RangeExpressionNode = {
|
|
416
|
+
type: 'RangeExpression',
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
this.next()
|
|
420
|
+
|
|
421
|
+
this.isMatchPattern = true
|
|
422
|
+
this.data.segments = []
|
|
423
|
+
|
|
424
|
+
let start = false,
|
|
425
|
+
hasColon = false
|
|
426
|
+
|
|
427
|
+
loop: while (true) {
|
|
428
|
+
switch (this.state.type) {
|
|
429
|
+
case colonTok:
|
|
430
|
+
hasColon = true
|
|
431
|
+
start = true
|
|
432
|
+
this.next()
|
|
433
|
+
break
|
|
434
|
+
case bracketRTok:
|
|
435
|
+
if (!hasColon && !node.end) {
|
|
436
|
+
node.end = node.start
|
|
437
|
+
}
|
|
438
|
+
break loop
|
|
439
|
+
case commaTok:
|
|
440
|
+
// never reach
|
|
441
|
+
throw this.unExpect()
|
|
442
|
+
case eofTok:
|
|
443
|
+
// never reach
|
|
444
|
+
break loop
|
|
445
|
+
default:
|
|
446
|
+
if (!start) {
|
|
447
|
+
node.start = this.parseAtom(this.state.type) as IdentifierNode
|
|
448
|
+
} else {
|
|
449
|
+
node.end = this.parseAtom(this.state.type) as IdentifierNode
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
this.next()
|
|
455
|
+
|
|
456
|
+
this.append(parent, this.parseAtom(this.state.type))
|
|
457
|
+
|
|
458
|
+
return node
|
|
459
|
+
}
|
|
460
|
+
}
|