|
| 1 | +import gleam/int |
| 2 | +import gleam/list |
| 3 | +import gleam/option.{None, Some} |
| 4 | +import gleam/result.{try} |
| 5 | +import gleam/string |
| 6 | +import pears/input.{type Char, type Input} |
| 7 | + |
| 8 | +pub type Parsed(i, a) { |
| 9 | + Parsed(input: Input(i), value: a) |
| 10 | +} |
| 11 | + |
| 12 | +pub type ParseError(i) { |
| 13 | + ParseError(input: Input(i), expected: List(String)) |
| 14 | +} |
| 15 | + |
| 16 | +pub type ParseResult(i, a) = |
| 17 | + Result(Parsed(i, a), ParseError(i)) |
| 18 | + |
| 19 | +pub type Parser(i, a) = |
| 20 | + fn(Input(i)) -> ParseResult(i, a) |
| 21 | + |
| 22 | +pub fn parse(parser: Parser(Char, a), input: String) -> ParseResult(Char, a) { |
| 23 | + parser(string.to_graphemes(input)) |
| 24 | +} |
| 25 | + |
| 26 | +fn ok(input: Input(i), value: a) -> ParseResult(i, a) { |
| 27 | + Ok(Parsed(input, value)) |
| 28 | +} |
| 29 | + |
| 30 | +/// Maps the result of a parser to a new value using a mapper function |
| 31 | +pub fn map(p: Parser(i, a), fun: fn(a) -> b) -> Parser(i, b) { |
| 32 | + fn(input: Input(i)) { |
| 33 | + p(input) |
| 34 | + |> result.map(fn(parsed) { |
| 35 | + Parsed(input: parsed.input, value: fun(parsed.value)) |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Maps the result of a parser to a constant value |
| 41 | +pub fn to(parser: Parser(i, a), value: b) -> Parser(i, b) { |
| 42 | + map(parser, fn(_) { value }) |
| 43 | +} |
| 44 | + |
| 45 | +pub fn alt(parser_1: Parser(i, a), parser_2: Parser(i, a)) -> Parser(i, a) { |
| 46 | + fn(input: Input(i)) { |
| 47 | + case parser_1(input) { |
| 48 | + Ok(result) -> Ok(result) |
| 49 | + Error(_) -> parser_2(input) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +pub fn any() -> Parser(i, i) { |
| 55 | + fn(in: Input(i)) { |
| 56 | + case input.get(in) { |
| 57 | + None -> Error(ParseError(in, ["any"])) |
| 58 | + Some(#(value, next)) -> ok(next, value) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub fn eof() -> Parser(i, Nil) { |
| 64 | + fn(input: Input(i)) { |
| 65 | + case input { |
| 66 | + [] -> ok(input, Nil) |
| 67 | + _ -> Error(ParseError(input, ["EOF"])) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub type Predicate(i) = |
| 73 | + fn(i) -> Bool |
| 74 | + |
| 75 | +pub fn satisfying(f: Predicate(i)) -> Parser(i, i) { |
| 76 | + fn(in: Input(i)) { |
| 77 | + case input.get(in) { |
| 78 | + None -> Error(ParseError(in, ["satisfying"])) |
| 79 | + Some(#(value, next)) -> |
| 80 | + case f(value) { |
| 81 | + True -> ok(next, value) |
| 82 | + False -> Error(ParseError(in, ["satisfying"])) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +pub fn then(parser_a: Parser(i, a), parser_b: Parser(i, b)) -> Parser(i, b) { |
| 89 | + fn(input: Input(i)) { |
| 90 | + case parser_a(input) { |
| 91 | + Ok(parsed) -> parser_b(parsed.input) |
| 92 | + Error(err) -> Error(err) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub fn tag(tag: i) -> Parser(i, i) { |
| 98 | + fn(input: Input(i)) { |
| 99 | + case input { |
| 100 | + [head, ..next] if head == tag -> ok(next, head) |
| 101 | + _ -> Error(ParseError(input, ["tag"])) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +pub fn pair( |
| 107 | + parser_1 p1: Parser(i, a), |
| 108 | + parser_2 p2: Parser(i, b), |
| 109 | +) -> Parser(i, #(a, b)) { |
| 110 | + fn(in: Input(i)) { |
| 111 | + use parsed_1 <- try(p1(in)) |
| 112 | + use parsed_2 <- try(p2(parsed_1.input)) |
| 113 | + ok(parsed_2.input, #(parsed_1.value, parsed_2.value)) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +pub fn left( |
| 118 | + parser_1 p1: Parser(i, a), |
| 119 | + parser_2 p2: Parser(i, b), |
| 120 | +) -> Parser(i, a) { |
| 121 | + fn(in: Input(i)) { |
| 122 | + use parsed_1 <- try(p1(in)) |
| 123 | + use parsed_2 <- try(p2(parsed_1.input)) |
| 124 | + ok(parsed_2.input, parsed_1.value) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +pub fn right( |
| 129 | + parser_1 p1: Parser(i, a), |
| 130 | + parser_2 p2: Parser(i, b), |
| 131 | +) -> Parser(i, b) { |
| 132 | + fn(in: Input(i)) { |
| 133 | + use parsed_1 <- try(p1(in)) |
| 134 | + use parsed_2 <- try(p2(parsed_1.input)) |
| 135 | + ok(parsed_2.input, parsed_2.value) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/// Parses zero or more occurrences of the given parser |
| 140 | +pub fn many0(parser: Parser(i, a)) -> Parser(i, List(a)) { |
| 141 | + fn(in: Input(i)) { |
| 142 | + case parser(in) { |
| 143 | + Ok(parsed) -> { |
| 144 | + use next <- try(many0(parser)(parsed.input)) |
| 145 | + ok(next.input, [parsed.value, ..next.value]) |
| 146 | + } |
| 147 | + Error(_) -> ok(in, []) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Parses one or more occurrences of the given parser |
| 153 | +pub fn many1(parser: Parser(i, a)) -> Parser(i, List(a)) { |
| 154 | + fn(in: Input(i)) { |
| 155 | + use parsed <- try(parser(in)) |
| 156 | + use rest <- try(many0(parser)(parsed.input)) |
| 157 | + ok(rest.input, [parsed.value, ..rest.value]) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/// Lazily evaluates the given parser allowing for recursive parsers |
| 162 | +pub fn lazy(f: fn() -> Parser(i, a)) -> Parser(i, a) { |
| 163 | + fn(input) { f()(input) } |
| 164 | +} |
| 165 | + |
| 166 | +pub fn char(c: Char) -> Parser(Char, Char) { |
| 167 | + tag(c) |
| 168 | +} |
| 169 | + |
| 170 | +pub fn string(str: String) -> Parser(Char, String) { |
| 171 | + fn(input: Input(Char)) { |
| 172 | + let s = string.to_graphemes(str) |
| 173 | + let length = list.length(s) |
| 174 | + case list.length(input) >= length { |
| 175 | + True -> { |
| 176 | + let candidate = list.take(input, length) |
| 177 | + case candidate == s { |
| 178 | + True -> ok(list.drop(input, length), str) |
| 179 | + False -> Error(ParseError(input, [str])) |
| 180 | + } |
| 181 | + } |
| 182 | + False -> Error(ParseError(input, [str])) |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +pub fn one_of(items: List(i)) -> Parser(i, i) { |
| 188 | + satisfying(fn(c) { list.contains(items, c) }) |
| 189 | +} |
| 190 | + |
| 191 | +pub fn digit() -> Parser(Char, Char) { |
| 192 | + one_of(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) |
| 193 | +} |
| 194 | + |
| 195 | +pub fn number() -> Parser(Char, Int) { |
| 196 | + many1(digit()) |
| 197 | + |> map(fn(digits) { |
| 198 | + list.fold(digits, 0, fn(acc, digit) { |
| 199 | + let assert Ok(digit) = int.parse(digit) |
| 200 | + acc * 10 + digit |
| 201 | + }) |
| 202 | + }) |
| 203 | +} |
| 204 | + |
| 205 | +pub fn between( |
| 206 | + open: Parser(i, a), |
| 207 | + close: Parser(i, b), |
| 208 | + parser: Parser(i, c), |
| 209 | +) -> Parser(i, c) { |
| 210 | + open |
| 211 | + |> right(parser) |
| 212 | + |> left(close) |
| 213 | +} |
| 214 | + |
| 215 | +pub fn whitespace() -> Parser(Char, Char) { |
| 216 | + satisfying(fn(c) { string.trim(c) == "" }) |
| 217 | +} |
| 218 | + |
| 219 | +pub fn whitespace0() -> Parser(Char, List(Char)) { |
| 220 | + many0(whitespace()) |
| 221 | +} |
| 222 | + |
| 223 | +pub fn whitespace1() -> Parser(Char, List(Char)) { |
| 224 | + many1(whitespace()) |
| 225 | +} |
0 commit comments