Skip to content

Commit 1bde900

Browse files
authored
Merge pull request #849 from ahoppen/ahoppen/swiftparserdiagnostics-module
Move SwiftParser/Diagnostics to its own module
2 parents 87d4a8e + 3f7fb2e commit 1bde900

23 files changed

+155
-72
lines changed

Examples/.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
.DS_Store
2-
/.build
1+
# Ignore Data Xcode stores when opening the project directly
2+
/.swiftpm/xcode
Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
11
import SwiftSyntaxBuilder
22

3+
extension String {
4+
func withFirstLetterUppercased() -> String {
5+
if let firstLetter = self.first {
6+
return firstLetter.uppercased() + self.dropFirst()
7+
} else {
8+
return self
9+
}
10+
}
11+
}
12+
313
/// This example will print the following code:
414
///
515
/// ```
6-
/// import Foundation
7-
/// import UIKit
8-
/// class SomeViewController{
9-
/// let tableView: UITableView
16+
/// struct Person {
17+
/// var lastName: String
18+
/// func withLastName(_ lastName: String) -> Person {
19+
/// var result = self
20+
/// result.lastName = lastName
21+
/// return result
22+
/// }
23+
/// var firstName: String
24+
/// func withFirstName(_ firstName: String) -> Person {
25+
/// var result = self
26+
/// result.firstName = firstName
27+
/// return result
28+
/// }
29+
/// var age: Int
30+
/// func withAge(_ age: Int) -> Person {
31+
/// var result = self
32+
/// result.age = age
33+
/// return result
34+
/// }
1035
/// }
1136
/// ```
1237
///
1338
@main
1439
struct Main {
1540
static func main() {
41+
let properties = [
42+
"firstName": "String",
43+
"lastName": "String",
44+
"age": "Int",
45+
]
46+
1647
let source = SourceFile {
17-
ImportDecl(path: "Foundation")
18-
ImportDecl(path: "UIKit")
19-
ClassDecl(identifier: "SomeViewController") {
20-
VariableDecl(.let, name: "tableView", type: "UITableView")
48+
StructDecl(identifier: "Person") {
49+
for (propertyName, propertyType) in properties {
50+
VariableDecl("var \(propertyName): \(propertyType)")
51+
52+
FunctionDecl("""
53+
func with\(propertyName.withFirstLetterUppercased())(_ \(propertyName): \(propertyType)) -> Person {
54+
var result = self
55+
result.\(propertyName) = \(propertyName)
56+
return result
57+
}
58+
"""
59+
)
60+
}
2161
}
2262
}
2363

24-
let syntax = source.build()
25-
26-
var text = ""
27-
syntax.write(to: &text)
28-
29-
print(text)
64+
print(source.formatted().description)
3065
}
3166
}

Package.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let package = Package(
4848
.library(name: "SwiftDiagnostics", type: .static, targets: ["SwiftDiagnostics"]),
4949
.library(name: "SwiftOperators", type: .static, targets: ["SwiftOperators"]),
5050
.library(name: "SwiftParser", type: .static, targets: ["SwiftParser"]),
51+
.library(name: "SwiftParserDiagnostics", type: .static, targets: ["SwiftParserDiagnostics"]),
5152
.library(name: "SwiftSyntax", type: .static, targets: ["SwiftSyntax"]),
5253
.library(name: "SwiftSyntaxParser", type: .static, targets: ["SwiftSyntaxParser"]),
5354
.library(name: "SwiftSyntaxBuilder", type: .static, targets: ["SwiftSyntaxBuilder"]),
@@ -96,7 +97,7 @@ let package = Package(
9697
),
9798
.target(
9899
name: "SwiftSyntaxBuilder",
99-
dependencies: ["SwiftBasicFormat", "SwiftSyntax", "SwiftParser"],
100+
dependencies: ["SwiftBasicFormat", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics"],
100101
exclude: [
101102
"CMakeLists.txt",
102103
"SyntaxExpressibleByStringInterpolationConformances.swift.gyb",
@@ -117,7 +118,7 @@ let package = Package(
117118
),
118119
.target(
119120
name: "SwiftParser",
120-
dependencies: ["SwiftBasicFormat", "SwiftDiagnostics", "SwiftSyntax"],
121+
dependencies: ["SwiftDiagnostics", "SwiftSyntax"],
121122
exclude: [
122123
"CMakeLists.txt",
123124
"README.md",
@@ -126,6 +127,13 @@ let package = Package(
126127
"DeclarationAttribute.swift.gyb",
127128
]
128129
),
130+
.target(
131+
name: "SwiftParserDiagnostics",
132+
dependencies: ["SwiftBasicFormat", "SwiftDiagnostics", "SwiftParser", "SwiftSyntax"],
133+
exclude: [
134+
"CMakeLists.txt",
135+
]
136+
),
129137
.target(
130138
name: "SwiftOperators",
131139
dependencies: ["SwiftSyntax", "SwiftParser", "SwiftDiagnostics"],
@@ -135,8 +143,8 @@ let package = Package(
135143
),
136144
.target(
137145
name: "SwiftCompilerSupport",
138-
dependencies: [
139-
"SwiftSyntax", "SwiftParser", "SwiftDiagnostics", "SwiftOperators"],
146+
dependencies: ["SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics",
147+
"SwiftDiagnostics", "SwiftOperators"],
140148
exclude: [
141149
"CMakeLists.txt",
142150
"SwiftCompilerSupport.h"
@@ -156,12 +164,12 @@ let package = Package(
156164
),
157165
.executableTarget(
158166
name: "swift-parser-cli",
159-
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftOperators", "_SwiftSyntaxMacros",
167+
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators", "_SwiftSyntaxMacros",
160168
.product(name: "ArgumentParser", package: "swift-argument-parser")]
161169
),
162170
.testTarget(
163171
name: "SwiftDiagnosticsTest",
164-
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftParser"]
172+
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftParser", "SwiftParserDiagnostics"]
165173
),
166174
.testTarget(
167175
name: "SwiftSyntaxTest",
@@ -192,6 +200,10 @@ let package = Package(
192200
dependencies: ["SwiftDiagnostics", "SwiftOperators", "SwiftParser",
193201
"_SwiftSyntaxTestSupport", "SwiftSyntaxBuilder"]
194202
),
203+
.testTarget(
204+
name: "SwiftParserDiagnosticsTest",
205+
dependencies: ["SwiftDiagnostics", "SwiftParserDiagnostics"]
206+
),
195207
.testTarget(
196208
name: "SwiftOperatorsTest",
197209
dependencies: ["SwiftOperators", "_SwiftSyntaxTestSupport",

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_subdirectory(SwiftBasicFormat)
1010
add_subdirectory(SwiftSyntax)
1111
add_subdirectory(SwiftDiagnostics)
1212
add_subdirectory(SwiftParser)
13+
add_subdirectory(SwiftParserDiagnostics)
1314
add_subdirectory(SwiftOperators)
1415
add_subdirectory(SwiftSyntaxBuilder)
1516
add_subdirectory(_SwiftSyntaxMacros)

Sources/SwiftCompilerSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ target_link_libraries(SwiftCompilerSupport PUBLIC
1414
SwiftSyntax
1515
SwiftDiagnostics
1616
SwiftParser
17+
SwiftParserDiagnostics
1718
SwiftOperators
1819
)
1920

Sources/SwiftCompilerSupport/ConsistencyCheck.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import SwiftOperators
1414
@_spi(Testing) @_spi(RawSyntax) import SwiftParser
15+
import SwiftParserDiagnostics
1516
@_spi(RawSyntax) import SwiftSyntax
1617

1718
extension Syntax {

Sources/SwiftParser/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,11 @@ add_library(SwiftParser STATIC
3131
TriviaParser.swift
3232
Types.swift
3333

34-
Diagnostics/DiagnosticExtensions.swift
35-
Diagnostics/MissingNodesError.swift
36-
Diagnostics/ParserDiagnosticMessages.swift
37-
Diagnostics/ParseDiagnosticsGenerator.swift
38-
Diagnostics/PresenceUtils.swift
39-
Diagnostics/SyntaxExtensions.swift
40-
Diagnostics/Utils.swift
41-
4234
gyb_generated/DeclarationAttribute.swift
4335
gyb_generated/DeclarationModifier.swift
4436
gyb_generated/TypeAttribute.swift)
4537

4638
target_link_libraries(SwiftParser PUBLIC
47-
SwiftBasicFormat
4839
SwiftSyntax
4940
SwiftDiagnostics)
5041

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,46 @@ enum SwitchCaseStart: RawTokenKindSubset {
631631
}
632632
}
633633

634+
public enum TypeSpecifier: RawTokenKindSubset {
635+
case inoutKeyword
636+
case owned
637+
case shared
638+
639+
init?(lexeme: Lexer.Lexeme) {
640+
switch (lexeme.tokenKind, lexeme.tokenText) {
641+
case (.inoutKeyword, _): self = .inoutKeyword
642+
case (.identifier, "__owned"): self = .owned
643+
case (.identifier, "__shared"): self = .shared
644+
default: return nil
645+
}
646+
}
647+
648+
public init?(token: TokenSyntax) {
649+
switch (token.tokenKind, token.text) {
650+
case (.inoutKeyword, _): self = .inoutKeyword
651+
case (.contextualKeyword, "__owned"): self = .owned
652+
case (.contextualKeyword, "__shared"): self = .shared
653+
default: return nil
654+
}
655+
}
656+
657+
var rawTokenKind: RawTokenKind {
658+
switch self {
659+
case .inoutKeyword: return .inoutKeyword
660+
case .owned: return .identifier
661+
case .shared: return .identifier
662+
}
663+
}
664+
665+
var contextualKeyword: SyntaxText? {
666+
switch self {
667+
case .inoutKeyword: return nil
668+
case .owned: return "__owned"
669+
case .shared: return "__shared"
670+
}
671+
}
672+
}
673+
634674
// MARK: Expression start
635675

636676
enum AwaitTryMove: RawTokenKindSubset {
@@ -839,46 +879,6 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
839879
}
840880
}
841881

842-
enum TypeSpecifier: RawTokenKindSubset {
843-
case inoutKeyword
844-
case owned
845-
case shared
846-
847-
init?(lexeme: Lexer.Lexeme) {
848-
switch (lexeme.tokenKind, lexeme.tokenText) {
849-
case (.inoutKeyword, _): self = .inoutKeyword
850-
case (.identifier, "__owned"): self = .owned
851-
case (.identifier, "__shared"): self = .shared
852-
default: return nil
853-
}
854-
}
855-
856-
init?(token: TokenSyntax) {
857-
switch (token.tokenKind, token.text) {
858-
case (.inoutKeyword, _): self = .inoutKeyword
859-
case (.contextualKeyword, "__owned"): self = .owned
860-
case (.contextualKeyword, "__shared"): self = .shared
861-
default: return nil
862-
}
863-
}
864-
865-
var rawTokenKind: RawTokenKind {
866-
switch self {
867-
case .inoutKeyword: return .inoutKeyword
868-
case .owned: return .identifier
869-
case .shared: return .identifier
870-
}
871-
}
872-
873-
var contextualKeyword: SyntaxText? {
874-
switch self {
875-
case .inoutKeyword: return nil
876-
case .owned: return "__owned"
877-
case .shared: return "__shared"
878-
}
879-
}
880-
}
881-
882882
/// Union of the following token kind subsets:
883883
/// - `AwaitTry`
884884
/// - `ExpressionPrefixOperator`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_library(SwiftParserDiagnostics STATIC
10+
DiagnosticExtensions.swift
11+
MissingNodesError.swift
12+
ParserDiagnosticMessages.swift
13+
ParseDiagnosticsGenerator.swift
14+
PresenceUtils.swift
15+
SyntaxExtensions.swift
16+
Utils.swift)
17+
18+
target_link_libraries(SwiftParserDiagnostics PUBLIC
19+
SwiftBasicFormat
20+
SwiftSyntax
21+
SwiftDiagnostics)
22+
23+
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParserDiagnostics)
24+
25+
# NOTE: workaround for CMake not setting up include flags yet
26+
set_target_properties(SwiftParserDiagnostics PROPERTIES
27+
INTERFACE_INCLUDE_DIRECTORIES
28+
"${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}")
29+
30+
install(TARGETS SwiftParserDiagnostics
31+
EXPORT SwiftSyntaxTargets
32+
ARCHIVE DESTINATION lib
33+
LIBRARY DESTINATION lib
34+
RUNTIME DESTINATION bin)

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift renamed to Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SwiftDiagnostics
14+
import SwiftParser
1415
@_spi(RawSyntax) import SwiftSyntax
1516

1617
public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {

Sources/SwiftSyntaxBuilder/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ add_library(SwiftSyntaxBuilder STATIC
3636
)
3737

3838
target_link_libraries(SwiftSyntaxBuilder PUBLIC
39+
SwiftBasicFormat
3940
SwiftParser
41+
SwiftParserDiagnostics
4042
SwiftSyntax
4143
)
4244

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
@_spi(RawSyntax) import SwiftSyntax
1414
@_spi(RawSyntax) import SwiftParser
15+
import SwiftParserDiagnostics
1516
import SwiftDiagnostics
1617
import SwiftBasicFormat
1718

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import SwiftDiagnostics
1414
import SwiftSyntax
1515
@_spi(Testing) import _SwiftSyntaxMacros
1616
import SwiftParser
17+
import SwiftParserDiagnostics
1718
import SwiftOperators
1819
import Foundation
1920
import ArgumentParser

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import _SwiftSyntaxTestSupport
1414
import XCTest
1515
import SwiftDiagnostics
1616
import SwiftParser
17+
import SwiftParserDiagnostics
1718

1819
final class DiagnosticsFormatterTests: XCTestCase {
1920

Tests/SwiftParserTest/DiagnosticInfrastructureTests.swift renamed to Tests/SwiftParserDiagnosticsTest/DiagnosticInfrastructureTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import XCTest
1414
import SwiftDiagnostics
15-
import SwiftParser
15+
import SwiftParserDiagnostics
1616

1717
public class DiagnosticInfrastructureTests: XCTestCase {
1818
public func testDiagnosticID() {

Tests/SwiftParserTest/Assertions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import XCTest
1414
@_spi(RawSyntax) import SwiftSyntax
1515
@_spi(Testing) @_spi(RawSyntax) import SwiftParser
16+
import SwiftParserDiagnostics
1617
import SwiftDiagnostics
1718
import _SwiftSyntaxTestSupport
1819

0 commit comments

Comments
 (0)