Monadic parser combinators for JavScript, inspired by Parsec.
import * as lq from "@loquat/simple";
const nat = lq.digit.manyChars1().map(str => parseInt(str, 10));
const op = lq.choice([
lq.char("+").return((a, b) => a + b),
lq.char("-").return((a, b) => a - b),
]);
const expr =
nat.bind(a =>
op.bind(f =>
nat.map(b =>
f(a, b)
)
)
);
const res = expr.parse("", "14+28"); // { success: true, value: 42 }
If you are not familiar with loquat, or you are using TypeScript, @loquat/simple
is recommended.
For a more flexible and extensible framework, use @loquat/framework
.
The core features and extensions are provided as separate packages.
For extension development, a chai plugin and some test helpers are available.
Copyright 2019 Susisu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.