| 1 | # Marpa::R3 is Copyright (C) 2018, Jeffrey Kegler. | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 4 | # under the same terms as Perl 5.10.1. For more details, see the full text | ||||||||||||||||||||
| 5 | # of the licenses in the directory LICENSES. | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # This program is distributed in the hope that it will be | ||||||||||||||||||||
| 8 | # useful, but it is provided "as is" and without any express | ||||||||||||||||||||
| 9 | # or implied warranties. For details, see the full text of | ||||||||||||||||||||
| 10 | # of the licenses in the directory LICENSES. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | package Marpa::R3::Common; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # Marpa::R3 "common" methods | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 104 | ▶ | use 5.010001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 104 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 104 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 104 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 104 | ▶ | use vars qw($VERSION $STRING_VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 104 | $VERSION = '4.001_053'; | |||||||||||||||||||
| 23 | 104 | $STRING_VERSION = $VERSION; | |||||||||||||||||||
| 24 | ## no critic(BuiltinFunctions::ProhibitStringyEval) | ||||||||||||||||||||
| 25 | 104 | $VERSION = eval $VERSION; | |||||||||||||||||||
| 26 | ## use critic | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | package Marpa::R3::Internal; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 104 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # Viewing methods, for debugging | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 104 | my @escape_by_ord = (); | |||||||||||||||||||
| 35 | 104 | $escape_by_ord[ ord q{\\} ] = q{\\\\}; | |||||||||||||||||||
| 36 | $escape_by_ord[ ord eval qq{"$_"} ] = $_ | ||||||||||||||||||||
| 37 | 104 | for "\\t", "\\r", "\\f", "\\b", "\\a", "\\e"; | |||||||||||||||||||
| 38 | 104 | $escape_by_ord[0xa] = '\\n'; | |||||||||||||||||||
| 39 | 104 | ▶ | $escape_by_ord[$_] //= chr $_ for 32 .. 126; | ||||||||||||||||||
|
Condition100%3/3
$escape_by_ord[$_] //= chr $_
MC/DC100%2/2
$escape_by_ord[$_] //= chr $_
$escape_by_ord[$_]
chr $_
Truth table100%3/3
A //= B
A= $escape_by_ord[$_]B= chr $_
| |||||||||||||||||||||
| 40 | 104 | ▶ | $escape_by_ord[$_] //= sprintf( "\\x%02x", $_ ) for 0 .. 255; | ||||||||||||||||||
|
Condition66%2/3
$escape_by_ord[$_] //= sprintf("\\x%02x", $_)
MC/DC0%0/2
$escape_by_ord[$_] //= sprintf("\\x%02x", $_)
$escape_by_ord[$_]
sprintf("\\x%02x", $_)
Truth table66%2/3
A //= B
A= $escape_by_ord[$_]B= sprintf("\\x%02x", $_)
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | sub Marpa::R3::escape_string { | ||||||||||||||||||||
| 43 | 0 | ▶ | my ( $string, $length ) = @_; | ||||||||||||||||||
|
Subroutine
escape_string: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 44 | 0 | my $reversed = $length < 0; | |||||||||||||||||||
| 45 | 0 | ▶ | if ($reversed) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | $string = reverse $string; | |||||||||||||||||||
| 47 | 0 | $length = -$length; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | 0 | my @escaped_chars = (); | |||||||||||||||||||
| 50 | 0 | ORD: for my $ord ( map {ord} split //xms, $string ) { | |||||||||||||||||||
| 51 | 0 | ▶ | last ORD if $length <= 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | ▶ | my $escaped_char = $escape_by_ord[$ord] // sprintf( "\\x{%04x}", $ord ); | ||||||||||||||||||
|
Condition0%0/3
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
MC/DC0%0/2
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
$escape_by_ord[$ord]
sprintf("\\x{%04x}", $ord)
Truth table0%0/3
A // B
A= $escape_by_ord[$ord]B= sprintf("\\x{%04x}", $ord)
| |||||||||||||||||||||
| 53 | 0 | $length -= length $escaped_char; | |||||||||||||||||||
| 54 | 0 | push @escaped_chars, $escaped_char; | |||||||||||||||||||
| 55 | } ## end for my $ord ( map {ord} split //xms, $string ) | ||||||||||||||||||||
| 56 | 0 | ▶ | @escaped_chars = reverse @escaped_chars if $reversed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | IX: for my $ix ( reverse 0 .. $#escaped_chars ) { | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # only trailing spaces are escaped | ||||||||||||||||||||
| 60 | 0 | ▶ | last IX if $escaped_chars[$ix] ne q{ }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | $escaped_chars[$ix] = '\\s'; | |||||||||||||||||||
| 62 | } ## end IX: for my $ix ( reverse 0 .. $#escaped_chars ) | ||||||||||||||||||||
| 63 | 0 | return join q{}, @escaped_chars; | |||||||||||||||||||
| 64 | } ## end sub escape_string | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub Marpa::R3::flatten_hash_args { | ||||||||||||||||||||
| 67 | 2710 | ▶ | my ($hash_arg_array) = @_; | ||||||||||||||||||
|
Subroutine
flatten_hash_args: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||
| 68 | 2710 | my %flat_args = (); | |||||||||||||||||||
| 69 | 2710 | for my $hash_ref (@{$hash_arg_array}) { | |||||||||||||||||||
| 70 | 3154 | my $ref_type = ref $hash_ref; | |||||||||||||||||||
| 71 | 3154 | ▶ | if ( not $ref_type ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | return undef, qq{"%s expects args as ref to HASH, got non-reference instead}; | |||||||||||||||||||
| 73 | } ## end if ( not $ref_type ) | ||||||||||||||||||||
| 74 | 3154 | ▶ | if ( $ref_type ne 'HASH' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | return undef, qq{"%s expects args as ref to HASH, got ref to $ref_type instead}; | |||||||||||||||||||
| 76 | } ## end if ( $ref_type ne 'HASH' ) | ||||||||||||||||||||
| 77 | 3154 | ARG: for my $arg_name ( keys %{$hash_ref} ) { | |||||||||||||||||||
| 78 | 3793 | $flat_args{$arg_name} = $hash_ref->{$arg_name}; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | } ## end for my $args (@hash_ref_args) | ||||||||||||||||||||
| 81 | 2710 | return \%flat_args; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub Marpa::R3::exception { | ||||||||||||||||||||
| 85 | 50 | ▶ | my $exception = join q{}, @_; | ||||||||||||||||||
|
Subroutine
exception: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 86 | 50 | $exception =~ s/ \n* \z /\n/xms; | |||||||||||||||||||
| 87 | 50 | ▶ | die($exception) if $Marpa::R3::JUST_DIE; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 48 | CALLER: for ( my $i = 0; 1; $i++) { | |||||||||||||||||||
| 89 | 198 | my ($package ) = caller($i); | |||||||||||||||||||
| 90 | 198 | ▶ | last CALLER if not $package; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 198 | ▶ | last CALLER if not 'Marpa::R3::' eq substr $package, 0, 11; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 150 | $Carp::Internal{ $package } = 1; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | 48 | Carp::croak($exception, q{Marpa::R3 exception}); | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # Could/should this be made more efficient by caching line starts, | ||||||||||||||||||||
| 98 | # then binary searching? | ||||||||||||||||||||
| 99 | sub Marpa::R3::Internal::line_column { | ||||||||||||||||||||
| 100 | 10 | ▶ | my ( $p_string, $pos ) = @_; | ||||||||||||||||||
|
Subroutine
line_column: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 101 | 10 | state $EOL = "\n"; | |||||||||||||||||||
| 102 | 10 | my $line = () = substr( ${$p_string}, 0, $pos ) =~ /$EOL/g; | |||||||||||||||||||
| 103 | 10 | ▶ | my $column = $line ? $pos - $+[0] + 1 : $pos + 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 10 | return [$line+1, $column]; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # Returns a one-line string that is the escaped equivalent | ||||||||||||||||||||
| 108 | # of its arguments, and whose length is at most $max. | ||||||||||||||||||||
| 109 | # Returns a list of two elements: the escaped string and | ||||||||||||||||||||
| 110 | # a boolean indicating if it was truncated | ||||||||||||||||||||
| 111 | sub Marpa::R3::Internal::substr_as_line { | ||||||||||||||||||||
| 112 | 10 | ▶ | my ( $p_string, $pos, $length, $max ) = @_; | ||||||||||||||||||
|
Subroutine
substr_as_line: called CC=8 SCAR=21.5
Pod
undocumented
| |||||||||||||||||||||
| 113 | 10 | my $truncated = 0; | |||||||||||||||||||
| 114 | 10 | my $used = 0; | |||||||||||||||||||
| 115 | 10 | my @escaped_chars = (); | |||||||||||||||||||
| 116 | 10 | my $trailing_ws = 0; | |||||||||||||||||||
| 117 | 10 | ▶ | my $last_ix = $max > $length ? $pos + $length : $pos + $max; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 10 | ▶ | CHAR: for ( my $ix = $pos ; $ix <= $last_ix ; $ix++ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 244 | ▶ | last CHAR if $used >= $max; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 244 | my $char = substr ${$p_string}, $ix, 1; | |||||||||||||||||||
| 121 | 244 | ▶ | $trailing_ws = $char =~ /\s/ ? $trailing_ws + 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 244 | my $ord = ord $char; | |||||||||||||||||||
| 123 | 244 | ▶ | my $escaped_char = $escape_by_ord[$ord] // sprintf( "\\x{%04x}", $ord ); | ||||||||||||||||||
|
Condition33%1/3
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
MC/DC0%0/2
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
$escape_by_ord[$ord]
sprintf("\\x{%04x}", $ord)
Truth table33%1/3
A // B
A= $escape_by_ord[$ord]B= sprintf("\\x{%04x}", $ord)
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # say STDERR "ord=$ord $escaped_char"; | ||||||||||||||||||||
| 126 | 244 | $used += length $escaped_char; | |||||||||||||||||||
| 127 | 244 | push @escaped_chars, $escaped_char; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | 10 | ▶ | while ( $trailing_ws-- ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 10 | my $ws_char = pop @escaped_chars; | |||||||||||||||||||
| 131 | 10 | $used -= length $ws_char; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 10 | ▶ | while ( $used > $max ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | my $excess_char = pop @escaped_chars; | |||||||||||||||||||
| 135 | 0 | $used -= length $excess_char; | |||||||||||||||||||
| 136 | 0 | $truncated = 1; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | 10 | return ( join q{}, @escaped_chars ), $truncated; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Returns a two-line summary of a substring -- | ||||||||||||||||||||
| 142 | # a first line with descriptive information and | ||||||||||||||||||||
| 143 | # a one-line escaped version, indented 2 spaces | ||||||||||||||||||||
| 144 | sub Marpa::R3::Internal::substr_as_2lines { | ||||||||||||||||||||
| 145 | 10 | ▶ | my ( $what, $p_string, $pos, $length, $max ) = @_; | ||||||||||||||||||
|
Subroutine
substr_as_2lines: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 146 | 10 | my ($escaped, $trunc) = substr_as_line( $p_string, $pos, $length, $max ); | |||||||||||||||||||
| 147 | 10 | my ($line_no, $column) = @{line_column( $p_string, $pos)}; | |||||||||||||||||||
| 148 | 10 | my @pieces = ($what); | |||||||||||||||||||
| 149 | 10 | ▶ | push @pieces, $trunc ? 'begins' : 'is'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 10 | push @pieces, qq{at line $line_no, column $column:}; | |||||||||||||||||||
| 151 | 10 | my $line1 = join q{ }, @pieces; | |||||||||||||||||||
| 152 | 10 | return "$line1\n $escaped"; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 104 | 1; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | # vim: set expandtab shiftwidth=4: | ||||||||||||||||||||