set_action: Attach an (optional) action to a specified rule

Description Usage Arguments Value Examples

View source: R/set_action.R

Description

Attach an (optional) action to a specified rule

Usage

1
set_action(pegR, rule.id, action)

Arguments

parser,

a peg parser produced by new.parser

rule.id,

a character string naming the rule

action

to be attached to the specified rule. The action may be may take three forms:

  1. a function accepting a list as input and a list as output

  2. a string of text to be interpreted as a function body with an input parameter consisting of a list named v, and return value which is also a list. If the list which is the return value has NULL for names, (i.e. no names have been assigned to the list members) then the members of the list are assigned the names given by the rule id)

  3. NULL, in which case the action associated with the given rule is removed.

Value

a peg parser with the action attached

Examples

1
2
3
4
5
6
7
#Capitalize all occurances of 'a' using inline actions
peg<-new.parser()
peg<-add_rule(peg, "A<-'a'")
peg<-add_rule(peg, "R<-(A / .)+")
peg<-set_action(peg, "A", "list('A')")
peg<-set_action(peg, "R", "list(paste(v, collapse=''))" )
value(apply_rule(peg, "R", "cat in the hat", exe=T))

mslegrand/pegr documentation built on May 23, 2019, 7:53 a.m.