mirror of
https://github.com/github/codeql.git
synced 2026-06-25 14:47:04 +02:00
unified/swift: Use tree! instead of ctx.node
Cleans up a few places where we were constructing trees piece by piece
rather than using the `tree!` macro.
In the process, Copilot noticed an issue that should probably be
addressed: the labeled_statement rule can never fire, since there are no
such nodes in the input. This is possibly a simple as making
_labeled_statement (which _does_ exist) named, but I haven't attempted
this.
Finally, a small change to yeast makes it so that the contents of a {}
interpolation can be a Rust block (previously it could only be a single
expression). This avoids the need to double-wrap instances where you
want to interpolate a single node produced as the final value of some
block.
This commit is contained in:
@@ -359,7 +359,7 @@ fn parse_direct_node(tokens: &mut Tokens, ctx: &Ident) -> Result<TokenStream> {
|
||||
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => {
|
||||
let group = expect_group(tokens, Delimiter::Brace)?;
|
||||
let expr = group.stream();
|
||||
Ok(quote! { ::std::convert::Into::<usize>::into(#expr) })
|
||||
Ok(quote! { ::std::convert::Into::<usize>::into({ #expr }) })
|
||||
}
|
||||
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis => {
|
||||
let group = expect_group(tokens, Delimiter::Parenthesis)?;
|
||||
@@ -396,7 +396,7 @@ fn parse_direct_node_inner(tokens: &mut Tokens, ctx: &Ident) -> Result<TokenStre
|
||||
let expr = group.stream();
|
||||
return Ok(quote! {
|
||||
{
|
||||
let __expr = (#expr);
|
||||
let __expr = { #expr };
|
||||
let __value = yeast::YeastDisplay::yeast_to_string(&__expr, &*#ctx.ast);
|
||||
let __source_range = yeast::YeastSourceRange::yeast_source_range(&__expr, &*#ctx.ast);
|
||||
#ctx.literal_with_source_range(#kind_str, &__value, __source_range)
|
||||
@@ -448,11 +448,11 @@ fn parse_direct_node_inner(tokens: &mut Tokens, ctx: &Ident) -> Result<TokenStre
|
||||
inner.next(); // consume second .
|
||||
let expr: TokenStream = inner.collect();
|
||||
quote! {
|
||||
(#expr).into_iter().map(::std::convert::Into::<usize>::into)
|
||||
{ #expr }.into_iter().map(::std::convert::Into::<usize>::into)
|
||||
}
|
||||
} else {
|
||||
let expr = group.stream();
|
||||
quote! { (#expr).into_iter() }
|
||||
quote! { { #expr }.into_iter() }
|
||||
};
|
||||
let chained = parse_chain_suffix(tokens, ctx, base)?;
|
||||
stmts.push(quote! {
|
||||
@@ -617,11 +617,11 @@ fn parse_direct_list(tokens: &mut Tokens, ctx: &Ident) -> Result<Vec<TokenStream
|
||||
inner.next(); // consume second .
|
||||
let expr: TokenStream = inner.collect();
|
||||
quote! {
|
||||
(#expr).into_iter().map(::std::convert::Into::<usize>::into)
|
||||
{ #expr }.into_iter().map(::std::convert::Into::<usize>::into)
|
||||
}
|
||||
} else {
|
||||
let expr = group.stream();
|
||||
quote! { (#expr).into_iter() }
|
||||
quote! { { #expr }.into_iter() }
|
||||
};
|
||||
let chained = parse_chain_suffix(tokens, ctx, base)?;
|
||||
items.push(quote! {
|
||||
@@ -630,7 +630,7 @@ fn parse_direct_list(tokens: &mut Tokens, ctx: &Ident) -> Result<Vec<TokenStream
|
||||
} else {
|
||||
let expr = group.stream();
|
||||
items.push(quote! {
|
||||
__nodes.push(::std::convert::Into::<usize>::into(#expr));
|
||||
__nodes.push(::std::convert::Into::<usize>::into({ #expr }));
|
||||
});
|
||||
}
|
||||
continue;
|
||||
|
||||
@@ -265,7 +265,21 @@ occurrences of the same `$name` within one `BuildCtx` share the same value:
|
||||
)
|
||||
```
|
||||
|
||||
`{..expr}` splices a `Vec<Id>` (or any iterable of `Id`):
|
||||
The contents of `{…}` are treated as a Rust block, so multi-statement
|
||||
expressions (with `let` bindings) work too:
|
||||
|
||||
```rust
|
||||
(assignment
|
||||
left: {tmp}
|
||||
right: {
|
||||
let lit = ctx.literal("integer", "0");
|
||||
tree!((binary_expr op: (operator "+") left: {tmp} right: {lit}))
|
||||
})
|
||||
```
|
||||
|
||||
`{..expr}` splices a `Vec<Id>` (or any iterable of `Id`); the contents
|
||||
are likewise a Rust block, so the splice can be the result of arbitrary
|
||||
computation:
|
||||
|
||||
```rust
|
||||
yeast::trees!(ctx,
|
||||
|
||||
@@ -725,11 +725,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
|
||||
body: (block stmt: {..body}))
|
||||
),
|
||||
// Labeled statement (e.g. `outer: for ...`). Strip the trailing ':' from the label token.
|
||||
rule!((labeled_statement label: (statement_label) @lbl statement: @stmt) => {..{
|
||||
rule!((labeled_statement label: (statement_label) @lbl statement: @stmt) => {
|
||||
let text = ctx.ast.source_text(lbl.into());
|
||||
let name = ctx.literal("identifier", &text[..text.len() - 1]);
|
||||
vec![ctx.node("labeled_stmt", vec![("label", vec![name]), ("stmt", vec![stmt.into()])])]
|
||||
}}),
|
||||
let name = &text[..text.len() - 1];
|
||||
tree!((labeled_stmt label: (identifier #{name}) stmt: {stmt}))
|
||||
}),
|
||||
// ---- Collections ----
|
||||
// Array literal
|
||||
rule!((array_literal element: _* @elems) => (array_literal element: {..elems})),
|
||||
@@ -739,16 +739,9 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
|
||||
rule!(
|
||||
(dictionary_literal key: _* @keys value: _* @vals)
|
||||
=>
|
||||
(map_literal element: {..{
|
||||
keys.iter().zip(vals.iter()).map(|(&k, &v)| {
|
||||
let k_id: usize = k.into();
|
||||
let v_id: usize = v.into();
|
||||
ctx.node("key_value_pair", vec![
|
||||
("key", vec![k_id]),
|
||||
("value", vec![v_id]),
|
||||
])
|
||||
}).collect::<Vec<_>>()
|
||||
}})
|
||||
(map_literal element: {..keys.into_iter().zip(vals).map(|(k, v)|
|
||||
tree!((key_value_pair key: {k} value: {v}))
|
||||
)})
|
||||
),
|
||||
rule!((dictionary_literal element: _* @elems) => (map_literal element: {..elems})),
|
||||
rule!((dictionary_literal_item key: @k value: @v) => (key_value_pair key: {k} value: {v})),
|
||||
|
||||
Reference in New Issue
Block a user