Skip to content

Commit e674d66

Browse files
authored
Merge pull request #2812 from MAJKFL/swiftLexicalLookup-docc
[SwiftLexicalLookup][GSoC] Add a DocC article with description of `SwiftLexicalLookup`.
2 parents 8706e0f + 0374d87 commit e674d66

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleName</key>
6+
<string>SwiftLexicalLookup</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>SwiftLexicalLookup</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>com.apple.swift-lexical-lookup</string>
11+
<key>CFBundleDevelopmentRegion</key>
12+
<string>en</string>
13+
<key>CFBundleIconFile</key>
14+
<string>DocumentationIcon</string>
15+
<key>CFBundleIconName</key>
16+
<string>DocumentationIcon</string>
17+
<key>CFBundlePackageType</key>
18+
<string>DOCS</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>0.1.0</string>
21+
<key>CDDefaultCodeListingLanguage</key>
22+
<string>swift</string>
23+
<key>CFBundleVersion</key>
24+
<string>0.1.0</string>
25+
<key>CDAppleDefaultAvailability</key>
26+
<dict>
27+
<key>SwiftLexicalLookup</key>
28+
<array>
29+
<dict>
30+
<key>name</key>
31+
<string>macOS</string>
32+
<key>version</key>
33+
<string>10.15</string>
34+
</dict>
35+
</array>
36+
</dict>
37+
</dict>
38+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SwiftLexicalLookup
2+
3+
A library for performing lexical queries on Swift syntax trees.
4+
5+
## Overview
6+
7+
> Important: `SwiftLexicalLookup` is currently in it's experimental phase. Use `@_spi(Experimental) import SwiftLexicalLookup` to work with the library. Clients should not rely on the current implementation.
8+
9+
`SwiftLexicalLookup` provides APIs that traverse and extract information from the Swift syntax tree. Queries on the syntax tree can include responses to questions like: "Where is an error thrown here handled?" or "What is the source and destination of this `fallthrough` keyword?".
10+
11+
```swift
12+
func foo() throws {
13+
try! f() // <-- Error handled by `try!`
14+
15+
do {
16+
try f() // <-- Error handled by `do ... catch`
17+
} catch {
18+
throw f() // <-- Error handled by the throwing function
19+
}
20+
}
21+
```
22+
23+
There are also more complex queries, such as unqualified name lookup. In Swift, variables, declarations, and other names can be visible through scopes, and the goal of unqualified name lookup is to extract that information and produce a result that specifies what names are available at a given location in the source code. This process preserves information that is crucial for determining the visibility of variables, for instance, due to shadowing. The result of such a query is an array that associates scopes with names introduced within their bounds that matched the lookup.
24+
25+
```swift
26+
struct Foo<A, B> {
27+
let a = A()
28+
let b = B()
29+
30+
func bar(b: B) { // <-- `B` refers to the generic parameter `B`
31+
let a: A = a // <-- `a` refers to the member `a`
32+
let b: B = b // <-- `b` refers to the function parameter `b`
33+
print(self) // <-- `self` refers to the `struct` declaration
34+
}
35+
}
36+
```
37+
38+
`SwiftLexicalLookup` provides a lightweight and stateless unqualified lookup API that traverses the syntax tree. Every syntax node serves as an entry point for the name lookup through the `someNode.lookup(myIdentifier)` method. The behavior of the lookup can also be optionally configured through configurations that might, for example, change the behavior of top-level code. The result of the lookup method is an array of enums that associate appropriate `ScopeSyntax` nodes with names that match the lookup, ordered from the innermost to the outermost scope available at the origin of the lookup.
39+
40+
* Simple lookup queries return relevant syntax nodes.
41+
* The `LookupResult` enum associates a `ScopeSyntax` node with names matching the lookup within a particular lexical scope.
42+
* Names are represented by the `LookupName` enum, which stores the associated syntax node and might represent one of several kinds of names, including identifiers, declarations, or implicit names such as `self`.
43+
* Some scopes share common functionality between themselves. For this reason, `SwiftLexicalLookup` includes more specialized scope protocols, such as `TypeDeclarationScope`, which contains the `implicitInstanceAndTypeNames` property representing implicit `self` and `Self` names introduced at those scopes.

0 commit comments

Comments
 (0)