Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public let DECL_NODES: [Node] = [
kind: .accessorBlock,
base: .syntax,
nameForDiagnostics: nil,
parserFunction: "parseAccessorBlock",
traits: [
"Braced"
],
Expand All @@ -56,7 +57,16 @@ public let DECL_NODES: [Node] = [
),
Child(
name: "Accessors",
kind: .collection(kind: .accessorDeclList, collectionElementName: "Accessor")
kind: .nodeChoices(choices: [
Child(
name: "Accessors",
kind: .collection(kind: .accessorDeclList, collectionElementName: "Accessor")
),
Child(
name: "Getter",
kind: .node(kind: .codeBlockItemList)
),
])
),
Child(
name: "RightBrace",
Expand Down Expand Up @@ -1629,18 +1639,9 @@ public let DECL_NODES: [Node] = [
isOptional: true
),
Child(
name: "Accessors",
name: "AccessorBlock",
deprecatedName: "Accessor",
kind: .nodeChoices(choices: [
Child(
name: "Accessors",
kind: .node(kind: .accessorBlock)
),
Child(
name: "Getter",
kind: .node(kind: .codeBlock)
),
]),
kind: .node(kind: .accessorBlock),
isOptional: true
),
Child(
Expand Down Expand Up @@ -2190,18 +2191,9 @@ public let DECL_NODES: [Node] = [
isOptional: true
),
Child(
name: "Accessors",
name: "AccessorBlock",
deprecatedName: "Accessor",
kind: .nodeChoices(choices: [
Child(
name: "Accessors",
kind: .node(kind: .accessorBlock)
),
Child(
name: "Getter",
kind: .node(kind: .codeBlock)
),
]),
kind: .node(kind: .accessorBlock),
isOptional: true
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
[
// SwiftParser
GeneratedFileSpec(swiftParserGeneratedDir + ["IsLexerClassified.swift"], isLexerClassifiedFile),
GeneratedFileSpec(swiftParserGeneratedDir + ["Parser+Entry.swift"], parserEntryFile),
GeneratedFileSpec(swiftParserGeneratedDir + ["LayoutNodes+Parsable.swift"], parserEntryFile),
GeneratedFileSpec(swiftParserGeneratedDir + ["Parser+TokenSpecSet.swift"], parserTokenSpecSetFile),
GeneratedFileSpec(swiftParserGeneratedDir + ["TokenSpecStaticMembers.swift"], tokenSpecStaticMembersFile),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,6 @@ import Utils
let parserEntryFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")

try! ExtensionDeclSyntax("extension Parser") {
DeclSyntax(
"""
/// Parse the source code in the given string as Swift source file. See
/// `Parser.init` for more details.
public static func parse(
source: String
) -> SourceFileSyntax {
var parser = Parser(source)
return SourceFileSyntax.parse(from: &parser)
}
"""
)

DeclSyntax(
"""
/// Parse the source code in the given buffer as Swift source file. See
/// `Parser.init` for more details.
public static func parse(
source: UnsafeBufferPointer<UInt8>,
maximumNestingLevel: Int? = nil
) -> SourceFileSyntax {
var parser = Parser(source, maximumNestingLevel: maximumNestingLevel)
return SourceFileSyntax.parse(from: &parser)
}
"""
)

DeclSyntax(
"""
/// Parse the source code in the given string as Swift source file with support
/// for incremental parsing.
///
/// When parsing a source file for the first time, invoke `parseIncrementally`
/// with `parseTransition: nil`. This returns the initial tree as well as
/// ``LookaheadRanges``. If an edit is made to the source file, an
/// ``IncrementalParseTransition`` can be constructed from the initial tree
/// and its ``LookaheadRanges``. When invoking `parseIncrementally` again with
/// the post-edit source and that parse transition, the parser will re-use
/// nodes that haven’t changed.
///
/// - Parameters:
/// - source: The source code to parse
/// - parseTransition: If a similar source file has already been parsed, the
/// ``IncrementalParseTransition`` that contains the previous tree as well
/// as the edits that were performed to it.
/// - Returns: The parsed tree as well as the ``LookaheadRanges`` that describe
/// how far the parser looked ahead while parsing a node, which is
/// necessary to construct an ``IncrementalParseTransition`` for a
/// subsequent incremental parse
public static func parseIncrementally(
source: String,
parseTransition: IncrementalParseTransition?
) -> (tree: SourceFileSyntax, lookaheadRanges: LookaheadRanges) {
var parser = Parser(source, parseTransition: parseTransition)
return (SourceFileSyntax.parse(from: &parser), parser.lookaheadRanges)
}
"""
)

DeclSyntax(
"""
/// Parse the source code in the given buffer as Swift source file with support
/// for incremental parsing.
///
/// See doc comments in ``Parser/parseIncrementally(source:parseTransition:)``
public static func parseIncrementally(
source: UnsafeBufferPointer<UInt8>,
maximumNestingLevel: Int? = nil,
parseTransition: IncrementalParseTransition?
) -> (tree: SourceFileSyntax, lookaheadRanges: LookaheadRanges) {
var parser = Parser(source, maximumNestingLevel: maximumNestingLevel, parseTransition: parseTransition)
return (SourceFileSyntax.parse(from: &parser), parser.lookaheadRanges)
}
"""
)
}

DeclSyntax(
"""
public protocol SyntaxParseable: SyntaxProtocol {
Expand Down Expand Up @@ -128,33 +50,6 @@ let parserEntryFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}

try! ExtensionDeclSyntax("fileprivate extension Parser") {
DeclSyntax(
"""
mutating func parseRemainder<R: RawSyntaxNodeProtocol>(into: R) -> R {
guard !into.raw.kind.isSyntaxCollection, let layout = into.raw.layoutView else {
preconditionFailure("Only support parsing of non-collection layout nodes")
}

let remainingTokens = self.consumeRemainingTokens()
if remainingTokens.isEmpty {
return into
}

let existingUnexpected: [RawSyntax]
if let unexpectedNode = layout.children[layout.children.count - 1] {
precondition(unexpectedNode.is(RawUnexpectedNodesSyntax.self))
existingUnexpected = unexpectedNode.as(RawUnexpectedNodesSyntax.self).elements
} else {
existingUnexpected = []
}
let unexpected = RawUnexpectedNodesSyntax(elements: existingUnexpected + remainingTokens, arena: self.arena)

let withUnexpected = layout.replacingChild(at: layout.children.count - 1, with: unexpected.raw, arena: self.arena)
return R.init(withUnexpected)!
}
"""
)

DeclSyntax(
"""
mutating func parseNonOptionalCodeBlockItem() -> RawCodeBlockItemSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
} else {
try EnumDeclSyntax(
"""
public enum Element: SyntaxChildChoices
public enum Element: SyntaxChildChoices, SyntaxHashable
"""
) {
for choiceName in node.elementChoices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private func generateSyntaxChildChoices(for child: Child) throws -> EnumDeclSynt
return nil
}

return try! EnumDeclSyntax("public enum \(raw: child.name): SyntaxChildChoices") {
return try! EnumDeclSyntax("public enum \(raw: child.name): SyntaxChildChoices, SyntaxHashable") {
for choice in choices {
DeclSyntax("case `\(choice.varOrCaseName)`(\(raw: choice.syntaxNodeKind.syntaxType))")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax(
"""
/// Add all the elements of `expression` to this result builder, effectively flattening them.
///
/// - Note: This overload is disfavored to resolve an ambiguity when both the final result and
/// the elements are expressible by string interpolation. In that case we favor creating a
/// single element from the string literal.
@_disfavoredOverload
public static func buildExpression(_ expression: Self.FinalResult) -> Self.Component {
return expression.map { $0 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ let syntaxExpressibleByStringInterpolationConformancesFile = SourceFileSyntax(le
for node in SYNTAX_NODES where node.parserFunction != nil {
DeclSyntax("extension \(node.kind.syntaxType): SyntaxExpressibleByStringInterpolation {}")
}

// `SyntaxParsable` conformance for collection nodes is hand-written.
// We also need to hand-write the corresponding `SyntaxExpressibleByStringInterpolation` conformances.
DeclSyntax("extension AccessorDeclListSyntax: SyntaxExpressibleByStringInterpolation {}")
DeclSyntax("extension AttributeListSyntax: SyntaxExpressibleByStringInterpolation {}")
DeclSyntax("extension CodeBlockItemListSyntax: SyntaxExpressibleByStringInterpolation {}")
DeclSyntax("extension MemberBlockItemListSyntax: SyntaxExpressibleByStringInterpolation {}")
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ let package = Package(

.target(
name: "SwiftSyntaxMacroExpansion",
dependencies: ["SwiftSyntax", "SwiftSyntaxMacros", "SwiftDiagnostics"],
dependencies: ["SwiftSyntax", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftDiagnostics"],
exclude: ["CMakeLists.txt"]
),

Expand Down
7 changes: 5 additions & 2 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ open class BasicFormat: SyntaxRewriter {
private var previousToken: TokenSyntax? = nil

public init(
indentationWidth: Trivia = .spaces(4),
indentationWidth: Trivia? = nil,
initialIndentation: Trivia = [],
viewMode: SyntaxTreeViewMode = .sourceAccurate
) {
self.indentationWidth = indentationWidth
// Default to 4 spaces if no indentation was passed.
// In the future, we could consider inferring the indentation width from the
// source file to format in case it is already partially formatted.
self.indentationWidth = indentationWidth ?? .spaces(4)
self.indentationStack = [(indentation: initialIndentation, isUserDefined: false)]
super.init(viewMode: viewMode)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_swift_host_library(SwiftParser
Attributes.swift
Availability.swift
CharacterInfo.swift
CollectionNodes+Parsable.swift
Declarations.swift
Directives.swift
Expressions.swift
Expand All @@ -21,6 +22,7 @@ add_swift_host_library(SwiftParser
Nominals.swift
Parameters.swift
Parser.swift
ParseSourceFile.swift
Patterns.swift
TokenSpec.swift
TokenSpecSet.swift
Expand All @@ -37,7 +39,7 @@ add_swift_host_library(SwiftParser
Types.swift

generated/IsLexerClassified.swift
generated/Parser+Entry.swift
generated/LayoutNodes+Parsable.swift
generated/Parser+TokenSpecSet.swift
generated/TokenSpecStaticMembers.swift

Expand Down
Loading