| 1 | package Text::MicroMason::Base; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 39 | require Carp; | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 39 | ▶ | use Class::MixinFactory -hasafactory; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 39 | for my $factory ( (__PACKAGE__)->mixin_factory ) { | |||||||||||||||||||||||||||||||
| 12 | 39 | $factory->base_class( "Text::MicroMason::Base" ); | |||||||||||||||||||||||||||||||
| 13 | 39 | $factory->mixin_prefix( "Text::MicroMason" ); | |||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 164 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 21 | 164 | my $callee = shift; | |||||||||||||||||||||||||||||||
| 22 | 164 | my ( @traits, @attribs ); | |||||||||||||||||||||||||||||||
| 23 | 164 | ▶ | while ( scalar @_ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | 304 | ▶ | if ( $_[0] =~ /^\-(\w+)$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 266 | push @traits, $1; | |||||||||||||||||||||||||||||||
| 26 | 266 | shift; | |||||||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||||||
| 28 | 38 | push @attribs, splice(@_, 0, 2); | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | 164 | ▶ | if ( scalar @traits ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 154 | ▶ | die("Adding moxins to an existing class not supported yet!") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | unless ( $callee eq __PACKAGE__ ); | ||||||||||||||||||||||||||||||||
| 34 | 154 | $callee->class( @traits )->create( @attribs ) | |||||||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||||||
| 36 | 10 | $callee->create( @attribs ) | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # $mason = $class->create( %options ); | ||||||||||||||||||||||||||||||||
| 43 | # $clone = $object->create( %options ); | ||||||||||||||||||||||||||||||||
| 44 | 204 | ▶ | sub create { | ||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 204 | my $referent = shift; | |||||||||||||||||||||||||||||||
| 46 | 204 | ▶ | if ( ! ref $referent ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 155 | bless { $referent->defaults(), @_ }, $referent; | |||||||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||||||
| 49 | 49 | bless { $referent->defaults(), %$referent, @_ }, ref $referent; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 204 | ▶ | sub defaults { | ||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 54 | return () | ||||||||||||||||||||||||||||||||
| 55 | 204 | } | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # $code_ref = $mason->compile( text => $template, %options ); | ||||||||||||||||||||||||||||||||
| 62 | # $code_ref = $mason->compile( file => $filename, %options ); | ||||||||||||||||||||||||||||||||
| 63 | # $code_ref = $mason->compile( handle => $filehandle, %options ); | ||||||||||||||||||||||||||||||||
| 64 | 312 | ▶ | sub compile { | ||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 65 | 312 | my ( $self, $src_type, $src_data, %options ) = @_; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 312 | ($self, $src_type, $src_data) = $self->prepare($src_type, $src_data,%options); | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 312 | my $code = $self->interpret( $src_type, $src_data ); | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 312 | ▶ | unless ( $self->eval_sub($code) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 17 | ▶ | ref($@) and die $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 17 | $self->croak_msg( "MicroMason compilation failed: $@\n" . _number_lines($code) . "\n" ); | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | # Internal helper to number the lines in the compiled template when compilation croaks | ||||||||||||||||||||||||||||||||
| 79 | 17 | ▶ | sub _number_lines { | ||||||||||||||||||||||||||||||
|
Subroutine
_number_lines: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 80 | 17 | my $code = shift; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 17 | my $n = 0; | |||||||||||||||||||||||||||||||
| 83 | 17 | return join("\n", map { sprintf("%4d %s", $n++, $_) } split(/\n/, $code)). | |||||||||||||||||||||||||||||||
| 84 | "\n** Please use Text::MicroMason->new\(-LineNumbers\) for better diagnostics!"; | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | # $result = $mason->execute( code => $subref, @arguments ); | ||||||||||||||||||||||||||||||||
| 91 | # $result = $mason->execute( $src_type, $src_data, @arguments ); | ||||||||||||||||||||||||||||||||
| 92 | # $result = $mason->execute( $src_type, $src_data, \%options, @arguments ); | ||||||||||||||||||||||||||||||||
| 93 | 263 | ▶ | sub execute { | ||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 94 | 263 | my $self = shift; | |||||||||||||||||||||||||||||||
| 95 | 263 | ▶ | my $sub = ( $_[0] eq 'code' ) ? do { shift; shift } : $self->compile( shift, shift, ref( $_[0] ) ? %{ shift() } : () ); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 96 | 258 | ▶ | unless ($sub) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 5 | ▶ | ref($@) and die $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 5 | $self->croak_msg("MicroMason compilation failed: $@"); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | 253 | &$sub( @_ ); | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | # ($self, $src_type, $src_data) = $self->prepare($src_type, $src_data, %options) | ||||||||||||||||||||||||||||||||
| 108 | 312 | ▶ | sub prepare { | ||||||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 109 | 312 | my ( $self, $src_type, $src_data, %options ) = @_; | |||||||||||||||||||||||||||||||
| 110 | 312 | ▶ | $self = $self->create( %options ) if ( scalar keys %options ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 312 | return ( $self, $src_type, $src_data ); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # $perl_code = $mason->interpret( $src_type, $src_data ); | ||||||||||||||||||||||||||||||||
| 117 | 314 | ▶ | sub interpret { | ||||||||||||||||||||||||||||||
|
Subroutine
interpret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 118 | 314 | my ( $self, $src_type, $src_data ) = @_; | |||||||||||||||||||||||||||||||
| 119 | 314 | my $template = $self->read( $src_type, $src_data ); | |||||||||||||||||||||||||||||||
| 120 | 314 | my @tokens = $self->lex( $template ); | |||||||||||||||||||||||||||||||
| 121 | 314 | my $code = $self->assemble( @tokens ); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # Source file and line number | ||||||||||||||||||||||||||||||||
| 124 | 314 | my $source_line = $self->source_file_line_label( $src_type, $src_data ); | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 314 | return $source_line . "\n" . $code; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | # $line_number_comment = $mason->source_file_line_label( $src_type, $src_data ); | ||||||||||||||||||||||||||||||||
| 130 | 314 | ▶ | sub source_file_line_label { | ||||||||||||||||||||||||||||||
|
Subroutine
source_file_line_label: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 131 | 314 | my ( $self, $src_type, $src_data ) = @_; | |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 314 | ▶ | if ( $src_type eq 'file' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 48 | return qq(# line 1 "$src_data"); | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 266 | my @caller; | |||||||||||||||||||||||||||||||
| 138 | my $call_level; | ||||||||||||||||||||||||||||||||
| 139 | 266 | ▶ | do { @caller = caller( ++ $call_level ) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0])
MC/DC100%2/2
$caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0])
$caller[0] =~ /^Text::MicroMason/
$self->isa($caller[0])
Truth table100%3/3
A or B
A= $caller[0] =~ /^Text::MicroMason/B= $self->isa($caller[0])
| |||||||||||||||||||||||||||||||||
| 140 | while ( $caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0]) ); | ||||||||||||||||||||||||||||||||
| 141 | 266 | ▶ | my $package = ( $caller[1] || $0 ); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$caller[1] || $0
MC/DC0%0/2
$caller[1] || $0
$caller[1]
$0
Truth table33%1/3
A || B
A= $caller[1]B= $0
| |||||||||||||||||||||||||||||||||
| 142 | 266 | qq{# line 1 "text template (compiled at $package line $caller[2])"} | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | # $code_ref = $mason->eval_sub( $perl_code ); | ||||||||||||||||||||||||||||||||
| 149 | 293 | ▶ | sub eval_sub { | ||||||||||||||||||||||||||||||
|
Subroutine
eval_sub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 150 | 293 | my $m = shift; | |||||||||||||||||||||||||||||||
| 151 | package Text::MicroMason::Commands; | ||||||||||||||||||||||||||||||||
| 152 | eval( shift ) | ||||||||||||||||||||||||||||||||
| 153 | 293 | ▶ | } | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | # $template = $mason->read( $src_type, $src_data ); | ||||||||||||||||||||||||||||||||
| 160 | 314 | ▶ | sub read { | ||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 161 | 314 | my ( $self, $src_type, $src_data ) = @_; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 314 | my $src_method = "read_$src_type"; | |||||||||||||||||||||||||||||||
| 164 | 314 | ▶ | $self->can($src_method) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | or $self->croak_msg("Unsupported source type '$src_type'"); | ||||||||||||||||||||||||||||||||
| 166 | 314 | $self->$src_method( $src_data ); | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | # $template = $mason->read_text( $template ); | ||||||||||||||||||||||||||||||||
| 170 | 263 | ▶ | sub read_text { | ||||||||||||||||||||||||||||||
|
Subroutine
read_text: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 171 | 263 | ▶ | ref($_[1]) ? $$_[1] : $_[1]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | # $contents = $mason->read_file( $filename ); | ||||||||||||||||||||||||||||||||
| 175 | 48 | ▶ | sub read_file { | ||||||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 176 | 48 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||||||
| 177 | 48 | local *FILE; | |||||||||||||||||||||||||||||||
| 178 | 48 | ▶ | open FILE, "$file" or $self->croak_msg("MicroMason can't open $file: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 48 | local $/ = undef; | |||||||||||||||||||||||||||||||
| 180 | 48 | local $_ = <FILE>; | |||||||||||||||||||||||||||||||
| 181 | 48 | ▶ | close FILE or $self->croak_msg("MicroMason can't close $file: $!");; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 182 | 48 | return $_; | |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | # $contents = $mason->read_handle( $filehandle ); | ||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | sub read_handle { | ||||||||||||||||||||||||||||||
|
Subroutine
read_handle: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 187 | 3 | my ( $self, $handle ) = @_; | |||||||||||||||||||||||||||||||
| 188 | 3 | ▶ | my $fh = (ref $handle eq 'GLOB') ? $handle : $$handle; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | 3 | local $/ = undef; | |||||||||||||||||||||||||||||||
| 190 | <$fh> | ||||||||||||||||||||||||||||||||
| 191 | 3 | } | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | # @token_pairs = $mason->lex( $template ); | ||||||||||||||||||||||||||||||||
| 196 | 299 | ▶ | sub lex { | ||||||||||||||||||||||||||||||
|
Subroutine
lex: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 197 | 299 | my $self = shift; | |||||||||||||||||||||||||||||||
| 198 | 299 | local $_ = "$_[0]"; | |||||||||||||||||||||||||||||||
| 199 | 299 | my @tokens; | |||||||||||||||||||||||||||||||
| 200 | 299 | ▶ | my $lexer = $self->can('lex_token') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | or $self->croak_msg('Unable to lex_token(); must select a syntax mixin'); | ||||||||||||||||||||||||||||||||
| 202 | # warn "Lexing: " . pos($_) . " of " . length($_) . "\n"; | ||||||||||||||||||||||||||||||||
| 203 | 299 | ▶ | until ( /\G\z/gc ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 1161 | ▶ | my @parsed = &$lexer( $self ) or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
(my(@parsed) = &$lexer($self)) || /\G ( .{0,20} ) /cgsx && die("MicroMason parsing halted at '$1'\n")
/\G ( .{0,20} ) /cgsx and die "MicroMason parsing halted at '$1'\n"
MC/DC0%0/2
(my(@parsed) = &$lexer($self)) || /\G ( .{0,20} ) /cgsx && die("MicroMason parsing halted at '$1'\n")
(my(@parsed) = &$lexer($self))
/\G ( .{0,20} ) /cgsx
Truth table33%1/3
A || B
A= (my(@parsed) = &$lexer($self))B= /\G ( .{0,20} ) /cgsx
| |||||||||||||||||||||||||||||||||
| 205 | /\G ( .{0,20} ) /gcxs | ||||||||||||||||||||||||||||||||
| 206 | && die "MicroMason parsing halted at '$1'\n"; | ||||||||||||||||||||||||||||||||
| 207 | 1161 | push @tokens, @parsed; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | 299 | return @tokens; | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | # ( $type, $value ) = $mason->lex_token(); | ||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub lex_token { | ||||||||||||||||||||||||||||||
|
Subroutine
lex_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 214 | 0 | die "The lex_token() method is abstract and must be provided by a subclass"; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | # Text elements used for subroutine assembly | ||||||||||||||||||||||||||||||||
| 222 | 314 | ▶ | sub assembler_rules { | ||||||||||||||||||||||||||||||
|
Subroutine
assembler_rules: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 223 | template => [ qw( $sub_start $init_errs $init_output | ||||||||||||||||||||||||||||||||
| 224 | $init_args @perl $return_output $sub_end ) ], | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # Subroutine scafolding | ||||||||||||||||||||||||||||||||
| 227 | sub_start => 'sub { ', | ||||||||||||||||||||||||||||||||
| 228 | sub_end => '}', | ||||||||||||||||||||||||||||||||
| 229 | init_errs => | ||||||||||||||||||||||||||||||||
| 230 | 'local $SIG{__DIE__} = sub { ref($_[0]) and die $_[0]; die "MicroMason execution failed: ", @_ };', | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | # Argument processing elements | ||||||||||||||||||||||||||||||||
| 233 | init_args => 'my %ARGS = @_ if ($#_ % 2);', | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | # Output generation | ||||||||||||||||||||||||||||||||
| 236 | 309 | ▶ | init_output => sub { my $m = shift; my $sub = $m->{output_sub} ? '$m->{output_sub}' : 'sub {push @OUT, @_}'; 'my @OUT; my $_out = ' . $sub . ';' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | 309 | ▶ | add_output => sub { my $m = shift; $m->{output_sub} ? '&$_out' : 'push @OUT,' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 314 | return_output => 'join("", @OUT)', | |||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | # Mapping between token types | ||||||||||||||||||||||||||||||||
| 241 | text_token => 'perl OUT( QUOTED );', | ||||||||||||||||||||||||||||||||
| 242 | expr_token => "perl OUT( \"\".do{\nTOKEN\n} );", | ||||||||||||||||||||||||||||||||
| 243 | # the "". here forces string context, and should hopefully make | ||||||||||||||||||||||||||||||||
| 244 | # 'uninitialized' warnings appear closer to their source, rather | ||||||||||||||||||||||||||||||||
| 245 | # than at the big join "", @OUT; at the end | ||||||||||||||||||||||||||||||||
| 246 | file_token => "perl OUT( \$m->execute( file => do {\nTOKEN\n} ) );", | ||||||||||||||||||||||||||||||||
| 247 | # Note that we need newline after TOKEN here in case it ends with a comment. | ||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 314 | ▶ | sub assembler_vars { | ||||||||||||||||||||||||||||||
|
Subroutine
assembler_vars: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 251 | 314 | my $self = shift; | |||||||||||||||||||||||||||||||
| 252 | 314 | my %assembler = $self->assembler_rules(); | |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 314 | my @assembly = @{ delete $assembler{ template } }; | |||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 1051 | my %token_map = map { ( /^(.*?)_token$/ )[0] => delete $assembler{$_} } | |||||||||||||||||||||||||||||||
| 257 | 314 | grep { /_token$/ } keys %assembler; | |||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | 314 | ▶ | my %fragments = map { $_ => map { ref($_) ? &{$_}( $self ) : $_ } $assembler{$_} } keys %assembler; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 314 | return( \@assembly, \%fragments, \%token_map ); | |||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | # $perl_code = $mason->assemble( @tokens ); | ||||||||||||||||||||||||||||||||
| 265 | 314 | ▶ | sub assemble { | ||||||||||||||||||||||||||||||
|
Subroutine
assemble: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 266 | 314 | my $self = shift; | |||||||||||||||||||||||||||||||
| 267 | 314 | my @tokens = @_; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 314 | my ( $order, $fragments, $token_map ) = $self->assembler_vars(); | |||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | 314 | my %token_streams = map { $_ => [] } map { ( /^\W?\@(\w+)$/ ) } @$order; | |||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | 314 | ▶ | while ( scalar @tokens ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | 1252 | my ( $type, $token ) = splice( @tokens, 0, 2 ); | |||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 1252 | ▶ | unless ( $token_streams{$type} or $token_map->{$type} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token_streams{$type} or $token_map->{$type}
MC/DC100%2/2
$token_streams{$type} or $token_map->{$type}
$token_streams{$type}
$token_map->{$type}
Truth table100%3/3
A or B
A= $token_streams{$type}B= $token_map->{$type}
| |||||||||||||||||||||||||||||||||
| 277 | 40 | my $method = "assemble_$type"; | |||||||||||||||||||||||||||||||
| 278 | 40 | ▶ | my $sub = $self->can( $method ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 279 | or $self->croak_msg( "Unexpected token type '$type': '$token'" ); | ||||||||||||||||||||||||||||||||
| 280 | 40 | ($type, $token) = &$sub( $self, $token ); | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 1252 | ▶ | if ( my $typedef = $token_map->{ $type } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 284 | # Perform token map substitution in a single pass so that uses of | ||||||||||||||||||||||||||||||||
| 285 | # OUT in the token text are not improperly converted to output calls. | ||||||||||||||||||||||||||||||||
| 286 | # -- Simon, 2009-11-14 | ||||||||||||||||||||||||||||||||
| 287 | my %substitution_map = ( | ||||||||||||||||||||||||||||||||
| 288 | 'OUT' => $fragments->{add_output}, | ||||||||||||||||||||||||||||||||
| 289 | 978 | 'TOKEN' => $token, | |||||||||||||||||||||||||||||||
| 290 | 'QUOTED' => "qq(\Q$token\E)", | ||||||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||||||
| 292 | 978 | $typedef =~ s/\b(OUT|TOKEN|QUOTED)\b/$substitution_map{$1}/g; | |||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | 978 | ( $type, $token ) = split ' ', $typedef, 2; | |||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 1252 | ▶ | my $ary = $token_streams{$type} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 298 | or $self->croak_msg( "Unexpected token type '$type': '$token'" ); | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 1252 | push @$ary, $token | |||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | join( "\n", map { | ||||||||||||||||||||||||||||||||
| 304 | 314 | ▶ | /^(\W+)(\w+)$/ or $self->croak_msg("Can't assemble $_"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 305 | 3414 | ▶ | if ( $1 eq '$' ) { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 306 | 1912 | $fragments->{ $2 } | |||||||||||||||||||||||||||||||
| 307 | } elsif ( $1 eq '@' ) { | ||||||||||||||||||||||||||||||||
| 308 | 908 | @{ $token_streams{ $2 } } | |||||||||||||||||||||||||||||||
| 309 | } elsif ( $1 eq '!@' ) { | ||||||||||||||||||||||||||||||||
| 310 | 297 | reverse @{ $token_streams{ $2 } } | |||||||||||||||||||||||||||||||
| 311 | } elsif ( $1 eq '-@' ) { | ||||||||||||||||||||||||||||||||
| 312 | () | ||||||||||||||||||||||||||||||||
| 313 | 297 | } else { | |||||||||||||||||||||||||||||||
| 314 | 0 | $self->croak_msg("Can't assemble $_"); | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | } @$order ); | ||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 22 | ▶ | sub croak_msg { | ||||||||||||||||||||||||||||||
|
Subroutine
croak_msg: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 324 | 22 | local $Carp::CarpLevel = 2; | |||||||||||||||||||||||||||||||
| 325 | 22 | ▶ | shift and Carp::croak( ( @_ == 1 ) ? $_[0] : join(' ', map _printable(), @_) ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | my %Escape = ( | ||||||||||||||||||||||||||||||||
| 329 | 39 | ( map { chr($_), unpack('H2', chr($_)) } (0..255) ), | |||||||||||||||||||||||||||||||
| 330 | "\\"=>'\\', "\r"=>'r', "\n"=>'n', "\t"=>'t', "\""=>'"' | ||||||||||||||||||||||||||||||||
| 331 | ); | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | # $special_characters_escaped = _printable( $source_string ); | ||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | sub _printable { | ||||||||||||||||||||||||||||||
|
Subroutine
_printable: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | local $_ = scalar(@_) ? (shift) : $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | return "(undef)" unless defined; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 337 | 0 | s/([\r\n\t\"\\\x00-\x1f\x7F-\xFF])/\\$Escape{$1}/sgo; | |||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | /[^\w\d\-\:\.\']/ ? "q($_)" : $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | ###################################################################### | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 52 | ▶ | sub cache_key { | ||||||||||||||||||||||||||||||
|
Subroutine
cache_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 345 | 52 | my $self = shift; | |||||||||||||||||||||||||||||||
| 346 | 52 | my ($src_type, $src_data, %options) = @_; | |||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 52 | return $src_data; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 39 | 1; | |||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | __END__ | ||||||||||||||||||||||||||||||||