Skip to content

Commit 11afcce

Browse files
committed
yeast: Fix bug in matching (_)
Turns out, `(_)` would match both named and unnamed nodes, as we never checked the value of the `match_unnamed` field. This is the real reason why the final catch-all rule we removed in the last commit was superfluous -- unnamed nodes were being caught by the penultimate rule instead (and mapped to `unsupported_node`). Having fixed the bug, we now (correctly) get errors due to unmatched unnamed nodes in the input. To fix this, we change the catch-all rule to match unnamed nodes as well. This restores the previous behaviour exactly. At some point, we should find a better way to handle unnamed nodes, as it seems wasteful to map these to `unsupported_node` (since we in practice only use them for their string content). Perhaps we should not attempt to translate unnamed nodes at all?
1 parent ee04938 commit 11afcce

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

shared/yeast/src/query.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ impl QueryNode {
6666

6767
pub fn do_match(&self, ast: &Ast, node: Id, matches: &mut Captures) -> Result<bool, String> {
6868
match self {
69-
QueryNode::Any { .. } => Ok(true),
69+
QueryNode::Any { match_unnamed } => {
70+
if *match_unnamed {
71+
Ok(true)
72+
} else {
73+
// `(_)` only matches named nodes, matching tree-sitter
74+
// semantics. Bare `_` (with `match_unnamed = true`)
75+
// matches any node.
76+
let n = ast.get_node(node).unwrap();
77+
Ok(n.is_named())
78+
}
79+
}
7080
QueryNode::Node { kind, children } => {
7181
let node = ast.get_node(node).unwrap();
7282
let target_kind = ast

unified/extractor/src/languages/swift/swift.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
12151215
// Preprocessor conditionals — unsupported
12161216
rule!((diagnostic) => (unsupported_node)),
12171217
// ---- Fallbacks ----
1218-
rule!(
1219-
(_)
1218+
// Bare `_` (rather than `(_)`) so this matches both named nodes
1219+
// and unnamed tokens. Any unnamed token that escapes the
1220+
// input-schema-specific rules (e.g. captured operators in
1221+
// `additive_expression op: @op`) has its auto-translated value
1222+
// replaced with an `unsupported_node` whose source range is
1223+
// inherited from the original token, so `#{op}` still reads the
1224+
// original text.
1225+
rule!(
1226+
_
12201227
=>
12211228
(unsupported_node)
12221229
),

0 commit comments

Comments
 (0)