| 1 | package Math::SymbolicX::ParserExtensionFactory; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Math::Symbolic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Text::Balanced; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | our $BeenUsedBefore = {}; | |||||||||||||||||||||||||||
| 11 | 4 | our $Functions = {}; | |||||||||||||||||||||||||||
| 12 | 4 | our $Order = []; | |||||||||||||||||||||||||||
| 13 | 4 | our $RegularExpression = qr/(?!)/; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | our $VERSION = '3.02'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 18 | 6 | my $package = shift; | |||||||||||||||||||||||||||
| 19 | 6 | ▶ | croak("Uneven number of arguments in usage of " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | . "Math::SymbolicX::ParserExtensionFactory") | ||||||||||||||||||||||||||||
| 21 | if @_ % 2; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | my %args = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | _extend_parser(); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | foreach my $key ( keys %args ) { | |||||||||||||||||||||||||||
| 28 | croak("Invalid keys => value pairs as arguments in usage of " | ||||||||||||||||||||||||||||
| 29 | . "Math::SymbolicX::ParserExtensionFactory") | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | if not ref( $args{$key} ) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | if ( not exists $Functions->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 3 | push @$Order, $key; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 3 | $Functions->{$key} = $args{$key}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | $RegularExpression = _regenerate_regex($Order); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | return (); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 6 | ▶ | sub _extend_parser { | ||||||||||||||||||||||||||
|
Subroutine
_extend_parser: called CC=6 SCAR=21.2
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | my $parser = shift; | |||||||||||||||||||||||||||
| 45 | 6 | ▶ | $parser = $Math::Symbolic::Parser if not defined $parser; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # make sure there is a parser | ||||||||||||||||||||||||||||
| 48 | 6 | ▶ | if (not defined $parser) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $parser = $Math::Symbolic::Parser = Math::Symbolic::Parser->new(); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if ( not exists $BeenUsedBefore->{"$parser"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if ($parser->isa('Parse::RecDescent')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 54 | 3 | _extend_parser_recdescent($parser) | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | elsif ($parser->isa('Math::Symbolic::Parser::Yapp')) { | ||||||||||||||||||||||||||||
| 57 | 0 | _extend_parser_yapp($parser); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 0 | die "Unsupported parser type!"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 3 | $BeenUsedBefore->{"$parser"} = 1; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub _extend_parser_yapp { | ||||||||||||||||||||||||||
|
Subroutine
_extend_parser_yapp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | # This is a no-op since ::Parser::Yapp has built-in support for | ||||||||||||||||||||||||||||
| 68 | # ::ParserExtensionFactory. This would probably not be possible | ||||||||||||||||||||||||||||
| 69 | # otherwise. | ||||||||||||||||||||||||||||
| 70 | 0 | return(1); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub _extend_parser_recdescent { | ||||||||||||||||||||||||||
|
Subroutine
_extend_parser_recdescent: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 3 | my $parser = shift; | |||||||||||||||||||||||||||
| 75 | 3 | $parser->{__PRIV_EXT_FUNC_REGEX} = qr/(?!)/; | |||||||||||||||||||||||||||
| 76 | 3 | $parser->Extend(<<'EXTENSION'); | |||||||||||||||||||||||||||
| 77 | function: /$thisparser->{__PRIV_EXT_FUNC_REGEX}\s*(?=\()/ {extract_bracketed($text, '(')} | ||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | warn 'function_private_msx_parser_extension_factory ' | ||||||||||||||||||||||||||||
| 80 | if $Math::Symbolic::Parser::DEBUG; | ||||||||||||||||||||||||||||
| 81 | my $function = $item[1]; | ||||||||||||||||||||||||||||
| 82 | $function =~ s/\s+$//; | ||||||||||||||||||||||||||||
| 83 | my $argstring = substr($item[2], 1, length($item[2])-2); | ||||||||||||||||||||||||||||
| 84 | die "Invalid extension function and/or arguments '$function$item[2]' ". | ||||||||||||||||||||||||||||
| 85 | "(Math::SymbolicX::ParserExtensionFactory)" | ||||||||||||||||||||||||||||
| 86 | if not exists | ||||||||||||||||||||||||||||
| 87 | $thisparser->{__PRIV_EXT_FUNCTIONS}{$function}; | ||||||||||||||||||||||||||||
| 88 | my $result = $thisparser->{__PRIV_EXT_FUNCTIONS}{$function}->($argstring); | ||||||||||||||||||||||||||||
| 89 | die "Invalid result of extension function application " | ||||||||||||||||||||||||||||
| 90 | ."('$item[1]($argstring)'). Also refer to the " | ||||||||||||||||||||||||||||
| 91 | ."Math::SymbolicX::ParserExtensionFactory manpage." | ||||||||||||||||||||||||||||
| 92 | if ref($result) !~ /^Math::Symbolic/; | ||||||||||||||||||||||||||||
| 93 | $return = $result; | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | | /$Math::SymbolicX::ParserExtensionFactory::RegularExpression\s*(?=\()/ {extract_bracketed($text, '(')} | ||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | warn 'function_global_msx_parser_extension_factory ' | ||||||||||||||||||||||||||||
| 99 | if $Math::Symbolic::Parser::DEBUG; | ||||||||||||||||||||||||||||
| 100 | my $function = $item[1]; | ||||||||||||||||||||||||||||
| 101 | $function =~ s/\s+$//; | ||||||||||||||||||||||||||||
| 102 | my $argstring = substr($item[2], 1, length($item[2])-2); | ||||||||||||||||||||||||||||
| 103 | die "Invalid extension function and/or arguments '$function$item[2]' ". | ||||||||||||||||||||||||||||
| 104 | "(Math::SymbolicX::ParserExtensionFactory)" | ||||||||||||||||||||||||||||
| 105 | if not exists | ||||||||||||||||||||||||||||
| 106 | $Math::SymbolicX::ParserExtensionFactory::Functions->{$function}; | ||||||||||||||||||||||||||||
| 107 | my $result = $Math::SymbolicX::ParserExtensionFactory::Functions->{$function}->($argstring); | ||||||||||||||||||||||||||||
| 108 | die "Invalid result of extension function application " | ||||||||||||||||||||||||||||
| 109 | ."('$item[1]($argstring)'). Also refer to the " | ||||||||||||||||||||||||||||
| 110 | ."Math::SymbolicX::ParserExtensionFactory manpage." | ||||||||||||||||||||||||||||
| 111 | if ref($result) !~ /^Math::Symbolic/; | ||||||||||||||||||||||||||||
| 112 | $return = $result; | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | EXTENSION | ||||||||||||||||||||||||||||
| 116 | 3 | return(1); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 8 | ▶ | sub _regenerate_regex { | ||||||||||||||||||||||||||
|
Subroutine
_regenerate_regex: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 8 | my @arrays = @_; | |||||||||||||||||||||||||||
| 121 | 8 | my $string = join '|', map {"\Q$_\E"} map {@$_} @arrays; | |||||||||||||||||||||||||||
| 122 | 8 | ▶ | return qr/(?!)/ if $string eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 6 | return qr/(?:$string)/; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | sub add_private_functions { | ||||||||||||||||||||||||||
|
Subroutine
add_private_functions: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | shift if not ref $_[0] and $_[0] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $_[0] and $_[0] eq __PACKAGE__
MC/DC0%0/2
not ref $_[0] and $_[0] eq __PACKAGE__
not ref $_[0]
$_[0] eq __PACKAGE__
Truth table33%1/3
not(A) and B
A= not ref $_[0]B= $_[0] eq __PACKAGE__
| |||||||||||||||||||||||||||||
| 128 | 2 | my $parser = shift; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | croak("Invalid number of arguments!") if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | $parser->{__PRIV_EXT_FUNCTIONS} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parser->{"__PRIV_EXT_FUNCTIONS"} ||= {}
MC/DC0%0/1
$parser->{"__PRIV_EXT_FUNCTIONS"} ||= {}
$parser->{"__PRIV_EXT_FUNCTIONS"}
Truth table50%1/2
A
A= $parser->{"__PRIV_EXT_FUNCTIONS"}
| |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | $parser->{__PRIV_EXT_FUNC_ORDER} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parser->{"__PRIV_EXT_FUNC_ORDER"} ||= []
MC/DC0%0/1
$parser->{"__PRIV_EXT_FUNC_ORDER"} ||= []
$parser->{"__PRIV_EXT_FUNC_ORDER"}
Truth table50%1/2
A
A= $parser->{"__PRIV_EXT_FUNC_ORDER"}
| |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 2 | my $name = shift; | |||||||||||||||||||||||||||
| 135 | 2 | push @{$parser->{__PRIV_EXT_FUNC_ORDER}}, $name; | |||||||||||||||||||||||||||
| 136 | 2 | $parser->{__PRIV_EXT_FUNCTIONS}{$name} = shift; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | $parser->{__PRIV_EXT_FUNC_REGEX} = _regenerate_regex( $parser->{__PRIV_EXT_FUNC_ORDER} ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | 1; | |||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||