| 1 |
#!/usr/bin/perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use English qw( -no_match_vars ); |
| 21 |
use Data::Dumper; |
| 22 |
|
| 23 |
# This is a 'meta' tool, so I relax some of the |
| 24 |
# restrictions I use to guarantee portability. |
| 25 |
use autodie; |
| 26 |
|
| 27 |
# I expect to be run from a subdirectory in the |
| 28 |
# development heirarchy |
| 29 |
use lib '../../../'; |
| 30 |
use lib '../../../../blib/arch'; |
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
use Getopt::Long; |
| 34 |
my $verbose = 1; |
| 35 |
my $help_flag = 0; |
| 36 |
my $result = Getopt::Long::GetOptions( |
| 37 |
'help' => \$help_flag, |
| 38 |
); |
| 39 |
die "usage $PROGRAM_NAME [--help] file ...\n" if $help_flag; |
| 40 |
|
| 41 |
my $bnf = do { local $RS = undef; \(<>) }; |
| 42 |
my $ast = Marpa::R2::Internal::MetaAST->new($bnf); |
| 43 |
my $parse_result = $ast->ast_to_hash(); |
| 44 |
|
| 45 |
sub sort_bnf { |
| 46 |
my $cmp = $a->{lhs} cmp $b->{lhs}; |
| 47 |
return $cmp if $cmp; |
| 48 |
my $a_rhs_length = scalar @{ $a->{rhs} }; |
| 49 |
my $b_rhs_length = scalar @{ $b->{rhs} }; |
| 50 |
$cmp = $a_rhs_length <=> $b_rhs_length; |
| 51 |
return $cmp if $cmp; |
| 52 |
for my $ix ( 0 .. ( $a_rhs_length - 1 ) ) { |
| 53 |
$cmp = $a->{rhs}->[$ix] cmp $b->{rhs}->[$ix]; |
| 54 |
return $cmp if $cmp; |
| 55 |
} |
| 56 |
return 0; |
| 57 |
} ## end sub sort_bnf |
| 58 |
|
| 59 |
my %cooked_parse_result = ( |
| 60 |
character_classes => $parse_result->{character_classes}, |
| 61 |
symbols => $parse_result->{symbols}, |
| 62 |
discard_default_adverbs => $parse_result->{discard_default_adverbs}, |
| 63 |
lexeme_default_adverbs => $parse_result->{lexeme_default_adverbs}, |
| 64 |
first_lhs => $parse_result->{first_lhs}, |
| 65 |
start_lhs => $parse_result->{start_lhs}, |
| 66 |
); |
| 67 |
|
| 68 |
my @rule_sets = keys %{ $parse_result->{rules} }; |
| 69 |
for my $rule_set (@rule_sets) { |
| 70 |
my $aoh = $parse_result->{rules}->{$rule_set}; |
| 71 |
my $sorted_aoh = [ sort sort_bnf @{$aoh} ]; |
| 72 |
$cooked_parse_result{rules}->{$rule_set} = $sorted_aoh; |
| 73 |
} |
| 74 |
|
| 75 |
say "## The code after this line was automatically generated by ", |
| 76 |
$PROGRAM_NAME; |
| 77 |
say "## Date: ", scalar localtime(); |
| 78 |
$Data::Dumper::Sortkeys = 1; |
| 79 |
print Data::Dumper->Dump( [ \%cooked_parse_result ], [qw(hashed_metag)] ); |
| 80 |
say "## The code before this line was automatically generated by ", |
| 81 |
$PROGRAM_NAME; |