Merge pull request #2795 from savetheclocktower/fix-quantified-predicates

fix: Handle quantified predicates on `web-tree-sitter` properly
This commit is contained in:
Amaan Qureshi 2024-01-25 13:31:40 -05:00 committed by GitHub
commit ef5621c0e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 122 additions and 29 deletions

View file

@ -842,78 +842,88 @@ class Language {
const operator = steps[0].value;
let isPositive = true;
let matchAll = true;
let captureName;
switch (operator) {
case 'any-not-eq?':
isPositive = false;
matchAll = false;
case 'any-eq?':
matchAll = false;
case 'not-eq?':
isPositive = false;
case 'any-eq?':
case 'eq?':
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`#eq?\` predicate. Expected 2, got ${steps.length - 1}`
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}`
);
if (steps[1].type !== 'capture') throw new Error(
`First argument of \`#eq?\` predicate must be a capture. Got "${steps[1].value}"`
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}"`
);
matchAll = !operator.startsWith('any-');
if (steps[2].type === 'capture') {
const captureName1 = steps[1].name;
const captureName2 = steps[2].name;
textPredicates[i].push(function(captures) {
textPredicates[i].push(function (captures) {
let nodes_1 = [];
let nodes_2 = [];
for (const c of captures) {
if (c.name === captureName1) nodes_1.push(c.node);
if (c.name === captureName2) nodes_2.push(c.node);
}
let compare = (n1, n2, positive) => {
return positive ?
n1.text === n2.text :
n1.text !== n2.text;
};
return matchAll
? nodes_1.every(n1 => nodes_2.some(n2 => n1.text === n2.text)) === isPositive
: nodes_1.some(n1 => nodes_2.some(n2 => n1.text === n2.text)) === isPositive;
? nodes_1.every(n1 => nodes_2.some(n2 => compare(n1, n2, isPositive)))
: nodes_1.some(n1 => nodes_2.some(n2 => compare(n1, n2, isPositive)));
});
} else {
const captureName = steps[1].name;
captureName = steps[1].name;
const stringValue = steps[2].value;
textPredicates[i].push(function(captures) {
let matches = (n) => n.text === stringValue;
let doesNotMatch = (n) => n.text !== stringValue;
textPredicates[i].push(function (captures) {
let nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node);
}
let test = isPositive ? matches : doesNotMatch;
return matchAll
? nodes.every(n => n.text === stringValue) === isPositive
: nodes.some(n => n.text === stringValue) === isPositive;
? nodes.every(test)
: nodes.some(test);
});
}
break;
case 'not-any-match?':
isPositive = false;
matchAll = false;
case 'any-match?':
matchAll = false;
case 'not-match?':
isPositive = false;
case 'match?':
case "any-not-match?":
case "not-match?":
isPositive = false;
case "any-match?":
case "match?":
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`#match?\` predicate. Expected 2, got ${steps.length - 1}.`
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}.`
);
if (steps[1].type !== 'capture') throw new Error(
`First argument of \`#match?\` predicate must be a capture. Got "${steps[1].value}".`
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}".`
);
if (steps[2].type !== 'string') throw new Error(
`Second argument of \`#match?\` predicate must be a string. Got @${steps[2].value}.`
`Second argument of \`#${operator}\` predicate must be a string. Got @${steps[2].value}.`
);
const captureName = steps[1].name;
captureName = steps[1].name;
const regex = new RegExp(steps[2].value);
textPredicates[i].push(function(captures) {
matchAll = !operator.startsWith('any-');
textPredicates[i].push(function (captures) {
const nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node.text);
}
let test = (text, positive) => {
return positive ?
regex.test(text) :
!regex.test(text);
};
if (nodes.length === 0) return !isPositive;
return matchAll
? nodes.every(text => regex.test(text)) === isPositive
: nodes.some(text => regex.test(text)) === isPositive;
? nodes.every(text => test(text, isPositive))
: nodes.some(text => test(text, isPositive))
});
break;
@ -957,7 +967,7 @@ class Language {
}
captureName = steps[1].name;
const values = steps.slice(2).map(s => s.value);
textPredicates[i].push(function(captures) {
textPredicates[i].push(function (captures) {
const nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node.text);