charInSetParser: Single character, belonging to a given set, token

View source: R/charInSetParser.R

charInSetParserR Documentation

Single character, belonging to a given set, token

Description

Recognises a single character satisfying a predicate function.

Usage

  
  charInSetParser(fun,
                 action = function(s) list(type="charInSet",value=s), 
                 error  = function(p) list(type="charInSet",pos  =p)) 

Arguments

fun

Function to determine if character belongs to a set. Argument "fun" is a signature function: character -> logical (boolean)

action

Function to be executed if recognition succeeds. Character stream making up the token is passed as parameter to this function

error

Function to be executed if recognition does not succeed. Position of streamParser obtained with streamParserPosition is passed as parameter to this function

Value

Anonymous function, returning a list.

function(stream) –> list(status,node,stream)

From input parameters, an anonymous function is defined. This function admits just one parameter, stream, with type streamParser, and returns a three-field list:

  • status

    "ok" or "fail"

  • node

    With action or error function output, depending on the case

  • stream

    With information about the input, after success or failure in recognition

Examples


# fail
stream  <- streamParserFromString("H")
( charInSetParser(isDigit)(stream) )[c("status","node")]

# ok 
stream  <- streamParserFromString("a")
( charInSetParser(isLetter)(stream) )[c("status","node")]



qmrparser documentation built on April 24, 2022, 1:05 a.m.