|
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 |
@@ -301,14 +312,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
301 | 312 | bindings: (patternBinding |
302 | 313 | pattern: (identifierPattern identifier: @@name) |
303 | 314 | typeAnnotation: (typeAnnotation type: @ty) |
304 | | - accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body))) |
| 315 | + accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block)) |
305 | 316 | => |
306 | 317 | (accessor_declaration |
307 | 318 | modifier: (modifier #{spec}) |
308 | 319 | name_node: (identifier #{name}) |
309 | 320 | type: {ty} |
310 | | - accessor_kind: (accessor_kind "get") |
311 | | - body: (block stmt: {body})) |
| 321 | + accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)} |
| 322 | + body: {block_with_anchor(&mut ctx, body, Some(accessor_block))}) |
312 | 323 | ), |
313 | 324 | // A property with an explicit accessor block. swift-syntax makes both |
314 | 325 | // shapes plain `accessorDecl`s, so they are told apart by the presence |
@@ -564,15 +575,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
564 | 575 | signature: (functionSignature |
565 | 576 | parameterClause: (functionParameterClause parameters: _* @params) |
566 | 577 | returnClause: (returnClause type: @ret)?) |
567 | | - body: (codeBlock statements: _* @body)) |
| 578 | + body: (codeBlock) @body) |
568 | 579 | => |
569 | 580 | (function_declaration |
570 | 581 | modifier: {mods} |
571 | 582 | name_node: (identifier #{name}) |
572 | 583 | type_parameter: {type_params} |
573 | 584 | parameter: {params} |
574 | 585 | return_type: {ret} |
575 | | - body: (block stmt: {body})) |
| 586 | + body: {body}) |
576 | 587 | ), |
577 | 588 | rule!( |
578 | 589 | (functionDecl |
@@ -747,14 +758,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
747 | 758 | capture: (closureCaptureClause items: _* @captures)? |
748 | 759 | parameterClause: _* @params |
749 | 760 | returnClause: (returnClause type: @ret)?)? |
750 | | - statements: _* @body) |
| 761 | + statements: _* @body) @@closure |
751 | 762 | => |
752 | 763 | (function_expr |
753 | 764 | modifier: {attrs} |
754 | 765 | capture_declaration: {captures} |
755 | 766 | parameter: {params} |
756 | 767 | return_type: {ret} |
757 | | - body: (block stmt: {body})) |
| 768 | + body: {block_with_anchor(&mut ctx, body, Some(closure))}) |
758 | 769 | ), |
759 | 770 | // A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional |
760 | 771 | // 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