3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

CGrammar: add support for precompiling grammar

This commit is contained in:
Pragmatic Software 2014-06-15 04:28:54 +00:00
parent 9389edd2df
commit 7f84708a5e
2 changed files with 25 additions and 9 deletions

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 642, BUILD_REVISION => 643,
BUILD_DATE => "2014-06-14", BUILD_DATE => "2014-06-14",
}; };

View File

@ -14,13 +14,8 @@ use Data::Dumper;
# 4. functions to handle the nesting levels (ordinal number generator and CPP stack) # 4. functions to handle the nesting levels (ordinal number generator and CPP stack)
# 6. change returns to prints where appropriate. # 6. change returns to prints where appropriate.
open GRAMMAR, 'CGrammar.pm' or die "Could not open CGrammar.pm: $!"; our ($opt_T, $opt_t, $opt_o, $opt_P);
local $/; getopts('TPto:');
my $grammar = <GRAMMAR>;
close GRAMMAR;
our ($opt_T, $opt_t, $opt_o);
getopts('Tto:');
if ($opt_T ) { if ($opt_T ) {
$::RD_TRACE = 1; $::RD_TRACE = 1;
@ -34,7 +29,18 @@ $Parse::RecDescent::skip = '\s*';
# This may be necessary.. # This may be necessary..
# $::RD_AUTOACTION = q { [@item] }; # $::RD_AUTOACTION = q { [@item] };
my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n"; my $parser;
if($opt_P) {
precompile_grammar();
exit 0;
} else {
if(!eval { require PCGrammar }) {
precompile_grammar();
}
require PCGrammar;
$parser = PCGrammar->new() or die "Bad grammar!\n";
}
if ($opt_o) { if ($opt_o) {
open(OUTFILE, ">>$opt_o"); open(OUTFILE, ">>$opt_o");
@ -64,3 +70,13 @@ foreach my $arg (@ARGV) {
$text =~ s/\s+//g; $text =~ s/\s+//g;
print "\n[$text]\n" if length $text; print "\n[$text]\n" if length $text;
sub precompile_grammar {
print STDERR "Precompiling grammar...\n";
open GRAMMAR, 'CGrammar.pm' or die "Could not open CGrammar.pm: $!";
local $/;
my $grammar = <GRAMMAR>;
close GRAMMAR;
Parse::RecDescent->Precompile($grammar, "PCGrammar") or die "Could not precompile: $!";
}