@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/src/shared.ts ADDED
@@ -0,0 +1,83 @@
1
+ const toString = Object.prototype.toString
2
+ function isType<T>(type: string) {
3
+ return (obj: unknown): obj is T => toString.call(obj) === `[object ${type}]`
4
+ }
5
+ export const isFn = isType<(...args: any[]) => any>('Function')
6
+ export const isArr = Array.isArray || isType<unknown[]>('Array')
7
+ export const isPlainObj = isType<object>('Object')
8
+ export const isStr = isType<string>('String')
9
+ export const isBool = isType<boolean>('Boolean')
10
+ export const isNum = isType<number>('Number')
11
+ export function isObj(val: unknown): val is object {
12
+ return typeof val === 'object'
13
+ }
14
+ export const isRegExp = isType<RegExp>('RegExp')
15
+ export function isNumberLike(t: any) {
16
+ return isNum(t) || /^(\d+)(\.\d+)?$/.test(t)
17
+ }
18
+ const isArray = isArr
19
+ const keyList = Object.keys
20
+ const hasProp = Object.prototype.hasOwnProperty
21
+
22
+ export function toArr<T>(val: T | T[]): T[] {
23
+ return Array.isArray(val) ? val : val !== undefined ? [val] : []
24
+ }
25
+ export function isAssignable(val: any) {
26
+ return typeof val === 'object' || typeof val === 'function'
27
+ }
28
+ export function isEqual(a: any, b: any) {
29
+ if (a === b) {
30
+ return true
31
+ }
32
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
33
+ const arrA = isArray(a)
34
+ const arrB = isArray(b)
35
+ let i
36
+ let length
37
+ let key
38
+
39
+ if (arrA && arrB) {
40
+ length = a.length
41
+ if (length !== b.length) {
42
+ return false
43
+ }
44
+ for (i = length; i-- !== 0;) {
45
+ if (!isEqual(a[i], b[i])) {
46
+ return false
47
+ }
48
+ }
49
+ return true
50
+ }
51
+
52
+ if (arrA !== arrB) {
53
+ return false
54
+ }
55
+
56
+ const keys = keyList(a)
57
+ length = keys.length
58
+
59
+ if (length !== keyList(b).length) {
60
+ return false
61
+ }
62
+
63
+ for (i = length; i-- !== 0;) {
64
+ if (!hasProp.call(b, keys[i])) {
65
+ return false
66
+ }
67
+ }
68
+ for (i = length; i-- !== 0;) {
69
+ key = keys[i]
70
+ if (!isEqual(a[key], b[key])) {
71
+ return false
72
+ }
73
+ }
74
+
75
+ return true
76
+ }
77
+ return a !== a && b !== b
78
+ }
79
+ export function isSegmentEqual(a: any, b: any) {
80
+ a = typeof a === 'symbol' ? a : `${a}`
81
+ b = typeof b === 'symbol' ? b : `${b}`
82
+ return a === b
83
+ }
@@ -0,0 +1,306 @@
1
+ import type { Context } from './contexts'
2
+ import { bracketDContext } from './contexts'
3
+ import type { Token } from './tokens'
4
+ import {
5
+ bangTok,
6
+ braceLTok,
7
+ braceRTok,
8
+ bracketDLTok,
9
+ bracketDRTok,
10
+ bracketLTok,
11
+ bracketRTok,
12
+ colonTok,
13
+ commaTok,
14
+ dbStarTok,
15
+ dotTok,
16
+ eofTok,
17
+ expandTok,
18
+ ignoreTok,
19
+ nameTok,
20
+ parenLTok,
21
+ parenRTok,
22
+ starTok,
23
+ } from './tokens'
24
+
25
+ const nonASCIIWhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/
26
+
27
+ function fullCharCodeAtPos(input: string, pos: number) {
28
+ return input.codePointAt(pos)
29
+ }
30
+
31
+ function isRewordCode(code: number) {
32
+ return (
33
+ code === 42 ||
34
+ code === 46 ||
35
+ code === 33 ||
36
+ code === 91 ||
37
+ code === 93 ||
38
+ code === 40 ||
39
+ code === 41 ||
40
+ code === 44 ||
41
+ code === 58 ||
42
+ code === 126 ||
43
+ code === 123 ||
44
+ code === 125
45
+ )
46
+ }
47
+
48
+ function getError(message?: string, props?: any) {
49
+ const err = new Error(message)
50
+ Object.assign(err, props)
51
+ return err
52
+ }
53
+
54
+ function slice(string: string, start: number, end: number) {
55
+ let str = ''
56
+ for (let i = start; i < end; i++) {
57
+ const ch = string.charAt(i)
58
+ if (ch !== '\\') {
59
+ str += ch
60
+ }
61
+ }
62
+ return str
63
+ }
64
+
65
+ export class Tokenizer {
66
+ public input: string
67
+ public state: {
68
+ context: Context[]
69
+ type?: Token
70
+ pos: number
71
+ value?: any
72
+ }
73
+ public type_?: Token
74
+ constructor(input: string) {
75
+ this.input = input
76
+ this.state = {
77
+ context: [],
78
+ pos: 0,
79
+ }
80
+ }
81
+
82
+ curContext() {
83
+ return this.state.context[this.state.context.length - 1]
84
+ }
85
+
86
+ includesContext(context: Context) {
87
+ for (let len = this.state.context.length - 1; len >= 0; len--) {
88
+ if (this.state.context[len] === context) {
89
+ return true
90
+ }
91
+ }
92
+ return false
93
+ }
94
+
95
+ unExpect(type?: Token) {
96
+ type = type || (this.state.type as Token)
97
+ return getError(`UnExpect token "${type.flag}" in ${this.state.pos} char.`, {
98
+ pos: this.state.pos,
99
+ })
100
+ }
101
+
102
+ expectNext(type?: Token, next?: Token) {
103
+ if (type && type.expectNext) {
104
+ if (next && !type.expectNext.call(this, next)) {
105
+ throw getError(
106
+ `UnExpect token "${next.flag}" token should not be behind "${type.flag}" token.(${this.state.pos}th char)`,
107
+ {
108
+ pos: this.state.pos,
109
+ }
110
+ )
111
+ }
112
+ }
113
+ }
114
+
115
+ expectPrev(type?: Token, prev?: Token) {
116
+ if (type && type.expectPrev) {
117
+ if (prev && !type.expectPrev.call(this, prev)) {
118
+ throw getError(`Unexpect token "${type.flag}" should not be behind "${prev.flag}"(${this.state.pos}th char).`, {
119
+ pos: this.state.pos,
120
+ })
121
+ }
122
+ }
123
+ }
124
+
125
+ match(type?: Token) {
126
+ return this.state.type === type
127
+ }
128
+
129
+ skipSpace() {
130
+ if (this.curContext() === bracketDContext) return
131
+ loop: while (this.state.pos < this.input.length) {
132
+ const ch = this.input.charCodeAt(this.state.pos)
133
+ switch (ch) {
134
+ case 32:
135
+ case 160:
136
+ ++this.state.pos
137
+ break
138
+
139
+ case 13:
140
+ if (this.input.charCodeAt(this.state.pos + 1) === 10) {
141
+ ++this.state.pos
142
+ }
143
+
144
+ // eslint-disable-next-line no-fallthrough
145
+ case 10:
146
+ case 8232:
147
+ case 8233:
148
+ ++this.state.pos
149
+ break
150
+ default:
151
+ if ((ch > 8 && ch < 14) || (ch >= 5760 && nonASCIIWhitespace.test(String.fromCharCode(ch)))) {
152
+ ++this.state.pos
153
+ } else {
154
+ break loop
155
+ }
156
+ }
157
+ }
158
+ }
159
+
160
+ next() {
161
+ this.type_ = this.state.type
162
+ if (this.input.length <= this.state.pos) {
163
+ return this.finishToken(eofTok)
164
+ }
165
+ this.skipSpace()
166
+ this.readToken(this.getCode(), this.state.pos > 0 ? this.getCode(this.state.pos - 1) : -Infinity)
167
+ }
168
+
169
+ getCode(pos = this.state.pos) {
170
+ return fullCharCodeAtPos(this.input, pos)!
171
+ }
172
+
173
+ eat(type?: Token) {
174
+ if (this.match(type)) {
175
+ this.next()
176
+ return true
177
+ } else {
178
+ return false
179
+ }
180
+ }
181
+
182
+ readKeyWord() {
183
+ const startPos = this.state.pos
184
+ let string = ''
185
+ while (true) {
186
+ const code = this.getCode()!
187
+ const prevCode = this.getCode(this.state.pos - 1)
188
+ if (this.input.length === this.state.pos) {
189
+ string = slice(this.input, startPos, this.state.pos + 1)
190
+ break
191
+ }
192
+ if (!isRewordCode(code) || prevCode === 92) {
193
+ if (code === 32 || code === 160 || code === 10 || code === 8232 || code === 8233) {
194
+ string = slice(this.input, startPos, this.state.pos)
195
+ break
196
+ }
197
+ if (code === 13 && this.input.charCodeAt(this.state.pos + 1) === 10) {
198
+ string = slice(this.input, startPos, this.state.pos)
199
+ break
200
+ }
201
+ if ((code > 8 && code < 14) || (code >= 5760 && nonASCIIWhitespace.test(String.fromCharCode(code)))) {
202
+ string = slice(this.input, startPos, this.state.pos)
203
+ break
204
+ }
205
+ this.state.pos++
206
+ } else {
207
+ string = slice(this.input, startPos, this.state.pos)
208
+ break
209
+ }
210
+ }
211
+
212
+ this.finishToken(nameTok, string)
213
+ }
214
+
215
+ readIgnoreString() {
216
+ const startPos = this.state.pos
217
+ let prevCode: number | undefined
218
+ let string = ''
219
+ while (true) {
220
+ const code = this.getCode()
221
+ if (this.state.pos >= this.input.length) break
222
+ if ((code === 91 || code === 93) && prevCode === 92) {
223
+ this.state.pos++
224
+ prevCode = undefined
225
+ } else if (code == 93 && prevCode === 93) {
226
+ string = this.input.slice(startPos, this.state.pos - 1).replace(/\\([\\[\]])/g, '$1')
227
+ this.state.pos++
228
+ break
229
+ } else {
230
+ this.state.pos++
231
+ prevCode = code
232
+ }
233
+ }
234
+
235
+ this.finishToken(ignoreTok, string)
236
+ this.finishToken(bracketDRTok)
237
+ }
238
+
239
+ finishToken(type: Token, value?: any) {
240
+ const preType = this.state.type
241
+ this.state.type = type
242
+ if (value !== undefined) this.state.value = value
243
+ this.expectNext(preType, type)
244
+ this.expectPrev(type, preType)
245
+ if (type.updateContext) {
246
+ type.updateContext.call(this, preType)
247
+ }
248
+ }
249
+
250
+ readToken(code: number | undefined, prevCode: number) {
251
+ if (prevCode === 92) {
252
+ return this.readKeyWord()
253
+ }
254
+ if (this.input.length <= this.state.pos) {
255
+ this.finishToken(eofTok)
256
+ } else if (this.curContext() === bracketDContext) {
257
+ this.readIgnoreString()
258
+ } else if (code === 123) {
259
+ this.state.pos++
260
+ this.finishToken(braceLTok)
261
+ } else if (code === 125) {
262
+ this.state.pos++
263
+ this.finishToken(braceRTok)
264
+ } else if (code === 42) {
265
+ this.state.pos++
266
+ if (this.getCode() === 42) {
267
+ this.state.pos++
268
+ return this.finishToken(dbStarTok)
269
+ }
270
+ this.finishToken(starTok)
271
+ } else if (code === 33) {
272
+ this.state.pos++
273
+ this.finishToken(bangTok)
274
+ } else if (code === 46) {
275
+ this.state.pos++
276
+ this.finishToken(dotTok)
277
+ } else if (code === 91) {
278
+ this.state.pos++
279
+ if (this.getCode() === 91) {
280
+ this.state.pos++
281
+ return this.finishToken(bracketDLTok)
282
+ }
283
+ this.finishToken(bracketLTok)
284
+ } else if (code === 126) {
285
+ this.state.pos++
286
+ this.finishToken(expandTok)
287
+ } else if (code === 93) {
288
+ this.state.pos++
289
+ this.finishToken(bracketRTok)
290
+ } else if (code === 40) {
291
+ this.state.pos++
292
+ this.finishToken(parenLTok)
293
+ } else if (code === 41) {
294
+ this.state.pos++
295
+ this.finishToken(parenRTok)
296
+ } else if (code === 44) {
297
+ this.state.pos++
298
+ this.finishToken(commaTok)
299
+ } else if (code === 58) {
300
+ this.state.pos++
301
+ this.finishToken(colonTok)
302
+ } else {
303
+ this.readKeyWord()
304
+ }
305
+ }
306
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,233 @@
1
+ import {
2
+ braceContext,
3
+ bracketArrayContext,
4
+ bracketContext,
5
+ bracketDContext,
6
+ destructorContext,
7
+ parenContext,
8
+ } from './contexts'
9
+ import type { Tokenizer } from './tokenizer'
10
+
11
+ interface ITokenProps {
12
+ expectNext?: (this: Tokenizer, next?: Token) => boolean
13
+ expectPrev?: (this: Tokenizer, prev?: Token) => boolean
14
+ updateContext?: (this: Tokenizer, prev?: Token) => void
15
+ }
16
+
17
+ export type Token = ITokenProps & {
18
+ flag: string
19
+ }
20
+
21
+ function TokenType(flag: string, props?: ITokenProps): Token {
22
+ return {
23
+ flag,
24
+ ...props,
25
+ }
26
+ }
27
+
28
+ export const nameTok = TokenType('name', {
29
+ expectNext(next) {
30
+ if (this.includesContext(destructorContext)) {
31
+ return next === nameTok || next === commaTok || next === bracketRTok || next === braceRTok || next === colonTok
32
+ }
33
+ return (
34
+ next === dotTok ||
35
+ next === commaTok ||
36
+ next === eofTok ||
37
+ next === bracketRTok ||
38
+ next === parenRTok ||
39
+ next === colonTok ||
40
+ next === expandTok ||
41
+ next === bracketLTok
42
+ )
43
+ },
44
+ })
45
+ export const starTok = TokenType('*', {
46
+ expectNext(next) {
47
+ return (
48
+ next === dotTok ||
49
+ next === parenLTok ||
50
+ next === bracketLTok ||
51
+ next === eofTok ||
52
+ next === commaTok ||
53
+ next === parenRTok
54
+ )
55
+ },
56
+ })
57
+ export const dbStarTok = TokenType('**', {
58
+ expectNext(next) {
59
+ return next === dotTok || next === bracketLTok || next === eofTok || next === commaTok || next === parenRTok
60
+ },
61
+ })
62
+ export const dotTok = TokenType('.', {
63
+ expectNext(next) {
64
+ return (
65
+ next === dotTok ||
66
+ next === nameTok ||
67
+ next === bracketDLTok ||
68
+ next === starTok ||
69
+ next === dbStarTok ||
70
+ next === bracketLTok ||
71
+ next === braceLTok ||
72
+ next === eofTok
73
+ )
74
+ },
75
+ expectPrev(prev) {
76
+ return (
77
+ prev === dotTok ||
78
+ prev === nameTok ||
79
+ prev === bracketDRTok ||
80
+ prev === starTok ||
81
+ prev === parenRTok ||
82
+ prev === bracketRTok ||
83
+ prev === expandTok ||
84
+ prev === braceRTok
85
+ )
86
+ },
87
+ })
88
+ export const bangTok = TokenType('!', {
89
+ expectNext(next) {
90
+ return next === nameTok || next === bracketDLTok
91
+ },
92
+ })
93
+ export const colonTok = TokenType(':', {
94
+ expectNext(next) {
95
+ if (this.includesContext(destructorContext)) {
96
+ return next === nameTok || next === braceLTok || next === bracketLTok
97
+ }
98
+ return next === nameTok || next === bracketDLTok || next === bracketRTok
99
+ },
100
+ })
101
+
102
+ export const braceLTok = TokenType('{', {
103
+ expectNext(next) {
104
+ return next === nameTok
105
+ },
106
+ expectPrev(prev) {
107
+ if (this.includesContext(destructorContext)) {
108
+ return prev === colonTok || prev === commaTok || prev === bracketLTok
109
+ }
110
+ return prev === dotTok || prev === colonTok || prev === parenLTok
111
+ },
112
+ updateContext() {
113
+ this.state.context.push(braceContext)
114
+ },
115
+ })
116
+
117
+ export const braceRTok = TokenType('}', {
118
+ expectNext(next) {
119
+ if (this.includesContext(destructorContext)) {
120
+ return next === commaTok || next === braceRTok || next === eofTok || next === bracketRTok
121
+ }
122
+ return next === dotTok || next === eofTok || next === commaTok
123
+ },
124
+ expectPrev(prev) {
125
+ return prev === nameTok || prev === braceRTok || prev === bracketRTok
126
+ },
127
+ updateContext() {
128
+ this.state.context.pop()
129
+ },
130
+ })
131
+
132
+ export const bracketLTok = TokenType('[', {
133
+ expectNext(next) {
134
+ if (this.includesContext(destructorContext)) {
135
+ return next === nameTok || next === bracketLTok || next === braceLTok || next === bracketRTok
136
+ }
137
+ return (
138
+ next === nameTok ||
139
+ next === bracketDLTok ||
140
+ next === colonTok ||
141
+ next === bracketLTok ||
142
+ next === ignoreTok ||
143
+ next === bracketRTok
144
+ )
145
+ },
146
+ expectPrev(prev) {
147
+ if (this.includesContext(destructorContext)) {
148
+ return prev === colonTok || prev === commaTok || prev === bracketLTok
149
+ }
150
+ return (
151
+ prev === starTok ||
152
+ prev === bracketLTok ||
153
+ prev === dotTok ||
154
+ prev === nameTok ||
155
+ prev === parenLTok ||
156
+ // never reach
157
+ prev == commaTok
158
+ )
159
+ },
160
+ updateContext() {
161
+ this.state.context.push(bracketContext)
162
+ },
163
+ })
164
+
165
+ export const bracketRTok = TokenType(']', {
166
+ expectNext(next) {
167
+ if (this.includesContext(destructorContext)) {
168
+ return next === commaTok || next === braceRTok || next === bracketRTok || next === eofTok
169
+ }
170
+ return next === dotTok || next === eofTok || next === commaTok || next === parenRTok || next === bracketRTok
171
+ },
172
+ updateContext() {
173
+ if (this.includesContext(bracketArrayContext)) return
174
+ if (!this.includesContext(bracketContext)) throw this.unExpect()
175
+ this.state.context.pop()
176
+ },
177
+ })
178
+
179
+ export const bracketDLTok = TokenType('[[', {
180
+ updateContext() {
181
+ this.state.context.push(bracketDContext)
182
+ },
183
+ })
184
+
185
+ export const bracketDRTok = TokenType(']]', {
186
+ updateContext() {
187
+ if (this.curContext() !== bracketDContext) throw this.unExpect()
188
+ this.state.context.pop()
189
+ },
190
+ })
191
+
192
+ export const parenLTok = TokenType('(', {
193
+ expectNext(next) {
194
+ return next === nameTok || next === bracketDLTok || next === braceLTok || next === bangTok || next === bracketLTok
195
+ },
196
+ expectPrev(prev) {
197
+ return prev === starTok
198
+ },
199
+ updateContext() {
200
+ this.state.context.push(parenContext)
201
+ },
202
+ })
203
+ export const parenRTok = TokenType(')', {
204
+ expectNext(next) {
205
+ return next === dotTok || next === eofTok || next === commaTok || next === parenRTok
206
+ },
207
+ updateContext() {
208
+ if (this.curContext() !== parenContext) throw this.unExpect()
209
+ this.state.context.pop()
210
+ },
211
+ })
212
+
213
+ export const commaTok = TokenType(',', {
214
+ expectNext(next) {
215
+ return next === nameTok || next === bracketDLTok || next === bracketLTok || next === braceLTok
216
+ },
217
+ })
218
+ export const ignoreTok = TokenType('ignore', {
219
+ expectNext(next) {
220
+ return next === bracketDRTok
221
+ },
222
+ expectPrev(prev) {
223
+ return prev == bracketDLTok
224
+ },
225
+ })
226
+
227
+ export const expandTok = TokenType('expandTok', {
228
+ expectNext(next) {
229
+ return next === dotTok || next === eofTok || next === commaTok || next === parenRTok
230
+ },
231
+ })
232
+
233
+ export const eofTok = TokenType('eof')