Skip to content

Commit

Permalink
Put parens around integer conversions in repetition. Fixes peggyjs#381.
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Mar 20, 2023
1 parent 66d73f6 commit 7a71ba4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ Released: TBD

### Bug Fixes

- [#371](https://github.com/peggyjs/peggy/issues/371) Error using online Peggy - Can't find variable: util
- [#371](https://github.com/peggyjs/peggy/issues/371) Error using online Peggy - "Can't find variable: util". From @hildjj.
- [#374](https://github.com/peggyjs/peggy/issues/374) CLI throws exception
on grammar errors. From @hildjj
- [#381](https://github.com/peggyjs/peggy/issues/381) Repetitions with code blocks
for min or max not handling non-integer returns correctly. From @hildjj.

3.0.1
-----
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ function generateJS(ast, options) {
break;

case op.IF_LT_DYNAMIC: // IF_LT_DYNAMIC min, t, f
compileCondition(stack.top() + ".length < " + stack.index(bc[ip + 1]) + "|0", 1);
compileCondition(stack.top() + ".length < (" + stack.index(bc[ip + 1]) + "|0)", 1);
break;

case op.IF_GE_DYNAMIC: // IF_GE_DYNAMIC max, t, f
compileCondition(stack.top() + ".length >= " + stack.index(bc[ip + 1]) + "|0", 1);
compileCondition(stack.top() + ".length >= (" + stack.index(bc[ip + 1]) + "|0)", 1);
break;

case op.WHILE_NOT_ERROR: // WHILE_NOT_ERROR b
Expand Down
34 changes: 34 additions & 0 deletions test/behavior/generated-parser-behavior.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,40 @@ describe("generated parser behavior", () => {
expect(parser).to.parse("abb", ["a", ["b", "b"]]);
});
});

// Regression tests for https://github.com/peggyjs/peggy/issues/381
it("with functions returning non-integers", () => {
// Like |0|
let parser = peg.generate("start = 'a'|{ return 'a' }|", options);
expect(parser).to.parse("", []);
expect(parser).to.failToParse("aaa", {
expected: [{ type: "end" }],
});

// Like |0..2|
parser = peg.generate("start = 'a'|{ return 'a' }..2|", options);
expect(parser).to.parse("", []);
expect(parser).to.parse("a", ["a"]);
expect(parser).to.parse("aa", ["a", "a"]);
expect(parser).to.failToParse("aaa", {
expected: [{ type: "end" }],
});

// Like |..0|
parser = peg.generate("start = 'a'|..{ return 'a' }|", options);
expect(parser).to.parse("", []);
expect(parser).to.failToParse("a", {
expected: [{ type: "end" }],
});

// Like |1..2|
parser = peg.generate("start = 'a'|{return '1.8e0' }..{ return 2.8 }|", options);
expect(parser).to.parse("a", ["a"]);
expect(parser).to.parse("aa", ["a", "a"]);
expect(parser).to.failToParse("aaa", {
expected: [{ type: "end" }],
});
});
});

describe("with delimiter", () => {
Expand Down

0 comments on commit 7a71ba4

Please sign in to comment.