|
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 |
@@ -304,14 +315,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
304 | 315 | bindings: (patternBinding |
305 | 316 | pattern: (identifierPattern identifier: @@name) |
306 | 317 | typeAnnotation: (typeAnnotation type: @ty) |
307 | | - accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body))) |
| 318 | + accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block)) |
308 | 319 | => |
309 | 320 | (accessor_declaration |
310 | 321 | modifier: (modifier #{spec}) |
311 | 322 | name_node: (identifier #{name}) |
312 | 323 | type: {ty} |
313 | | - accessor_kind: (accessor_kind "get") |
314 | | - body: (block stmt: {body})) |
| 324 | + accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)} |
| 325 | + body: {block_with_anchor(&mut ctx, body, Some(accessor_block))}) |
315 | 326 | ), |
316 | 327 | // A property with an explicit accessor block. swift-syntax makes both |
317 | 328 | // shapes plain `accessorDecl`s, so they are told apart by the presence |
@@ -567,15 +578,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
567 | 578 | signature: (functionSignature |
568 | 579 | parameterClause: (functionParameterClause parameters: _* @params) |
569 | 580 | returnClause: (returnClause type: @ret)?) |
570 | | - body: (codeBlock statements: _* @body)) |
| 581 | + body: (codeBlock) @body) |
571 | 582 | => |
572 | 583 | (function_declaration |
573 | 584 | modifier: {mods} |
574 | 585 | name_node: (identifier #{name}) |
575 | 586 | type_parameter: {type_params} |
576 | 587 | parameter: {params} |
577 | 588 | return_type: {ret} |
578 | | - body: (block stmt: {body})) |
| 589 | + body: {body}) |
579 | 590 | ), |
580 | 591 | rule!( |
581 | 592 | (functionDecl |
@@ -750,14 +761,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
750 | 761 | capture: (closureCaptureClause items: _* @captures)? |
751 | 762 | parameterClause: _* @params |
752 | 763 | returnClause: (returnClause type: @ret)?)? |
753 | | - statements: _* @body) |
| 764 | + statements: _* @body) @@closure |
754 | 765 | => |
755 | 766 | (function_expr |
756 | 767 | modifier: {attrs} |
757 | 768 | capture_declaration: {captures} |
758 | 769 | parameter: {params} |
759 | 770 | return_type: {ret} |
760 | | - body: (block stmt: {body})) |
| 771 | + body: {block_with_anchor(&mut ctx, body, Some(closure))}) |
761 | 772 | ), |
762 | 773 | // A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional |
763 | 774 | // ownership specifier (`weak`/`unowned`) becomes a modifier; the |
@@ -1277,22 +1288,33 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
1277 | 1288 | modifiers: _* @mods |
1278 | 1289 | signature: (functionSignature |
1279 | 1290 | parameterClause: (functionParameterClause parameters: _* @params)) |
1280 | | - body: (codeBlock statements: _* @body_stmts)?) |
| 1291 | + body: (codeBlock) @body) |
1281 | 1292 | => |
1282 | 1293 | (constructor_declaration |
1283 | 1294 | modifier: {mods} |
1284 | 1295 | parameter: {params} |
1285 | | - body: (block stmt: {body_stmts})) |
| 1296 | + body: {body}) |
| 1297 | + ), |
| 1298 | + rule!( |
| 1299 | + (initializerDecl |
| 1300 | + modifiers: _* @mods |
| 1301 | + signature: (functionSignature |
| 1302 | + parameterClause: (functionParameterClause parameters: _* @params))) |
| 1303 | + => |
| 1304 | + (constructor_declaration |
| 1305 | + modifier: {mods} |
| 1306 | + parameter: {params} |
| 1307 | + body: (block)) |
1286 | 1308 | ), |
1287 | 1309 | // Deinit declaration → destructor_declaration. Body statements optional. |
1288 | 1310 | rule!( |
1289 | 1311 | (deinitializerDecl |
1290 | 1312 | modifiers: _* @mods |
1291 | | - body: (codeBlock statements: _* @body_stmts)) |
| 1313 | + body: (codeBlock) @body) |
1292 | 1314 | => |
1293 | 1315 | (destructor_declaration |
1294 | 1316 | modifier: {mods} |
1295 | | - body: (block stmt: {body_stmts})) |
| 1317 | + body: {body}) |
1296 | 1318 | ), |
1297 | 1319 | // Typealias declaration |
1298 | 1320 | rule!( |
|
0 commit comments