| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # #################################################################################### # | ||
| 3 | # Program testmarpagrammarfromfile # | ||
| 4 | # # | ||
| 5 | # Author Axel Zuber # | ||
| 6 | # Created 29.05.2022 # | ||
| 7 | # # | ||
| 8 | # Description read a Marpa grammar and an input file # | ||
| 9 | # apply the grammar to the input and dump the parse tree # | ||
| 10 | # # | ||
| 11 | # ------------------------------------------------------------------------------------ # | ||
| 12 | # # | ||
| 13 | # Input Marpa::R2 grammar # | ||
| 14 | # input file # | ||
| 15 | # # | ||
| 16 | # ------------------------------------------------------------------------------------ # | ||
| 17 | # # | ||
| 18 | # Output Parse tree # | ||
| 19 | # # | ||
| 20 | # ------------------------------------------------------------------------------------ # | ||
| 21 | # # | ||
| 22 | # Exit code # | ||
| 23 | # 0 : Successful # | ||
| 24 | # 4 : Warnings # | ||
| 25 | # 8 : Errors # | ||
| 26 | # # | ||
| 27 | # ------------------------------------------------------------------------------------ # | ||
| 28 | # # | ||
| 29 | # History # | ||
| 30 | # Ver Date Name Description # | ||
| 31 | # 0.9 29.05.2022 A. Zuber Initial version # | ||
| 32 | # # | ||
| 33 | # #################################################################################### # | ||
| 34 | |||
| 35 | |||
| 36 | # ------------------------------------------------------------------------------------ # | ||
| 37 | # Subroutines # | ||
| 38 | # ------------------------------------------------------------------------------------ # | ||
| 39 | |||
| 40 | package MarpaX::Test; | ||
| 41 | use strict; | ||
| 42 | use warnings FATAL => 'all'; | ||
| 43 | |||
| 44 | use strict; | ||
| 45 | |||
| 46 | use lib 'lib'; | ||
| 47 | use Data::Dumper; | ||
| 48 | use Getopt::Std; | ||
| 49 | use Marpa::R2; | ||
| 50 | |||
| 51 | # ------------------------------------------------------------------------------------ # | ||
| 52 | # Subroutines # | ||
| 53 | # ------------------------------------------------------------------------------------ # | ||
| 54 | |||
| 55 | sub readFile | ||
| 56 | { | ||
| 57 | my ($infile) = @_; | ||
| 58 | |||
| 59 | my $inph = *STDIN if $infile eq '-'; | ||
| 60 | open($inph, "< $infile") || die "can't open input file $infile : $!" if $infile ne '-'; | ||
| 61 | |||
| 62 | my $file_text = do | ||
| 63 | { | ||
| 64 | local $/; | ||
| 65 | <$inph>; | ||
| 66 | }; | ||
| 67 | |||
| 68 | close($inph) if $infile ne '-'; | ||
| 69 | |||
| 70 | return $file_text; | ||
| 71 | } | ||
| 72 | |||
| 73 | # ------------------------------------------------------------------------------------ # | ||
| 74 | # MAIN # | ||
| 75 | # ------------------------------------------------------------------------------------ # | ||
| 76 | |||
| 77 | my $usage = "testmarpagrammar_from_file -g grammarfile <inputfile>"; | ||
| 78 | my $optStr = 'g:v'; | ||
| 79 | my $options = {}; | ||
| 80 | |||
| 81 | die 'Invalid option(s) given' if !getopts( "$optStr", $options ); | ||
| 82 | |||
| 83 | die "$usage" if !exists $options->{g} || scalar @ARGV < 1; | ||
| 84 | |||
| 85 | my $grammartext = readFile($options->{g}); | ||
| 86 | |||
| 87 | my $dsl = <<"END_OF_DSL"; | ||
| 88 | ${grammartext} | ||
| 89 | END_OF_DSL | ||
| 90 | |||
| 91 | my $grammar = Marpa::R2::Scanless::G->new({ | ||
| 92 | action_object => 'MarpaX::Test::Actions', | ||
| 93 | # default_action => '[name, values]', | ||
| 94 | default_action => 'default_action', | ||
| 95 | # default_action => 'flattenArray', | ||
| 96 | source => \$dsl | ||
| 97 | }); | ||
| 98 | |||
| 99 | my $parser = Marpa::R2::Scanless::R->new({ grammar => $grammar }); | ||
| 100 | my $input = readFile($ARGV[0]); | ||
| 101 | |||
| 102 | $parser->read(\$input); | ||
| 103 | $input = undef; | ||
| 104 | |||
| 105 | my $value_ref = $parser->value(); | ||
| 106 | |||
| 107 | print Dumper($value_ref); | ||
| 108 | |||
| 109 | exit 0; | ||
| 110 | |||
| 111 | # ------------------------------------------------------------------------------------ # | ||
| 112 | # Example grammar actions # | ||
| 113 | # ------------------------------------------------------------------------------------ # | ||
| 114 | |||
| 115 | package MarpaX::Test::Actions; | ||
| 116 | use strict; | ||
| 117 | |||
| 118 | sub new | ||
| 119 | { | ||
| 120 | my ($class) = @_; | ||
| 121 | return bless {}, $class; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub flattenArray | ||
| 125 | { | ||
| 126 | my ($self, $items) = @_; | ||
| 127 | my $result = $items; | ||
| 128 | if (ref $items eq "ARRAY") | ||
| 129 | { | ||
| 130 | if (scalar @$items != 1) | ||
| 131 | { | ||
| 132 | die "\$items must hold exactly 1 items"; | ||
| 133 | } | ||
| 134 | $result = @{$items}[0]; | ||
| 135 | } | ||
| 136 | return $result; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub default_action | ||
| 140 | { | ||
| 141 | my ($self, @items ) = @_; | ||
| 142 | |||
| 143 | my $result = \@items; | ||
| 144 | if (scalar @items == 1) | ||
| 145 | { | ||
| 146 | $result = $items[0]; | ||
| 147 | } | ||
| 148 | |||
| 149 | return $result; | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | # ABSTRACT: create a parse tree from an input file by applying a Marpa grammar | ||
| 155 | |||
| 156 | =head1 SYNOPSIS | ||
| 157 | |||
| 158 | testmarpagrammar_from_file.pl -g ./plsql.mx ./ddl/create_package.sql | ||
| 159 | |||
| 160 | =head1 DESCRIPTION | ||
| 161 | Create the Marpa grammar from option '-g' and apply it to the text from the input file, | ||
| 162 | then dump the parse tree. | ||
| 163 | =cut |