| 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_054'; | |||||||||||
| 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%2/2
$escape_by_ord[$_] //= chr $_
MC/DC100%1/1
$escape_by_ord[$_] //= chr $_
$escape_by_ord[$_]
Truth table100%2/2
A
A= $escape_by_ord[$_]
| |||||||||||||
| 40 | 104 | ▶ | $escape_by_ord[$_] //= sprintf( "\\x%02x", $_ ) for 0 .. 255; | ||||||||||
|
Condition100%2/2
$escape_by_ord[$_] //= sprintf("\\x%02x", $_)
MC/DC100%1/1
$escape_by_ord[$_] //= sprintf("\\x%02x", $_)
$escape_by_ord[$_]
Truth table100%2/2
A
A= $escape_by_ord[$_]
| |||||||||||||
| 41 | |||||||||||||
| 42 | 0 | ▶ | sub Marpa::R3::escape_string { | ||||||||||
|
Subroutine
escape_string: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||
| 43 | 0 | my ( $string, $length ) = @_; | |||||||||||
| 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/2
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
MC/DC0%0/1
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
$escape_by_ord[$ord]
Truth table0%0/2
A
A= $escape_by_ord[$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 | 2716 | ▶ | sub Marpa::R3::flatten_hash_args { | ||||||||||
|
Subroutine
flatten_hash_args: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||
| 67 | 2716 | my ($hash_arg_array) = @_; | |||||||||||
| 68 | 2716 | my %flat_args = (); | |||||||||||
| 69 | 2716 | for my $hash_ref (@{$hash_arg_array}) { | |||||||||||
| 70 | 3160 | my $ref_type = ref $hash_ref; | |||||||||||
| 71 | 3160 | ▶ | 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 | 3160 | ▶ | 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 | 3160 | ARG: for my $arg_name ( keys %{$hash_ref} ) { | |||||||||||
| 78 | 3801 | $flat_args{$arg_name} = $hash_ref->{$arg_name}; | |||||||||||
| 79 | } | ||||||||||||
| 80 | } ## end for my $args (@hash_ref_args) | ||||||||||||
| 81 | 2716 | return \%flat_args; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 51 | ▶ | sub Marpa::R3::exception { | ||||||||||
|
Subroutine
exception: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 85 | 51 | my $exception = join q{}, @_; | |||||||||||
| 86 | 51 | $exception =~ s/ \n* \z /\n/xms; | |||||||||||
| 87 | 51 | ▶ | die($exception) if $Marpa::R3::JUST_DIE; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 49 | CALLER: for ( my $i = 0; 1; $i++) { | |||||||||||
| 89 | 200 | my ($package ) = caller($i); | |||||||||||
| 90 | 200 | ▶ | last CALLER if not $package; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 200 | ▶ | last CALLER if not 'Marpa::R3::' eq substr $package, 0, 11; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 151 | $Carp::Internal{ $package } = 1; | |||||||||||
| 93 | } | ||||||||||||
| 94 | 49 | 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 | 10 | ▶ | sub Marpa::R3::Internal::line_column { | ||||||||||
|
Subroutine
line_column: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 100 | 10 | my ( $p_string, $pos ) = @_; | |||||||||||
| 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 | 10 | ▶ | sub Marpa::R3::Internal::substr_as_line { | ||||||||||
|
Subroutine
substr_as_line: called CC=8 SCAR=21.3
Pod
undocumented
| |||||||||||||
| 112 | 10 | my ( $p_string, $pos, $length, $max ) = @_; | |||||||||||
| 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 ); | ||||||||||
|
Condition50%1/2
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
MC/DC0%0/1
$escape_by_ord[$ord] // sprintf("\\x{%04x}", $ord)
$escape_by_ord[$ord]
Truth table50%1/2
A
A= $escape_by_ord[$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 | 10 | ▶ | sub Marpa::R3::Internal::substr_as_2lines { | ||||||||||
|
Subroutine
substr_as_2lines: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 145 | 10 | my ( $what, $p_string, $pos, $length, $max ) = @_; | |||||||||||
| 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: | ||||||||||||