parrotcode: compile a Perl 6 grammar | |
Contents | Libraries |
PGE::P6Grammar.pir - compile a Perl 6 grammar
This module defines the "PGE::P6Grammar" compiler, which can be used to compile complete sets of rules (grammars). Input to the grammar compiler is a string containing statements of the form
grammar <name> ;
rule <ident> { <p6rule> } ;
# comment
A grammar
statement indicates where to load any subsequent rules. Unlike Perl 6, there can be multiple grammar
statements in the string.
Here's a code snippet to load a set of rules using the grammar compiler:
load_bytecode "PGE.pbc"
load_bytecode "PGE/P6Grammar.pir"
$S0 = <<'END'
grammar PGE::Rule;
rule ident { [ _ | <?alpha> ] \w* }
rule name { <ident> [ \:\: <ident> ]* }
END
$P0 = compreg "PGE::P6Grammar"
(code, onload) = $P0($S0)
# rules <PGE::Rule::ident> and <PGE::Rule::name> have now been loaded
The return value from the compiler is a string containing the PIR generated from the rules in the grammar, and code for generating any classes defined by the grammar. This code can be used to generate precompiled .pbc files for other packages.
__onload()
compile_rules(STR src)
src
.
|