@@ -290,6 +290,10 @@ public struct Type: SyntaxExpressibleByStringInterpolation {
290
290
_syntax = syntax
291
291
}
292
292
293
+ public init ( _ syntax: any TypeSyntaxProtocol ) {
294
+ _syntax = TypeSyntax ( syntax)
295
+ }
296
+
293
297
public init ( stringInterpolation: SyntaxStringInterpolation ) {
294
298
_syntax = TypeSyntax ( stringInterpolation: stringInterpolation)
295
299
}
@@ -448,6 +452,104 @@ extension Collection where Element == AttributeListElement {
448
452
}
449
453
}
450
454
455
+ public struct Expression {
456
+ public var _syntax : ExprSyntax
457
+
458
+ public init ( _ syntax: ExprSyntax ) {
459
+ _syntax = syntax
460
+ }
461
+
462
+ public init ( _ syntax: any ExprSyntaxProtocol ) {
463
+ _syntax = ExprSyntax ( syntax)
464
+ }
465
+
466
+ /// Gets the contents of the expression if it's a string literal with no interpolation.
467
+ public var asSimpleStringLiteral : String ? {
468
+ guard
469
+ let literal = _syntax. as ( StringLiteralExprSyntax . self) ,
470
+ literal. segments. count == 1 ,
471
+ case let . stringSegment( segment) ? = literal. segments. first
472
+ else {
473
+ return nil
474
+ }
475
+ return segment. content. text
476
+ }
477
+ }
478
+
479
+ public struct MacroAttribute {
480
+ public var _syntax : AttributeSyntax
481
+ public var _argumentListSyntax : TupleExprElementListSyntax
482
+
483
+ public init ? ( _ syntax: AttributeSyntax ) {
484
+ guard case let . argumentList( arguments) = syntax. argument else {
485
+ return nil
486
+ }
487
+ _syntax = syntax
488
+ _argumentListSyntax = arguments
489
+ }
490
+
491
+ public func argument( labeled label: String ) -> Expression ? {
492
+ ( _argumentListSyntax. first { element in
493
+ return element. label? . text == label
494
+ } ? . expression) . map ( Expression . init)
495
+ }
496
+
497
+ public var arguments : [ Expression ] {
498
+ Array ( _argumentListSyntax) . map { argument in
499
+ Expression ( argument. expression)
500
+ }
501
+ }
502
+
503
+ public var name : Type {
504
+ Type ( _syntax. attributeName)
505
+ }
506
+ }
507
+
508
+ public struct Struct {
509
+ public var _syntax : StructDeclSyntax
510
+
511
+ public init ( _ syntax: StructDeclSyntax ) {
512
+ _syntax = syntax
513
+ }
514
+
515
+ public init ? ( _ syntax: any DeclGroupSyntax ) {
516
+ guard let syntax = syntax. as ( StructDeclSyntax . self) else {
517
+ return nil
518
+ }
519
+ _syntax = syntax
520
+ }
521
+
522
+ // TODO: Add members property to all declgroupsyntax decls through protocol default impl
523
+ public var members : [ Decl ] {
524
+ _syntax. memberBlock. members. map ( \. decl) . map ( Decl . init)
525
+ }
526
+
527
+ public var inheritedTypes : [ Type ] {
528
+ _syntax. inheritanceClause? . inheritedTypeCollection. map ( \. typeName) . map ( Type . init) ?? [ ]
529
+ }
530
+ }
531
+
532
+ public struct Decl {
533
+ public var _syntax : DeclSyntax
534
+
535
+ public init ( _ syntax: DeclSyntax ) {
536
+ _syntax = syntax
537
+ }
538
+
539
+ public init ( _ syntax: any DeclSyntaxProtocol ) {
540
+ _syntax = DeclSyntax ( syntax)
541
+ }
542
+
543
+ // TODO: Add conversions for all possible member types
544
+ public var asEnum : Enum ? {
545
+ _syntax. as ( EnumDeclSyntax . self) . map ( Enum . init)
546
+ }
547
+
548
+ public var asStruct : Struct ? {
549
+ _syntax. as ( StructDeclSyntax . self) . map ( Struct . init)
550
+ }
551
+ }
552
+
451
553
extension FunctionDeclSyntax {
452
554
/// Gets the signature's effect specifiers, or returns a default effect specifiers
453
555
/// syntax (without any specifiers).
@@ -552,6 +654,12 @@ extension CodeBlockSyntax {
552
654
}
553
655
}
554
656
657
+ extension DeclGroupSyntax {
658
+ public var isPublic : Bool {
659
+ modifiers? . contains { $0. name. tokenKind == . keyword( . public) } == true
660
+ }
661
+ }
662
+
555
663
// TODO: Figure out a destructuring implementation that uses variadic generics (tricky without same type requirements)
556
664
public func destructure< Element> ( _ elements: some Sequence < Element > ) -> ( ) ? {
557
665
let array = Array ( elements)
@@ -561,7 +669,9 @@ public func destructure<Element>(_ elements: some Sequence<Element>) -> ()? {
561
669
return ( )
562
670
}
563
671
564
- public func destructure< Element> ( _ elements: some Sequence < Element > ) -> ( Element ) ? {
672
+ /// Named differently to allow type inference to still work correctly (single element tuples
673
+ /// are weird in Swift).
674
+ public func destructureSingle< Element> ( _ elements: some Sequence < Element > ) -> ( Element ) ? {
565
675
let array = Array ( elements)
566
676
guard array. count == 1 else {
567
677
return nil
@@ -607,8 +717,10 @@ public func destructure(_ type: NominalType) -> (String, ())? {
607
717
}
608
718
}
609
719
610
- public func destructure( _ type: NominalType ) -> ( String , ( Type ) ) ? {
611
- destructure ( type. genericArguments ?? [ ] ) . map { arguments in
720
+ /// Named differently to allow type inference to still work correctly (single element tuples
721
+ /// are weird in Swift).
722
+ public func destructureSingle( _ type: NominalType ) -> ( String , ( Type ) ) ? {
723
+ destructureSingle ( type. genericArguments ?? [ ] ) . map { arguments in
612
724
( type. name, arguments)
613
725
}
614
726
}
@@ -643,8 +755,10 @@ public func destructure(_ type: FunctionType) -> ((), Type)? {
643
755
}
644
756
}
645
757
646
- public func destructure( _ type: FunctionType ) -> ( ( Type ) , Type ) ? {
647
- destructure ( type. parameters) . map { parameters in
758
+ /// Named differently to allow type inference to still work correctly (single element tuples
759
+ /// are weird in Swift).
760
+ public func destructureSingle( _ type: FunctionType ) -> ( ( Type ) , Type ) ? {
761
+ destructureSingle ( type. parameters) . map { parameters in
648
762
( parameters, type. returnType)
649
763
}
650
764
}
@@ -687,13 +801,15 @@ public func destructure(_ type: Type) -> DestructuredType<()>? {
687
801
}
688
802
}
689
803
690
- public func destructure( _ type: Type ) -> DestructuredType < ( Type ) > ? {
804
+ /// Named differently to allow type inference to still work correctly (single element tuples
805
+ /// are weird in Swift).
806
+ public func destructureSingle( _ type: Type ) -> DestructuredType < ( Type ) > ? {
691
807
if let type = type. asNominalType {
692
- return destructure ( type) . map { destructured in
808
+ return destructureSingle ( type) . map { destructured in
693
809
. nominal( name: destructured. 0 , genericArguments: destructured. 1 )
694
810
}
695
811
} else if let type = type. asFunctionType {
696
- return destructure ( type) . map { destructured in
812
+ return destructureSingle ( type) . map { destructured in
697
813
. function( parameterTypes: destructured. 0 , returnType: destructured. 1 )
698
814
}
699
815
} else {
0 commit comments