logo       

PGE: return values in embedded closures: msg#00021

lang.perl.perl6.compiler

Subject: PGE: return values in embedded closures

I've just applied a change (r11921) to PGE that allows
embedded closures in rules to designate a "return value" for
a match (recently added to S05).

Essentially this means that a rule such as

/:lang(PIR) foo {{ .return "bar" }} /

will match a string containing "foo" but gives the returned match
object a value of "bar".

We can also do things such as

/:lang(PIR) \d+ {{ # return 200 more than matched
$I0 = match
$I0 += 200
.return ($I0) }} /

/:lang(PIR) <moose> {{
$P0 = match["moose"] # get subpattern match
$P1 = "Moose_new"($P0) # create a new Moose object from it
.return ($P0) }} / # and return that as the Match value

To obtain the return value of a Match object, use the value() method:

match = rule(string) # match string against rule
$P0 = match.value()

If a match object doesn't have an explicit return value, then
its value is the substring that it matched.

There are some small examples in t/compilers/pge/p6rules/closure.t .
Comments, questions and tests welcomed.

Pm



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise