| 1 | #!/usr/bin/perl | ||
| 2 | # A Marpa2 parser for gdb list range hs | ||
| 3 | # Many thanks to Jeffrey Kegler | ||
| 4 | |||
| 5 | use 5.010; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Marpa::R2 4.000; | ||
| 9 | |||
| 10 | use Exporter; | ||
| 11 | |||
| 12 | my $grammar_rules = <<'END_OF_GRAMMAR'; | ||
| 13 | |||
| 14 | # Use longest acceptable token match | ||
| 15 | lexeme default = latm => 1 | ||
| 16 | :default ::= action => [name,values] | ||
| 17 | |||
| 18 | # ======== productions =========== | ||
| 19 | |||
| 20 | range ::= location | ||
| 21 | | location COMMA NUMBER | ||
| 22 | | location COMMA OFFSET | ||
| 23 | | location COMMA | ||
| 24 | | COMMA location | ||
| 25 | | direction | ||
| 26 | |||
| 27 | direction ::= DIRECTION | ||
| 28 | |||
| 29 | location ::= FILE_LINE | FUNCNAME | ||
| 30 | |||
| 31 | # If just a number is given, the filename is implied | ||
| 32 | location ::= NUMBER | offset | ||
| 33 | offset ::= OFFSET | ||
| 34 | |||
| 35 | #======== tokens =============== | ||
| 36 | |||
| 37 | :discard ~ whitespace | ||
| 38 | |||
| 39 | # Note no space is allowed between FILENAME, COLON, and number | ||
| 40 | FILE_LINE ~ FILENAME COLON number | ||
| 41 | COLON ~ ':' | ||
| 42 | COMMA ~ ',' | ||
| 43 | NUMBER ~ number | ||
| 44 | OFFSET ~ [+-] digits | ||
| 45 | number ~ digits | ||
| 46 | digits ~ [\d]+ | ||
| 47 | DIRECTION ~ '+' | '-' | '.' | ||
| 48 | whitespace ~ [\s]+ | ||
| 49 | FILENAME ~ [^:\s]+ | ||
| 50 | FUNCNAME ~ name '()' | ||
| 51 | name ~ name_first_char name_later_chars | ||
| 52 | name_first_char ~ [A-Za-z_] | ||
| 53 | name_later_chars ~ name_later_char* | ||
| 54 | name_later_char ~ [\w] | ||
| 55 | |||
| 56 | END_OF_GRAMMAR | ||
| 57 | |||
| 58 | my $range_grammar = Marpa::R2::Scanless::G->new( { source => \$grammar_rules } ); | ||
| 59 | |||
| 60 | package Devel::Trepan::CmdProcessor::Parse::Range; | ||
| 61 | use English qw( -no_match_vars ); | ||
| 62 | |||
| 63 | use vars qw(@EXPORT @ISA); | ||
| 64 | @ISA = qw(Exporter); | ||
| 65 | @EXPORT = qw(range_build parse_range); | ||
| 66 | |||
| 67 | |||
| 68 | sub parse_range | ||
| 69 | { | ||
| 70 | my ( $input ) = @_; | ||
| 71 | my $recce = Marpa::R2::Scanless::R->new( { grammar => $range_grammar } ); | ||
| 72 | my $input_length = length ${$input}; | ||
| 73 | my $pos = $recce->read($input); | ||
| 74 | if ( $pos < $input_length ) { | ||
| 75 | die sprintf qq{Unfinished parse: remainder="%" }, substr(${$input}, $pos); | ||
| 76 | } | ||
| 77 | my $value_ref = $recce->value(); | ||
| 78 | if ( !$value_ref ) { | ||
| 79 | die "input read, but there was no parse"; | ||
| 80 | } | ||
| 81 | return $value_ref; | ||
| 82 | } | ||
| 83 | |||
| 84 | #===== info-building routines ================ | ||
| 85 | sub ref2hash | ||
| 86 | { | ||
| 87 | my %h = @_; | ||
| 88 | return \%h; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub location_build | ||
| 92 | { | ||
| 93 | my $loc = shift; | ||
| 94 | if (ref $loc eq 'ARRAY') { | ||
| 95 | # FIXME: handle offset or number $start | ||
| 96 | my @ary = @$loc; | ||
| 97 | my $ary = ref2hash(@ary); | ||
| 98 | return {$ary[0] => $ary[1]} | ||
| 99 | } else { | ||
| 100 | if (substr($loc, -2, 2) eq '()') { | ||
| 101 | return {'funcname' => $loc } | ||
| 102 | } elsif ($loc =~ /(\S+):(\d+)$/) { | ||
| 103 | return {location => { | ||
| 104 | filename => $1, | ||
| 105 | line_num => $2, | ||
| 106 | }}; | ||
| 107 | } else { | ||
| 108 | return $loc; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub position_build | ||
| 114 | { | ||
| 115 | my $pos = shift; | ||
| 116 | if (ref $pos eq 'ARRAY') { | ||
| 117 | die unless $pos->[0] eq 'location'; | ||
| 118 | $pos = $pos->[1]; | ||
| 119 | } | ||
| 120 | if (substr($pos, 0, 1) =~ /^[+-]$/) { | ||
| 121 | return {offset => $pos} | ||
| 122 | } else { | ||
| 123 | return {line_num => $pos}; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub range_build | ||
| 128 | { | ||
| 129 | my @rhs = @_; | ||
| 130 | my %result; | ||
| 131 | my @first = shift @rhs; | ||
| 132 | my $first_name; | ||
| 133 | my $first_val; | ||
| 134 | if (ref $first[0]) { | ||
| 135 | $first_name = $first[0]->[0]; | ||
| 136 | $first_val = $first[0]->[1]; | ||
| 137 | } else { | ||
| 138 | $first_name = $first[0]; | ||
| 139 | } | ||
| 140 | |||
| 141 | if ($first_name eq 'location') { | ||
| 142 | $result{'start'} = location_build $first_val; | ||
| 143 | return %result unless @rhs; | ||
| 144 | my $second = shift @rhs; | ||
| 145 | if ($second eq ',') { | ||
| 146 | if (@rhs) { | ||
| 147 | $second = shift @rhs; | ||
| 148 | $result{end} = position_build $second; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | return %result unless @rhs; | ||
| 152 | # FIXME: Handle end value number or offset | ||
| 153 | return %result; | ||
| 154 | } elsif ($first_name eq ',') { | ||
| 155 | my $end_val = shift @rhs; | ||
| 156 | $result{end} = position_build $end_val; | ||
| 157 | return %result; | ||
| 158 | |||
| 159 | } elsif ($first_name eq 'direction') { | ||
| 160 | $result{direction} = $first_val; | ||
| 161 | return %result; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | ## Demo/test ### | ||
| 166 | # unless (caller()) { | ||
| 167 | # eval {use Test::More}; | ||
| 168 | # eval {use Data::Dumper}; | ||
| 169 | |||
| 170 | # my @test = ( | ||
| 171 | # [ 'List.pm:1', 'OK', [ 'range', [ 'location', 'List.pm:1' ] ] ], | ||
| 172 | # [ 'abc()', 'OK', [ 'range', [ 'location', 'abc()' ] ] ], | ||
| 173 | # [ '+', 'OK', [ 'range', [ 'direction', '+' ] ] ], | ||
| 174 | # [ '-', 'OK', [ 'range', [ 'direction', '-' ] ] ], | ||
| 175 | # [ '+9', 'OK', [ 'range', [ 'location', [ 'offset', '+9' ] ] ] ], | ||
| 176 | # [ '-2', 'OK', [ 'range', [ 'location', [ 'offset', '-2' ] ] ] ], | ||
| 177 | # [ 'xyz:3,9', 'OK', [ 'range', [ 'location', 'xyz:3' ], ',', '9' ] ], | ||
| 178 | # [ ',42', 'OK', [ 'range', ',', [ 'location', '42' ] ] ], | ||
| 179 | # [ ', 42', 'OK', [ 'range', ',', [ 'location', '42' ] ] ], | ||
| 180 | # [ '42,', 'OK', [ 'range', [ 'location', '42' ], ',' ] ], | ||
| 181 | # ); | ||
| 182 | |||
| 183 | # for my $ix (0 .. $#test) { | ||
| 184 | # my ($input, $expected_result, $expected_value) = @{$test[$ix]}; | ||
| 185 | # my $i = $ix + 1; | ||
| 186 | # say "\n** Test #$i: ", $input; | ||
| 187 | |||
| 188 | # my $value_ref; | ||
| 189 | # my $result = 'OK'; | ||
| 190 | |||
| 191 | # # Parse input and build tree | ||
| 192 | # my $eval_ok = eval { $value_ref = parse_range( \$input ); 1; }; | ||
| 193 | # if ( !$eval_ok ) { | ||
| 194 | # my $eval_error = $EVAL_ERROR; | ||
| 195 | # PARSE_EVAL_ERROR: { | ||
| 196 | # $result = "Error: $EVAL_ERROR"; | ||
| 197 | # Test::More::diag($result); | ||
| 198 | # } | ||
| 199 | # $result = "no parse"; | ||
| 200 | # } | ||
| 201 | # if ($result ne $expected_result) { | ||
| 202 | # Test::More::fail(qq{Parse of "$input" "$result"; expected "$expected_result"}); | ||
| 203 | # } else { | ||
| 204 | # Test::More::pass(qq{Parse of "$input" okay}); | ||
| 205 | # } | ||
| 206 | |||
| 207 | # my $value = '[fail]'; | ||
| 208 | # my $dump_expected = '[fail]'; | ||
| 209 | # if ($value_ref) { | ||
| 210 | # $value = Data::Dumper::Dumper($value_ref); | ||
| 211 | # $dump_expected = Data::Dumper::Dumper(\$expected_value); | ||
| 212 | # } | ||
| 213 | # if ($value ne $dump_expected) { | ||
| 214 | # Test::More::fail(qq{Test of "$input" value was "$value"; expected "$dump_expected"}); | ||
| 215 | # } else { | ||
| 216 | # Test::More::pass(qq{Parsed Value of "$input" matches}); | ||
| 217 | # } | ||
| 218 | # } | ||
| 219 | # done_testing(); | ||
| 220 | # } | ||
| 221 | 1; |