@thi.ng/parse
Version:
Purely functional parser combinators & AST generation for generic inputs
30 lines (29 loc) • 1.06 kB
JavaScript
import { alt } from "../combinators/alt.js";
import { maybe } from "../combinators/maybe.js";
import { zeroOrMore } from "../combinators/repeat.js";
import { seq } from "../combinators/seq.js";
import { xform } from "../combinators/xform.js";
import { lit } from "../prims/lit.js";
import { oneOf } from "../prims/one-of.js";
import { comp } from "../xform/comp.js";
import { join } from "../xform/join.js";
import { xfFloat, xfInt } from "../xform/number.js";
import { xfID } from "../xform/with-id.js";
import { DIGIT, DIGITS } from "./digits.js";
const SIGN = maybe(oneOf("-+"));
const INT = xform(seq([SIGN, DIGITS], "int"), xfInt());
const UINT = xform(DIGITS, comp(xfID("uint"), xfInt()));
const EXP = maybe(seq([maybe(oneOf("eE")), SIGN, DIGITS]));
const DOT = lit(".");
const FRACT0 = maybe(seq([DOT, zeroOrMore(DIGIT)]));
const FRACT1 = seq([DOT, DIGITS]);
const _REAL = seq([SIGN, alt([FRACT1, seq([DIGITS, FRACT0])]), EXP], "real");
const REAL = join(_REAL);
const FLOAT = xform(_REAL, xfFloat);
export {
FLOAT,
INT,
REAL,
SIGN,
UINT
};