Fetching contributors…
Cannot retrieve contributors at this time
112 lines (71 sloc) 2.51 KB


DRAFT: Synopsis 32: Setting Library - Rules


Created: 27 Feb 2009

Last Modified: 5 Jul 2013
Version: 2

The document is a draft.


This synopsis deals with objects produced by the regexes and grammars described in much greater detail in S05.



class Regex is Method {...}

Regex objects are created through the syntax described in S05:

/ ... /
rx/ ... /
regex { ... }

They can be stored in variables for later use, as with the qr// syntax in Perl 5.

method Bool(Regex:D: )

In boolean context, a regex object will match against $_, and return the result as a Bool.


class Match is Cool does Positional does Associative {
    method from( --> Int )  {...}
    method to( --> Int )    {...}
    method chars( --> Int ) {...}
    method orig()           {...}
    method ast()            {...}
    method caps()           {...}
    method chunks()         {...}
    method pos()            {...}

    method bool()           {...}
    method Str()            {...}
    method Num()            {...}
    method ast()            {...}


class Cursor {
    method pos( --> Int ) {...}
    method orig() {...}


class Grammar is Cursor

Much as a class is a collection of named attributes and methods, a grammar is a collection of named regexes and rules. For more on creating and using grammars, see "Grammars" in S05.

parse / subparse
method parse ($target, :$rule = 'TOP', Mu :$actions = Mu, *%opts)
method subparse ($target, :$rule = 'TOP', Mu :$actions = Mu, *%opts)

Parses the $target string with given <:rule>, and returns the result as a Match object. Defaults to the TOP rule. A class containing actions may be attached using the actions named argument.

The parse method automatically anchors the rule to the beginning and end of the target string (adding ^ and $ around the rule). The subparse method does not add anchors, and will match substrings against the rule.

method parsefile (Cool $filename, :$rule = 'TOP', Mu :$actions = Mu, *%opts)

Parse the file as with .parse, but matches the grammar against the contents of $filename instead.

See Abstractions.pod


Tim Nelson <>
Larry Wall <>
Brent Laabs <>