-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOr.ts
More file actions
37 lines (34 loc) · 1.07 KB
/
Or.ts
File metadata and controls
37 lines (34 loc) · 1.07 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
import { Criteria } from "./Criteria"
import { Token } from "./lexer"
import { create, Rule } from "./Rule"
import { Type } from "./Type"
export class Or extends Rule {
static readonly precedence = 30
readonly precedence = Or.precedence
readonly class = "Or"
readonly criteria: Rule[]
constructor(criterias: Rule[]) {
super()
this.criteria = criterias.reduce<Rule[]>((r, c) => (c instanceof Or ? [...r, ...c.criteria] : [...r, c]), []) // flatten
}
is(value: any): boolean {
return this.criteria.some(c => c.is(value))
}
toString() {
return this.criteria.map(c => c.stringify(this.precedence)).join(" | ")
}
}
export function or(...criterias: Criteria[]): Or {
return new Or(criterias.map(create))
}
function complete(tokens: Token[], type?: Type, baseObject?: Type.Object): Type.Completion[] | Type.Completion {
return Type.Completor.operators(
tokens,
(tokens?: Token[]) => (tokens && baseObject ? baseObject?.complete(tokens, undefined, undefined) : []),
{
value: " | ",
suggestion: { value: "|", description: "or" },
}
)
}
Type.Number.addArgument(complete)