peggy icon indicating copy to clipboard operation
peggy copied to clipboard

Reduce if statements for always-succeed cases

Open hildjj opened this issue 1 year ago • 4 comments

If a rule always succeeds, the code that calls that rule does not need to check the result of its parse function. The else from that check is difficult to test without resorting to tricks.

hildjj avatar Nov 25 '24 22:11 hildjj

This is separate, but potentially related to #576.

hildjj avatar Nov 25 '24 22:11 hildjj

This is already should be implemented, inference-match-result pass does the job. You found an example where something else can be removed?

Mingun avatar Nov 26 '24 04:11 Mingun

Here is a cut-down example:

Foo
  = either " "

either
  = "."
  / [a-z]*

which has this in the output js:

  function peg$parseFoo() {
    var s0, s1, s2;

    s0 = peg$currPos;
    s1 = peg$parseeither();
    if (s1 !== peg$FAILED) {
...

hildjj avatar Nov 26 '24 19:11 hildjj

Just came to look at this again, and it took me a few seconds to realize that either always matches because the last choice matches the empty string.

hildjj avatar Apr 08 '25 21:04 hildjj