Skip to content

Commit 47f642f

Browse files
authored
Merge pull request #2870 from ahoppen/document-environment-variables
Move contributor documentation into a dedicated folder
2 parents f40481f + 6aa6d8b commit 47f642f

File tree

16 files changed

+51
-81
lines changed

16 files changed

+51
-81
lines changed

CONTRIBUTING.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Contributing
22

3+
This document contains notes about development and testing of swift-syntax, the [Contributor Documentation](Contributor%20Documentation) folder has some more in-depth documents.
4+
35
## Building & Testing
46

57
swift-syntax is a SwiftPM package, so you can build and test it using anything that supports packages - opening in Xcode, Visual Studio Code with [Swift for Visual Studio Code](https://github.com/swift-server/vscode-swift) installed, or through the command line using `swift build` and `swift test`.
@@ -13,7 +15,7 @@ swift-syntax is a SwiftPM package, so you can build and test it using anything t
1315
> - On the command line: Set the `SKIP_LONG_TESTS` environment variable to `1` when running tests, e.g by running `SKIP_LONG_TESTS=1 swift test`
1416
1517
> [!NOTE]
16-
> If you are only modifying the `SwiftParser` module, you can run only the parser tests by selecting the `SwiftParserTest` target.
18+
> If you are only modifying the `SwiftParser` module, you can run only the parser tests by selecting the `SwiftParserTest` target.
1719
> - In Xcode: Select the `SwiftParserTest` scheme. If you can’t find it in your Schemes, you need to manually add it using Product -> Scheme -> New Scheme…
1820
> - On the command line: Run `swift test --test-product SwiftParserTests`
1921
@@ -30,7 +32,7 @@ It will build a local copy of swift-format from the `main` branch and format the
3032
Generated source code is not formatted to make it easier to spot changes when re-running code generation.
3133

3234
> [!NOTE]
33-
> You can add a git hook to ensure all commits to the swift-syntax repository are correctly formatted.
35+
> You can add a git hook to ensure all commits to the swift-syntax repository are correctly formatted.
3436
> 1. Save the following contents to `swift-syntax/.git/hooks/pre-commit`
3537
> ```bash
3638
> #!/usr/bin/env bash
@@ -39,7 +41,7 @@ Generated source code is not formatted to make it easier to spot changes when re
3941
> swift "$SOURCE_DIR/swift-syntax-dev-utils" format --lint
4042
> ```
4143
> 2. Mark the file as executable: `chmod a+x swift-syntax/.git/hooks/pre-commit`
42-
> 3. If you have global git hooks installed, be sure to call them at the end of the script with `path/to/global/hooks/pre-commit "$@"`
44+
> 3. If you have global git hooks installed, be sure to call them at the end of the script with `path/to/global/hooks/pre-commit "$@"`
4345
4446
4547
## Generating Source Code
@@ -54,7 +56,7 @@ Alternatively, you can generate the files from the command line by running the f
5456
5557
## Running Pre-PR Checks Script
5658

57-
To ensure that your changes to the project are implemented correctly and do not introduce issues across the repository, a script has been provided to automate the necessary pre-PR checks.
59+
To ensure that your changes to the project are implemented correctly and do not introduce issues across the repository, a script has been provided to automate the necessary pre-PR checks.
5860

5961
```bash
6062
./swift-syntax-dev-utils local-pr-precheck
@@ -69,7 +71,7 @@ Prefer to squash the commits of your PR (*pull request*) and avoid adding commit
6971

7072
We prefer to not squash commits when merging a PR because, especially for larger PRs, it sometimes makes sense to split the PR into multiple self-contained chunks of changes. For example, a PR might do a refactoring first before adding a new feature or fixing a bug. This separation is useful for two reasons:
7173
- During review, the commits can be reviewed individually, making each review chunk smaller
72-
- In case this PR introduced a bug that is identified later, it is possible to check if it resulted from the refactoring or the actual change, thereby making it easier find the lines that introduce the issue.
74+
- In case this PR introduced a bug that is identified later, it is possible to check if it resulted from the refactoring or the actual change, thereby making it easier find the lines that introduce the issue.
7375

7476
## Opening a PR
7577

@@ -92,17 +94,17 @@ See the [dedicated section][section] on the Swift project website.
9294

9395
After you opened your PR, a maintainer will review it and test your changes in CI (*Continuous Integration*) by adding a `@swift-ci Please test` comment on the pull request. Once your PR is approved and CI has passed, the maintainer will merge your pull request.
9496

95-
Only contributors with [commit access](https://www.swift.org/contributing/#commit-access) are able to approve pull requests and trigger CI.
97+
Only contributors with [commit access](https://www.swift.org/contributing/#commit-access) are able to approve pull requests and trigger CI.
9698

9799
## Additional Verification
98100

99101
swift-syntax has additional verification methods (see the sections below) that provide more extensive validation. They have a significant runtime impact on swift-syntax and are thus not enabled by default when building swift-syntax, but are enabled in CI. If CI fails and you are unable to reproduce the failure locally, make sure that `SKIP_LONG_TESTS` is not set and try enabling these validations.
100102

101103
### RawSyntax Validation
102104

103-
When the `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION` environment variable is set while building swift-syntax, SwiftSyntax will perform additional validation that the layout of the syntax tree is correct. It validates that
105+
When the `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION` environment variable is set while building swift-syntax, SwiftSyntax will perform additional validation that the layout of the syntax tree is correct. It validates that
104106
1. every child of a syntax node has the correct kind, which should be guaranteed by the Swift type system in most cases
105-
2. each token only has one of the token kinds that is specified in the syntax tree layout of the `CodeGeneration` package.
107+
2. each token only has one of the token kinds that is specified in the syntax tree layout of the `CodeGeneration` package.
106108

107109
If this validation hits an assertion failure that a token is not accepted at a certain position in the syntax tree, double-check if the token kind that is being stored in the syntax tree actually makes sense here. If it does not, check if there is a parser bug or whether you need to remap the token kind. If it does make sense, add the token kind to `.token(choices:)` of the syntax node in CodeGeneration, re-generate that source code and run tests again.
108110

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift

-28
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,6 @@ let nodesSections: String = {
8181
return result
8282
}()
8383

84-
var contributingDocs: String = {
85-
let contributingDocsFolder = URL(fileURLWithPath: #filePath)
86-
.deletingLastPathComponent()
87-
.deletingLastPathComponent()
88-
.deletingLastPathComponent()
89-
.deletingLastPathComponent()
90-
.deletingLastPathComponent()
91-
.deletingLastPathComponent()
92-
.appendingPathComponent("Sources")
93-
.appendingPathComponent("SwiftSyntax")
94-
.appendingPathComponent("Documentation.docc")
95-
.appendingPathComponent("Contributing")
96-
97-
let files =
98-
(try? FileManager.default.contentsOfDirectory(at: contributingDocsFolder, includingPropertiesForKeys: nil)) ?? []
99-
100-
return files.compactMap { file in
101-
if file.pathExtension != "md" {
102-
return nil
103-
}
104-
let doccName = file.lastPathComponent
105-
.replacingOccurrences(of: ".md", with: "")
106-
.replacingOccurrences(of: " ", with: "-")
107-
return "- <doc:\(doccName)>"
108-
}.sorted().joined(separator: "\n")
109-
}()
110-
11184
let swiftSyntaxDoccIndex: String = {
11285
let templateURL = URL(fileURLWithPath: #filePath)
11386
.deletingLastPathComponent()
@@ -117,5 +90,4 @@ let swiftSyntaxDoccIndex: String = {
11790
return
11891
template
11992
.replacingOccurrences(of: "{{Nodes}}", with: nodesSections)
120-
.replacingOccurrences(of: "{{ContributingDocs}}", with: contributingDocs)
12193
}()

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndexTemplate.md

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
2525

2626
- <doc:Tutorial-Table-of-Contents>
2727

28-
### Contributing
29-
30-
These articles are intended for developers wishing to contribute to SwiftSyntax
31-
32-
{{ContributingDocs}}
33-
3428
### Syntax
3529

3630
- <doc:SwiftSyntax/Syntax>

Sources/SwiftSyntax/Documentation.docc/Contributing/ChangingSwiftSyntax.md renamed to Contributor Documentation/Changing Swift Syntax.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Discover the steps necessary to add, remove, and update syntax nodes
66

77
The surface area of the entire Swift programming language is represented in the
88
Swift Syntax API and its associated data structures. In order to keep these
9-
structures in sync, this project generates code using the Swift package
10-
`CodeGeneration`, located in the root of this repository.
9+
structures in sync, this project generates code using the Swift package
10+
`CodeGeneration`, located in the root of this repository.
1111

1212
## Regenerating Files
1313

14-
To re-generate the files after changing `CodeGeneration` run the `generate-swift-syntax`
14+
To re-generate the files after changing `CodeGeneration` run the `generate-swift-syntax`
1515
target of `CodeGeneration`.
1616

1717
On the command line, this would be
@@ -22,16 +22,16 @@ swift run --package-path CodeGeneration generate-swift-syntax
2222
## Adding and Removing Syntax Nodes
2323

2424
The files containing the definition of all of the syntax nodes are available
25-
under the [SyntaxSupport][SyntaxSupport] directory. These files
25+
under the [SyntaxSupport](https://github.com/swiftlang/swift-syntax/tree/main/CodeGeneration/Sources/SyntaxSupport) directory. These files
2626
are roughly divided according to broad syntactic categories in the Swift
2727
programming language. That is, the syntax nodes for classes, structs, and actors
28-
are defined in `DeclNodes.swift`, while the syntax nodes for string literals,
28+
are defined in `DeclNodes.swift`, while the syntax nodes for string literals,
2929
arrays, and tuples is defined in `ExprNodes.swift`.
3030

31-
To add a node to these files, it can be helpful to copy an existing node and
31+
To add a node to these files, it can be helpful to copy an existing node and
3232
alter its definition to suit the needs of the new syntax being defined. A syntax
33-
node consists of a kind (which also defines the node’s name), a base kind, and a list of
34-
child nodes. The base kind of a syntax node defines the class of syntax the node belongs to.
33+
node consists of a kind (which also defines the node’s name), a base kind, and a list of
34+
child nodes. The base kind of a syntax node defines the class of syntax the node belongs to.
3535
All nodes are at least of the `Syntax`
3636
kind, though some nodes may have a more specific kind like `Stmt` for
3737
statements or `Expr` for expressions. The SwiftSyntax library expands these
@@ -63,20 +63,16 @@ Node(
6363

6464
## Committing Changes
6565

66-
Added syntactic elements will require corresponding changes to the included
67-
SwiftParser library. For an introduction on parsing Swift nodes, see
68-
[the article on Parsing Basics][ParserBasics].
66+
Added syntactic elements will require corresponding changes to the included
67+
SwiftParser library. For an introduction on parsing Swift nodes, see
68+
[the article on Parsing Basics](https://github.com/swiftlang/swift-syntax/tree/main/Sources/SwiftParser/SwiftParser.docc/ParsingBasics.md).
6969

7070
When updating nodes, certain clients of SwiftSyntax that are relying upon those
71-
nodes will need to be changed in tandem. For example, the
72-
[swift-stress-tester][swift-stress-tester] uses SwiftSyntax, and the CI
71+
nodes will need to be changed in tandem. For example, the
72+
[swift-stress-tester](https://github.com/swiftlang/swift-stress-tester) uses SwiftSyntax, and the CI
7373
system will not allow changes to SwiftSyntax that break `swift-stress-tester`
7474
without a paired change to that repository.
7575

76-
[SyntaxSupport]: https://github.com/swiftlang/swift-syntax/tree/main/CodeGeneration/Sources/SyntaxSupport
77-
[swift-stress-tester]: https://github.com/swiftlang/swift-stress-tester
78-
[ParserBasics]: https://github.com/swiftlang/swift-syntax/tree/main/Sources/SwiftParser/SwiftParser.docc/ParsingBasics.md
79-
8076
@Comment {
8177
When docc resolves https://github.com/apple/swift-docc/issues/255, `ParserBasic` should be an inter-target link.
8278
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Environment variables
2+
3+
The following environment variables can be used to control some behavior in swift-syntax.
4+
5+
## Build time
6+
7+
- `SWIFT_BUILD_SCRIPT_ENVIRONMENT`: Enable assertions in release builds and remove the dependency of swift-syntax on os_log.
8+
- `SWIFTCI_USE_LOCAL_DEPS`: Assume that all of SourceKit-LSP’s dependencies are checked out next to it and use those instead of cloning the repositories. Primarily intended for CI environments that check out related branches.
9+
- `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION`: Mutate the input of `assertParse` test cases. See [CONTRIBUTING](../CONTRIBUTING.md) for more information.
10+
- `SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARIES`: Build all libraries as *dynamic* instead of *automatic*. This allows us to build swift-syntax as dynamic libraries, which in turn allows us to build SourceKit-LSP using SwiftPM on Windows. Linking swift-syntax statically into sourcekit-lsp exceeds the maximum number of exported symbols on Windows.
11+
- `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION`: Check that the layout of the syntax tree is correct when creating or modifying nodes. See [CONTRIBUTING](../CONTRIBUTING.md) for more information.
12+
13+
## Testing
14+
15+
- `SKIP_LONG_TESTS`: Skip tests that typically take more than 1s to execute.

Contributor Documentation/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributor Documentation
2+
3+
The following documentation documents are primarily intended for developers of swift-syntax.
4+
5+
- [Changing Swift Syntax](Changing%20Swift%20Syntax.md)
6+
- [Filing Parser Bug Reports](Filing%20Parser%20Bug%20Reports.md)
7+
- [Fixing Bugs](Fixing%20Bugs.md)
8+
- [Parser Design](Parser%20Design.md)
9+
- [Parser Recovery](Parser%20Recovery.md)
10+
- [Parsing Basics](Parsing%20Basics.md)
11+
- [When to use protocols in SwiftSyntax](When%20to%20use%20protocols%20in%20SwiftSyntax.md)
12+
- [`@_spi` Attribute](SPI%20Attribute.md)

Sources/SwiftParser/SwiftParser.docc/SwiftParser.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A parser for the Swift programming language.
44

55
## Overview
66

7-
The `SwiftParser` framework implements a parser that accepts Swift source text as input and produces a SwiftSyntax syntax tree.
7+
The `SwiftParser` framework implements a parser that accepts Swift source text as input and produces a SwiftSyntax syntax tree.
88

99
## Quickstart
1010

@@ -49,8 +49,3 @@ There are several design principles that govern the parser:
4949
- **Source-preserving**: SwiftSyntax is designed to maintain all “trivia” (including whitespace, comments, etc.) precisely as it occurs in the source text, so that a syntax tree can be rendered back into text that is byte-for-byte identical to the original source. The parser must maintain this property, regardless of whether the input text was well-formed Swift code.
5050
- **Minimal context**: The parser requires minimal context to parse Swift code, which consists of only those things required to handle a suitable Swift dialect, e.g., whether [regex literals](https://github.com/apple/swift-evolution/blob/main/proposals/0354-regex-literals.md) are supported. The parser can be invoked on any input source code, starting at any major production in the grammar (e.g., full source file, an individual type, an individual expression).
5151
- **Incremental**: A parse tree produced for a source file can be incrementally updated for a new version of that source file, reusing syntax nodes where possible to reduce computation overhead and memory.
52-
53-
### Development
54-
55-
- <doc:SwiftParser/FilingBugReports>
56-
- <doc:SwiftParser/FixingBugs>

Sources/SwiftSyntax/Documentation.docc/Contributing/Swift Version.md

-7
This file was deleted.

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
2525

2626
- <doc:Tutorial-Table-of-Contents>
2727

28-
### Contributing
29-
30-
These articles are intended for developers wishing to contribute to SwiftSyntax
31-
32-
- <doc:ChangingSwiftSyntax>
33-
- <doc:Existentials>
34-
- <doc:SPI>
35-
- <doc:Swift-Version>
36-
3728
### Syntax
3829

3930
- <doc:SwiftSyntax/Syntax>

0 commit comments

Comments
 (0)