Skip to content

Commit

Permalink
Byte number parser
Browse files Browse the repository at this point in the history
  • Loading branch information
barsdeveloper committed Jan 3, 2024
1 parent 9f75f52 commit 4fb5fad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
16 changes: 11 additions & 5 deletions dist/parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,17 @@ class Parsernostrum {
static numberNatural = this.regexp(/\d+/).map(this.#numberMapper)

/** Parser accepting any valid decimal, possibly signed, possibly in the exponential form number */
static numberExponential = this.regexp(new RegExp(
this.#numberRegex.source + String.raw`(?:[eE][\+\-]?\d+)?(?!\.)`)
).map(this.#numberMapper)
static numberExponential = this.regexp(new RegExp(this.#numberRegex.source + String.raw`(?:[eE][\+\-]?\d+)?(?!\.)`))
.map(this.#numberMapper)

/** Parser accepting any valid decimal number between 0 and 1 */
static numberUnit = this.regexp(/\+?(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\.\d])/)
.map(this.#numberMapper)

/** Parser accepting any integer between 0 and 255 */
static numberByte = this.regexp(/0*(?:25[0-5]|2[0-4]\d|1?\d?\d)(?!\d|\.)/)
.map(this.#numberMapper)

/** Parser accepting whitespace */
static whitespace = this.regexp(/\s+/)

Expand Down Expand Up @@ -807,11 +810,14 @@ class Parsernostrum {
return result.status && result.position === input.length ? result : Reply.makeFailure(result.position)
}

/** @param {String} input */
/**
* @param {String} input
* @throws when the parser fails to match
*/
parse(input) {
const result = this.run(input);
if (!result.status) {
throw new Error("Parsing error")
throw new Error(`Could not parse "${input.length > 20 ? input.substring(0, 17) + "..." : input}"`)
}
return result.value
}
Expand Down
2 changes: 1 addition & 1 deletion dist/parsernostrum.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parsernostrum",
"type": "module",
"version": "1.0.4",
"version": "1.0.6",
"description": "A tiny LL parser combinator library for javascript",
"main": "src/Parsernostrum.js",
"types": "src/types.js",
Expand Down
9 changes: 6 additions & 3 deletions src/Parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ export default class Parsernostrum {
static numberNatural = this.regexp(/\d+/).map(this.#numberMapper)

/** Parser accepting any valid decimal, possibly signed, possibly in the exponential form number */
static numberExponential = this.regexp(new RegExp(
this.#numberRegex.source + String.raw`(?:[eE][\+\-]?\d+)?(?!\.)`)
).map(this.#numberMapper)
static numberExponential = this.regexp(new RegExp(this.#numberRegex.source + String.raw`(?:[eE][\+\-]?\d+)?(?!\.)`))
.map(this.#numberMapper)

/** Parser accepting any valid decimal number between 0 and 1 */
static numberUnit = this.regexp(/\+?(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\.\d])/)
.map(this.#numberMapper)

/** Parser accepting any integer between 0 and 255 */
static numberByte = this.regexp(/0*(?:25[0-5]|2[0-4]\d|1?\d?\d)(?!\d|\.)/)
.map(this.#numberMapper)

/** Parser accepting whitespace */
static whitespace = this.regexp(/\s+/)

Expand Down
17 changes: 17 additions & 0 deletions tests/simple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ test("Number unit", async ({ page }) => {
expect(() => p.parse("alpha")).toThrowError()
})

test("Number byte", async ({ page }) => {
const p = P.numberByte
expect(p.parse("0")).toEqual(0)
expect(p.parse("1")).toEqual(1)
expect(p.parse("250")).toEqual(250)
expect(p.parse("255")).toEqual(255)
expect(() => p.parse("256")).toThrowError()
expect(() => p.parse("+0")).toThrowError()
expect(() => p.parse("+1")).toThrowError()
expect(() => p.parse("1.1")).toThrowError()
expect(() => p.parse("-0.1")).toThrowError()
expect(() => p.parse("+ 0")).toThrowError()
expect(() => p.parse(" unrelated")).toThrowError()
expect(() => p.parse("betabeta")).toThrowError()
expect(() => p.parse("alpha")).toThrowError()
})

test("Whitespace inline", async ({ page }) => {
const p = P.whitespaceInline
expect(p.parse(" ")).toEqual(" ")
Expand Down

0 comments on commit 4fb5fad

Please sign in to comment.