Skip to content

Commit

Permalink
✨ Added percentNum
Browse files Browse the repository at this point in the history
  • Loading branch information
mationai committed Sep 10, 2022
1 parent 9540b57 commit a0c1772
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 28 deletions.
23 changes: 19 additions & 4 deletions dist/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -4372,15 +4372,29 @@ const parser = {};
const innerChilds = (node = {}) => {
return node.child(1).child(0).children;
};
parse$1.literal = (literal = {}) => {
const { ctorName, sourceString } = literal.child(0);
const round = (n, decimals = 14) => {
if (n == null)
return n;
const k = Math.pow(10, decimals);
const res = Math.round(n * k) / k;
return res === 0 ? 0 : res;
};
parse$1.numericLiteral = (sourceString) => {
if (sourceString.endsWith("%")) {
const n = Number(sourceString.replace("%", ""));
return round(n * 0.01);
}
return Number(sourceString);
};
parse$1.literal = (node = {}) => {
const { ctorName, sourceString } = node.child(0);
switch (ctorName) {
case "nullLiteral":
return null;
case "booleanLiteral":
return sourceString === "true";
case "numericLiteral":
return Number(sourceString);
return parse$1.numericLiteral(sourceString);
case "stringLiteral":
return sourceString.slice(1, -1);
}
Expand Down Expand Up @@ -4598,14 +4612,15 @@ var grammar = `mationGrammar {
// Note that the ordering of hexNum and decimalNum is reversed w.r.t. the spec
// This is intentional: the order decimalNum | hexNum will parse
// "0x..." as a decimal literal "0" followed by "x..."
numericLiteral = "-"? (octalNum | hexNum | decimalNum)
numericLiteral = "-"? (percentNum | octalNum | hexNum | decimalNum)
decimalNum = integer "." decimalDigit* exponentPart -- bothParts
| "." decimalDigit+ exponentPart -- decimalsOnly
| integer exponentPart -- integerOnly
integer = nonZeroDigit decimalDigit* -- nonZero
| "0" -- zero
hexNum = "0x" hexDigit+
| "0X" hexDigit+
percentNum = decimalNum "%"
// hexDigit defined in Ohm's built-in rules (otherwise: hexDigit = "0".."9" | "a".."f" | "A".."F")
octalNum = "0" octalDigit+
Expand Down
33 changes: 17 additions & 16 deletions dist/index.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mation-spec",
"version": "0.2.0",
"version": "0.2.2",
"description": "Readable Configuration Specification Format for Automation and Task Runners",
"packageManager": "[email protected]",
"main": "./dist/index.umd.js",
Expand Down
3 changes: 2 additions & 1 deletion src/mationGrammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ mationGrammar {
// Note that the ordering of hexNum and decimalNum is reversed w.r.t. the spec
// This is intentional: the order decimalNum | hexNum will parse
// "0x..." as a decimal literal "0" followed by "x..."
numericLiteral = "-"? (octalNum | hexNum | decimalNum)
numericLiteral = "-"? (percentNum | octalNum | hexNum | decimalNum)
decimalNum = integer "." decimalDigit* exponentPart -- bothParts
| "." decimalDigit+ exponentPart -- decimalsOnly
| integer exponentPart -- integerOnly
integer = nonZeroDigit decimalDigit* -- nonZero
| "0" -- zero
hexNum = "0x" hexDigit+
| "0X" hexDigit+
percentNum = decimalNum "%"
// hexDigit defined in Ohm's built-in rules (otherwise: hexDigit = "0".."9" | "a".."f" | "A".."F")
octalNum = "0" octalDigit+

Expand Down
22 changes: 19 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@ const innerChilds = (node={}) => {
return node.child(1).child(0).children // child(1) left-terminal, eg. [, {, <, (
}

parse.literal = (literal={}) => {
const { ctorName, sourceString } = literal.child(0)
export const round = (n, decimals=14) => {
if (n == null)
return n
const k = Math.pow(10, decimals)
const res = Math.round(n * k) / k
return res === 0 ? 0 : res // no -0
}

parse.numericLiteral = (sourceString) => {
if (sourceString.endsWith('%')) {
const n = Number(sourceString.replace('%', ''))
return round(n * .01)
}
return Number(sourceString)
}

parse.literal = (node={}) => {
const { ctorName, sourceString } = node.child(0)
switch (ctorName) {
case 'nullLiteral':
return null
case 'booleanLiteral':
return sourceString === 'true'
case 'numericLiteral':
return Number(sourceString)
return parse.numericLiteral(sourceString)
case 'stringLiteral':
return sourceString.slice(1, -1)
}
Expand Down
8 changes: 5 additions & 3 deletions test/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ test(`Commands - Variations of Multiple keyvalues map args`, () => {
]})
})

test(`Commands - Mixed strings and values args`, () => {
test.only(`Commands - Mixed strings and values args`, () => {
expect(parse(`do: [
turn left 1 right 2;
turn left -1.23 right 2;
move [1] 12.3% [2] -.5% beyond:[1 2];
]`).result)
.toStrictEqual({ do: [
['turn', 'left', 1, 'right', 2],
['turn', 'left', -1.23, 'right', 2],
['move', [1], .123, [2], -.005, { beyond: [1, 2] }],
]})
})
8 changes: 8 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test, expect } from 'vitest'
import { round } from '../src/parser'

test(`round`, () => {
const eg1 = 12.3 / 100
expect(eg1).toBe(0.12300000000000001)
expect(round(eg1)).toBe(0.123)
})

0 comments on commit a0c1772

Please sign in to comment.