Skip to content

Commit 1537a26

Browse files
committed
Add type and class mappings
1 parent 22f6a14 commit 1537a26

5 files changed

Lines changed: 539 additions & 22 deletions

File tree

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

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,274 @@ fn translation_rules() -> Vec<yeast::Rule> {
629629
path: {parts}.map(p -> (identifier #{p}))
630630
modifier: {..mods})
631631
),
632+
// ---- Types and classes ----
633+
// Self expression → name_expr
634+
rule!((self_expression) => (name_expr identifier: (identifier "self"))),
635+
// Super expression → super_expr
636+
rule!((super_expression) => (super_expr)),
637+
// Modifiers — unwrap to individual modifier children
638+
rule!((modifiers _* @mods) => {..mods}),
639+
rule!((attribute) @m => (modifier #{m})),
640+
rule!((visibility_modifier) @m => (modifier #{m})),
641+
rule!((function_modifier) @m => (modifier #{m})),
642+
rule!((member_modifier) @m => (modifier #{m})),
643+
rule!((mutation_modifier) @m => (modifier #{m})),
644+
rule!((ownership_modifier) @m => (modifier #{m})),
645+
rule!((property_modifier) @m => (modifier #{m})),
646+
rule!((parameter_modifier) @m => (modifier #{m})),
647+
rule!((inheritance_modifier) @m => (modifier #{m})),
648+
rule!((property_behavior_modifier) @m => (modifier #{m})),
649+
// Type annotations — unwrap
650+
rule!((type_annotation type: @inner) => {inner}),
651+
// New grammar: user_type is split into simple_user_type parts.
652+
// Keep a conservative textual fallback to avoid dropping type information.
653+
rule!((user_type) @ty => (named_type_expr name: (identifier #{ty}))),
654+
// Tuple type → tuple_type_expr
655+
rule!((tuple_type element: _* @elems) => (tuple_type_expr element: {..elems})),
656+
rule!((tuple_type_item name: @name type: @ty) => (tuple_type_element name: (identifier #{name}) type: {ty})),
657+
rule!((tuple_type_item type: @ty) => (tuple_type_element type: {ty})),
658+
// Array type `[T]` → generic_type_expr with Array base
659+
rule!((array_type element: @e) => (generic_type_expr
660+
base: (named_type_expr name: (identifier "Array"))
661+
type_argument: {e})),
662+
// Dictionary type `[K: V]` → generic_type_expr with Dictionary base
663+
rule!((dictionary_type key: @k value: @v) => (generic_type_expr
664+
base: (named_type_expr name: (identifier "Dictionary"))
665+
type_argument: {k}
666+
type_argument: {v})),
667+
// Optional type `T?` → generic_type_expr with Optional base
668+
rule!((optional_type wrapped: @w) => (generic_type_expr
669+
base: (named_type_expr name: (identifier "Optional"))
670+
type_argument: {w})),
671+
// Function type `(Params) -> Ret` → function_type_expr.
672+
rule!((function_type parameter: _* @ps return_type: @ret) => (function_type_expr parameter: {..ps} return_type: {ret})),
673+
rule!((function_type_parameter name: @name type: @ty) => (parameter external_name: (identifier #{name}) type: {ty})),
674+
rule!((function_type_parameter type: @ty) => (parameter type: {ty})),
675+
// Selector expression: `#selector(inner)` -- not yet supported
676+
rule!(
677+
(selector_expression _ @inner)
678+
=>
679+
(unsupported_node)
680+
),
681+
// Key path expressions are currently unsupported.
682+
rule!((key_path_expression) => (unsupported_node)),
683+
// Inheritance specifier → base_type
684+
rule!((inheritance_specifier inherits_from: @ty) => (base_type type: {ty})),
685+
// Class declaration with body containing members
686+
rule!(
687+
(class_declaration
688+
declaration_kind: @kind
689+
name: @name
690+
body: (class_body member: _* @members)
691+
(inheritance_specifier)* @bases
692+
(modifiers)* @mods)
693+
=>
694+
(class_like_declaration
695+
modifier: (modifier #{kind})
696+
modifier: {..mods}
697+
name: (identifier #{name})
698+
base_type: {..bases}
699+
member: {..members})
700+
),
701+
// Enum class declaration: same as a regular class but with an enum body.
702+
rule!(
703+
(class_declaration
704+
declaration_kind: @kind
705+
name: @name
706+
body: (enum_class_body member: _* @members)
707+
(inheritance_specifier)* @bases
708+
(modifiers)* @mods)
709+
=>
710+
(class_like_declaration
711+
modifier: (modifier #{kind})
712+
modifier: {..mods}
713+
name: (identifier #{name})
714+
base_type: {..bases}
715+
member: {..members})
716+
),
717+
// Class declaration with empty body
718+
rule!(
719+
(class_declaration
720+
declaration_kind: @kind
721+
name: @name
722+
body: _
723+
(inheritance_specifier)* @bases
724+
(modifiers)* @mods)
725+
=>
726+
(class_like_declaration
727+
modifier: (modifier #{kind})
728+
modifier: {..mods}
729+
name: (identifier #{name})
730+
base_type: {..bases})
731+
),
732+
// Protocol declaration
733+
rule!(
734+
(protocol_declaration
735+
name: @name
736+
body: (protocol_body member: _* @members)
737+
(inheritance_specifier)* @bases
738+
(modifiers)* @mods)
739+
=>
740+
(class_like_declaration
741+
modifier: (modifier "protocol")
742+
modifier: {..mods}
743+
name: (identifier #{name})
744+
base_type: {..bases}
745+
member: {..members})
746+
),
747+
// Protocol function — return type and body statements both optional.
748+
rule!(
749+
(protocol_function_declaration
750+
name: @name
751+
(parameter)* @params
752+
return_type: _? @ret
753+
body: (block statement: _* @body_stmts)?
754+
(modifiers)* @mods)
755+
=>
756+
(function_declaration
757+
modifier: {..mods}
758+
name: (identifier #{name})
759+
parameter: {..params}
760+
return_type: {..ret}
761+
body: (block stmt: {..body_stmts}))
762+
),
763+
// Init declaration → constructor_declaration. Body statements optional;
764+
// body itself is also optional (protocol requirement).
765+
rule!(
766+
(init_declaration
767+
(parameter)* @params
768+
body: (block statement: _* @body_stmts)?
769+
(modifiers)* @mods)
770+
=>
771+
(constructor_declaration
772+
modifier: {..mods}
773+
parameter: {..params}
774+
body: (block stmt: {..body_stmts}))
775+
),
776+
// Deinit declaration → destructor_declaration. Body statements optional.
777+
rule!(
778+
(deinit_declaration
779+
body: (block statement: _* @body_stmts)
780+
(modifiers)* @mods)
781+
=>
782+
(destructor_declaration
783+
modifier: {..mods}
784+
body: (block stmt: {..body_stmts}))
785+
),
786+
// Typealias declaration
787+
rule!(
788+
(typealias_declaration name: @name value: @val (modifiers)* @mods)
789+
=>
790+
(type_alias_declaration
791+
modifier: {..mods}
792+
name: (identifier #{name})
793+
r#type: {val})
794+
),
795+
// Subscript declaration (not yet supported -- grammar needs to distinguish plain calls from subscript calls)
796+
rule!(
797+
(subscript_declaration (parameter)* @params (modifiers)* @mods)
798+
=>
799+
(unsupported_node)
800+
),
801+
// Associated type declaration (with optional bound)
802+
rule!(
803+
(associatedtype_declaration name: @name inherits_from: _? @bound (modifiers)* @mods)
804+
=>
805+
(associated_type_declaration
806+
modifier: {..mods}
807+
name: (identifier #{name})
808+
bound: {..bound})
809+
),
810+
// Protocol property declaration: translate each accessor requirement to an
811+
// accessor_declaration without a body, carrying the property name and type.
812+
// Subsequent accessors get chained_declaration (same flattening as computed properties).
813+
rule!(
814+
(protocol_property_declaration
815+
name: (pattern bound_identifier: @name)
816+
requirements: (protocol_property_requirements accessor: _+ @accessors)
817+
type: _? @ty
818+
(modifiers)* @mods)
819+
=>
820+
{..{
821+
let name_text = __yeast_ctx.ast.source_text(name.into());
822+
let mod_ids: Vec<usize> = mods.iter().map(|&m| m.into()).collect();
823+
let ty_ids: Vec<usize> = ty.iter().map(|&t| t.into()).collect();
824+
let acc_ids: Vec<usize> = accessors.iter().map(|&a| a.into()).collect();
825+
for (i, &acc_id) in acc_ids.iter().enumerate() {
826+
if i > 0 {
827+
let chained = __yeast_ctx.literal("modifier", "chained_declaration");
828+
__yeast_ctx.prepend_field(acc_id, "modifier", chained);
829+
}
830+
for &mod_id in mod_ids.iter().rev() {
831+
__yeast_ctx.prepend_field(acc_id, "modifier", mod_id);
832+
}
833+
for &ty_id in ty_ids.iter().rev() {
834+
__yeast_ctx.prepend_field(acc_id, "type", ty_id);
835+
}
836+
let ident = __yeast_ctx.literal("identifier", &name_text);
837+
__yeast_ctx.prepend_field(acc_id, "name", ident);
838+
}
839+
acc_ids
840+
}}
841+
),
842+
// getter_specifier / setter_specifier → bodyless accessor_declaration
843+
rule!((getter_specifier) => (accessor_declaration accessor_kind: (accessor_kind "get"))),
844+
rule!((setter_specifier) => (accessor_declaration accessor_kind: (accessor_kind "set"))),
845+
// protocol_property_requirements wrapper — should be consumed by above; fallback
846+
rule!((protocol_property_requirements accessor: _* @accs) => {..accs}),
847+
// Computed getter → accessor_declaration (body optional).
848+
rule!(
849+
(computed_getter body: (block statement: _* @body)?)
850+
=>
851+
(accessor_declaration
852+
accessor_kind: (accessor_kind "get")
853+
body: (block stmt: {..body}))
854+
),
855+
// Computed setter with explicit parameter name.
856+
rule!(
857+
(computed_setter parameter: @param body: (block statement: _* @body))
858+
=>
859+
(accessor_declaration
860+
accessor_kind: (accessor_kind "set")
861+
parameter: (parameter pattern: (name_pattern identifier: (identifier #{param})))
862+
body: (block stmt: {..body}))
863+
),
864+
// Computed setter without explicit parameter name; body optional.
865+
rule!(
866+
(computed_setter body: (block statement: _* @body)?)
867+
=>
868+
(accessor_declaration
869+
accessor_kind: (accessor_kind "set")
870+
body: (block stmt: {..body}))
871+
),
872+
// Computed modify → accessor_declaration
873+
rule!(
874+
(computed_modify body: (block statement: _* @body))
875+
=>
876+
(accessor_declaration
877+
accessor_kind: (accessor_kind "modify")
878+
body: (block stmt: {..body}))
879+
),
880+
// willset/didset block — spread to children
881+
rule!((willset_didset_block _* @clauses) => {..clauses}),
882+
// willset clause → accessor_declaration (body optional).
883+
rule!(
884+
(willset_clause body: (block statement: _* @body)?)
885+
=>
886+
(accessor_declaration
887+
accessor_kind: (accessor_kind "willSet")
888+
body: (block stmt: {..body}))
889+
),
890+
// didset clause → accessor_declaration (body optional).
891+
rule!(
892+
(didset_clause body: (block statement: _* @body)?)
893+
=>
894+
(accessor_declaration
895+
accessor_kind: (accessor_kind "didSet")
896+
body: (block stmt: {..body}))
897+
),
898+
// Preprocessor conditionals — unsupported
899+
rule!((diagnostic) => (unsupported_node)),
632900
// ---- Fallbacks ----
633901
rule!(
634902
(_)

unified/extractor/tests/corpus/swift/closures.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ top_level
7373
pattern:
7474
name_pattern
7575
identifier: identifier "x"
76-
type: unsupported_node "Int"
77-
return_type: unsupported_node "Int"
76+
type:
77+
named_type_expr
78+
name: identifier "Int"
79+
return_type:
80+
named_type_expr
81+
name: identifier "Int"
7882

7983
===
8084
Closure with shorthand parameters
@@ -246,10 +250,12 @@ top_level
246250
function:
247251
member_access_expr
248252
member: identifier "doThing"
249-
target: unsupported_node "self"
253+
target:
254+
name_expr
255+
identifier: identifier "self"
250256
capture_declaration:
251257
variable_declaration
252-
modifier: unsupported_node "weak" <-- ERROR: The field variable_declaration.modifier should contain modifier, but got unsupported_node
258+
modifier: modifier "weak"
253259
pattern:
254260
name_pattern
255261
identifier: identifier "self"
@@ -363,5 +369,9 @@ top_level
363369
pattern:
364370
name_pattern
365371
identifier: identifier "x"
366-
type: unsupported_node "Int"
367-
return_type: unsupported_node "Int"
372+
type:
373+
named_type_expr
374+
name: identifier "Int"
375+
return_type:
376+
named_type_expr
377+
name: identifier "Int"

unified/extractor/tests/corpus/swift/functions.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ top_level
135135
pattern:
136136
name_pattern
137137
identifier: identifier "b"
138-
return_type: unsupported_node "Int"
138+
return_type:
139+
named_type_expr
140+
name: identifier "Int"
139141

140142
===
141143
Function with named parameters
@@ -364,7 +366,9 @@ top_level
364366
pattern:
365367
name_pattern
366368
identifier: identifier "values"
367-
return_type: unsupported_node "Int"
369+
return_type:
370+
named_type_expr
371+
name: identifier "Int"
368372

369373
===
370374
Function call
@@ -553,4 +557,6 @@ top_level
553557
pattern:
554558
name_pattern
555559
identifier: identifier "x"
556-
return_type: unsupported_node "T"
560+
return_type:
561+
named_type_expr
562+
name: identifier "T"

unified/extractor/tests/corpus/swift/optionals-and-errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ top_level
227227
return_expr
228228
value: string_literal "\"\""
229229
name: identifier "read"
230-
return_type: unsupported_node "String"
230+
return_type:
231+
named_type_expr
232+
name: identifier "String"
231233

232234
===
233235
Do-catch

0 commit comments

Comments
 (0)