@thi.ng/parse
Version:
Purely functional parser combinators & AST generation for generic inputs
24 lines (23 loc) • 637 B
JavaScript
import { xform } from "../combinators/xform.js";
import { __indent } from "../utils.js";
const xfPrint = (fn = console.log) => {
const $print = (scope, _, level = 0) => {
if (!scope) return;
const prefix = __indent(level);
const state = scope.state;
const info = state ? ` (${state.l}:${state.c})` : "";
fn(`${prefix}${scope.id}${info}: ${JSON.stringify(scope.result)}`);
if (scope.children) {
for (let c of scope.children) {
$print(c, _, level + 1);
}
}
return scope;
};
return $print;
};
const print = (parser, fn) => xform(parser, xfPrint(fn));
export {
print,
xfPrint
};