# Define a parser with a semantic name
Nucleotide <- function() {
named(
satisfy(function(x) grepl("^[GATC]+$", x)),
"nucleotide sequence"
)
}
# When this parser fails, the error will say "Expected: nucleotide sequence"
try(reporter(Nucleotide() %then% eof())(c("GATCxTC")))
# Combine named parsers with %or% to get helpful "Expected one of:" messages
Nucleotide_or_Protein <- function() {
named(satisfy(function(x) grepl("^[GATC]+$", x)), "nucleotide") %or%
named(satisfy(function(x) grepl("^[ARNDCQEGHILKMFPSTWYV]+$", x)), "protein")
}
try(reporter(Nucleotide_or_Protein() %then% eof())(c("Some text")))
Run the code above in your browser using DataLab