|
| 1 | +// RUN: %target-swift-frontend -emit-ir %s -disable-availability-checking |
| 2 | + |
| 3 | +// https://github.com/swiftlang/swift/issues/70429 |
| 4 | + |
| 5 | +public protocol Node: Sendable {} |
| 6 | +public protocol BlockNode: Node {} |
| 7 | +public protocol InlineNode: Node {} |
| 8 | + |
| 9 | +// MARK: - Block nodes |
| 10 | + |
| 11 | +public struct Paragraph: BlockNode { |
| 12 | + public init() {} |
| 13 | +} |
| 14 | + |
| 15 | +// MARK: - NodeGroup |
| 16 | + |
| 17 | +/// Intermediate aggregate for nodes. |
| 18 | +/// |
| 19 | +/// Its intended usage is for building node groups through result builders. |
| 20 | +public struct NodeGroup<each N: Node>: Sendable { |
| 21 | + @usableFromInline let nodes: (repeat each N) |
| 22 | + |
| 23 | + @usableFromInline init(nodes: repeat each N) { |
| 24 | + self.nodes = (repeat each nodes) |
| 25 | + } |
| 26 | + |
| 27 | + @usableFromInline func merging<each M>(_ group: NodeGroup<repeat each M>) -> NodeGroup<repeat each N, repeat each M> { |
| 28 | + NodeGroup<repeat each N, repeat each M>(nodes: repeat each self.nodes, repeat each group.nodes) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +@resultBuilder public enum GroupBuilder { |
| 33 | + public static func buildExpression<N: Node>(_ expression: N) -> NodeGroup<N> { |
| 34 | + NodeGroup(nodes: expression) |
| 35 | + } |
| 36 | + |
| 37 | + public static func buildPartialBlock<each N: Node>(first: consuming NodeGroup<repeat each N>) -> NodeGroup<repeat each N> { |
| 38 | + first |
| 39 | + } |
| 40 | + |
| 41 | + public static func buildPartialBlock<each A, each N>(accumulated: consuming NodeGroup<repeat each A>, next: consuming NodeGroup<repeat each N>) -> NodeGroup<repeat each A, repeat each N> { |
| 42 | + let tmp = accumulated.merging(next) |
| 43 | + return tmp |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// MARK: - Zero or More |
| 48 | + |
| 49 | +/// Group defining a bunch of children nodes, which can occur zero or more times in a non-sequential order. |
| 50 | +public struct ZeroOrMore<each N: Node>: Node { |
| 51 | + /// All nodes hold by this group. |
| 52 | + public let nodes: (repeat each N) |
| 53 | + |
| 54 | + public init(nodes: repeat each N) where repeat each N: InlineNode { |
| 55 | + self.nodes = (repeat each nodes) |
| 56 | + } |
| 57 | + |
| 58 | + public init(nodes: repeat each N) where repeat each N: BlockNode { |
| 59 | + self.nodes = (repeat each nodes) |
| 60 | + } |
| 61 | + |
| 62 | + public init(@GroupBuilder group: () -> NodeGroup<repeat each N>) where repeat each N: InlineNode { |
| 63 | + self.nodes = group().nodes |
| 64 | + } |
| 65 | + |
| 66 | + public init(@GroupBuilder group: () -> NodeGroup<repeat each N>) where repeat each N: BlockNode { |
| 67 | + self.nodes = group().nodes |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// When all children blocks are block nodes, the group is a block node itself. |
| 72 | +extension ZeroOrMore: BlockNode where repeat each N: BlockNode {} |
| 73 | +// When all children blocks are inline nodes, the group is an inline node itself. |
| 74 | +extension ZeroOrMore: InlineNode where repeat each N: InlineNode {} |
| 75 | + |
| 76 | +// MARK: - Schema |
| 77 | + |
| 78 | +/// A ``Schema`` defines what a text document can contain/display. |
| 79 | +public protocol Schema: Sendable { |
| 80 | + associatedtype Document: BlockNode |
| 81 | + |
| 82 | + var document: Document { get } |
| 83 | +} |
| 84 | + |
| 85 | +public struct MarkdownSchema: Schema { |
| 86 | + public init() {} |
| 87 | + |
| 88 | + public var document: some BlockNode { |
| 89 | + ZeroOrMore { |
| 90 | + Paragraph() |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments