| %or% {parcr} | R Documentation |
Applying alternative parsers
Description
The %or% combinator (p1 %or% p2) returns the result of p1 if p1 is
successful or, if p1 fails that of p2 if p2 parses successfully,
otherwise it returns a fail.
Usage
p1 %or% p2
Arguments
p1, p2 |
two parsers. |
Value
A parser.
Pseudocode
(p1 %or% p2)(x):
if p1(x)==[] then
if p2(x)==[] then fail()(x) else p2(x)
else p1(x)
where [] is the empty list.
Examples
(literal("A") %or% literal("a"))(LETTERS[1:5]) # success on first parser
(literal("A") %or% literal("a"))(letters[1:5]) # success on second parser
(literal("A") %or% literal("a"))(LETTERS[2:6]) # failure
starts_with_a <- function(x) grepl("^a",x[1])
# success on both parsers, but returns result of p1 only
(literal("a") %or% satisfy(starts_with_a)) (letters[1:5])
[Package parcr version 0.5.2 Index]