question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

p.or and p.alt wont backtrack

See original GitHub issue

I’ve these two examples which fails

// Input
var input = "hello hello";

//  Parser
var hello = p.string("hello");
var hellos = p.sepBy(hello, p.whitespace);

// Examples
console.info(p.alt(hello, hellos).parse(input));
console.info(hello.or(hellos).parse(input));

// Output
{ status: false,
  index: { offset: 5, line: 1, column: 6 },
  expected: [ 'EOF' ] }
{ status: false,
  index: { offset: 5, line: 1, column: 6 },
  expected: [ 'EOF' ] }

I was expecting both to try the first parser hello and on failure backtrack and continue with hellos. How come this isn’t possible and how would I solve this without having to reimplement the different parsers?

Real world use case: In my application I’ve have created different isolated parsers which initially overlap. Here is some example input (:highlights in vim):

var example1 = "SpellLocal     xxx ctermbg=14 gui=undercurl"
var example2 = "rubyEval       xxx links to Statement"
var example3 = "rubyKeywordAsMethod xxx cleared"

Each line represents a different parser, but happens to look the same in the beginning. Each example have it’s own parser. A line can only be mapped to one parser. I was hoping to do something like below as this would allow me keep the parsers independent.

var input = [example1, example2, example3].sample();
exampleParser1.or(exampleParser2).or(exampleParser3).parse(input)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jneencommented, Aug 6, 2016

Alternation in parser combinators is not generally commutative - the only requirement is that it’s associative - alt(a, alt(b, c)) == alt(alt(a, b), c), which it is in this case. Parsec’s alternation (the <|> operator, or the choice function) is also not commutative.

1reaction
wavebeemcommented, Jul 25, 2016

Just so you know, x.or(y) is just defined as P.alt(x, y), so the results of those should never be different.

The error in your parser is that you need to put the longer of two parsers first when they start with the same prefix.

The documentation gives an example of this limitation and how to work around it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 Keyboard Shortcuts to Use When Presenting ...
If you need to backtrack, press the Backspace, P, or left arrow key to go back to the previous slide or animation. 5....
Read more >
Parsec: “try a <|> b” considered harmful - ezyang's blog
After a little Googling, they discover that Parsec doesn't backtrack by default. Well, that's fine; why not just insert a try into the...
Read more >
Backtrack - keyboard problem
Hey, I need some help with Backtrack my keyboard is not working. I can login and "startx" after that I need a usb...
Read more >
P-99: Ninety-Nine Prolog Problems
P-99: Ninety-Nine Prolog Problems ... has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.
Read more >
backtrack linux - How to enter null password for WiFi?
1 / Backtrack. I've googled that null on Win is alt + 255, but that didn't work for me. Any ideas are nice....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found