|
| 1 | +extension CaseIterable where Self: RawRepresentable, RawValue == Int { |
| 2 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 3 | + /// collection of a substring. |
| 4 | + /// |
| 5 | + /// See <doc:CaseIterable> for more info. |
| 6 | + /// |
| 7 | + /// - Parameter inputType: The `Substring` type. This parameter is included to mirror the |
| 8 | + /// interface that parses any collection of UTF-8 code units. |
| 9 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 10 | + /// of a substring. |
| 11 | + @inlinable |
| 12 | + public static func parser( |
| 13 | + of inputType: Substring.Type = Substring.self |
| 14 | + ) -> Parsers.CaseIterableRawRepresentableParser<Substring, Self, String> { |
| 15 | + .init(toPrefix: { String($0) }, areEquivalent: ==) |
| 16 | + } |
| 17 | + |
| 18 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 19 | + /// collection of a substring's UTF-8 view. |
| 20 | + /// |
| 21 | + /// See <doc:CaseIterable> for more info. |
| 22 | + /// |
| 23 | + /// - Parameter inputType: The `Substring.UTF8View` type. This parameter is included to mirror the |
| 24 | + /// interface that parses any collection of UTF-8 code units. |
| 25 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 26 | + /// of a substring's UTF-8 view. |
| 27 | + @inlinable |
| 28 | + public static func parser( |
| 29 | + of inputType: Substring.UTF8View.Type = Substring.UTF8View.self |
| 30 | + ) -> Parsers.CaseIterableRawRepresentableParser<Substring.UTF8View, Self, String.UTF8View> { |
| 31 | + .init(toPrefix: { String($0).utf8 }, areEquivalent: ==) |
| 32 | + } |
| 33 | + |
| 34 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 35 | + /// collection of UTF-8 code units. |
| 36 | + /// |
| 37 | + /// - Parameter inputType: The collection type of UTF-8 code units to parse. |
| 38 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 39 | + /// of a collection of UTF-8 code units. |
| 40 | + @inlinable |
| 41 | + public static func parser<Input>( |
| 42 | + of inputType: Input.Type = Input.self |
| 43 | + ) -> Parsers.CaseIterableRawRepresentableParser<Input, Self, String.UTF8View> |
| 44 | + where |
| 45 | + Input.SubSequence == Input, |
| 46 | + Input.Element == UTF8.CodeUnit |
| 47 | + { |
| 48 | + .init(toPrefix: { String($0).utf8 }, areEquivalent: ==) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension CaseIterable where Self: RawRepresentable, RawValue == String { |
| 53 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 54 | + /// collection of a substring. |
| 55 | + /// |
| 56 | + /// See <doc:CaseIterable> for more info. |
| 57 | + /// |
| 58 | + /// - Parameter inputType: The `Substring` type. This parameter is included to mirror the |
| 59 | + /// interface that parses any collection of UTF-8 code units. |
| 60 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 61 | + /// of a substring. |
| 62 | + @inlinable |
| 63 | + public static func parser( |
| 64 | + of inputType: Substring.Type = Substring.self |
| 65 | + ) -> Parsers.CaseIterableRawRepresentableParser<Substring, Self, String> { |
| 66 | + .init(toPrefix: { $0 }, areEquivalent: ==) |
| 67 | + } |
| 68 | + |
| 69 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 70 | + /// collection of a substring's UTF-8 view. |
| 71 | + /// |
| 72 | + /// See <doc:CaseIterable> for more info. |
| 73 | + /// |
| 74 | + /// - Parameter inputType: The `Substring.UTF8View` type. This parameter is included to mirror the |
| 75 | + /// interface that parses any collection of UTF-8 code units. |
| 76 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 77 | + /// of a substring's UTF-8 view. |
| 78 | + @inlinable |
| 79 | + public static func parser( |
| 80 | + of inputType: Substring.UTF8View.Type = Substring.UTF8View.self |
| 81 | + ) -> Parsers.CaseIterableRawRepresentableParser<Substring.UTF8View, Self, String.UTF8View> { |
| 82 | + .init(toPrefix: { $0.utf8 }, areEquivalent: ==) |
| 83 | + } |
| 84 | + |
| 85 | + /// A parser that consumes a case-iterable, raw representable value from the beginning of a |
| 86 | + /// collection of UTF-8 code units. |
| 87 | + /// |
| 88 | + /// - Parameter inputType: The collection type of UTF-8 code units to parse. |
| 89 | + /// - Returns: A parser that consumes a case-iterable, raw representable value from the beginning |
| 90 | + /// of a collection of UTF-8 code units. |
| 91 | + @inlinable |
| 92 | + public static func parser<Input>( |
| 93 | + of inputType: Input.Type = Input.self |
| 94 | + ) -> Parsers.CaseIterableRawRepresentableParser<Input, Self, String.UTF8View> |
| 95 | + where |
| 96 | + Input.SubSequence == Input, |
| 97 | + Input.Element == UTF8.CodeUnit |
| 98 | + { |
| 99 | + .init(toPrefix: { $0.utf8 }, areEquivalent: ==) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +extension Parsers { |
| 104 | + public struct CaseIterableRawRepresentableParser< |
| 105 | + Input: Collection, Output: CaseIterable & RawRepresentable, Prefix: Collection |
| 106 | + >: Parser |
| 107 | + where |
| 108 | + Input.SubSequence == Input, |
| 109 | + Output.RawValue: Comparable, |
| 110 | + Prefix.Element == Input.Element |
| 111 | + { |
| 112 | + @usableFromInline |
| 113 | + let cases: [(case: Output, prefix: Prefix, count: Int)] |
| 114 | + |
| 115 | + @usableFromInline |
| 116 | + let areEquivalent: (Input.Element, Input.Element) -> Bool |
| 117 | + |
| 118 | + @usableFromInline |
| 119 | + init( |
| 120 | + toPrefix: @escaping (Output.RawValue) -> Prefix, |
| 121 | + areEquivalent: @escaping (Input.Element, Input.Element) -> Bool |
| 122 | + ) { |
| 123 | + self.areEquivalent = areEquivalent |
| 124 | + self.cases = Output.allCases |
| 125 | + .map { |
| 126 | + let prefix = toPrefix($0.rawValue) |
| 127 | + return ($0, prefix, prefix.count) |
| 128 | + } |
| 129 | + .sorted(by: { $0.count > $1.count }) |
| 130 | + } |
| 131 | + |
| 132 | + @inlinable |
| 133 | + public func parse(_ input: inout Input) throws -> Output { |
| 134 | + for (`case`, prefix, count) in self.cases { |
| 135 | + if input.starts(with: prefix, by: self.areEquivalent) { |
| 136 | + input.removeFirst(count) |
| 137 | + return `case` |
| 138 | + } |
| 139 | + } |
| 140 | + throw ParsingError.expectedInput("case of \"\(Output.self)\"", at: input) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments