Skip to content

Commit c00ae39

Browse files
committed
Better fraction and % parser
1 parent 68e1e59 commit c00ae39

File tree

3 files changed

+75
-53
lines changed

3 files changed

+75
-53
lines changed

dist/index.es.js

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ var W = {
716716
return (o) => "" + o + s++;
717717
})();
718718
})(Q);
719-
const { abstract: Nt, isSyntactic: fu } = C, J = W, I = A, Lt = Q;
719+
const { abstract: Nt, isSyntactic: fu } = C, J = W, I = A, Ot = Q;
720720
let ru;
721-
Lt.awaitBuiltInRules((u) => {
721+
Ot.awaitBuiltInRules((u) => {
722722
ru = u;
723723
});
724724
let cu;
@@ -770,8 +770,8 @@ I.Apply.prototype._assertAllApplicationsAreValid = function(u, e, t = !1) {
770770
throw J.invalidParameter(this.ruleName, D);
771771
});
772772
};
773-
const Ot = C, pe = W, N = A;
774-
N.PExpr.prototype.assertChoicesHaveUniformArity = Ot.abstract(
773+
const Lt = C, pe = W, N = A;
774+
N.PExpr.prototype.assertChoicesHaveUniformArity = Lt.abstract(
775775
"assertChoicesHaveUniformArity"
776776
);
777777
N.any.assertChoicesHaveUniformArity = N.end.assertChoicesHaveUniformArity = N.Terminal.prototype.assertChoicesHaveUniformArity = N.Range.prototype.assertChoicesHaveUniformArity = N.Param.prototype.assertChoicesHaveUniformArity = N.Lex.prototype.assertChoicesHaveUniformArity = N.UnicodeChar.prototype.assertChoicesHaveUniformArity = function(u) {
@@ -807,28 +807,28 @@ N.Lookahead.prototype.assertChoicesHaveUniformArity = function(u) {
807807
};
808808
N.Apply.prototype.assertChoicesHaveUniformArity = function(u) {
809809
};
810-
const Pt = C, Rt = W, L = A;
811-
L.PExpr.prototype.assertIteratedExprsAreNotNullable = Pt.abstract(
810+
const Pt = C, Rt = W, O = A;
811+
O.PExpr.prototype.assertIteratedExprsAreNotNullable = Pt.abstract(
812812
"assertIteratedExprsAreNotNullable"
813813
);
814-
L.any.assertIteratedExprsAreNotNullable = L.end.assertIteratedExprsAreNotNullable = L.Terminal.prototype.assertIteratedExprsAreNotNullable = L.Range.prototype.assertIteratedExprsAreNotNullable = L.Param.prototype.assertIteratedExprsAreNotNullable = L.UnicodeChar.prototype.assertIteratedExprsAreNotNullable = function(u) {
814+
O.any.assertIteratedExprsAreNotNullable = O.end.assertIteratedExprsAreNotNullable = O.Terminal.prototype.assertIteratedExprsAreNotNullable = O.Range.prototype.assertIteratedExprsAreNotNullable = O.Param.prototype.assertIteratedExprsAreNotNullable = O.UnicodeChar.prototype.assertIteratedExprsAreNotNullable = function(u) {
815815
};
816-
L.Alt.prototype.assertIteratedExprsAreNotNullable = function(u) {
816+
O.Alt.prototype.assertIteratedExprsAreNotNullable = function(u) {
817817
for (let e = 0; e < this.terms.length; e++)
818818
this.terms[e].assertIteratedExprsAreNotNullable(u);
819819
};
820-
L.Seq.prototype.assertIteratedExprsAreNotNullable = function(u) {
820+
O.Seq.prototype.assertIteratedExprsAreNotNullable = function(u) {
821821
for (let e = 0; e < this.factors.length; e++)
822822
this.factors[e].assertIteratedExprsAreNotNullable(u);
823823
};
824-
L.Iter.prototype.assertIteratedExprsAreNotNullable = function(u) {
824+
O.Iter.prototype.assertIteratedExprsAreNotNullable = function(u) {
825825
if (this.expr.assertIteratedExprsAreNotNullable(u), this.expr.isNullable(u))
826826
throw Rt.kleeneExprHasNullableOperand(this, []);
827827
};
828-
L.Opt.prototype.assertIteratedExprsAreNotNullable = L.Not.prototype.assertIteratedExprsAreNotNullable = L.Lookahead.prototype.assertIteratedExprsAreNotNullable = L.Lex.prototype.assertIteratedExprsAreNotNullable = function(u) {
828+
O.Opt.prototype.assertIteratedExprsAreNotNullable = O.Not.prototype.assertIteratedExprsAreNotNullable = O.Lookahead.prototype.assertIteratedExprsAreNotNullable = O.Lex.prototype.assertIteratedExprsAreNotNullable = function(u) {
829829
this.expr.assertIteratedExprsAreNotNullable(u);
830830
};
831-
L.Apply.prototype.assertIteratedExprsAreNotNullable = function(u) {
831+
O.Apply.prototype.assertIteratedExprsAreNotNullable = function(u) {
832832
this.args.forEach((e) => {
833833
e.assertIteratedExprsAreNotNullable(u);
834834
});
@@ -914,7 +914,7 @@ Object.defineProperties(_.prototype, {
914914
}
915915
});
916916
var Nu = _;
917-
const kt = Nu, Lu = C, $t = "✗", jt = "✓", Mt = "⋅", qt = "⇒", Gt = "␉", Ut = "␊", Vt = "␍", vu = {
917+
const kt = Nu, Ou = C, $t = "✗", jt = "✓", Mt = "⋅", qt = "⇒", Gt = "␉", Ut = "␊", Vt = "␍", vu = {
918918
succeeded: 1,
919919
isRootNode: 2,
920920
isImplicitSpaces: 4,
@@ -923,11 +923,11 @@ const kt = Nu, Lu = C, $t = "✗", jt = "✓", Mt = "⋅", qt = "⇒", Gt = "␉
923923
terminatesLR: 32
924924
};
925925
function Kt(u) {
926-
return Lu.repeat(" ", u).join("");
926+
return Ou.repeat(" ", u).join("");
927927
}
928928
function zt(u, e, t) {
929929
const r = De(u.slice(e, e + t));
930-
return r.length < t ? r + Lu.repeat(" ", t - r.length).join("") : r;
930+
return r.length < t ? r + Ou.repeat(" ", t - r.length).join("") : r;
931931
}
932932
function De(u) {
933933
return typeof u == "string" ? u.replace(/ /g, Mt).replace(/\t/g, Gt).replace(/\n/g, Ut).replace(/\r/g, Vt) : String(u);
@@ -992,7 +992,7 @@ V.prototype.walk = function(u, e) {
992992
}) : r(this, null, 0);
993993
};
994994
V.prototype.toString = function() {
995-
const u = new Lu.StringBuffer();
995+
const u = new Ou.StringBuffer();
996996
return this.walk((e, t, r) => {
997997
if (!e)
998998
return this.SKIP;
@@ -1007,7 +1007,7 @@ V.prototype.toString = function() {
10071007
}), u.contents();
10081008
};
10091009
var he = V;
1010-
const Ht = he, Ae = C, Wt = W, Ou = wu, y = A, { TerminalNode: ou } = Ou, { NonterminalNode: Jt } = Ou, { IterationNode: Zt } = Ou;
1010+
const Ht = he, Ae = C, Wt = W, Lu = wu, y = A, { TerminalNode: ou } = Lu, { NonterminalNode: Jt } = Lu, { IterationNode: Zt } = Lu;
10111011
y.PExpr.prototype.eval = Ae.abstract("eval");
10121012
y.any.eval = function(u) {
10131013
const { inputStream: e } = u, t = e.pos, r = e.next();
@@ -1257,30 +1257,30 @@ v.Apply.prototype.outputRecipe = function(u, e) {
12571257
v.UnicodeChar.prototype.outputRecipe = function(u, e) {
12581258
return ["unicodeChar", z(this, e), this.category];
12591259
};
1260-
const Xt = C, O = A;
1261-
O.PExpr.prototype.introduceParams = Xt.abstract("introduceParams");
1262-
O.any.introduceParams = O.end.introduceParams = O.Terminal.prototype.introduceParams = O.Range.prototype.introduceParams = O.Param.prototype.introduceParams = O.UnicodeChar.prototype.introduceParams = function(u) {
1260+
const Xt = C, L = A;
1261+
L.PExpr.prototype.introduceParams = Xt.abstract("introduceParams");
1262+
L.any.introduceParams = L.end.introduceParams = L.Terminal.prototype.introduceParams = L.Range.prototype.introduceParams = L.Param.prototype.introduceParams = L.UnicodeChar.prototype.introduceParams = function(u) {
12631263
return this;
12641264
};
1265-
O.Alt.prototype.introduceParams = function(u) {
1265+
L.Alt.prototype.introduceParams = function(u) {
12661266
return this.terms.forEach((e, t, r) => {
12671267
r[t] = e.introduceParams(u);
12681268
}), this;
12691269
};
1270-
O.Seq.prototype.introduceParams = function(u) {
1270+
L.Seq.prototype.introduceParams = function(u) {
12711271
return this.factors.forEach((e, t, r) => {
12721272
r[t] = e.introduceParams(u);
12731273
}), this;
12741274
};
1275-
O.Iter.prototype.introduceParams = O.Not.prototype.introduceParams = O.Lookahead.prototype.introduceParams = O.Lex.prototype.introduceParams = function(u) {
1275+
L.Iter.prototype.introduceParams = L.Not.prototype.introduceParams = L.Lookahead.prototype.introduceParams = L.Lex.prototype.introduceParams = function(u) {
12761276
return this.expr = this.expr.introduceParams(u), this;
12771277
};
1278-
O.Apply.prototype.introduceParams = function(u) {
1278+
L.Apply.prototype.introduceParams = function(u) {
12791279
const e = u.indexOf(this.ruleName);
12801280
if (e >= 0) {
12811281
if (this.args.length > 0)
12821282
throw new Error("Parameterized rules cannot be passed as arguments to another rule.");
1283-
return new O.Param(e).withSource(this.source);
1283+
return new L.Param(e).withSource(this.source);
12841284
} else
12851285
return this.args.forEach((t, r, n) => {
12861286
n[r] = t.introduceParams(u);
@@ -2512,7 +2512,7 @@ uu.ProtoBuiltInRules = new uu(
25122512
}
25132513
);
25142514
var Eu = uu;
2515-
const Su = Eu, Nr = Tu, Se = C, Z = W, Lr = tu;
2515+
const Su = Eu, Nr = Tu, Se = C, Z = W, Or = tu;
25162516
function G(u) {
25172517
this.name = u;
25182518
}
@@ -2606,11 +2606,11 @@ G.prototype.override = function(u, e, t, r, n) {
26062606
G.prototype.extend = function(u, e, t, r, n) {
26072607
if (!this.ensureSuperGrammar().rules[u])
26082608
throw Z.cannotExtendUndeclaredRule(u, this.superGrammar.name, n);
2609-
const s = new Lr.Extend(this.superGrammar, u, t);
2609+
const s = new Or.Extend(this.superGrammar, u, t);
26102610
return s.source = t.source, this.installOverriddenOrExtendedRule(u, e, s, n), this;
26112611
};
2612-
var Or = G;
2613-
const Pr = Eu, ee = Or, F = tu;
2612+
var Lr = G;
2613+
const Pr = Eu, ee = Lr, F = tu;
26142614
function be() {
26152615
}
26162616
be.prototype = {
@@ -3284,7 +3284,7 @@ var gn = pu.exports;
32843284
function Bn(u) {
32853285
return u && u.__esModule && Object.prototype.hasOwnProperty.call(u, "default") ? u.default : u;
32863286
}
3287-
var Le = {};
3287+
var Oe = {};
32883288
(function(u) {
32893289
u.abstract = function(t) {
32903290
const r = t || "";
@@ -3378,8 +3378,8 @@ var Le = {};
33783378
return r;
33793379
}
33803380
};
3381-
})(Le);
3382-
const { assert: eu } = Le;
3381+
})(Oe);
3382+
const { assert: eu } = Oe;
33833383
function yn(u, e, t) {
33843384
return t(e[u]);
33853385
}
@@ -3462,7 +3462,7 @@ Fu.prototype.addOperation = function(u, e) {
34623462
}, this;
34633463
};
34643464
var Sn = Fu;
3465-
const Oe = {
3465+
const Le = {
34663466
_terminal() {
34673467
return this.sourceString;
34683468
},
@@ -3502,15 +3502,15 @@ function bn(u, e) {
35023502
if (typeof u.failed != "function" || u.failed())
35033503
throw new Error("toAST() expects a succesful MatchResult as first parameter");
35043504
e = Object.assign({}, e);
3505-
const t = Object.assign({}, Oe);
3505+
const t = Object.assign({}, Le);
35063506
for (const i in e)
35073507
typeof e[i] == "function" && (t[i] = e[i], delete e[i]);
35083508
return u._cst.grammar.createSemantics().addOperation("toAST(mapping)", t)(u).toAST(e);
35093509
}
35103510
function wn(u) {
35113511
if (typeof u.createSemantics != "function")
35123512
throw new Error("semanticsToAST() expects a Grammar as parameter");
3513-
return u.createSemantics().addOperation("toAST(mapping)", Oe);
3513+
return u.createSemantics().addOperation("toAST(mapping)", Le);
35143514
}
35153515
var oe = {
35163516
helper: bn,
@@ -3520,10 +3520,18 @@ var oe = {
35203520
semanticsForToAST: oe.semantics,
35213521
toAST: oe.helper
35223522
};
3523-
const Nn = /* @__PURE__ */ Bn(_n), ae = Nn.toAST, f = {}, Pe = {}, Re = (u = {}) => u.child(1).child(0).children, Ln = (u = "") => {
3524-
const e = u.split("/"), t = e.length > 1 ? Number(e[1]) : 0;
3525-
return t ? Number(e[0]) / t : 1;
3526-
}, On = (u, e = 14) => {
3523+
const Nn = /* @__PURE__ */ Bn(_n), ae = Nn.toAST, f = {}, Pe = {}, Re = (u = {}) => u.child(1).child(0).children, On = (u = "") => {
3524+
const e = u.trim().replace(/%$/, "");
3525+
return Number(e) / 100;
3526+
}, Ln = (u = "") => {
3527+
if (u.trim().endsWith("%"))
3528+
return On(u);
3529+
const e = u.trim().split("/");
3530+
if (e.length !== 2)
3531+
return NaN;
3532+
const t = Number(e[1]);
3533+
return t ? Number(e[0]) / t : Number.POSITIVE_INFINITY;
3534+
}, Pn = (u, e = 14) => {
35273535
if (u == null)
35283536
return u;
35293537
const t = Math.pow(10, e), r = Math.round(u * t) / t;
@@ -3532,7 +3540,7 @@ const Nn = /* @__PURE__ */ Bn(_n), ae = Nn.toAST, f = {}, Pe = {}, Re = (u = {})
35323540
f.numericLiteral = (u) => {
35333541
if (u.endsWith("%")) {
35343542
const e = Number(u.replace("%", ""));
3535-
return On(e * 0.01);
3543+
return Pn(e * 0.01);
35363544
}
35373545
return u.includes("/") ? Ln(u) : Number(u);
35383546
};
@@ -3602,7 +3610,7 @@ f.Command = (u = {}) => {
36023610
}
36033611
return r;
36043612
};
3605-
const Pn = {
3613+
const Rn = {
36063614
KeyValues: (u, e) => ({
36073615
type: "Map",
36083616
value: f.Map(u, !0)
@@ -3615,14 +3623,14 @@ const Pn = {
36153623
Pe.parse = (u, e, t = {}) => {
36163624
const r = e.match(u);
36173625
r.succeeded() || console.error("Match failed: " + r.message);
3618-
const { outputAST: n, outputMD: i } = t, s = [], o = ae(r, Pn);
3626+
const { outputAST: n, outputMD: i } = t, s = [], o = ae(r, Rn);
36193627
let p = {};
36203628
for (let a of o)
36213629
a.type === "Markdown" ? s.push(a.value) : p = { ...p, ...a.value };
36223630
const D = { result: p };
36233631
return n && (D.AST = ae(r)), i && (D.markdowns = s), D;
36243632
};
3625-
const Rn = `mationGrammar {
3633+
const Tn = `mationGrammar {
36263634
Program = (markdown | KeyValues)*
36273635
36283636
KeyValues = (KeyValue ","?)+ //> ","? // Nonempty needed for + on parent
@@ -3753,12 +3761,12 @@ const Rn = `mationGrammar {
37533761
octalNum = "0" octalDigit+
37543762
37553763
sourceChar = any
3756-
}`, Tn = (u = "", e = {}) => {
3764+
}`, kn = (u = "", e = {}) => {
37573765
const t = gn.grammar(
3758-
typeof e.grammarText == "string" ? e.grammarText : Rn
3766+
typeof e.grammarText == "string" ? e.grammarText : Tn
37593767
);
37603768
return Pe.parse(u, t, e);
37613769
};
37623770
export {
3763-
Tn as parse
3771+
kn as parse
37643772
};

0 commit comments

Comments
 (0)