-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatch.ts
More file actions
33 lines (30 loc) · 903 Bytes
/
Match.ts
File metadata and controls
33 lines (30 loc) · 903 Bytes
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
import { Token } from "./lexer"
import { Rule } from "./Rule"
import { Type } from "./Type"
import { Completor } from "./Type/Completor"
export class Match extends Rule {
readonly precedence = Number.MAX_SAFE_INTEGER
readonly class = "Match"
constructor(readonly criteria: RegExp) {
super()
}
is(value: any): boolean {
return typeof value == "string" && this.criteria.test(value)
}
toString() {
return `/${this.criteria}/`
}
}
export function match(criteria: RegExp): Match {
return new Match(criteria)
}
function complete(tokens: Token[], string: Type.String): Type.Completion[] | Type.Completion {
return Completor.expressions(
tokens,
(tokens?: Token[]) => {
return tokens && tokens?.length == 1 && tokens[0].value == "/" ? [{ value: "//", cursor: 1 }] : []
},
{ value: "//", cursor: 1, suggestion: { value: "//", description: "match" } }
)
}
Type.String.add(complete)