package Pugs::Compiler::Rule; $Pugs::Compiler::Rule::VERSION = '0.23'; # Documentation in the __END__ use 5.006; use strict; use warnings; use base 'Pugs::Compiler::Regex'; sub compile { my ( $class, $rule_source, $param ) = @_; $param = ref $param ? { %$param } : {}; $param->{ratchet} = 1 unless defined $param->{ratchet}; $param->{sigspace} = 1 unless defined $param->{sigspace} || defined $param->{s}; $class->SUPER::compile( $rule_source, $param ); } 1; __END__ =head1 NAME Pugs::Compiler::Rule - Compiler for Perl 6 Rules =head1 SYNOPSIS Un-named rules are objects: use Pugs::Compiler::Rule; my $rule = Pugs::Compiler::Rule->compile( '((.).).' ); my $match = $rule->match( 'abc' ); if ($match) { # true print $match; # "abc" print $match->from; # 0 print $match->to; # 3 print $match->[0]; # "ab" print $match->[0][0]; # "a" } Named rules are methods in a Grammar: package MyGrammar; use Pugs::Compiler::Rule; use base 'Pugs::Grammar::Base'; Pugs::Compiler::Rule->install( rule => '((.).).' ); my $match = MyGrammar->rule( 'abc' ); Rules may have parameters: $grammar->install(subrule => $source, { signature => $sig } ); $grammar->install(rule => q{ }); =head1 DESCRIPTION This module provides an implementation for Perl 6 Rules. It is a front-end to several other modules: Front-end Modules =over 4 =item * L compiles Perl 6 Rules to Perl 5. =item * L compiles Perl 6 Tokens to Perl 5. =item * L compiles Perl 6 Regexes to Perl 5. =item * L wraps Perl 5 Regexes to return a B object. =back Runtime Classes =over 4 =item * L provides the runtime engine for Rules. =item * L represents a B object. =item * L represents a B class / object. =back Grammars =over 4 =item * L parses the Rules syntax. =item * L is the base Grammar: , . =back Code Emitters =over 4 =item * L converts parsed Rules to Perl 5 code. =item * L converts parsed :ratchet Rules to Perl 5 code. =back =head1 METHODS =over =item compile (Str $rule_source, \%options) Class method. Returns a compiled rule object, or throws an exception on invalid rule syntax. options: =over =item * grammar => $class Specify which namespace (Grammar) the rule belongs to. =item * ratchet => 1 Disable backtracking. Match faster. Defaults to 1 in Rules and Tokens. =item * pos => $pos Specify a string position to match. Starts in zero. Defaults to C, which matches anywhere in the string. =item * sigspace => 1 Whitespace is significant. Defaults to 1 in Rules. =item * ignorecase => 1 Ignore character case. The default is significant case. =item * Perl5 => 1 Use Perl 5 grammar and semantics for Regex. =back =item match (Str $match_against) Instance method. Returns a L object. =item install (Str $name, Str $rule_source, \%options) Install a rule into the method C<$name>. If C<$name> is fully qualified then it will be installed into that path e.g C, otherwise it will install it into the current package. =item perl Instance method. Returns a string that can be eval'ed into a rule/token/regex object. =back =head1 CAVEATS This is an experimental development version. The API is still in flux. The set of implemented features depends on the C switch. =head1 AUTHORS The Pugs Team C<< >>. Please join us on irc.freenode.net #perl6 if you'd like to participate. =head1 SEE ALSO The Perl 6 Rules Spec: L =head1 COPYRIGHT Copyright 2006 by Flavio Soibelmann Glock and others. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut