|
1 | 1 | use codeql_extractor::extractor::desugaring; |
2 | | -use yeast::{ConcreteDesugarer, DesugaringConfig, PhaseKind, Rule, rule, tree}; |
| 2 | +use yeast::{ConcreteDesugarer, DesugaringConfig, PhaseKind, Rule, rule, tree, tree_at}; |
3 | 3 |
|
4 | 4 | /// User context propagated from outer rules down to the inner rules that |
5 | 5 | /// emit the corresponding output declarations, so that each emitted node |
@@ -60,6 +60,17 @@ impl SwiftContext { |
60 | 60 | } |
61 | 61 | } |
62 | 62 |
|
| 63 | +fn block_with_anchor( |
| 64 | + ctx: &mut yeast::build::BuildCtx<'_, SwiftContext>, |
| 65 | + statements: Vec<yeast::Id>, |
| 66 | + anchor: Option<yeast::Id>, |
| 67 | +) -> yeast::Id { |
| 68 | + match anchor { |
| 69 | + Some(anchor) => tree_at!(ctx, anchor, (block stmt: {statements})), |
| 70 | + None => tree!(ctx, (block stmt: {statements})), |
| 71 | + } |
| 72 | +} |
| 73 | + |
63 | 74 | /// Build a freshly-created `chained_declaration` modifier node if |
64 | 75 | /// `ctx.is_chained`, else `None`. Used by inner declaration rules to |
65 | 76 | /// emit the chained tag for non-first children of a flattening outer |
@@ -298,14 +309,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
298 | 309 | bindings: (patternBinding |
299 | 310 | pattern: (identifierPattern identifier: @@name) |
300 | 311 | typeAnnotation: (typeAnnotation type: @ty) |
301 | | - accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body))) |
| 312 | + accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block)) |
302 | 313 | => |
303 | 314 | (accessor_declaration |
304 | 315 | modifier: (modifier #{spec}) |
305 | 316 | name_node: (identifier #{name}) |
306 | 317 | type: {ty} |
307 | | - accessor_kind: (accessor_kind "get") |
308 | | - body: (block stmt: {body})) |
| 318 | + accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)} |
| 319 | + body: {block_with_anchor(&mut ctx, body, Some(accessor_block))}) |
309 | 320 | ), |
310 | 321 | // A property with an explicit accessor block. swift-syntax makes both |
311 | 322 | // shapes plain `accessorDecl`s, so they are told apart by the presence |
@@ -561,15 +572,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
561 | 572 | signature: (functionSignature |
562 | 573 | parameterClause: (functionParameterClause parameters: _* @params) |
563 | 574 | returnClause: (returnClause type: @ret)?) |
564 | | - body: (codeBlock statements: _* @body)) |
| 575 | + body: (codeBlock) @body) |
565 | 576 | => |
566 | 577 | (function_declaration |
567 | 578 | modifier: {mods} |
568 | 579 | name_node: (identifier #{name}) |
569 | 580 | type_parameter: {type_params} |
570 | 581 | parameter: {params} |
571 | 582 | return_type: {ret} |
572 | | - body: (block stmt: {body})) |
| 583 | + body: {body}) |
573 | 584 | ), |
574 | 585 | rule!( |
575 | 586 | (functionDecl |
@@ -744,14 +755,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
744 | 755 | capture: (closureCaptureClause items: _* @captures)? |
745 | 756 | parameterClause: _* @params |
746 | 757 | returnClause: (returnClause type: @ret)?)? |
747 | | - statements: _* @body) |
| 758 | + statements: _* @body) @@closure |
748 | 759 | => |
749 | 760 | (function_expr |
750 | 761 | modifier: {attrs} |
751 | 762 | capture_declaration: {captures} |
752 | 763 | parameter: {params} |
753 | 764 | return_type: {ret} |
754 | | - body: (block stmt: {body})) |
| 765 | + body: {block_with_anchor(&mut ctx, body, Some(closure))}) |
755 | 766 | ), |
756 | 767 | // A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional |
757 | 768 | // ownership specifier (`weak`/`unowned`) becomes a modifier; the |
@@ -1271,22 +1282,33 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
1271 | 1282 | modifiers: _* @mods |
1272 | 1283 | signature: (functionSignature |
1273 | 1284 | parameterClause: (functionParameterClause parameters: _* @params)) |
1274 | | - body: (codeBlock statements: _* @body_stmts)?) |
| 1285 | + body: (codeBlock) @body) |
1275 | 1286 | => |
1276 | 1287 | (constructor_declaration |
1277 | 1288 | modifier: {mods} |
1278 | 1289 | parameter: {params} |
1279 | | - body: (block stmt: {body_stmts})) |
| 1290 | + body: {body}) |
| 1291 | + ), |
| 1292 | + rule!( |
| 1293 | + (initializerDecl |
| 1294 | + modifiers: _* @mods |
| 1295 | + signature: (functionSignature |
| 1296 | + parameterClause: (functionParameterClause parameters: _* @params))) |
| 1297 | + => |
| 1298 | + (constructor_declaration |
| 1299 | + modifier: {mods} |
| 1300 | + parameter: {params} |
| 1301 | + body: (block)) |
1280 | 1302 | ), |
1281 | 1303 | // Deinit declaration → destructor_declaration. Body statements optional. |
1282 | 1304 | rule!( |
1283 | 1305 | (deinitializerDecl |
1284 | 1306 | modifiers: _* @mods |
1285 | | - body: (codeBlock statements: _* @body_stmts)) |
| 1307 | + body: (codeBlock) @body) |
1286 | 1308 | => |
1287 | 1309 | (destructor_declaration |
1288 | 1310 | modifier: {mods} |
1289 | | - body: (block stmt: {body_stmts})) |
| 1311 | + body: {body}) |
1290 | 1312 | ), |
1291 | 1313 | // Typealias declaration |
1292 | 1314 | rule!( |
|
0 commit comments