blob: ca89c9c27e6cbe2d1d91a23c9917ce93d55769ff [file] [log] [blame]
%{
# -*- mode: Swift -*-
from gyb_syntax_support import *
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From TokenKind.swift.gyb.
//// Do Not Edit Directly!
//===----------------- TokenKind.swift - Token Kind Enum ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Enumerates the kinds of tokens in the Swift language.
public enum TokenKind: Codable {
case eof
% for token in SYNTAX_TOKENS:
% kind = token.swift_kind()
%
% # Tokens that don't have a set text have an associated value that
% # contains their text.
% if not token.text:
% kind += '(String)'
% end
case ${kind}
% end
/// The textual representation of this token kind.
var text: String {
switch self {
case .eof: return ""
% for token in SYNTAX_TOKENS:
% if token.text:
case .${token.swift_kind()}: return "${token.text}"
% else:
case .${token.swift_kind()}(let text): return text
% end
% end
}
}
/// Keys for serializing and deserializing token kinds.
enum CodingKeys: String, CodingKey {
case kind, text
}
enum DecodeError: Error {
case unknownTokenKind(String)
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(String.self, forKey: .kind)
switch kind {
case "eof": self = .eof
% for token in SYNTAX_TOKENS:
case "${token.kind}":
% if token.text:
self = .${token.swift_kind()}
% else:
let text = try container.decode(String.self, forKey: .text)
self = .${token.swift_kind()}(text)
% end
% end
default: throw DecodeError.unknownTokenKind(kind)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(kind, forKey: .kind)
try container.encode(text, forKey: .text)
}
var kind: String {
switch self {
case .eof: return "eof"
% for token in SYNTAX_TOKENS:
% kind = token.swift_kind()
% if not token.text:
% kind += '(_)'
% end
case .${kind}: return "${token.kind}"
% end
}
}
}
extension TokenKind: Equatable {
public static func ==(lhs: TokenKind, rhs: TokenKind) -> Bool {
switch (lhs, rhs) {
case (.eof, .eof): return true
% for token in SYNTAX_TOKENS:
% kind = token.swift_kind()
% if token.text:
case (.${kind}, .${kind}): return true
% else:
case (.${kind}(let lhsText), .${kind}(let rhsText)):
return lhsText == rhsText
% end
% end
default: return false
}
}
}
extension TokenKind: ByteTreeObjectDecodable {
static func read(from reader: ByteTreeObjectReader, numFields: Int) -> TokenKind {
// Explicitly spell out all TokenKinds to keep the serialized value stable
// even if its members get reordered or members get removed
let kind = reader.readField(UInt8.self, index: 0)
switch kind {
case 0: return .eof
% for token in SYNTAX_TOKENS:
case ${token.serialization_code}:
% if token.text: # The token does not have text associated with it
return .${token.swift_kind()}
% else:
let text = reader.readField(String.self, index: 1)
return .${token.swift_kind()}(text)
% end
% end
default:
fatalError("Unknown TokenKind \(kind)")
}
}
}