Skip to content

Commit a12f46d

Browse files
authored
Merge pull request #80 from akyrtzi/syntaxkind-to-internal
Make `SyntaxKind` internal
2 parents 4e710bf + 141949b commit a12f46d

File tree

5 files changed

+40
-71
lines changed

5 files changed

+40
-71
lines changed

Sources/SwiftSyntax/IncrementalEditTransition.swift renamed to Sources/SwiftSyntax/IncrementalParseTransition.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===------ IncrementalEditTransition.swift - Edit Transition State -------===//
1+
//===------ IncrementalParseTransition.swift - Parse Transition State -----===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Accepts the re-used `Syntax` nodes that `IncrementalEditTransition`
13+
/// Accepts the re-used `Syntax` nodes that `IncrementalParseTransition`
1414
/// determined they should be re-used for a parse invocation.
1515
///
1616
/// The client can use this information to potentially avoid unnecessary work
@@ -42,10 +42,10 @@ public final class IncrementalParseReusedNodeCollector:
4242
}
4343

4444
/// Keeps track of a previously parsed syntax tree and the source edits that
45-
/// occurred since it was created and provides the `IncrementalParseLookup`
46-
/// implementation to allow the parser to skip regions of the new source that
47-
/// can be associated with valid `Syntax` nodes from the previous tree.
48-
public final class IncrementalEditTransition: IncrementalParseLookup {
45+
/// occurred since it was created and provides a mechanism for the parser to
46+
/// skip regions of an incrementally updated source that was already parsed
47+
/// during a previous parse invocation.
48+
public final class IncrementalParseTransition {
4949
// The implementation is based on `SyntaxParsingCache` from the swift
5050
// repository.
5151

@@ -65,7 +65,7 @@ public final class IncrementalEditTransition: IncrementalParseLookup {
6565
public init(previousTree: SourceFileSyntax,
6666
edits: [SourceEdit],
6767
reusedNodeDelegate: IncrementalParseReusedNodeDelegate? = nil) {
68-
assert(IncrementalEditTransition.isEditArrayValid(edits))
68+
assert(IncrementalParseTransition.isEditArrayValid(edits))
6969
self.previousTree = previousTree
7070
self.edits = edits
7171
self.reusedDelegate = reusedNodeDelegate
@@ -88,7 +88,19 @@ public final class IncrementalEditTransition: IncrementalParseLookup {
8888
return true
8989
}
9090

91-
public func lookUp(_ newOffset: Int, kind: SyntaxKind) -> Syntax? {
91+
/// Does a lookup to see if the current source `offset` should be associated
92+
/// with a known `Syntax` node and its region skipped during parsing.
93+
///
94+
/// The implementation is responsible for checking whether an incremental edit
95+
/// has invalidated the previous `Syntax` node.
96+
///
97+
/// - Parameters:
98+
/// - offset: The byte offset of the source string that is currently parsed.
99+
/// - kind: The `SyntaxKind` that the parser expects at this position.
100+
/// - Returns: A `Syntax` node from the previous parse invocation,
101+
/// representing the contents of this region, if it is still valid
102+
/// to re-use. `nil` otherwise.
103+
func lookUp(_ newOffset: Int, kind: SyntaxKind) -> _SyntaxBase? {
92104
guard let prevOffset = translateToPreEditOffset(newOffset) else {
93105
return nil
94106
}
@@ -117,7 +129,7 @@ public final class IncrementalEditTransition: IncrementalParseLookup {
117129
}
118130
let childEnd = childOffset + child.byteSize
119131
if childOffset <= prevOffset && prevOffset < childEnd {
120-
return lookUpFrom(child.base, nodeOffset: childOffset,
132+
return lookUpFrom(child, nodeOffset: childOffset,
121133
prevOffset: prevOffset, kind: kind)
122134
}
123135
// The next child starts where the previous child ended

Sources/SwiftSyntax/SyntaxKind.swift.gyb

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
//
2222
//===----------------------------------------------------------------------===//
2323

24-
import Foundation
25-
2624
/// Enumerates the known kinds of Syntax represented in the Syntax tree.
27-
public enum SyntaxKind: String {
28-
case token = "Token"
29-
case unknown = "Unknown"
25+
internal enum SyntaxKind: CSyntaxKind {
26+
case token = 0
27+
case unknown = 1
3028
% for node in SYNTAX_NODES:
31-
case ${node.swift_syntax_kind} = "${node.syntax_kind}"
29+
case ${node.swift_syntax_kind} = ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]}
3230
% end
3331

3432
% for name, nodes in grouped_nodes.items():
@@ -56,11 +54,11 @@ public enum SyntaxKind: String {
5654
default: return false
5755
}
5856
}
57+
}
5958

60-
public init(from decoder: Decoder) throws {
61-
let container = try decoder.singleValueContainer()
62-
let kind = try container.decode(String.self)
63-
self = SyntaxKind(rawValue: kind) ?? .unknown
59+
extension SyntaxKind {
60+
static func fromRawValue(_ rawValue: CSyntaxKind) -> SyntaxKind {
61+
return SyntaxKind(rawValue: rawValue)!
6462
}
6563
}
6664

@@ -83,25 +81,3 @@ internal func makeSyntax(_ data: SyntaxData) -> _SyntaxBase {
8381
% end
8482
}
8583
}
86-
87-
88-
extension SyntaxKind {
89-
static func fromRawValue(_ rawValue: CSyntaxKind) -> SyntaxKind {
90-
// Explicitly spell out all SyntaxKinds to keep the serialized value stable
91-
// even if its members get reordered or members get removed
92-
switch rawValue {
93-
case 0:
94-
return .token
95-
case 1:
96-
return .unknown
97-
% for name, nodes in grouped_nodes.items():
98-
% for node in nodes:
99-
case ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]}:
100-
return .${node.swift_syntax_kind}
101-
% end
102-
% end
103-
default:
104-
return .unknown
105-
}
106-
}
107-
}

Sources/SwiftSyntax/SyntaxParser.swift

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,8 @@ public enum ParserError: Error, CustomStringConvertible {
4949
}
5050
}
5151

52-
/// Provides a mechanism for the parser to skip regions of an incrementally
53-
/// updated source that were already parsed during a previous parse invocation.
54-
public protocol IncrementalParseLookup {
55-
/// Does a lookup to see if the current source `offset` should be associated
56-
/// with a known `Syntax` node and its region skipped during parsing.
57-
///
58-
/// The implementation is responsible for checking whether an incremental edit
59-
/// has invalidated the previous `Syntax` node.
60-
///
61-
/// - Parameters:
62-
/// - offset: The byte offset of the source string that is currently parsed.
63-
/// - kind: The `SyntaxKind` that the parser expects at this position.
64-
/// - Returns: A `Syntax` node from the previous parse invocation,
65-
/// representing the contents of this region, if it is still valid
66-
/// to re-use. `nil` otherwise.
67-
func lookUp(_ offset: Int, kind: SyntaxKind) -> Syntax?
68-
}
69-
7052
/// Namespace for functions to parse swift source and retrieve a syntax tree.
71-
public struct SyntaxParser {
53+
public enum SyntaxParser {
7254

7355
/// True if the parser library is compatible with the SwiftSyntax client;
7456
/// false otherwise.
@@ -78,13 +60,13 @@ public struct SyntaxParser {
7860
///
7961
/// - Parameters:
8062
/// - source: The source string to parse.
81-
/// - parseLookup: Optional mechanism for incremental re-parsing.
63+
/// - parseTransition: Optional mechanism for incremental re-parsing.
8264
/// - Returns: A top-level Syntax node representing the contents of the tree,
8365
/// if the parse was successful.
8466
/// - Throws: `ParserError`
8567
public static func parse(
8668
source: String,
87-
parseLookup: IncrementalParseLookup? = nil
69+
parseTransition: IncrementalParseTransition? = nil
8870
) throws -> SourceFileSyntax {
8971
guard nodeHashVerifyResult else {
9072
throw ParserError.parserCompatibilityCheckFailed
@@ -95,7 +77,7 @@ public struct SyntaxParser {
9577
var utf8Source = source
9678
utf8Source.makeNativeUTF8IfNeeded()
9779

98-
let rawSyntax = parseRaw(utf8Source, parseLookup: parseLookup)
80+
let rawSyntax = parseRaw(utf8Source, parseTransition)
9981

10082
guard let file = makeSyntax(.forRoot(rawSyntax)) as? SourceFileSyntax else {
10183
throw ParserError.invalidSyntaxData
@@ -122,7 +104,7 @@ public struct SyntaxParser {
122104

123105
private static func parseRaw(
124106
_ source: String,
125-
parseLookup: IncrementalParseLookup?
107+
_ parseTransition: IncrementalParseTransition?
126108
) -> RawSyntax {
127109
assert(source.isNativeUTF8)
128110
let c_parser = swiftparse_parser_create()
@@ -139,12 +121,11 @@ public struct SyntaxParser {
139121
}
140122
swiftparse_parser_set_node_handler(c_parser, nodeHandler);
141123

142-
if let parseLookup = parseLookup {
124+
if let parseTransition = parseTransition {
143125
let nodeLookup = {
144126
(offset: Int, kind: CSyntaxKind) -> CParseLookupResult in
145127
guard let foundNode =
146-
parseLookup.lookUp(offset,
147-
kind: SyntaxKind.fromRawValue(kind)) else {
128+
parseTransition.lookUp(offset, kind: .fromRawValue(kind)) else {
148129
return CParseLookupResult(length: 0, node: nil)
149130
}
150131
let lengthToSkip = foundNode.byteSize

Sources/lit-test-helper/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ func performParseIncremental(args: CommandLineArguments) throws {
272272
let preEditTree = try SyntaxParser.parse(preEditURL)
273273
let edits = try parseIncrementalEditArguments(args: args)
274274
let regionCollector = IncrementalParseReusedNodeCollector()
275-
let editTransition = IncrementalEditTransition(previousTree: preEditTree,
275+
let editTransition = IncrementalParseTransition(previousTree: preEditTree,
276276
edits: edits, reusedNodeDelegate: regionCollector)
277277

278278
let postEditText = try String(contentsOf: postEditURL)
279279
let postEditTree =
280-
try SyntaxParser.parse(source: postEditText, parseLookup: editTransition)
280+
try SyntaxParser.parse(source: postEditText, parseTransition: editTransition)
281281

282282
let postTreeDump = postEditTree.description
283283

Tests/SwiftSyntaxTest/IncrementalParsingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class IncrementalParsingTestCase: XCTestCase {
1414

1515
var tree = try! SyntaxParser.parse(source: original)
1616
let sourceEdit = SourceEdit(range: ByteSourceRange(offset: step.1.0, length: step.1.1), replacementLength: step.1.2.utf8.count)
17-
let lookup = IncrementalEditTransition(previousTree: tree, edits: [sourceEdit])
18-
tree = try! SyntaxParser.parse(source: step.0, parseLookup: lookup)
17+
let lookup = IncrementalParseTransition(previousTree: tree, edits: [sourceEdit])
18+
tree = try! SyntaxParser.parse(source: step.0, parseTransition: lookup)
1919
XCTAssertEqual("\(tree)", step.0)
2020
}
2121
}

0 commit comments

Comments
 (0)