| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | # A Marpa2 parser for gdb list range hs | ||||||||||||||||||||||||||||
| 3 | # Many thanks to Jeffrey Kegler | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Marpa::R2 4.000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | 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 | location_if ::= location 'if' tokens | ||||||||||||||||||||||||||||
| 21 | | location | ||||||||||||||||||||||||||||
| 22 | location ::= FILE_LINE | FUNCNAME | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # If location is just a number is given, the filename is implied | ||||||||||||||||||||||||||||
| 25 | location ::= NUMBER | ||||||||||||||||||||||||||||
| 26 | FILE_LINE ::= FILENAME ':' NUMBER | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # "tokens" is used to gobble up stuff after the "if" | ||||||||||||||||||||||||||||
| 29 | tokens ::= token+ | ||||||||||||||||||||||||||||
| 30 | token ::= ':' | FILENAME | FUNCNAME | NUMBER | SYMBOL | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # ======== tokens =========== | ||||||||||||||||||||||||||||
| 33 | :discard ~ whitespace | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Note no space is allowed between FILENAME, COLON, and number | ||||||||||||||||||||||||||||
| 36 | NUMBER ~ number | ||||||||||||||||||||||||||||
| 37 | number ~ digits | ||||||||||||||||||||||||||||
| 38 | digits ~ [\d]+ | ||||||||||||||||||||||||||||
| 39 | whitespace ~ [\s]+ | ||||||||||||||||||||||||||||
| 40 | FILENAME ~ [^:\s]+ | ||||||||||||||||||||||||||||
| 41 | FUNCNAME ~ name '()' | ||||||||||||||||||||||||||||
| 42 | name ~ name_first_char name_later_chars | ||||||||||||||||||||||||||||
| 43 | name_first_char ~ [A-Za-z_] | ||||||||||||||||||||||||||||
| 44 | name_later_chars ~ name_later_char* | ||||||||||||||||||||||||||||
| 45 | name_later_char ~ [\w] | ||||||||||||||||||||||||||||
| 46 | SYMBOL ~ [^:\d] | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | END_OF_GRAMMAR | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | my $range_grammar = Marpa::R2::Scanless::G->new( | |||||||||||||||||||||||||||
| 51 | { source => \$grammar_rules } ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | package Devel::Trepan::CmdProcessor::Parse::BPLocation; | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | use vars qw(@EXPORT @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 58 | 1 | @EXPORT = qw(bp_location_build parse_bp_location); | |||||||||||||||||||||||||||
| 59 | sub parse_bp_location | ||||||||||||||||||||||||||||
| 60 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_bp_location: called CC=3 SCAR=11.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 4 | my ( $input ) = @_; | |||||||||||||||||||||||||||
| 62 | 4 | my $recce = Marpa::R2::Scanless::R->new( { grammar => $range_grammar } ); | |||||||||||||||||||||||||||
| 63 | 4 | my $input_length = length ${$input}; | |||||||||||||||||||||||||||
| 64 | 4 | my $pos = $recce->read($input); | |||||||||||||||||||||||||||
| 65 | 4 | ▶ | if ( $pos < $input_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | die sprintf qq{Unfinished parse: remainder="%" }, substr(${$input}, $pos); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 4 | my $value_ref = $recce->value(); | |||||||||||||||||||||||||||
| 69 | 4 | ▶ | if ( !$value_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | die "input read, but there was no parse"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 4 | return $value_ref; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | #===== info-building routines ================ | ||||||||||||||||||||||||||||
| 76 | sub ref2hash | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ref2hash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | my %h = @_; | |||||||||||||||||||||||||||
| 79 | 0 | return \%h; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub bp_location_build | ||||||||||||||||||||||||||||
| 83 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
bp_location_build: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 4 | my $loc = shift; | |||||||||||||||||||||||||||
| 85 | 4 | my $result = {}; | |||||||||||||||||||||||||||
| 86 | 4 | ▶ | if (ref $$loc eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 4 | my @ary = @$$loc; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | if ($ary[0] eq 'location_if') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 4 | $result = location_build($ary[1]); | |||||||||||||||||||||||||||
| 91 | 4 | ▶ | if (@ary > 2 && $ary[2] eq 'if') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@ary > 2 and $ary[2] eq "if"
MC/DC50%1/2
@ary > 2 and $ary[2] eq "if"
@ary > 2
$ary[2] eq "if"
Truth table66%2/3
A and B
A= @ary > 2B= $ary[2] eq "if"
| |||||||||||||||||||||||||||||
| 92 | 2 | $result->{is_conditional} = 1; | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 2 | $result->{is_conditional} = 0; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 4 | return $result; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub location_build | ||||||||||||||||||||||||||||
| 102 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
location_build: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 4 | my $loc = shift; | |||||||||||||||||||||||||||
| 104 | 4 | ▶ | if (ref $loc eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | # FIXME: handle offset or number $start | ||||||||||||||||||||||||||||
| 106 | 4 | my @loc_ary = @$loc; | |||||||||||||||||||||||||||
| 107 | 4 | my $kind = $loc_ary[0]; | |||||||||||||||||||||||||||
| 108 | 4 | ▶ | if ($kind eq 'location') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 4 | my $func_or_ary = $loc_ary[1]; | |||||||||||||||||||||||||||
| 110 | 4 | ▶ | if (ref $func_or_ary) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 2 | $kind = $func_or_ary->[0]; | |||||||||||||||||||||||||||
| 112 | 2 | ▶ | if ($kind eq 'FILE_LINE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | return { | ||||||||||||||||||||||||||||
| 114 | 2 | filename => $func_or_ary->[1], | |||||||||||||||||||||||||||
| 115 | line_num => $func_or_ary->[3] | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | return { | ||||||||||||||||||||||||||||
| 120 | 2 | funcname => $func_or_ary | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # # Demo/test | ||||||||||||||||||||||||||||
| 128 | # unless (caller()) { | ||||||||||||||||||||||||||||
| 129 | # eval {use Test::More}; | ||||||||||||||||||||||||||||
| 130 | # eval {use Data::Dumper}; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # my @test = ( | ||||||||||||||||||||||||||||
| 133 | # [ 'List.pm:1', 'OK', | ||||||||||||||||||||||||||||
| 134 | # { | ||||||||||||||||||||||||||||
| 135 | # filename => "List.pm", | ||||||||||||||||||||||||||||
| 136 | # is_conditional => 0, | ||||||||||||||||||||||||||||
| 137 | # line_num => 1 | ||||||||||||||||||||||||||||
| 138 | # }], | ||||||||||||||||||||||||||||
| 139 | # [ 'abc()', 'OK', | ||||||||||||||||||||||||||||
| 140 | # { | ||||||||||||||||||||||||||||
| 141 | # funcname => "abc()", | ||||||||||||||||||||||||||||
| 142 | # is_conditional => 0, | ||||||||||||||||||||||||||||
| 143 | # }, | ||||||||||||||||||||||||||||
| 144 | # ], | ||||||||||||||||||||||||||||
| 145 | # [ 'abs() if 1', 'OK', | ||||||||||||||||||||||||||||
| 146 | # { | ||||||||||||||||||||||||||||
| 147 | # funcname => "abs()", | ||||||||||||||||||||||||||||
| 148 | # is_conditional => 1, | ||||||||||||||||||||||||||||
| 149 | # } | ||||||||||||||||||||||||||||
| 150 | # ], | ||||||||||||||||||||||||||||
| 151 | # [ 'List.pm:10 if y > 3', 'OK', | ||||||||||||||||||||||||||||
| 152 | # { | ||||||||||||||||||||||||||||
| 153 | # filename => "List.pm", | ||||||||||||||||||||||||||||
| 154 | # is_conditional => 1, | ||||||||||||||||||||||||||||
| 155 | # line_num => 10 | ||||||||||||||||||||||||||||
| 156 | # } | ||||||||||||||||||||||||||||
| 157 | # ] | ||||||||||||||||||||||||||||
| 158 | # ); | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # for my $ix (0 .. $#test) { | ||||||||||||||||||||||||||||
| 161 | # my ($input, $expected_result, $expected_value) = @{$test[$ix]}; | ||||||||||||||||||||||||||||
| 162 | # my $i = $ix + 1; | ||||||||||||||||||||||||||||
| 163 | # say "\n** Test #$i: ", $input; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # my $value_ref; | ||||||||||||||||||||||||||||
| 166 | # my $result = 'OK'; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # # Parse input and build tree | ||||||||||||||||||||||||||||
| 169 | # my $eval_ok = eval { $value_ref = parse_bp_location( \$input ); 1; }; | ||||||||||||||||||||||||||||
| 170 | # if ( !$eval_ok ) { | ||||||||||||||||||||||||||||
| 171 | # my $eval_error = $EVAL_ERROR; | ||||||||||||||||||||||||||||
| 172 | # PARSE_EVAL_ERROR: { | ||||||||||||||||||||||||||||
| 173 | # $result = "Error: $EVAL_ERROR"; | ||||||||||||||||||||||||||||
| 174 | # Test::More::diag($result); | ||||||||||||||||||||||||||||
| 175 | # } | ||||||||||||||||||||||||||||
| 176 | # $result = "no parse"; | ||||||||||||||||||||||||||||
| 177 | # } | ||||||||||||||||||||||||||||
| 178 | # if ($result ne $expected_result) { | ||||||||||||||||||||||||||||
| 179 | # Test::More::fail(qq{Parse of "$input" "$result"; expected "$expected_result"}); | ||||||||||||||||||||||||||||
| 180 | # } else { | ||||||||||||||||||||||||||||
| 181 | # Test::More::pass(qq{Parse of "$input" okay}); | ||||||||||||||||||||||||||||
| 182 | # } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # # say Data::Dumper::Dumper($value_ref); | ||||||||||||||||||||||||||||
| 185 | # my %bp_location = %{bp_location_build($value_ref)}; | ||||||||||||||||||||||||||||
| 186 | # # use Data::Printer; | ||||||||||||||||||||||||||||
| 187 | # # p $bp_location; | ||||||||||||||||||||||||||||
| 188 | # my %expected_value = %$expected_value; | ||||||||||||||||||||||||||||
| 189 | # if (%bp_location ne %expected_value) { | ||||||||||||||||||||||||||||
| 190 | # Test::More::fail(qq{Test of "$input" value was "%bp_location"; expected "%expected_value"}); | ||||||||||||||||||||||||||||
| 191 | # } else { | ||||||||||||||||||||||||||||
| 192 | # Test::More::pass(qq{Parsed Value of "$input" matches}); | ||||||||||||||||||||||||||||
| 193 | # } | ||||||||||||||||||||||||||||
| 194 | # } | ||||||||||||||||||||||||||||
| 195 | # done_testing(); | ||||||||||||||||||||||||||||
| 196 | # } | ||||||||||||||||||||||||||||
| 197 | 1 | 1; | |||||||||||||||||||||||||||