|
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 |
@@ -749,14 +760,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
749 | 760 | capture: (closureCaptureClause items: _* @captures)? |
750 | 761 | parameterClause: _* @params |
751 | 762 | returnClause: (returnClause type: @ret)?)? |
752 | | - statements: _* @body) |
| 763 | + statements: _* @body) @@closure |
753 | 764 | => |
754 | 765 | (function_expr |
755 | 766 | modifier: {attrs} |
756 | 767 | capture_declaration: {captures} |
757 | 768 | parameter: {params} |
758 | 769 | return_type: {ret} |
759 | | - body: (block stmt: {body})) |
| 770 | + body: {block_with_anchor(&mut ctx, body, Some(closure))}) |
760 | 771 | ), |
761 | 772 | // A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional |
762 | 773 | // ownership specifier (`weak`/`unowned`) becomes a modifier; the |
@@ -1278,23 +1289,34 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> { |
1278 | 1289 | modifiers: _* @mods |
1279 | 1290 | signature: (functionSignature |
1280 | 1291 | parameterClause: (functionParameterClause parameters: _* @params)) |
1281 | | - body: (codeBlock statements: _* @body_stmts)?) |
| 1292 | + body: (codeBlock) @body) |
1282 | 1293 | => |
1283 | 1294 | (constructor_declaration |
1284 | 1295 | modifier: {mods} |
1285 | 1296 | name_node: (identifier #{initK}) |
1286 | 1297 | parameter: {params} |
1287 | | - body: (block stmt: {body_stmts})) |
| 1298 | + body: {body}) |
| 1299 | + ), |
| 1300 | + rule!( |
| 1301 | + (initializerDecl |
| 1302 | + modifiers: _* @mods |
| 1303 | + signature: (functionSignature |
| 1304 | + parameterClause: (functionParameterClause parameters: _* @params))) |
| 1305 | + => |
| 1306 | + (constructor_declaration |
| 1307 | + modifier: {mods} |
| 1308 | + parameter: {params} |
| 1309 | + body: (block)) |
1288 | 1310 | ), |
1289 | 1311 | // Deinit declaration → destructor_declaration. Body statements optional. |
1290 | 1312 | rule!( |
1291 | 1313 | (deinitializerDecl |
1292 | 1314 | modifiers: _* @mods |
1293 | | - body: (codeBlock statements: _* @body_stmts)) |
| 1315 | + body: (codeBlock) @body) |
1294 | 1316 | => |
1295 | 1317 | (destructor_declaration |
1296 | 1318 | modifier: {mods} |
1297 | | - body: (block stmt: {body_stmts})) |
| 1319 | + body: {body}) |
1298 | 1320 | ), |
1299 | 1321 | // Typealias declaration |
1300 | 1322 | rule!( |
|
0 commit comments