Un-abstracting concrete syntax types, etc.

Changed files
+39 -39
Sources
+5 -5
Sources/PterodactylSyntax/Cursor.swift
···
import Foundation
-
public struct Cursor<L: Language>: Sendable {
-
let node: Child<L>
let utf16Offset: Int
-
let children: [Cursor<L>]
-
init(node: Child<L>, utf16Offset: Int) {
self.node = node
self.utf16Offset = utf16Offset
-
var children: [Cursor<L>] = []
var utf16Offset = utf16Offset
for childNode in node.children {
children.append(Self(node: childNode, utf16Offset: utf16Offset))
···
import Foundation
+
public struct Cursor: Sendable {
+
let node: SyntaxTree.Child
let utf16Offset: Int
+
let children: [Cursor]
+
init(node: SyntaxTree.Child, utf16Offset: Int) {
self.node = node
self.utf16Offset = utf16Offset
+
var children: [Cursor] = []
var utf16Offset = utf16Offset
for childNode in node.children {
children.append(Self(node: childNode, utf16Offset: utf16Offset))
-16
Sources/PterodactylSyntax/Protocols.swift
···
-
import Foundation
-
-
public protocol TreeKindType: Equatable, Sendable {
-
static var error: Self { get }
-
}
-
-
public protocol TokenKindType: Equatable, Sendable {
-
static var eof: Self { get }
-
}
-
-
public protocol Language {
-
associatedtype TokenKind: TokenKindType
-
associatedtype TreeKind: TreeKindType
-
associatedtype TokenMetadata: Sendable, Equatable
-
associatedtype TreeMetadata: Sendable, Equatable
-
}
···
+4 -4
Sources/PterodactylSyntax/Token.swift
···
import Foundation
-
public struct Token<Kind> {
-
public let kind: Kind
public let text: String
public let utf16Length: Int
-
init(kind: Kind, text: String) {
self.kind = kind
self.text = text
self.utf16Length = text.utf16.count
}
}
-
extension Token: Sendable where Kind: Sendable {}
···
import Foundation
+
public struct Token: Codable {
+
public let kind: TokenKind
public let text: String
public let utf16Length: Int
+
init(kind: TokenKind, text: String) {
self.kind = kind
self.text = text
self.utf16Length = text.utf16.count
}
}
+
extension Token: Sendable {}
+14 -14
Sources/PterodactylSyntax/Tree.swift Sources/PterodactylSyntax/SyntaxTree.swift
···
import Foundation
-
public struct Tree<L: Language>: Sendable {
-
public let kind: L.TreeKind
-
public let metadata: L.TreeMetadata?
-
public let children: [Child<L>]
public let utf16Length: Int
-
public init(kind: L.TreeKind, metadata: L.TreeMetadata?, children: [Child<L>]) {
self.kind = kind
self.metadata = metadata
self.children = children
···
}
}
-
public enum Child<L: Language>: Sendable {
-
case token(Token<L.TokenKind>, metadata: L.TokenMetadata?)
-
case tree(Tree<L>)
-
}
-
-
extension Tree {
public var text: String {
children.map(\.text).joined()
}
}
-
extension Child {
var text: String {
switch self {
case let .token(tok, _): tok.text
···
var utf16Length: Int {
switch self {
-
case let .token(token, metadata): token.utf16Length
case let .tree(tree): tree.utf16Length
}
}
···
}
}
-
extension Tree: CustomStringConvertible {
public var description: String {
prettyPrint()
}
···
import Foundation
+
public struct SyntaxTree: Codable, Sendable {
+
public let kind: SyntaxTreeKind
+
public let metadata: SyntaxTreeMetadata?
+
public let children: [Child]
public let utf16Length: Int
+
+
public enum Child: Codable, Sendable {
+
case token(Token, metadata: TokenMetadata?)
+
case tree(SyntaxTree)
+
}
+
public init(kind: SyntaxTreeKind, metadata: SyntaxTreeMetadata?, children: [SyntaxTree.Child]) {
self.kind = kind
self.metadata = metadata
self.children = children
···
}
}
+
extension SyntaxTree {
public var text: String {
children.map(\.text).joined()
}
}
+
extension SyntaxTree.Child {
var text: String {
switch self {
case let .token(tok, _): tok.text
···
var utf16Length: Int {
switch self {
+
case let .token(token, _): token.utf16Length
case let .tree(tree): tree.utf16Length
}
}
···
}
}
+
extension SyntaxTree: CustomStringConvertible {
public var description: String {
prettyPrint()
}
+16
Sources/PterodactylSyntax/Types.swift
···
···
+
import Foundation
+
+
public enum TokenKind: Codable, Equatable, Sendable {
+
case eof
+
}
+
+
public enum SyntaxTreeKind: Codable, Equatable, Sendable {
+
case error
+
}
+
+
public struct TokenMetadata: Codable, Equatable, Sendable {
+
}
+
+
public struct SyntaxTreeMetadata: Codable, Equatable, Sendable {
+
+
}