From d97f8b2fd62d71fd429fbfd0c897824728c1b8ad Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 22 Jun 2014 06:08:01 +0000 Subject: [PATCH] CGrammar: Return output instead of printing; improve error-handling of misparsed code --- PBot/VERSION.pm | 2 +- modules/c2english/CGrammar.pm | 3 ++- modules/c2english/c2eng.pl | 13 ++++++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index e6941359..056978c4 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 667, + BUILD_REVISION => 668, BUILD_DATE => "2014-06-21", }; diff --git a/modules/c2english/CGrammar.pm b/modules/c2english/CGrammar.pm index 8b918ea6..bf16024d 100644 --- a/modules/c2english/CGrammar.pm +++ b/modules/c2english/CGrammar.pm @@ -17,9 +17,10 @@ startrule: my $output = $item[-1]; $output =~ s/\^L(\s*.?)/\L$1/g; # lowercase specified characters $output =~ s/\^U(\s*.?)/\U$1/g; # uppercase specified characters - print $output; + push @$return, $output; } startrule(?) + { push @$return, $item[-1]; } translation_unit: comment diff --git a/modules/c2english/c2eng.pl b/modules/c2english/c2eng.pl index e6ffd332..059ca2d0 100755 --- a/modules/c2english/c2eng.pl +++ b/modules/c2english/c2eng.pl @@ -61,11 +61,18 @@ foreach my $arg (@ARGV) { undef $::RD_TRACE; } - defined $parser->startrule(\$text) or die "Bad text!\n$text\n"; + my $result = $parser->startrule(\$text) or die "Bad text!\n$text\n"; + + $text =~ s/^\s+//g; + $text =~ s/\s+$//g; + + if(length $text) { + die "Bad parse: $text"; + } else { + print join('', flatten($result)); + } } -$text =~ s/\s+//g; -print "\n[$text]\n" if length $text; sub precompile_grammar { print STDERR "Precompiling grammar...\n";