p.or and p.alt wont backtrack
See original GitHub issueI’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:
- Created 7 years ago
- Comments:19 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thechoice
function) is also not commutative.Just so you know,
x.or(y)
is just defined asP.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.