MustacheTemplate/Lexer.pm

filter: Statementstmt 100% 71 / 73 (2 uncoverable) Branchbran 96.4% 26 / 28 (1 uncoverable) Conditioncond 100% 9 / 10 (1 uncoverable) MC/DCmcdc 100% 5 / 6 (1 uncoverable) Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 99.2% 121 / 127 (5 uncoverable) CC 19 SCAR 18.2
CC
19
Cov
99%
_tokenize_tag
9.0
tokenize
8.0
_error
4.0
CRAP 6.2
« MustacheTemplate/Evaluator.pm ↑ Summary MustacheTemplate.pm »
1 package Text::MustacheTemplate::Lexer;
2 17 ▶use 5.022000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 our %EXPORT_TAGS = (
11 types => [qw/TOKEN_RAW_TEXT TOKEN_PADDING TOKEN_TAG TOKEN_DELIMITER/]
12 );
13 17 our @EXPORT_OK = map @$_, values %EXPORT_TAGS;
14  
15 17 our $OPEN_DELIMITER = '{{';
16 17 our $CLOSE_DELIMITER = '}}';
17  
18 17 my %CLOSE_DELIMITER_PREFIX = (
19 '{' => '}',
20 '=' => '=',
21 );
22  
23 17 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 # enum
25 17 TOKEN_RAW_TEXT => 0,
26 TOKEN_PADDING => 1,
27 TOKEN_TAG => 2,
28 TOKEN_DELIMITER => 3,
29 17 };
30  
31 17 our $_SOURCE;
32 17 our @_TOKENS;
33  
34 551 ▶sub tokenize {
Subroutine tokenize: called CC=8 SCAR=20.8
Pod documented
35 551 my ($class, $source) = @_;
36  
37 551 local $OPEN_DELIMITER = $OPEN_DELIMITER;
38 551 local $CLOSE_DELIMITER = $CLOSE_DELIMITER;
39 551 local $_SOURCE = $source;
40  
41 551 my @tokens = ([TOKEN_DELIMITER, 0, undef, $OPEN_DELIMITER, $CLOSE_DELIMITER]);
42 551 ▶ until ($_SOURCE =~ /\G\z/mgcano) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Text::MustacheTemplate::Lexer::_SOURCE =~ /\G\z/acgmno) 2557 544 3101
43 2557 ▶ my $pos = pos $_SOURCE || 0;
Condition100%2/2
pos $Text::MustacheTemplate::Lexer::_SOURCE || 0
l!l
2007550
MC/DC100%1/1
pos $Text::MustacheTemplate::Lexer::_SOURCE || 0
pos $Text::MustacheTemplate::Lexer::_SOURCE
Truth table100%2/2
A
A= pos $Text::MustacheTemplate::Lexer::_SOURCE
Aresult
11
00
44 2557 ▶ if ($_SOURCE =~ /\G\Q${OPEN_DELIMITER}\E([\{#\/&^!>\$<=])?/mgac) { # uncoverable branch false count:2
Branch: 2 branches
BranchTrueFalseTotal
if ($Text::MustacheTemplate::Lexer::_SOURCE =~ m[\G\Q$Text::MustacheTemplate::Lexer::OPEN_DELIMITER\E([\{#/&^!>\$<=])?]acgm) { } 1302 1255 2557
elsif ($Text::MustacheTemplate::Lexer::_SOURCE =~ /\G(?:(^[[:blank:]]+)|(.+?)(^[[:blank:]]+)?)(?=\Q$Text::MustacheTemplate::Lexer::OPEN_DELIMITER\E|\z)/acgms) { } 1255 0 1255
45 1302 push @tokens => _tokenize_tag($1, $pos);
46 } elsif ($_SOURCE =~ /\G(?:(^[[:blank:]]+)|(.+?)(^[[:blank:]]+)?)(?=\Q${OPEN_DELIMITER}\E|\z)/msgac) {
47 1255 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 26 1229 1255
48 26 push @tokens => [TOKEN_PADDING, $pos, $1];
49 } else {
50 1229 push @tokens => [TOKEN_RAW_TEXT, $pos, $2];
51 1229 ▶ push @tokens => [TOKEN_PADDING, $pos+length($2), $3] if defined $3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 66 1163 1229
52 }
53 } else {
54 0 _error('Syntax Error: Unexpected Token', pos $_SOURCE); # uncoverable statement
55 }
56 }
57 544 ▶ if (length $_SOURCE != pos $_SOURCE) { # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
if (length $Text::MustacheTemplate::Lexer::_SOURCE != pos $Text::MustacheTemplate::Lexer::_SOURCE) 0 544 544
58 0 _error('Syntax Error: Unexpected Token', pos $_SOURCE); # uncoverable statement
59 }
60  
61 544 return @tokens;
62 }
63  
64 1302 ▶sub _tokenize_tag {
Subroutine _tokenize_tag: called CC=9 SCAR=22.0
65 1302 my ($type, $pos) = @_;
66  
67 1302 ▶ my $prefix = defined $type ? ($CLOSE_DELIMITER_PREFIX{$type} || '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 974 328 1302
Condition100%2/2
$CLOSE_DELIMITER_PREFIX{$type} or ""
l!l
82892
MC/DC100%1/1
$CLOSE_DELIMITER_PREFIX{$type} or ""
$CLOSE_DELIMITER_PREFIX{$type}
Truth table100%2/2
A
A= $CLOSE_DELIMITER_PREFIX{$type}
Aresult
11
00
68 1302 ▶ if ($_SOURCE =~ /\G(.+?)\Q${prefix}${CLOSE_DELIMITER}\E/msgac) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::MustacheTemplate::Lexer::_SOURCE =~ /\G(.+?)\Q$prefix$Text::MustacheTemplate::Lexer::CLOSE_DELIMITER\E/acgms) { } 1298 4 1302
69 1298 my $body = $1;
70 1298 ▶ if (defined $type && $type eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq '=') { } 40 1258 1298
Condition100%3/3
defined $type and $type eq "="
!ll&&!rl&&r
32693240
MC/DC100%2/2
defined $type and $type eq "="
defined $type $type eq "="
Truth table100%3/3
A and B
A= defined $typeB= $type eq "="
ABresult
0X0
100
111
71 40 my $delimiters = $body;
72 40 $delimiters =~ s/^\s+//ano;
73 40 $delimiters =~ s/\s+$//ano;
74 40 ▶ if ($delimiters =~ /=/ano) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delimiters =~ /=/ano) 1 39 40
75 1 _error('Syntax Error: Invalid Delimiter', $pos);
76 }
77 39 my @delimiters = split /\s+/, $delimiters;
78 39 ▶ if (@delimiters != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@delimiters != 2) 2 37 39
79 2 _error('Syntax Error: Invalid Delimiter', $pos);
80 }
81 37 $OPEN_DELIMITER = $delimiters[0];
82 37 $CLOSE_DELIMITER = $delimiters[1];
83 37 return [TOKEN_DELIMITER, $pos, $body, $OPEN_DELIMITER, $CLOSE_DELIMITER];
84 } else {
85 1258 ▶ return [TOKEN_TAG, $pos, $type, $body] if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 932 326 1258
86 326 return [TOKEN_TAG, $pos, $body];
87 }
88 } else {
89 4 _error('Syntax Error: Unexpected Token', $pos);
90 }
91 }
92  
93 7 ▶sub _error {
Subroutine _error: called CC=4 SCAR=13.9
94 7 my ($msg, $curr) = @_;
95  
96 7 my $src = $_SOURCE;
97 7 my $line = 1;
98 7 my $start = 0;
99 7 ▶ while ($src =~ /$/smgco && pos $src <= $curr) {# uncoverable condition left
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /$/cgmosu and pos $src <= $curr) 3 7 10
Condition100%2/3
$src =~ /$/cgmosu and pos $src <= $curr
!ll&&!rl&&r
073
MC/DC100%1/2
$src =~ /$/cgmosu and pos $src <= $curr
$src =~ /$/cgmosu pos $src <= $curr
Truth table100%2/3
A and B
A= $src =~ /$/cgmosuB= pos $src <= $curr
ABresult
0X0
100
111
100 3 $start = pos $src;
101 3 $line++;
102 }
103 7 my $end = pos $src;
104 7 my $len = $curr - $start;
105 7 ▶ $len-- if $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 0 2 5 7
106  
107 7 my $trace = join "\n",
108 "${msg}: line:$line",
109 substr($src, $start, $end - $start),
110 (' ' x $len) . '^';
111 7 croak $trace, "\n";
112 }
113  
114 17 1;
115 __END__
« MustacheTemplate/Evaluator.pm ↑ Summary MustacheTemplate.pm »