Repository navigation
Expand file tree
/
Copy pathparsePredicates.ts
More file actions
65 lines (55 loc) · 1.99 KB
/
Copy pathparsePredicates.ts
File metadata and controls
65 lines (55 loc) · 1.99 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
import type { QueryPredicate } from "web-tree-sitter";
import type { PatternPredicate } from "./QueryCapture";
import { queryPredicateOperators } from "./queryPredicateOperators";
/**
* Parse a list of predicate descriptors per pattern into a list of predicate
* functions per pattern.
*
* @param predicateDescriptors The predicate descriptors to parse. Each entry
* in the outer array corresponds to a pattern, and each entry in the inner
* array corresponds to a predicate for that pattern.
* @returns A list of errors, and a list of predicates. The predicate list is
* structured the same as the input, but each predicate in the inner list is a
* function that takes a match and returns true if the match matches the
* predicate.
*/
export function parsePredicates(predicateDescriptors: QueryPredicate[][]) {
const errors: PredicateError[] = [];
const predicates: PatternPredicate[][] = [];
predicateDescriptors.forEach((patternPredicateDescriptors, patternIdx) => {
/** The predicates for a given pattern */
const patternPredicates: PatternPredicate[] = [];
patternPredicateDescriptors.forEach((predicateDescriptor, predicateIdx) => {
const operator = queryPredicateOperators.find(
({ name }) => name === predicateDescriptor.operator,
);
if (operator == null) {
errors.push({
patternIdx,
predicateIdx,
error: `Unknown predicate operator "${predicateDescriptor.operator}"`,
});
return;
}
const result = operator.createPredicate(predicateDescriptor.operands);
if (!result.success) {
errors.push(
...result.errors.map((error) => ({
patternIdx,
predicateIdx,
error,
})),
);
return;
}
patternPredicates.push(result.predicate);
});
predicates.push(patternPredicates);
});
return { errors, predicates };
}
interface PredicateError {
patternIdx: number;
predicateIdx: number;
error: string;
}