|
| 1 | +import Parser from "./parser.mjs"; |
| 2 | +import {Success, Failure} from "./result.mjs"; |
| 3 | +import {join} from "./mappers.mjs"; |
| 4 | + |
| 5 | +export function defer(fn) { |
| 6 | + // Parsers may be defined as defer(() => parser) to avoid cyclic |
| 7 | + // dependecies. |
| 8 | + return new Parser(stream => |
| 9 | + fn().run(stream)); |
| 10 | +} |
| 11 | + |
| 12 | +export function char(c) { |
| 13 | + return new Parser(stream => |
| 14 | + stream.head() === c |
| 15 | + ? new Success(c, stream.move(1)) |
| 16 | + : new Failure(`${c} not found`, stream)); |
| 17 | +} |
| 18 | + |
| 19 | +export function regex(re) { |
| 20 | + return new Parser(stream => { |
| 21 | + const result = stream.exec(re); |
| 22 | + |
| 23 | + if (result === null) { |
| 24 | + return new Failure("regex did not match", stream); |
| 25 | + } |
| 26 | + |
| 27 | + const [match] = result; |
| 28 | + |
| 29 | + return new Success(match, stream.move(match.length)); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +export function charset(range) { |
| 34 | + return regex(new RegExp(`[${range}]`)); |
| 35 | +} |
| 36 | + |
| 37 | +export function string(str) { |
| 38 | + return sequence(...str.split("").map(char)).map(join); |
| 39 | +} |
| 40 | + |
| 41 | +export function eof() { |
| 42 | + return new Parser(stream => |
| 43 | + stream.head() === Symbol.for("eof") |
| 44 | + ? new Success(stream.head(), stream.move(1)) |
| 45 | + : new Failure("not at EOF", stream)); |
| 46 | +} |
| 47 | + |
| 48 | +export function lookahead(parser) { |
| 49 | + return new Parser(stream => |
| 50 | + parser |
| 51 | + .run(stream) |
| 52 | + .fold( |
| 53 | + value => new Success(value, stream), |
| 54 | + value => new Failure(value, stream))); |
| 55 | +} |
| 56 | + |
| 57 | +export function not(parser) { |
| 58 | + return new Parser(stream => |
| 59 | + parser |
| 60 | + .run(stream) |
| 61 | + .fold( |
| 62 | + (value, tail) => new Failure("not failed", stream), |
| 63 | + (value, tail) => new Success(null, stream))); |
| 64 | +} |
| 65 | + |
| 66 | +export function and(...parsers) { |
| 67 | + const final = parsers.pop(); |
| 68 | + return sequence(...parsers.map(lookahead), final) |
| 69 | + .map(results => results[results.length - 1]); |
| 70 | +} |
| 71 | + |
| 72 | +export function either(...parsers) { |
| 73 | + return new Parser(stream => { |
| 74 | + for (const parser of parsers) { |
| 75 | + const result = parser.run(stream); |
| 76 | + if (result instanceof Success) { |
| 77 | + return result; |
| 78 | + } |
| 79 | + } |
| 80 | + return new Failure("either failed", stream); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +export function always(value) { |
| 85 | + return new Parser(stream => new Success(value, stream)); |
| 86 | +} |
| 87 | + |
| 88 | +export function never(value) { |
| 89 | + return new Parser(stream => new Failure(value, stream)); |
| 90 | +} |
| 91 | + |
| 92 | +export function maybe(parser) { |
| 93 | + return new Parser(stream => |
| 94 | + parser |
| 95 | + .run(stream) |
| 96 | + .fold( |
| 97 | + (value, tail) => new Success(value, tail), |
| 98 | + (value, tail) => new Success(undefined, stream))); |
| 99 | +} |
| 100 | + |
| 101 | +export function append(p1, p2) { |
| 102 | + return p1.chain(values => |
| 103 | + p2.map(value => values.concat([value]))); |
| 104 | +} |
| 105 | + |
| 106 | +export function after(prefix, parser) { |
| 107 | + return sequence(prefix, parser) |
| 108 | + .map(([left, value]) => value); |
| 109 | +} |
| 110 | + |
| 111 | +export function sequence(...parsers) { |
| 112 | + return parsers.reduce( |
| 113 | + (acc, parser) => append(acc, parser), |
| 114 | + always([])); |
| 115 | +} |
| 116 | + |
| 117 | +export function repeat(parser) { |
| 118 | + return new Parser(stream => |
| 119 | + parser |
| 120 | + .run(stream) |
| 121 | + .fold( |
| 122 | + (value, tail) => |
| 123 | + repeat(parser) |
| 124 | + .map(rest => [value].concat(rest)) |
| 125 | + .run(tail), |
| 126 | + (value, tail) => new Success([], stream))); |
| 127 | +} |
| 128 | + |
| 129 | +export function repeat1(parser) { |
| 130 | + return new Parser(stream => |
| 131 | + parser |
| 132 | + .run(stream) |
| 133 | + .fold( |
| 134 | + (value, tail) => |
| 135 | + repeat(parser) |
| 136 | + .map(rest => [value].concat(rest)) |
| 137 | + .run(tail), |
| 138 | + (value, tail) => new Failure("repeat1 failed", stream))); |
| 139 | +} |
0 commit comments