# Usage: perl compile_p6grammar.pl GrammarFile.pm > GrammarFile.pmc # The .pm file is in Perl 6 syntax # The .pmc file is in Perl 5 Pugs::Compiler::Rule syntax package Grammar::Compiler; use Pugs::Compiler::Rule; use base 'Pugs::Grammar::Base'; *grammar_name = Pugs::Compiler::Rule->compile(q( \S+ ))->code; *rule_name = Pugs::Compiler::Rule->compile(q( \w+ ))->code; *block = Pugs::Compiler::Rule->compile(q( \{ [|]*? \} ))->code; *regex = Pugs::Compiler::Rule->compile(q( <'regex'> { my $body = substr($(), 1, -1); $body =~ s/\\\\/\\\\\\\\/g; # duplicate every single backslashes return "*" . $() . " = Pugs::Compiler::Rule->compile(q(" . $body . "))->code;" } ))->code; *token = Pugs::Compiler::Rule->compile(q( <'token'> { my $body = substr($(), 1, -1); $body =~ s/\\\\/\\\\\\\\/g; # duplicate every single backslashes return "*" . $() . " = Pugs::Compiler::Rule->compile(q(" . $body . "), { ratchet => 1 } )->code;" } ))->code; # TODO - :sigspace not implemented yet *rule = Pugs::Compiler::Rule->compile(q( <'rule'> { my $body = substr($(), 1, -1); $body =~ s/\\\\/\\\\\\\\/g; # duplicate every single backslashes return "*" . $() . " = Pugs::Compiler::Rule->compile(q(" . $body . "), { ratchet => 1, sigspace => 1 } )->code;" } ))->code; *grammar = Pugs::Compiler::Rule->compile(q( grammar \; [||]* { return "package " . $() . ";\nuse Pugs::Compiler::Rule;\nuse base 'Pugs::Grammar::Base';\n\n" . join("\n", map { $_->() } @{$} ) . "\n" } ))->code; package main; use IO::File; my $source_file = shift(@ARGV); my $source = slurp($source_file); my $match = Grammar::Compiler->grammar($source); print $match->(); sub slurp { my $fh = IO::File->new(shift) || return; return join('', $fh->getlines); } __END__ =head1 NAME compile_p6grammar.pl - Compile Perl6 Grammars to Perl5 Modules =head1 SYNOPSIS # The .pm file is in Perl 6 syntax # The .pmc file is in Perl 5 Pugs::Compiler::Rule syntax perl compile_p6grammar.pl GrammarFile.pm > GrammarFile.pmc =head1 DESCRIPTION Used to convert grammars in Perl 6 syntax into Perl 5 modules. =head1 AUTHORS The Pugs Team Eperl6-compiler@perl.orgE. =head1 SEE ALSO The Perl 6 Rules Spec: L =head1 COPYRIGHT Copyright 2006 by Nathan Gray. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut