Learn R Programming

parcr (version 0.5.1)

reporter: Turn a parser into an error reporting parser

Description

Turns a parser into an error reporting parser, and when the parser is successful returns only the L-element of the parser output, the successfully parsed part of the input (see succeed()).

Usage

reporter(p)

Value

The L-part of a successful parser result or an error message about the line where the parser failed. A warning is thrown when the parser did not completely consume the input.

Arguments

p

a parser.

Details

The error object that this function returns is a list containing the elements linenr and linecontent, corresponding to the line in which the parser failed and its content. The user of this package can catch this object to create custom error messages instead of the message generated by this function.

A warning is issued when the parser did not completely consume the input. Complete consumption of input is only explicitly made when the parser ends with eof(). Therefore, even though all elements were parsed, a zero-length character vector will remain in the R element if the parser does not end with eof().

Examples

Run this code
at <- function() literal("a") %then% literal("t")
atat <- rep(c("a","t"),2)
# Yields an error message about parser failing on line 5
try(
  reporter(match_n(3,at()) %then% eof())(c(atat,"t","t"))
)
# No error, but parser result
reporter(match_n(2,at()) %then% eof())(atat)
# warning: the input is not completely consumed
try(
  reporter(match_n(2,at()))(atat)
)

Run the code above in your browser using DataLab