Skip to content

Commit

Permalink
Merge 93cb2a2 into 9d2245f
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mock committed Jul 26, 2019
2 parents 9d2245f + 93cb2a2 commit 9172dc1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
25 changes: 25 additions & 0 deletions API.md
Expand Up @@ -74,6 +74,7 @@
* [parser.lookahead(anotherParser)](#parserlookaheadanotherparser)
* [parser.lookahead(string)](#parserlookaheadstring)
* [parser.lookahead(regexp)](#parserlookaheadregexp)
* [parser.assert(condition, message)](#parserassertcondition-message)
* [parser.tie()](#parsertie)
* [parser.many()](#parsermany)
* [parser.times(n)](#parsertimesn)
Expand Down Expand Up @@ -1091,6 +1092,30 @@ Returns a parser that looks for `string` but does not consume it. Yields the sam

Returns a parser that wants the input to match `regexp`. Yields the same result as `parser`. Equivalent to `parser.skip(Parsimmon.lookahead(regexp))`.

## parser.assert(condition, message)

Passes the result of `parser` to the function `condition`, which returns a boolean. If the the condition is false, returns a failed parse with the given `message`.

```js
var evenLengthNumber =
P.digits
.assert(
function(s) {
return s.length % 2 === 0;
},
"even length number"
)
.map(Number);

evenLengthNumber.parse("34");
// { status: true, value: 34 }

evenLengthNumber.parse("1");
// { status: false,
// expected: ["even length number"],
// index: {...} }
```

## parser.tieWith(separator)

When called on a parser yielding an array of strings, yields all their strings concatenated with the `separator`. Asserts that its input is actually an array of strings.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## version 1.13.0 (2019-07-25)

- Adds `parser.assert(condition, message)`

## version 1.12.1 (2019-07-07)

- Adds links within documentation
Expand Down
25 changes: 19 additions & 6 deletions test/core/assert.test.js
Expand Up @@ -8,11 +8,24 @@ describe("assert", function() {
function condition2() {
return true;
}
var p1 = Parsimmon.string("a").assert(condition1, "parsing error");
var p2 = Parsimmon.string("a").assert(condition2, "parsing error");
assert.deepEqual(p1.parse("a").status, false);
assert.deepEqual(p1.parse("a").expected, ["parsing error"]);
assert.deepEqual(p2.parse("a").status, true);
assert.deepEqual(p2.parse("a").value, "a");
var p0 = Parsimmon.string("a");
var p1 = p0.assert(condition1, "parsing error");
var p2 = p0.assert(condition2, "parsing error");
assert.deepEqual(p1.parse("a"), {
status: false,
expected: ["parsing error"],
index: { offset: 1, line: 1, column: 2 }
});
assert.deepEqual(p1.parse("b"), {
status: false,
expected: ["'a'"],
index: { offset: 0, line: 1, column: 1 }
});
assert.deepEqual(p2.parse("a"), { status: true, value: "a" });
assert.deepEqual(p2.parse("b"), {
status: false,
expected: ["'a'"],
index: { offset: 0, line: 1, column: 1 }
});
});
});

0 comments on commit 9172dc1

Please sign in to comment.