%xthen% {parcr}R Documentation

Keeping only first or second result from a ⁠%then%⁠ sequence

Description

Two parsers composed in sequence produce a pair of results. Sometimes we are only interested in one component of the pair. For example in the case of reserved words such as 'begin' and 'end'. In such cases, two special versions of the ⁠%then%⁠ combinator are useful, which keep either the first or second result, as reflected by the position of the letter 'x' in their names.

Usage

p1 %xthen% p2

p1 %thenx% p2

Arguments

p1, p2

two parsers.

Value

A parser.

Pseudocode

(p1 %xthen% p2)(x):
  if p1(x)==[] or x==null then fail()(x)
  else
    if p2(x[-1])==[] then fail()(x)
    else succeed(p1(x)$L)(x[-2])

(p1 %thenx% p2)(x):
  if p1(x)==[] or x==null then fail()(x)
  else
    if p2(x[-1])==[] then fail()(x)
    else succeed(p2(x[-1])$L)(x[-2])

where null is the empty vector, x[-1] and x[-2] are the vector x without the first element and without the first two elements, respectively.

See Also

%then%

Examples

is_number <- function(x) grepl("\\d+",x[1])
# Numbers are preceded by ">" symbols, but we only want the number
(literal(">") %thenx% satisfy(is_number)) (c(">", "12"))
# Temperatures are followed by the unit 'C', but we only want the number
(satisfy(is_number) %xthen% literal("C")) (c("21", "C"))


[Package parcr version 0.5.1 Index]