Skip to content

Extract Codable conformance of RawSyntax into an extension #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
114 changes: 58 additions & 56 deletions Sources/SwiftSyntax/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fileprivate enum RawSyntaxData {
/// Represents the raw tree structure underlying the syntax tree. These nodes
/// have no notion of identity and only provide structure to the tree. They
/// are immutable and can be freely shared between syntax nodes.
struct RawSyntax: Codable {
struct RawSyntax {
fileprivate let data: RawSyntaxData
let presence: SourcePresence

Expand Down Expand Up @@ -198,61 +198,6 @@ struct RawSyntax: Codable {
case nodeLookupFailed(SyntaxNodeId)
}

/// Creates a RawSyntax from the provided Foundation Decoder.
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decodeIfPresent(SyntaxNodeId.self, forKey: .id)
let omitted = try container.decodeIfPresent(Bool.self, forKey: .omitted) ?? false

if omitted {
guard let id = id else {
throw IncrementalDecodingError.omittedNodeHasNoId
}
guard let lookupFunc = decoder.userInfo[.omittedNodeLookupFunction] as?
(SyntaxNodeId) -> RawSyntax? else {
throw IncrementalDecodingError.noLookupFunctionPassed
}
guard let lookupNode = lookupFunc(id) else {
throw IncrementalDecodingError.nodeLookupFailed(id)
}
self = lookupNode
return
}

let presence = try container.decode(SourcePresence.self, forKey: .presence)
if let kind = try container.decodeIfPresent(SyntaxKind.self, forKey: .kind) {
let layout = try container.decode([RawSyntax?].self, forKey: .layout)
self.init(kind: kind, layout: layout, presence: presence, id: id)
} else {
let kind = try container.decode(TokenKind.self, forKey: .tokenKind)
let leadingTrivia = try container.decode(Trivia.self, forKey: .leadingTrivia)
let trailingTrivia = try container.decode(Trivia.self, forKey: .trailingTrivia)
self.init(kind: kind, leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia, presence: presence, id: id)
}
if let callback = decoder.userInfo[.rawSyntaxDecodedCallback] as?
(RawSyntax) -> Void {
callback(self)
}
}

/// Encodes the RawSyntax to the provided Foundation Encoder.
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self.data {
case let .node(kind, layout):
try container.encode(id, forKey: .id)
try container.encode(kind, forKey: .kind)
try container.encode(layout, forKey: .layout)
case let .token(kind, leadingTrivia, trailingTrivia):
try container.encode(id, forKey: .id)
try container.encode(kind, forKey: .tokenKind)
try container.encode(leadingTrivia, forKey: .leadingTrivia)
try container.encode(trailingTrivia, forKey: .trailingTrivia)
}
try container.encode(presence, forKey: .presence)
}

/// Creates a RawSyntax node that's marked missing in the source with the
/// provided kind and layout.
/// - Parameters:
Expand Down Expand Up @@ -389,6 +334,63 @@ extension RawSyntax {
}
}

extension RawSyntax: Codable {
/// Creates a RawSyntax from the provided Foundation Decoder.
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decodeIfPresent(SyntaxNodeId.self, forKey: .id)
let omitted = try container.decodeIfPresent(Bool.self, forKey: .omitted) ?? false

if omitted {
guard let id = id else {
throw IncrementalDecodingError.omittedNodeHasNoId
}
guard let lookupFunc = decoder.userInfo[.omittedNodeLookupFunction] as?
(SyntaxNodeId) -> RawSyntax? else {
throw IncrementalDecodingError.noLookupFunctionPassed
}
guard let lookupNode = lookupFunc(id) else {
throw IncrementalDecodingError.nodeLookupFailed(id)
}
self = lookupNode
return
}

let presence = try container.decode(SourcePresence.self, forKey: .presence)
if let kind = try container.decodeIfPresent(SyntaxKind.self, forKey: .kind) {
let layout = try container.decode([RawSyntax?].self, forKey: .layout)
self.init(kind: kind, layout: layout, presence: presence, id: id)
} else {
let kind = try container.decode(TokenKind.self, forKey: .tokenKind)
let leadingTrivia = try container.decode(Trivia.self, forKey: .leadingTrivia)
let trailingTrivia = try container.decode(Trivia.self, forKey: .trailingTrivia)
self.init(kind: kind, leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia, presence: presence, id: id)
}
if let callback = decoder.userInfo[.rawSyntaxDecodedCallback] as?
(RawSyntax) -> Void {
callback(self)
}
}

/// Encodes the RawSyntax to the provided Foundation Encoder.
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self.data {
case let .node(kind, layout):
try container.encode(id, forKey: .id)
try container.encode(kind, forKey: .kind)
try container.encode(layout, forKey: .layout)
case let .token(kind, leadingTrivia, trailingTrivia):
try container.encode(id, forKey: .id)
try container.encode(kind, forKey: .tokenKind)
try container.encode(leadingTrivia, forKey: .leadingTrivia)
try container.encode(trailingTrivia, forKey: .trailingTrivia)
}
try container.encode(presence, forKey: .presence)
}
}

extension RawSyntax: ByteTreeObjectDecodable {
enum SyntaxType: UInt8, ByteTreeScalarDecodable {
case token = 0
Expand Down