6
6
SwiftSemantics is a package that lets you
7
7
parse Swift code into its constituent declarations.
8
8
9
- Use [ SwiftSyntax] [ swiftsyntax ] to construct
9
+ Use [ SwiftSyntax] [ swiftsyntax ] to construct
10
10
an abstract syntax tree from Swift source code,
11
11
then walk the AST with the provided ` DeclarationCollector `
12
12
(or with your own ` SyntaxVisitor ` -conforming type)
@@ -63,14 +63,14 @@ collector.variables[2].modifiers.first?.detail // "set"
63
63
> For more information about SwiftSyntax,
64
64
> see [ this article from NSHipster] [ nshipster swiftsyntax ] .
65
65
66
- This package is used by [ swift-doc] [ swift-doc ]
67
- in coordination with [ SwiftMarkup] [ swiftmarkup ]
66
+ This package is used by [ swift-doc] [ swift-doc ]
67
+ in coordination with [ SwiftMarkup] [ swiftmarkup ]
68
68
to generate documentation for Swift projects
69
69
_ ([ including this one] [ swiftsemantics documentation ] )_ .
70
70
71
71
## Requirements
72
72
73
- - Swift 5.1+
73
+ - Swift 5.2 or 5.3
74
74
75
75
## Installation
76
76
@@ -79,18 +79,22 @@ _([including this one][swiftsemantics documentation])_.
79
79
Add the SwiftSemantics package to your target dependencies in ` Package.swift ` :
80
80
81
81
``` swift
82
+ // swift-tools-version:5.3
83
+
82
84
import PackageDescription
83
85
84
86
let package = Package (
85
87
name : " YourProject" ,
86
88
dependencies : [
87
89
.package (
90
+ name : " SwiftSemantics" ,
88
91
url : " https://github.com/SwiftDocOrg/SwiftSemantics" ,
89
- from : " 0.0.1 "
92
+ from : " 0.2.0 "
90
93
),
91
94
.package (
92
- url : " https://github.com/apple/swift-syntax.git" ,
93
- from : " 0.50100.0"
95
+ name : " SwiftSyntax" ,
96
+ url : " https://github.com/apple/swift-syntax.git" ,
97
+ from : " 0.50300.0"
94
98
),
95
99
]
96
100
)
@@ -102,7 +106,7 @@ Then run the `swift build` command to build your project.
102
106
103
107
Swift defines 17 different kinds of declarations,
104
108
each of which is represented by a corresponding type in SwiftSemantics
105
- that conforms to the
109
+ that conforms to the
106
110
[ ` Declaration ` protocol] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Declaration ) :
107
111
108
112
- [ ` AssociatedType ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/AssociatedType )
@@ -128,19 +132,19 @@ that conforms to the
128
132
> as well as [ unit tests] ( https://github.com/SwiftDocOrg/SwiftSemantics/tree/master/Tests/SwiftSemanticsTests ) .
129
133
130
134
The ` Declaration ` protocol itself has no requirements.
131
- However,
132
- adopting types share many of the same properties,
133
- such as
135
+ However,
136
+ adopting types share many of the same properties,
137
+ such as
134
138
[ ` attributes ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Class#attributes ) ,
135
- [ ` modifiers ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Class#modifiers ) ,
139
+ [ ` modifiers ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Class#modifiers ) ,
136
140
and
137
141
[ ` keyword ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Class#keyword ) .
138
142
139
143
SwiftSemantics declaration types are designed to
140
144
maximize the information provided by SwiftSyntax,
141
145
closely following the structure and naming conventions of syntax nodes.
142
146
In some cases,
143
- the library takes additional measures to refine results
147
+ the library takes additional measures to refine results
144
148
into more conventional interfaces.
145
149
For example,
146
150
the ` PrecedenceGroup ` type defines nested
150
154
enumerations for greater convenience and type safety.
151
155
However, in other cases,
152
156
results may be provided in their original, raw ` String ` values;
153
- this decision is typically motivated either by
157
+ this decision is typically motivated either by
154
158
concern for possible future changes to the language
155
159
or simply out of practicality.
156
160
@@ -163,26 +167,26 @@ There are, however, some details that warrant further discussion:
163
167
164
168
In Swift,
165
169
a class, enumeration, or structure may contain
166
- one or more initializers, properties, subscripts, and methods,
170
+ one or more initializers, properties, subscripts, and methods,
167
171
known as _ members_ .
168
172
A type can itself be a member of another type,
169
173
such as with ` CodingKeys ` enumerations nested within ` Codable ` -conforming types.
170
174
Likewise, a type may also have one or more associated type or type alias members.
171
175
172
- SwiftSemantics doesn't provide built-in support for
176
+ SwiftSemantics doesn't provide built-in support for
173
177
accessing type members directly from declaration values.
174
- This is probably the most surprising
175
- (and perhaps contentious)
178
+ This is probably the most surprising
179
+ (and perhaps contentious)
176
180
design decision made in the library so far,
177
181
but we believe it to be the most reasonable option available.
178
182
179
183
One motivation comes down to delegation of responsibility:
180
184
` DeclarationCollector ` and other types conforming to ` SyntaxVisitor `
181
185
walk the abstract syntax tree,
182
186
respond to nodes as they're visited,
183
- and decide whether to visit or skip a node's children.
187
+ and decide whether to visit or skip a node's children.
184
188
If a ` Declaration ` were to initialize its own members,
185
- it would have the effect of overriding
189
+ it would have the effect of overriding
186
190
the tree walker's decision to visit or skip any children.
187
191
We believe that an approach involving direct member initialization is inflexible
188
192
and more likely to produce unexpected results.
@@ -192,8 +196,8 @@ there wouldn't be a clear way to avoid needlessly initializing
192
196
the members of each top-level class
193
197
without potentially missing class declarations nested in other types.
194
198
195
- But really,
196
- the controlling motivation has to do with extensions ---
199
+ But really,
200
+ the controlling motivation has to do with extensions ---
197
201
especially when used across multiple files in a module.
198
202
Consider the following two Swift files in the same module:
199
203
@@ -219,7 +223,7 @@ and therefore misleading.
219
223
<details >
220
224
<summary ><em >And if that weren't enough to dissuade you...</em ></summary >
221
225
222
- Consider what happens when we throw generically-constrained extensions
226
+ Consider what happens when we throw generically-constrained extensions
223
227
and conditional compilation into the mix...
224
228
225
229
``` swift
@@ -240,7 +244,7 @@ reconciling declaration contexts to API consumers.
240
244
241
245
This is the approach we settled on for [ swift-doc] [ swift-doc ] ,
242
246
and it's worked reasonably well so far.
243
- That said,
247
+ That said,
244
248
we're certainly open to hearing any alternative approaches
245
249
and invite you to share any feedback about project architecture
246
250
by [ opening a new Issue] ( https://github.com/SwiftDocOrg/SwiftSemantics/issues/new ) .
@@ -250,13 +254,13 @@ by [opening a new Issue](https://github.com/SwiftDocOrg/SwiftSemantics/issues/ne
250
254
Swift is a complex language with many different rules and concepts,
251
255
and not all of them are represented directly in SwiftSemantics.
252
256
253
- Declaration membership,
257
+ Declaration membership,
254
258
discussed in the previous section,
255
259
is one such example.
256
260
Another is how
257
261
declaration access modifiers like ` public ` and ` private(set) `
258
262
aren't given any special treatment;
259
- they're [ ` Modifier ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Modifier ) values
263
+ they're [ ` Modifier ` ] ( https://github.com/SwiftDocOrg/SwiftSemantics/wiki/Modifier ) values
260
264
like any other.
261
265
262
266
This design strategy keeps the library narrowly focused
@@ -265,7 +269,7 @@ and more adaptable to language evolution over time.
265
269
You can extend SwiftSemantics in your own code
266
270
to encode any missing language concepts that are relevant to your problem.
267
271
For example,
268
- SwiftSemantics doesn't encode the concept of
272
+ SwiftSemantics doesn't encode the concept of
269
273
[ property wrappers] ( https://nshipster.com/propertywrapper/ ) ,
270
274
but you could use it as the foundation of your own representation:
271
275
@@ -306,9 +310,9 @@ _does_ offer this functionality.
306
310
307
311
## Known Issues
308
312
309
- - Xcode 11 cannot run unit tests (<kbd >⌘</kbd ><kbd >U</kbd >)
313
+ - Xcode 11 cannot run unit tests (<kbd >⌘</kbd ><kbd >U</kbd >)
310
314
when opening the SwiftSemantics package directly,
311
- as opposed first to generating an Xcode project file with
315
+ as opposed first to generating an Xcode project file with
312
316
` swift package generate-xcodeproj ` .
313
317
(The reported error is:
314
318
` Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib ` ).
0 commit comments