-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathscanner.ts
More file actions
193 lines (164 loc) · 4.93 KB
/
Copy pathscanner.ts
File metadata and controls
193 lines (164 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { KeySet } from "./records.ts"
import { Backslash, whitespaceChars, type WhitespaceChar } from "./strings.ts"
export class Scanner<lookahead extends string = string> {
chars: string[]
i: number
def: string
constructor(def: string) {
this.def = def
this.chars = [...def]
this.i = 0
}
/** Get lookahead and advance scanner by one */
shift(): this["lookahead"] {
return (this.chars[this.i++] ?? "") as never
}
get lookahead(): lookahead {
return (this.chars[this.i] ?? "") as never
}
get nextLookahead(): string {
return this.chars[this.i + 1] ?? ""
}
get length(): number {
return this.chars.length
}
shiftUntil(condition: Scanner.UntilCondition): string {
let shifted = ""
while (this.lookahead) {
if (condition(this, shifted)) break
else shifted += this.shift()
}
return shifted
}
shiftUntilEscapable(
condition: Scanner.UntilCondition,
escapeEscape: typeof Backslash | "" = ""
): string {
let shifted = ""
while (this.lookahead) {
if (this.lookahead === Backslash) {
this.shift()
if (condition(this, shifted)) shifted += this.shift()
else if (this.lookahead === Backslash)
shifted += `${escapeEscape}${this.shift()}`
else shifted += `${Backslash}${this.shift()}`
} else if (condition(this, shifted)) break
else shifted += this.shift()
}
return shifted
}
shiftUntilLookahead(charOrSet: string | KeySet): string {
return typeof charOrSet === "string" ?
this.shiftUntil(s => s.lookahead === charOrSet)
: this.shiftUntil(s => s.lookahead in charOrSet)
}
shiftUntilNonWhitespace(): string {
return this.shiftUntil(() => !(this.lookahead in whitespaceChars))
}
jumpToIndex(i: number): void {
this.i = i < 0 ? this.length + i : i
}
jumpForward(count: number): void {
this.i += count
}
get location(): number {
return this.i
}
get unscanned(): string {
return this.chars.slice(this.i, this.length).join("")
}
get scanned(): string {
return this.chars.slice(0, this.i).join("")
}
sliceChars(start: number, end?: number): string {
return this.chars.slice(start, end).join("")
}
lookaheadIs<char extends lookahead>(char: char): this is Scanner<char> {
return this.lookahead === char
}
lookaheadIsIn<keySet extends KeySet>(
tokens: keySet
): this is Scanner<Extract<keyof keySet, string>> {
return this.lookahead in tokens
}
}
export declare namespace Scanner {
export type UntilCondition = (scanner: Scanner, shifted: string) => boolean
export type shift<
lookahead extends string,
unscanned extends string
> = `${lookahead}${unscanned}`
export type shiftUntil<
unscanned extends string,
terminator extends string,
appendTo extends string = ""
> =
unscanned extends shift<infer lookahead, infer nextUnscanned> ?
lookahead extends terminator ?
[appendTo, unscanned]
: shiftUntil<nextUnscanned, terminator, `${appendTo}${lookahead}`>
: [appendTo, ""]
export type shiftUntilEscapable<
unscanned extends string,
terminator extends string,
escapeEscape extends Backslash | "",
appendTo extends string = ""
> =
unscanned extends shift<infer lookahead, infer nextUnscanned> ?
lookahead extends terminator ? [appendTo, unscanned]
: lookahead extends Backslash ?
nextUnscanned extends (
shift<infer nextLookahead, infer postEscapedUnscanned>
) ?
shiftUntilEscapable<
postEscapedUnscanned,
terminator,
escapeEscape,
`${appendTo}${nextLookahead extends terminator ? ""
: nextLookahead extends Backslash ? escapeEscape
: Backslash}${nextLookahead}`
>
: [`${appendTo}${Backslash}`, ""]
: shiftUntilEscapable<
nextUnscanned,
terminator,
escapeEscape,
`${appendTo}${lookahead}`
>
: [appendTo, ""]
export type shiftUntilNot<
unscanned extends string,
nonTerminator extends string,
appendTo extends string = ""
> =
unscanned extends shift<infer lookahead, infer nextUnscanned> ?
lookahead extends nonTerminator ?
shiftUntilNot<nextUnscanned, nonTerminator, `${appendTo}${lookahead}`>
: [appendTo, unscanned]
: [appendTo, ""]
export type skipWhitespace<unscanned extends string> = shiftUntilNot<
unscanned,
WhitespaceChar
>[1]
export type shiftResult<scanned extends string, unscanned extends string> = [
scanned,
unscanned
]
}
export const writeUnmatchedGroupCloseMessage = <
char extends string,
unscanned extends string
>(
char: char,
unscanned: unscanned
): writeUnmatchedGroupCloseMessage<char, unscanned> =>
`Unmatched ${char}${(unscanned === "" ? "" : ` before ${unscanned}`) as any}`
export type writeUnmatchedGroupCloseMessage<
char extends string,
unscanned extends string
> = `Unmatched ${char}${unscanned extends "" ? "" : ` before ${unscanned}`}`
export const writeUnclosedGroupMessage = <missingChar extends string>(
missingChar: missingChar
): writeUnclosedGroupMessage<missingChar> => `Missing ${missingChar}`
export type writeUnclosedGroupMessage<missingChar extends string> =
`Missing ${missingChar}`