Skip to content

Commit 2ed43ae

Browse files
committed
Unified: Rewrite swift.rs to use nicer location syntax
1 parent 578d477 commit 2ed43ae

1 file changed

Lines changed: 35 additions & 43 deletions

File tree

  • unified/extractor/src/languages/swift

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codeql_extractor::extractor::desugaring;
2-
use yeast::{ConcreteDesugarer, DesugaringConfig, Rule, rule, tree, tree_at, tree_spanning};
2+
use yeast::{ConcreteDesugarer, DesugaringConfig, Rule, rule, tree};
33

44
/// User context propagated from outer rules down to the inner rules that
55
/// emit the corresponding output declarations, so that each emitted node
@@ -421,16 +421,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
421421
}
422422
None => None,
423423
};
424-
tree_spanning!(
425-
std::iter::once(spec).chain(body),
426-
(accessor_declaration
427-
modifier: {binding}
428-
modifier: {chained}
429-
name_node: {name}
430-
type: {ty}
431-
accessor_kind: (accessor_kind #{spec})
432-
body: {body})
433-
)
424+
tree!((accessor_declaration
425+
modifier: {binding}
426+
modifier: {chained}
427+
name_node: {name}
428+
type: {ty}
429+
accessor_kind: (accessor_kind #{spec})
430+
body: {body}
431+
location: {spec}
432+
location: {body}))
434433
}
435434
),
436435
// ---- Variables ----
@@ -500,10 +499,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
500499
parameterClause: (enumCaseParameterClause parameters: _* @params) @@clause)
501500
=>
502501
class_like_declaration {
503-
let constructor = tree_spanning!(
504-
[name, clause],
505-
(constructor_declaration parameter: {params} body: (block))
506-
);
502+
let constructor = tree!((constructor_declaration
503+
parameter: {params}
504+
body: (block)
505+
location: {name}
506+
location: {clause}));
507507
tree!((class_like_declaration
508508
modifier: {ctx.outer_modifiers.clone()}
509509
modifier: {chained_modifier(&mut ctx)}
@@ -675,12 +675,10 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
675675
additionalTrailingClosures: _* @additional)
676676
=>
677677
call_expr {
678-
let callee = tree_at!(
679-
array,
680-
(generic_type_expr
678+
let callee = tree!((generic_type_expr
681679
base: (identifier "Array")
682-
type_argument: {element})
683-
);
680+
type_argument: {element}
681+
location: {array}));
684682
tree!((call_expr
685683
callee: {callee}
686684
argument: {args}
@@ -695,12 +693,10 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
695693
arguments: _* @args)
696694
=>
697695
call_expr {
698-
let callee = tree_at!(
699-
array,
700-
(generic_type_expr
696+
let callee = tree!((generic_type_expr
701697
base: (identifier "Array")
702-
type_argument: {element})
703-
);
698+
type_argument: {element}
699+
location: {array}));
704700
tree!((call_expr callee: {callee} argument: {args}))
705701
}
706702
),
@@ -772,12 +768,10 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
772768
declName: (declReferenceExpr baseName: @@member))
773769
=>
774770
member_access_expr {
775-
let base = tree_at!(
776-
array,
777-
(generic_type_expr
778-
base: (identifier "Array")
779-
type_argument: {element})
780-
);
771+
let base = tree!((generic_type_expr
772+
base: (identifier "Array")
773+
type_argument: {element}
774+
location: {array}));
781775
tree!((member_access_expr
782776
base: {base}
783777
member_name_node: (identifier #{member})))
@@ -1065,10 +1059,9 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
10651059
=>
10661060
expr {
10671061
let op = format!("try{}", m.map(|m| ctx.source_text(m)).unwrap_or_default());
1068-
let operator = tree_spanning!(
1069-
std::iter::once(keyword).chain(m),
1070-
(prefix_operator #{op})
1071-
);
1062+
let operator = tree!((prefix_operator #{op}
1063+
location: {keyword}
1064+
location: {m}));
10721065
tree!((unary_expr operator: {operator} operand: {e}))
10731066
}
10741067
),
@@ -1107,25 +1100,24 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
11071100
// As expression (type cast) — as?, as!
11081101
rule!((asExpr expression: @val asKeyword: @@keyword questionOrExclamationMark: _? @@mark type: @ty) => type_cast_expr {
11091102
let op = format!("as{}", mark.map(|m| ctx.source_text(m)).unwrap_or_default());
1110-
let operator = tree_spanning!(
1111-
std::iter::once(keyword).chain(mark),
1112-
(infix_operator #{op})
1113-
);
1103+
let operator = tree!((infix_operator #{op}
1104+
location: {keyword}
1105+
location: {mark}));
11141106
tree!((type_cast_expr expr: {val} operator: {operator} type: {ty}))
11151107
}),
11161108
// Check expression (`x is T`) → type_test_expr
11171109
rule!((isExpr expression: @val isKeyword: @@keyword type: @ty) => (type_test_expr
11181110
expr: {val}
1119-
operator: {tree_at!(keyword, (infix_operator "is"))}
1111+
operator: (infix_operator "is" location: {keyword})
11201112
type: {ty})),
11211113
// Await expression → unary_expr with operator "await"
11221114
rule!((awaitExpr awaitKeyword: @@keyword expression: @val) => (unary_expr
1123-
operator: {tree_at!(keyword, (prefix_operator "await"))}
1115+
operator: (prefix_operator "await" location: {keyword})
11241116
operand: {val})),
11251117
// Force-unwrap (`x!`) → postfix unary_expr, via swift-syntax's dedicated
11261118
// `forceUnwrapExpr` node.
11271119
rule!((forceUnwrapExpr expression: @e exclamationMark: @@mark) => (unary_expr
1128-
operator: {tree_at!(mark, (postfix_operator "!"))}
1120+
operator: (postfix_operator "!" location: {mark})
11291121
operand: {e})),
11301122
// ---- Imports ----
11311123
// An import declaration. The dotted path (a list of
@@ -1147,7 +1139,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
11471139
let last = *parts.last().ok_or("import has no path")?;
11481140
let pattern = match kind {
11491141
None => {
1150-
let bulk = tree_at!(decl, (bulk_importing_pattern));
1142+
let bulk = tree!((bulk_importing_pattern location: {decl}));
11511143
tree!((named_pattern
11521144
name_node: (identifier #{last})
11531145
sub_pattern: {bulk}))

0 commit comments

Comments
 (0)