Description Usage Arguments Value Examples
That is invoke the parser to parse a given input text using the specified rule as the root.
1 | apply_rule(parser, rule.id, input.text, exe = NULL, record = NULL)
|
parser, |
a peg parser produced by new.parser |
rule.id, |
a character string naming the rule |
arg, |
a character string to be parsed |
exe, |
(optional, default=FALSE) a flag indicate whether actions should be performed. when FALSE, no actions will be executed |
record, |
(optional, default=FALSE) a flag which when set produces tree of snapshots of all nodes visited during a successful parse. |
A PEGResult, a container containing the status of the parsing attempt, a record of the characters parsed, and a list of one or more values computed during the parse. If exe not set (FALSE) or no actions have been attached to the rules, then the values will simply be a list of all literals (characters, or strings) that appeared in the input text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # The simplest example: a parser that only accepts a single character 'a'
peg<-new.parser()
peg<-add_rule(peg, "A<-'a'")
res<-apply_rule(peg, 'A', "a")
res # the results
# A more complex example:
# A number parser: Extracts number
peg<-new.parser()
peg<-add_rule(peg, "NUM<-[0-9]+ ('.' [0-9]*)?)", act = "paste1(v)", des = "numbers")
apply_rule(peg, "NUM", "12.3", exe=T)
peg<-new.parser()
# rule set to convert distances to meters
peg<-add_rule(peg, "NUM<-[0-9]+ ('.' [0-9]*)?)", act = "list( num=as.numeric(paste(v,collapse='')) )", des = "numbers")
peg<-add_rule(peg, "IN<- NUM ' '* 'in' ", act="list( num=0.0254*v$num)", des="inches; ins 2 meters")
peg<-add_rule(peg, "FT<- NUM ' '* 'ft' ", act="list( num=0.3048*v$num)", des="feet; ft 2 meters")
peg<-add_rule(peg, "CM<- NUM ' '* 'cm' ", act="list( num=.01*v$num)", des="centimeters; cm 2 meters")
peg<-add_rule(peg, "M<- NUM ' '* 'm' ", act="list(num=v$num)", des="meters; extract the num")
peg<-add_rule(peg, "SP<-(' ')", act="list()", des="spaces; eats spaces")
peg<-add_rule(peg, "GP<- (IN / FT / CM / M)", des="added for group t")
peg<-add_rule(peg, "LEN<-(GP SP+ LEN) / GP ", act="list(sum(unlist(v)))", des="length; sum all the units" )
#first test the components
apply_rule(peg, "NUM", "3", exe=T)
apply_rule(peg, "IN", "1.0 in", exe=T)
apply_rule(peg, "FT", "2 ft", exe=T)
apply_rule(peg, "CM", "100 cm", exe=T)
apply_rule(peg, "M", "1 m", exe=T)
#now test together
apply_rule(peg, "LEN", "3 ft 3.3701 in", exe=T)
apply_rule(peg, "LEN", "3 m 4.8 cm", exe=T)
apply_rule(peg, "LEN", "10 ft",exe=T)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.