Skip to content

Commit

Permalink
Optional whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
barsdeveloper committed Jan 3, 2024
1 parent 4fb5fad commit da577f1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
11 changes: 7 additions & 4 deletions dist/parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,15 +769,18 @@ class Parsernostrum {
/** Parser accepting whitespace */
static whitespace = this.regexp(/\s+/)

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

/** Parser accepting whitespace that spans on a single line */
static whitespaceInline = this.regexp(/[^\S\n]+/)

/** Parser accepting whitespace that spans on a single line */
static whitespaceInlineOpt = this.regexp(/[^\S\n]+/)

/** Parser accepting whitespace that contains a list a newline */
static whitespaceMultiline = this.regexp(/\s*?\n\s*/)

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

/** Parser accepting a double quoted string and returns the content */
static doubleQuotedString = this.regexpGroups(new RegExp(`"(${this.#createEscapeable('"')})"`))
.map(this.#secondElementGetter)
Expand Down Expand Up @@ -933,7 +936,7 @@ class Parsernostrum {
}

skipSpace() {
return this.Self.seq(this, this.Self.optWhitespace).map(Parsernostrum.#firstElementGetter)
return this.Self.seq(this, this.Self.whitespaceOpt).map(Parsernostrum.#firstElementGetter)
}

/**
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.6",
"version": "1.0.7",
"description": "A tiny LL parser combinator library for javascript",
"main": "src/Parsernostrum.js",
"types": "src/types.js",
Expand Down
11 changes: 7 additions & 4 deletions src/Parsernostrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ export default class Parsernostrum {
/** Parser accepting whitespace */
static whitespace = this.regexp(/\s+/)

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

/** Parser accepting whitespace that spans on a single line */
static whitespaceInline = this.regexp(/[^\S\n]+/)

/** Parser accepting whitespace that spans on a single line */
static whitespaceInlineOpt = this.regexp(/[^\S\n]+/)

/** Parser accepting whitespace that contains a list a newline */
static whitespaceMultiline = this.regexp(/\s*?\n\s*/)

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

/** Parser accepting a double quoted string and returns the content */
static doubleQuotedString = this.regexpGroups(new RegExp(`"(${this.#createEscapeable('"')})"`))
.map(this.#secondElementGetter)
Expand Down Expand Up @@ -222,7 +225,7 @@ export default class Parsernostrum {
}

skipSpace() {
return this.Self.seq(this, this.Self.optWhitespace).map(Parsernostrum.#firstElementGetter)
return this.Self.seq(this, this.Self.whitespaceOpt).map(Parsernostrum.#firstElementGetter)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/grammars/MathGrammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class MathGrammar {

static #opFragment = P.lazy(() =>
P.seq(
P.optWhitespace,
P.whitespaceOpt,
P.alt(
P.str("^").map(() => ({
precedence: 20,
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class MathGrammar {
function: (a, b) => a - b,
})),
).map(v => v),
P.optWhitespace,
P.whitespaceOpt,
MathGrammar.expressionFragment,
)
.map(([_0, operator, _2, expressionFragment]) => [operator, ...expressionFragment])
Expand All @@ -79,9 +79,9 @@ export default class MathGrammar {
MathGrammar.#number.map(v => [v]),
P.seq(
P.str("("),
P.optWhitespace,
P.whitespaceOpt,
P.lazy(() => MathGrammar.expressionFragment),
P.optWhitespace,
P.whitespaceOpt,
P.str(")"),
).map(([_0, _1, entries]) => [this.#evaluate(entries)])
))
Expand Down
2 changes: 1 addition & 1 deletion tests/serializing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test("Test 1", async ({ page }) => {
test("Test 2", async ({ page }) => {
const g = P.lazy(() => P.lazy(() => P.seq(
P.str("Italy"),
P.lazy(() => P.regexp(/Switzerland/).chain(v => P.optWhitespace)),
P.lazy(() => P.regexp(/Switzerland/).chain(v => P.whitespaceOpt)),
P.alt(
P.str("Austria").map(() => 123),
P.alt(
Expand Down
Loading

0 comments on commit da577f1

Please sign in to comment.