blib/lib/Text/Nunjucks/Lexer/Tokenizer.pm

filter: Statementstmt 53.4% 238 / 445 Branchbran 47.4% 132 / 278 Conditioncond 48.1% 78 / 162 MC/DCmcdc 32.5% 29 / 89 Subroutinesub 63.2% 31 / 49 Podpod 0.0% 0 / 22 totaltotal 48.6% 508 / 1045 CC 195 SCAR 58.1
CC
195
Cov
51%
next_token
635.0
next_token
635.0
_extract_matching
27.3
CRAP 333.9
↑ Summary blib/lib/Text/Nunjucks/Nodes.pm »
1 package Text::Nunjucks::Lexer::Tokenizer;
2 # ABSTRACT: The template tokenizer (lexer.js class Tokenizer)
3  
4 21 our $VERSION = '0.001';
5  
6 21 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Text::Nunjucks::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Text::Nunjucks::Lexer::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # upstream: class Tokenizer in lexer.js. A Moo class (ADR 0002) whose fields
14 # keep upstream's exact spelling (in_code, trimBlocks, lstripBlocks) the way
15 # the node classes keep their upstream field names, while methods snake_case
16 # (nextToken -> next_token). `token()` is a method (`$self->token`), a value
17 # constructor for Text::Nunjucks::Lexer::Token (house rule: dispatch through
18 # the invocant).
19 #
20 # lexer.js's `let` constants: the character sets and the six default tag
21 # delimiters are read by the Tokenizer alone, so they are this file's lexicals.
22 # The TOKEN_* type strings are also lexer.js's exports and stay with them in
23 # Text::Nunjucks::Lexer; the Tokenizer reads them there through ->lexer, as
24 # Text::Nunjucks::Parser::Parser does.
25 #
26 # lib.js is used through its real functions (Text::Nunjucks::Lib->index_of),
27 # never re-derived here. `throw new Error(...)` becomes a thrown
28 # Text::Nunjucks::Lib::Error (name 'Error'), the same mapping Lib.pm uses.
29  
30 #### module constants (upstream: the `let` bindings at the top of lexer.js)
31  
32 21 my $whitespace_chars = " \n\t\r\x{00A0}";
33 21 my $delim_chars = '()[]{}%*-+~/#,:|.<>=!';
34 21 my $int_chars = '0123456789';
35  
36 21 my $BLOCK_START = '{%';
37 21 my $BLOCK_END = '%}';
38 21 my $VARIABLE_START = '{{';
39 21 my $VARIABLE_END = '}}';
40 21 my $COMMENT_START = '{#';
41 21 my $COMMENT_END = '#}';
42  
43 # this.str / this.index / this.len / this.lineno / this.colno / this.in_code
44 # plus the resolved tag set and the two trim flags. index/lineno/colno/in_code
45 # are the mutated state; str/len/tags/trimBlocks/lstripBlocks are fixed at
46 # construction. Field spelling follows upstream (in_code, trimBlocks).
47 21 has str => ( is => 'ro' );
48 9727 ▶has index => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49 21 has len => ( is => 'ro' );
50 9727 ▶has lineno => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
51 9727 ▶has colno => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 9727 ▶has in_code => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
53 21 has tags => ( is => 'ro' );
54 21 has trimBlocks => ( is => 'ro' );
55 21 has lstripBlocks => ( is => 'ro' );
56  
57 327264 ▶sub token_class { 'Text::Nunjucks::Lexer::Token' }
Subroutine token_class: called CC=1 SCAR=0.0
Pod undocumented
58  
59 # The module that exports lexer.js's TOKEN_* type strings, named once (house
60 # rule), the same resolver Text::Nunjucks::Parser::Parser has.
61 327264 ▶sub lexer { 'Text::Nunjucks::Lexer' }
Subroutine lexer: called CC=1 SCAR=0.0
Pod undocumented
62  
63 # constructor(str, opts): positional, so BUILDARGS turns (str, opts) into the
64 # attribute hash. index/lineno/colno/in_code take their defaults (0/false).
65 9727 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=11 SCAR=24.8
66 9727 my ( $orig, $class, $str, $opts ) = @_;
67 9727 ▶ $opts = {} unless defined $opts; # opts = opts || {}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 332 9395 9727
68  
69 9727 ▶ my $tags = $opts->{tags} || {};
Condition100%2/2
$opts->{'tags'} || {}
l!l
139714
MC/DC100%1/1
$opts->{'tags'} || {}
$opts->{'tags'}
Truth table100%2/2
A
A= $opts->{'tags'}
Aresult
11
00
70 return $class->$orig(
71 str => $str,
72 len => length($str),
73 tags => {
74 BLOCK_START => ( $tags->{blockStart} || $BLOCK_START ),
75 BLOCK_END => ( $tags->{blockEnd} || $BLOCK_END ),
76 VARIABLE_START => ( $tags->{variableStart} || $VARIABLE_START ),
77 VARIABLE_END => ( $tags->{variableEnd} || $VARIABLE_END ),
78 COMMENT_START => ( $tags->{commentStart} || $COMMENT_START ),
79 COMMENT_END => ( $tags->{commentEnd} || $COMMENT_END )
80 },
81 trimBlocks => ( $opts->{trimBlocks} ? 1 : 0 ),
82 9727 ▶ lstripBlocks => ( $opts->{lstripBlocks} ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'trimBlocks'} ? : 12 9715 9727
$opts->{'lstripBlocks'} ? : 10 9717 9727
Condition66%12/18
$tags->{'blockStart'} || $BLOCK_START
l!l&&r!l&&!r
297250
$tags->{'blockEnd'} || $BLOCK_END
l!l&&r!l&&!r
297250
$tags->{'variableStart'} || $VARIABLE_START
l!l&&r!l&&!r
897190
$tags->{'variableEnd'} || $VARIABLE_END
l!l&&r!l&&!r
397240
$tags->{'commentStart'} || $COMMENT_START
l!l&&r!l&&!r
397240
$tags->{'commentEnd'} || $COMMENT_END
l!l&&r!l&&!r
397240
MC/DC0%0/2
$tags->{'blockStart'} || $BLOCK_START
$tags->{'blockStart'} $BLOCK_START
MC/DC0%0/2
$tags->{'blockEnd'} || $BLOCK_END
$tags->{'blockEnd'} $BLOCK_END
MC/DC0%0/2
$tags->{'variableStart'} || $VARIABLE_START
$tags->{'variableStart'} $VARIABLE_START
MC/DC0%0/2
$tags->{'variableEnd'} || $VARIABLE_END
$tags->{'variableEnd'} $VARIABLE_END
MC/DC0%0/2
$tags->{'commentStart'} || $COMMENT_START
$tags->{'commentStart'} $COMMENT_START
MC/DC0%0/2
$tags->{'commentEnd'} || $COMMENT_END
$tags->{'commentEnd'} $COMMENT_END
Truth table66%2/3
A || B
A= $tags->{'blockStart'}B= $BLOCK_START
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $tags->{'blockEnd'}B= $BLOCK_END
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $tags->{'variableStart'}B= $VARIABLE_START
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $tags->{'variableEnd'}B= $VARIABLE_END
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $tags->{'commentStart'}B= $COMMENT_START
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $tags->{'commentEnd'}B= $COMMENT_END
ABresult
1X1
011
000
83 );
84 21 };
85  
86 327264 ▶sub token {
Subroutine token: called CC=1 SCAR=1.2
Pod undocumented
87 0 ▶ my ( $self, $type, $value, $lineno, $colno ) = @_;
Subroutine token: not called CC=1 SCAR=1.2
Pod undocumented
88 0 return $self->token_class->new( $type, $value, $lineno, $colno );
89 }
90  
91 340719 ▶sub next_token {
Subroutine next_token: called CC=63 SCAR=64.5
Pod undocumented
92 0 ▶ my ($self) = @_;
Subroutine next_token: not called CC=63 SCAR=64.5
Pod undocumented
93 0 my $lineno = $self->lineno;
94 0 my $colno = $self->colno;
95 0 my $tok;
96  
97 0 ▶ if ( $self->in_code ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->in_code) { } 280045 60674 340719
if ($self->in_code) { } 0 0 0
98 # Otherwise, if we are in a block parse it as code
99 0 my $cur = $self->current;
100  
101 0 ▶ if ( $self->is_finished ) {
Branch: 14 branches
BranchTrueFalseTotal
if ($self->is_finished) { } 113 279932 280045
elsif ($cur eq '"' or $cur eq q[']) { } 9882 270050 279932
elsif ($tok = $self->_extract($whitespace_chars)) { } 105425 164625 270050
elsif ($tok = $self->_extract_string($self->tags->{'BLOCK_END'}) or $tok = $self->_extract_string('-' . $self->tags->{'BLOCK_END'})) { } 20063 144562 164625
elsif ($tok = $self->_extract_string($self->tags->{'VARIABLE_END'}) or $tok = $self->_extract_string('-' . $self->tags->{'VARIABLE_END'})) { } 12460 132102 144562
elsif ($cur eq 'r' and $self->_char_at($self->str, $self->index + 1) eq '/') { } 166 131936 132102
elsif (CORE::index($delim_chars, $cur) != -1) { } 53516 78420 131936
if ($self->is_finished) { } 0 0 0
elsif ($cur eq '"' or $cur eq q[']) { } 0 0 0
elsif ($tok = $self->_extract($whitespace_chars)) { } 0 0 0
elsif ($tok = $self->_extract_string($self->tags->{'BLOCK_END'}) or $tok = $self->_extract_string('-' . $self->tags->{'BLOCK_END'})) { } 0 0 0
elsif ($tok = $self->_extract_string($self->tags->{'VARIABLE_END'}) or $tok = $self->_extract_string('-' . $self->tags->{'VARIABLE_END'})) { } 0 0 0
elsif ($cur eq 'r' and $self->_char_at($self->str, $self->index + 1) eq '/') { } 0 0 0
elsif (CORE::index($delim_chars, $cur) != -1) { } 0 0 0
102 # We have nothing else to parse
103 0 return undef;
104 }
105 ▶ elsif ( $cur eq '"' || $cur eq "'" ) {
Condition50%3/6
$cur eq "\"" or $cur eq "'"
l!l&&r!l&&!r
72502632270050
$cur eq "\"" or $cur eq "'"
l!l&&r!l&&!r
000
MC/DC100%2/2
$cur eq "\"" or $cur eq "'"
$cur eq "\"" $cur eq "'"
MC/DC0%0/2
$cur eq "\"" or $cur eq "'"
$cur eq "\"" $cur eq "'"
Truth table100%3/3
A or B
A= $cur eq "\""B= $cur eq "'"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $cur eq "\""B= $cur eq "'"
ABresult
1X1
011
000
106 # We've hit a string
107 0 return $self->token( $self->lexer->TOKEN_STRING, $self->_parse_string($cur), $lineno, $colno );
108 }
109 elsif ( $tok = $self->_extract($whitespace_chars) ) {
110 # We hit some whitespace
111 0 return $self->token( $self->lexer->TOKEN_WHITESPACE, $tok, $lineno, $colno );
112 }
113 ▶ elsif ( ( $tok = $self->_extract_string( $self->tags->{BLOCK_END} ) )
Condition50%3/6
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
l!l&&r!l&&!r
2003132144562
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
l!l&&r!l&&!r
000
MC/DC100%2/2
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
MC/DC0%0/2
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
$tok = $self->_extract_string($self->tags->{"BLOCK_END"}) $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
Truth table100%3/3
A or B
A= $tok = $self->_extract_string($self->tags->{"BLOCK_END"})B= $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $tok = $self->_extract_string($self->tags->{"BLOCK_END"})B= $tok = $self->_extract_string('-' . $self->tags->{"BLOCK_END"})
ABresult
1X1
011
000
114 || ( $tok = $self->_extract_string( '-' . $self->tags->{BLOCK_END} ) ) )
115 {
116 # Special check for the block end tag
117 #
118 # It is a requirement that start and end tags are composed of
119 # delimiter characters (%{}[] etc), and our code always
120 # breaks on delimiters so we can assume the token parsing
121 # doesn't consume these elsewhere
122 0 $self->in_code(0);
123 0 ▶ if ( $self->trimBlocks ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->trimBlocks) 24 20039 20063
if ($self->trimBlocks) 0 0 0
124 0 $cur = $self->current;
125 0 ▶ if ( $cur eq "\n" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($cur eq "\n") { } 12 12 24
elsif ($cur eq "\r") { } 10 2 12
if ($cur eq "\n") { } 0 0 0
elsif ($cur eq "\r") { } 0 0 0
126 # Skip newline
127 0 $self->forward;
128 }
129 elsif ( $cur eq "\r" ) {
130 # Skip CRLF newline
131 0 $self->forward;
132 0 $cur = $self->current;
133 0 ▶ if ( $cur eq "\n" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur eq "\n") { } 6 4 10
if ($cur eq "\n") { } 0 0 0
134 0 $self->forward;
135 }
136 else {
137 # Was not a CRLF, so go back
138 0 $self->back;
139 }
140 }
141 }
142 0 return $self->token( $self->lexer->TOKEN_BLOCK_END, $tok, $lineno, $colno );
143 }
144 ▶ elsif ( ( $tok = $self->_extract_string( $self->tags->{VARIABLE_END} ) )
Condition50%3/6
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
l!l&&r!l&&!r
124555132102
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
l!l&&r!l&&!r
000
MC/DC100%2/2
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
MC/DC0%0/2
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) or $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
$tok = $self->_extract_string($self->tags->{"VARIABLE_END"}) $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
Truth table100%3/3
A or B
A= $tok = $self->_extract_string($self->tags->{"VARIABLE_END"})B= $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $tok = $self->_extract_string($self->tags->{"VARIABLE_END"})B= $tok = $self->_extract_string('-' . $self->tags->{"VARIABLE_END"})
ABresult
1X1
011
000
145 || ( $tok = $self->_extract_string( '-' . $self->tags->{VARIABLE_END} ) ) )
146 {
147 # Special check for variable end tag (see above)
148 0 $self->in_code(0);
149 0 return $self->token( $self->lexer->TOKEN_VARIABLE_END, $tok, $lineno, $colno );
150 }
151 ▶ elsif ( $cur eq 'r' && $self->_char_at( $self->str, $self->index + 1 ) eq '/' ) {
Condition50%3/6
$cur eq "r" and $self->_char_at($self->str, $self->index + 1) eq "/"
!ll&&!rl&&r
1301461790166
$cur eq "r" and $self->_char_at($self->str, $self->index + 1) eq "/"
!ll&&!rl&&r
000
MC/DC100%2/2
$cur eq "r" and $self->_char_at($self->str, $self->index + 1) eq "/"
$cur eq "r" $self->_char_at($self->str, $self->index + 1) eq "/"
MC/DC0%0/2
$cur eq "r" and $self->_char_at($self->str, $self->index + 1) eq "/"
$cur eq "r" $self->_char_at($self->str, $self->index + 1) eq "/"
Truth table100%3/3
A and B
A= $cur eq "r"B= $self->_char_at($self->str, $self->index + 1) eq "/"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $cur eq "r"B= $self->_char_at($self->str, $self->index + 1) eq "/"
ABresult
0X0
100
111
152 # Skip past 'r/'.
153 0 $self->forward_n(2);
154  
155 # Extract until the end of the regex -- / ends it, \/ does not.
156 0 my $regex_body = '';
157 0 ▶ while ( !$self->is_finished ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->is_finished) 650 0 650
unless ($self->is_finished) 0 0 0
158 0 ▶ if ( $self->current eq '/' && $self->previous ne '\\' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->current eq '/' and $self->previous ne '\\') { } 166 484 650
if ($self->current eq '/' and $self->previous ne '\\') { } 0 0 0
Condition50%3/6
$self->current eq "/" and $self->previous ne "\\"
!ll&&!rl&&r
4786166
$self->current eq "/" and $self->previous ne "\\"
!ll&&!rl&&r
000
MC/DC100%2/2
$self->current eq "/" and $self->previous ne "\\"
$self->current eq "/" $self->previous ne "\\"
MC/DC0%0/2
$self->current eq "/" and $self->previous ne "\\"
$self->current eq "/" $self->previous ne "\\"
Truth table100%3/3
A and B
A= $self->current eq "/"B= $self->previous ne "\\"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $self->current eq "/"B= $self->previous ne "\\"
ABresult
0X0
100
111
159 0 $self->forward;
160 0 last;
161 }
162 else {
163 0 $regex_body .= $self->current;
164 0 $self->forward;
165 }
166 }
167  
168 # Check for flags.
169 # The possible flags are according to https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
170 0 my @POSSIBLE_FLAGS = ( 'g', 'i', 'm', 'y' );
171 0 my $regex_flags = '';
172 0 ▶ while ( !$self->is_finished ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->is_finished) 267 0 267
unless ($self->is_finished) 0 0 0
173 0 my $is_current_a_flag =
174 Text::Nunjucks::Lib->index_of( \@POSSIBLE_FLAGS, $self->current ) != -1;
175 0 ▶ if ($is_current_a_flag) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_current_a_flag) { } 101 166 267
if ($is_current_a_flag) { } 0 0 0
176 0 $regex_flags .= $self->current;
177 0 $self->forward;
178 }
179 else {
180 0 last;
181 }
182 }
183  
184 0 return $self->token(
185 $self->lexer->TOKEN_REGEX,
186 { body => $regex_body, flags => $regex_flags },
187 $lineno, $colno
188 );
189 }
190 elsif ( CORE::index( $delim_chars, $cur ) != -1 ) {
191 # We've hit a delimiter (a special char like a bracket)
192 0 $self->forward;
193 0 my @complex_ops = ( '==', '===', '!=', '!==', '<=', '>=', '//', '**' );
194 0 my $cur_complex = $cur . $self->current;
195 0 my $type;
196  
197 0 ▶ if ( Text::Nunjucks::Lib->index_of( \@complex_ops, $cur_complex ) != -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->index_of(\@complex_ops, $cur_complex) != -1) 195 53321 53516
if ("Text::Nunjucks::Lib"->index_of(\@complex_ops, $cur_complex) != -1) 0 0 0
198 0 $self->forward;
199 0 $cur = $cur_complex;
200  
201 # See if this is a strict equality/inequality comparator
202 0 ▶ if ( Text::Nunjucks::Lib->index_of( \@complex_ops, $cur_complex . $self->current ) != -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->index_of(\@complex_ops, $cur_complex . $self->current) != -1) 18 177 195
if ("Text::Nunjucks::Lib"->index_of(\@complex_ops, $cur_complex . $self->current) != -1) 0 0 0
203 0 $cur = $cur_complex . $self->current;
204 0 $self->forward;
205 }
206 }
207  
208 0 ▶ if ( $cur eq '(' ) { $type = $self->lexer->TOKEN_LEFT_PAREN; }
Branch: 20 branches
BranchTrueFalseTotal
if ($cur eq '(') { } 9025 44491 53516
elsif ($cur eq ')') { } 9021 35470 44491
elsif ($cur eq '[') { } 2228 33242 35470
elsif ($cur eq ']') { } 2227 31015 33242
elsif ($cur eq '{') { } 801 30214 31015
elsif ($cur eq '}') { } 801 29413 30214
elsif ($cur eq ',') { } 7309 22104 29413
elsif ($cur eq ':') { } 1162 20942 22104
elsif ($cur eq '~') { } 139 20803 20942
elsif ($cur eq '|') { } 10467 10336 20803
if ($cur eq '(') { } 0 0 0
elsif ($cur eq ')') { } 0 0 0
elsif ($cur eq '[') { } 0 0 0
elsif ($cur eq ']') { } 0 0 0
elsif ($cur eq '{') { } 0 0 0
elsif ($cur eq '}') { } 0 0 0
elsif ($cur eq ',') { } 0 0 0
elsif ($cur eq ':') { } 0 0 0
elsif ($cur eq '~') { } 0 0 0
elsif ($cur eq '|') { } 0 0 0
209 0 elsif ( $cur eq ')' ) { $type = $self->lexer->TOKEN_RIGHT_PAREN; }
210 0 elsif ( $cur eq '[' ) { $type = $self->lexer->TOKEN_LEFT_BRACKET; }
211 0 elsif ( $cur eq ']' ) { $type = $self->lexer->TOKEN_RIGHT_BRACKET; }
212 0 elsif ( $cur eq '{' ) { $type = $self->lexer->TOKEN_LEFT_CURLY; }
213 0 elsif ( $cur eq '}' ) { $type = $self->lexer->TOKEN_RIGHT_CURLY; }
214 0 elsif ( $cur eq ',' ) { $type = $self->lexer->TOKEN_COMMA; }
215 0 elsif ( $cur eq ':' ) { $type = $self->lexer->TOKEN_COLON; }
216 0 elsif ( $cur eq '~' ) { $type = $self->lexer->TOKEN_TILDE; }
217 0 elsif ( $cur eq '|' ) { $type = $self->lexer->TOKEN_PIPE; }
218 0 else { $type = $self->lexer->TOKEN_OPERATOR; }
219  
220 0 return $self->token( $type, $cur, $lineno, $colno );
221 }
222 else {
223 # We are not at whitespace or a delimiter, so extract the
224 # text and parse it
225 0 $tok = $self->_extract_until( $whitespace_chars . $delim_chars );
226  
227 0 ▶ if ( $tok =~ /^[-+]?[0-9]+$/ ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($tok =~ /^[-+]?[0-9]+$/) { } 14352 64068 78420
elsif ($tok =~ /^(true|false)$/) { } 826 63242 64068
elsif ($tok eq 'none') { } 39 63203 63242
elsif ($tok eq 'null') { } 198 63005 63203
elsif (length $tok) { } 63005 0 63005
if ($tok =~ /^[-+]?[0-9]+$/) { } 0 0 0
elsif ($tok =~ /^(true|false)$/) { } 0 0 0
elsif ($tok eq 'none') { } 0 0 0
elsif ($tok eq 'null') { } 0 0 0
elsif (length $tok) { } 0 0 0
228 0 ▶ if ( $self->current eq '.' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->current eq '.') { } 359 13993 14352
if ($self->current eq '.') { } 0 0 0
229 0 $self->forward;
230 0 my $dec = $self->_extract($int_chars);
231 # At EOF _extract returns undef, JS's _extract returns null, and
232 # `tok + '.' + null` stringifies to "<tok>.null" ({{3. -> 3.null).
233 # Raw JS String(null) concat here, not the k2 null render (empty).
234 0 ▶ $dec = 'null' unless defined $dec;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $dec 2 357 359
unless defined $dec 0 0 0
235 0 return $self->token( $self->lexer->TOKEN_FLOAT, $tok . '.' . $dec, $lineno, $colno );
236 }
237 else {
238 0 return $self->token( $self->lexer->TOKEN_INT, $tok, $lineno, $colno );
239 }
240 }
241 elsif ( $tok =~ /^(true|false)$/ ) {
242 0 return $self->token( $self->lexer->TOKEN_BOOLEAN, $tok, $lineno, $colno );
243 }
244 elsif ( $tok eq 'none' ) {
245 0 return $self->token( $self->lexer->TOKEN_NONE, $tok, $lineno, $colno );
246 }
247 #
248 # Added to make the test `null is null` evaluate truthily.
249 # Otherwise, Nunjucks will look up null in the context and
250 # return `undefined`, which is not what we want. This *may* have
251 # consequences is someone is using null in their templates as a
252 # variable.
253 #
254 elsif ( $tok eq 'null' ) {
255 0 return $self->token( $self->lexer->TOKEN_NONE, $tok, $lineno, $colno );
256 }
257 elsif ( length $tok ) {
258 # JS `else if (tok)`: a non-empty string. length() not truthiness, so
259 # the (numeric, already handled) case of tok eq '0' cannot mislead.
260 0 return $self->token( $self->lexer->TOKEN_SYMBOL, $tok, $lineno, $colno );
261 }
262 else {
263 0 die Text::Nunjucks::Lib::Error->new( 'Unexpected value while parsing: ' . $tok );
264 }
265 }
266 }
267 else {
268 # Parse out the template text, breaking on tag
269 # delimiters because we need to look for block/variable start
270 # tags (don't use the full delimChars for optimization)
271 my $begin_chars =
272 substr( $self->tags->{BLOCK_START}, 0, 1 )
273 . substr( $self->tags->{VARIABLE_START}, 0, 1 )
274 . substr( $self->tags->{COMMENT_START}, 0, 1 )
275 0 . substr( $self->tags->{COMMENT_END}, 0, 1 );
276  
277 0 ▶ if ( $self->is_finished ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($self->is_finished) { } 13340 47334 60674
elsif ($tok = $self->_extract_string($self->tags->{'BLOCK_START'} . '-') or $tok = $self->_extract_string($self->tags->{'BLOCK_START'})) { } 20077 27257 47334
elsif ($tok = $self->_extract_string($self->tags->{'VARIABLE_START'} . '-') or $tok = $self->_extract_string($self->tags->{'VARIABLE_START'})) { } 12478 14779 27257
if ($self->is_finished) { } 0 0 0
elsif ($tok = $self->_extract_string($self->tags->{'BLOCK_START'} . '-') or $tok = $self->_extract_string($self->tags->{'BLOCK_START'})) { } 0 0 0
elsif ($tok = $self->_extract_string($self->tags->{'VARIABLE_START'} . '-') or $tok = $self->_extract_string($self->tags->{'VARIABLE_START'})) { } 0 0 0
278 0 return undef;
279 }
280 ▶ elsif ( ( $tok = $self->_extract_string( $self->tags->{BLOCK_START} . '-' ) )
Condition50%3/6
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') or $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
l!l&&r!l&&!r
242005327257
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') or $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
l!l&&r!l&&!r
000
MC/DC100%2/2
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') or $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
MC/DC0%0/2
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') or $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
$tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-') $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
Truth table100%3/3
A or B
A= $tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-')B= $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $tok = $self->_extract_string($self->tags->{"BLOCK_START"} . '-')B= $tok = $self->_extract_string($self->tags->{"BLOCK_START"})
ABresult
1X1
011
000
281 || ( $tok = $self->_extract_string( $self->tags->{BLOCK_START} ) ) )
282 {
283 0 $self->in_code(1);
284 0 return $self->token( $self->lexer->TOKEN_BLOCK_START, $tok, $lineno, $colno );
285 }
286 ▶ elsif ( ( $tok = $self->_extract_string( $self->tags->{VARIABLE_START} . '-' ) )
Condition50%3/6
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') or $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
l!l&&r!l&&!r
51247314779
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') or $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
l!l&&r!l&&!r
000
MC/DC100%2/2
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') or $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
MC/DC0%0/2
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') or $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
$tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-') $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
Truth table100%3/3
A or B
A= $tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-')B= $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $tok = $self->_extract_string($self->tags->{"VARIABLE_START"} . '-')B= $tok = $self->_extract_string($self->tags->{"VARIABLE_START"})
ABresult
1X1
011
000
287 || ( $tok = $self->_extract_string( $self->tags->{VARIABLE_START} ) ) )
288 {
289 0 $self->in_code(1);
290 0 return $self->token( $self->lexer->TOKEN_VARIABLE_START, $tok, $lineno, $colno );
291 }
292 else {
293 0 $tok = '';
294 0 my $data;
295 0 my $in_comment = 0;
296  
297 0 ▶ if ( $self->_matches( $self->tags->{COMMENT_START} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_matches($self->tags->{"COMMENT_START"})) 23 14756 14779
if ($self->_matches($self->tags->{"COMMENT_START"})) 0 0 0
298 0 $in_comment = 1;
299 0 $tok = $self->_extract_string( $self->tags->{COMMENT_START} );
300 }
301  
302 # Continually consume text, breaking on the tag delimiter
303 # characters and checking to see if it's a start tag.
304 #
305 # We could hit the end of the template in the middle of
306 # our looping, so check for the null return value from
307 # _extractUntil
308 0 ▶ while ( defined( $data = $self->_extract_until($begin_chars) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($data = $self->_extract_until($begin_chars))) 14786 3703 18489
if (defined($data = $self->_extract_until($begin_chars))) 0 0 0
309 0 $tok .= $data;
310  
311 0 ▶ if ( ( $self->_matches( $self->tags->{BLOCK_START} )
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_matches($self->tags->{'BLOCK_START'}) || $self->_matches($self->tags->{'VARIABLE_START'}) || $self->_matches($self->tags->{'COMMENT_START'}) and not $in_comment) { } 11053 3733 14786
elsif ($self->_matches($self->tags->{'COMMENT_END'})) { } 23 3710 3733
if ($self->_matches($self->tags->{'BLOCK_START'}) || $self->_matches($self->tags->{'VARIABLE_START'}) || $self->_matches($self->tags->{'COMMENT_START'}) and not $in_comment) { } 0 0 0
elsif ($self->_matches($self->tags->{'COMMENT_END'})) { } 0 0 0
Condition44%8/18
$self->_matches($self->tags->{"BLOCK_START"}) || $self->_matches($self->tags->{"VARIABLE_START"}) || $self->_matches($self->tags->{"COMMENT_START"}) and not $in_comment
!ll&&!rl&&r
3733011053
$self->_matches($self->tags->{"BLOCK_START"}) or $self->_matches($self->tags->{"VARIABLE_START"}) or $self->_matches($self->tags->{"COMMENT_START"})
l!l&&r!l&&!r
11042113733
$self->_matches($self->tags->{"BLOCK_START"}) or $self->_matches($self->tags->{"VARIABLE_START"})
l!l&&r!l&&!r
371973233744
$self->_matches($self->tags->{"BLOCK_START"}) || $self->_matches($self->tags->{"VARIABLE_START"}) || $self->_matches($self->tags->{"COMMENT_START"}) and not $in_comment
!ll&&!rl&&r
000
$self->_matches($self->tags->{"BLOCK_START"}) or $self->_matches($self->tags->{"VARIABLE_START"}) or $self->_matches($self->tags->{"COMMENT_START"})
l!l&&r!l&&!r
000
$self->_matches($self->tags->{"BLOCK_START"}) or $self->_matches($self->tags->{"VARIABLE_START"})
l!l&&r!l&&!r
000
MC/DC75%3/4
$self->_matches($self->tags->{"BLOCK_START"}) || $self->_matches($self->tags->{"VARIABLE_START"}) || $self->_matches($self->tags->{"COMMENT_START"}) and not $in_comment
$self->_matches($self->tags->{"BLOCK_START"}) $self->_matches($self->tags->{"VARIABLE_START"}) $self->_matches($self->tags->{"COMMENT_START"}) not $in_comment
MC/DC0%0/4
$self->_matches($self->tags->{"BLOCK_START"}) || $self->_matches($self->tags->{"VARIABLE_START"}) || $self->_matches($self->tags->{"COMMENT_START"}) and not $in_comment
$self->_matches($self->tags->{"BLOCK_START"}) $self->_matches($self->tags->{"VARIABLE_START"}) $self->_matches($self->tags->{"COMMENT_START"}) not $in_comment
Truth table57%4/7
A or B or C and not(D)
A= $self->_matches($self->tags->{"BLOCK_START"})B= $self->_matches($self->tags->{"VARIABLE_START"})C= $self->_matches($self->tags->{"COMMENT_START"})D= not $in_comment
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
Truth table0%0/7
A or B or C and not(D)
A= $self->_matches($self->tags->{"BLOCK_START"})B= $self->_matches($self->tags->{"VARIABLE_START"})C= $self->_matches($self->tags->{"COMMENT_START"})D= not $in_comment
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
312 || $self->_matches( $self->tags->{VARIABLE_START} )
313 || $self->_matches( $self->tags->{COMMENT_START} ) )
314 && !$in_comment )
315 {
316 0 ▶ if ( $self->lstripBlocks
Branch: 2 branches
BranchTrueFalseTotal
if ($self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok) 12 11041 11053
if ($self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok) 0 0 0
Condition38%7/18
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok
!ll&&!rl&&r
11039212
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0
!ll&&!rl&&r
11039014
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"})
!ll&&!rl&&r
11039014
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok
!ll&&!rl&&r
000
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0
!ll&&!rl&&r
000
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"})
!ll&&!rl&&r
000
MC/DC50%2/4
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok
$self->lstripBlocks $self->_matches($self->tags->{"BLOCK_START"}) $self->colno > 0 $self->colno <= length $tok
MC/DC0%0/4
$self->lstripBlocks and $self->_matches($self->tags->{"BLOCK_START"}) and $self->colno > 0 and $self->colno <= length $tok
$self->lstripBlocks $self->_matches($self->tags->{"BLOCK_START"}) $self->colno > 0 $self->colno <= length $tok
Truth table60%3/5
A and B and C and D
A= $self->lstripBlocksB= $self->_matches($self->tags->{"BLOCK_START"})C= $self->colno > 0D= $self->colno <= length $tok
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= $self->lstripBlocksB= $self->_matches($self->tags->{"BLOCK_START"})C= $self->colno > 0D= $self->colno <= length $tok
ABCDresult
0XXX0
10XX0
110X0
11100
11111
317 && $self->_matches( $self->tags->{BLOCK_START} )
318 && $self->colno > 0
319 && $self->colno <= length($tok) )
320 {
321 0 my $last_line = substr( $tok, -$self->colno );
322 # JS \s, not Perl \s: NBSP/FEFF are members and the match must not
323 # depend on the SV's UTF-8 flag (ADR 0005). The explicit class
324 # carries /u; anchors stay ^..+..$ as upstream.
325 0 my $ws = Text::Nunjucks::Lib->js_whitespace_class;
326 0 ▶ if ( $last_line =~ /^$ws+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last_line =~ /^$ws+$/) 11 1 12
if ($last_line =~ /^$ws+$/) 0 0 0
327 # Remove block leading whitespace from beginning of the string
328 0 $tok = substr( $tok, 0, -$self->colno );
329 0 ▶ unless ( length $tok ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (length $tok) 2 9 11
unless (length $tok) 0 0 0
330 # All data removed, collapse to avoid unnecessary nodes
331 # by returning next token (block start)
332 0 return $self->next_token;
333 }
334 }
335 }
336 # If it is a start tag, stop looping
337 0 last;
338 }
339 elsif ( $self->_matches( $self->tags->{COMMENT_END} ) ) {
340 0 ▶ if ( !$in_comment ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($in_comment) 0 23 23
unless ($in_comment) 0 0 0
341 0 die Text::Nunjucks::Lib::Error->new('unexpected end of comment');
342 }
343 0 $tok .= $self->_extract_string( $self->tags->{COMMENT_END} );
344 0 last;
345 }
346 else {
347 # It does not match any tag, so add the character and
348 # carry on
349 0 $tok .= $self->current;
350 0 $self->forward;
351 }
352 }
353  
354 0 ▶ if ( !defined($data) && $in_comment ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $data and $in_comment) 0 14777 14777
if (not defined $data and $in_comment) 0 0 0
Condition33%2/6
not defined $data and $in_comment
!ll&&!rl&&r
1107437030
not defined $data and $in_comment
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and $in_comment
not defined $data $in_comment
MC/DC0%0/2
not defined $data and $in_comment
not defined $data $in_comment
Truth table66%2/3
not(A) and B
A= not defined $dataB= $in_comment
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $dataB= $in_comment
ABresult
0X0
100
111
355 0 die Text::Nunjucks::Lib::Error->new('expected end of comment, got end of file');
356 }
357  
358 0 ▶ return $self->token(
Branch: 2 branches
BranchTrueFalseTotal
$in_comment ? : 23 14754 14777
$in_comment ? : 0 0 0
359 ( $in_comment ? $self->lexer->TOKEN_COMMENT : $self->lexer->TOKEN_DATA ),
360 $tok, $lineno, $colno
361 );
362 }
363 }
364 }
365  
366 9882 ▶sub _parse_string {
Subroutine _parse_string: called CC=7 SCAR=25.7
367 0 ▶ my ( $self, $delimiter ) = @_;
Subroutine _parse_string: not called CC=7 SCAR=25.7
368 0 $self->forward;
369  
370 0 my $str = '';
371  
372 0 ▶ while ( !$self->is_finished && $self->current ne $delimiter ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->is_finished and $self->current ne $delimiter) 51707 9882 61589
if (not $self->is_finished and $self->current ne $delimiter) 0 0 0
Condition50%3/6
not $self->is_finished and $self->current ne $delimiter
!ll&&!rl&&r
2988051707
not $self->is_finished and $self->current ne $delimiter
!ll&&!rl&&r
000
MC/DC100%2/2
not $self->is_finished and $self->current ne $delimiter
not $self->is_finished $self->current ne $delimiter
MC/DC0%0/2
not $self->is_finished and $self->current ne $delimiter
not $self->is_finished $self->current ne $delimiter
Truth table100%3/3
not(A) and B
A= not $self->is_finishedB= $self->current ne $delimiter
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $self->is_finishedB= $self->current ne $delimiter
ABresult
0X0
100
111
373 0 my $cur = $self->current;
374  
375 0 ▶ if ( $cur eq '\\' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur eq '\\') { } 17 51690 51707
if ($cur eq '\\') { } 0 0 0
376 0 $self->forward;
377 0 my $c = $self->current;
378 0 ▶ if ( $c eq 'n' ) { $str .= "\n"; }
Branch: 6 branches
BranchTrueFalseTotal
if ($c eq 'n') { } 3 14 17
elsif ($c eq 't') { } 4 10 14
elsif ($c eq 'r') { } 2 8 10
if ($c eq 'n') { } 0 0 0
elsif ($c eq 't') { } 0 0 0
elsif ($c eq 'r') { } 0 0 0
379 0 elsif ( $c eq 't' ) { $str .= "\t"; }
380 0 elsif ( $c eq 'r' ) { $str .= "\r"; }
381 0 else { $str .= $c; }
382 0 $self->forward;
383 }
384 else {
385 0 $str .= $cur;
386 0 $self->forward;
387 }
388 }
389  
390 0 $self->forward;
391 0 return $str;
392 }
393  
394 783210 ▶sub _matches {
Subroutine _matches: called CC=3 SCAR=14.2
395 0 ▶ my ( $self, $str ) = @_;
Subroutine _matches: not called CC=3 SCAR=14.2
396 0 ▶ if ( $self->index + length($str) > $self->len ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->index + length($str) > $self->len) 36495 746715 783210
if ($self->index + length($str) > $self->len) 0 0 0
397 0 return undef;
398 }
399  
400 0 my $m = substr( $self->str, $self->index, length($str) );
401 0 ▶ return $m eq $str ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$m eq $str ? : 76237 670478 746715
$m eq $str ? : 0 0 0
402 }
403  
404 735087 ▶sub _extract_string {
Subroutine _extract_string: called CC=2 SCAR=9.2
405 0 ▶ my ( $self, $str ) = @_;
Subroutine _extract_string: not called CC=2 SCAR=9.2
406 0 ▶ if ( $self->_matches($str) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_matches($str)) 65124 669963 735087
if ($self->_matches($str)) 0 0 0
407 0 $self->forward_n( length $str );
408 0 return $str;
409 }
410 0 return undef;
411 }
412  
413 96909 ▶sub _extract_until {
Subroutine _extract_until: called CC=2 SCAR=10.9
414 0 ▶ my ( $self, $char_string ) = @_;
Subroutine _extract_until: not called CC=2 SCAR=10.9
415 # Extract all non-matching chars, with the default matching set
416 # to everything
417 0 ▶ return $self->_extract_matching( 1, ( $char_string // '' ) );
Condition25%1/4
$char_string // ""
l!l
969090
$char_string // ""
l!l
00
MC/DC0%0/1
$char_string // ""
$char_string
MC/DC0%0/1
$char_string // ""
$char_string
Truth table50%1/2
A
A= $char_string
Aresult
11
00
Truth table0%0/2
A
A= $char_string
Aresult
11
00
418 }
419  
420 270409 ▶sub _extract {
Subroutine _extract: called CC=1 SCAR=1.2
421 0 ▶ my ( $self, $char_string ) = @_;
Subroutine _extract: not called CC=1 SCAR=1.2
422 # Extract all matching chars (no default, so charString must be
423 # explicit)
424 0 return $self->_extract_matching( 0, $char_string );
425 }
426  
427 367318 ▶sub _extract_matching {
Subroutine _extract_matching: called CC=11 SCAR=33.1
428 0 ▶ my ( $self, $break_on_match, $char_string ) = @_;
Subroutine _extract_matching: not called CC=11 SCAR=33.1
429 # Pull out characters until a breaking char is hit.
430 # If breakOnMatch is false, a non-matching char stops it.
431 # If breakOnMatch is true, a matching char stops it.
432  
433 0 ▶ if ( $self->is_finished ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_finished) 3705 363613 367318
if ($self->is_finished) 0 0 0
434 0 return undef;
435 }
436  
437 0 my $first = CORE::index( $char_string, $self->current );
438  
439 # Only proceed if the first character doesn't meet our condition
440 0 ▶ if ( ( $break_on_match && $first == -1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($break_on_match and $first == -1 or not $break_on_match and $first != -1) 198982 164631 363613
if ($break_on_match and $first == -1 or not $break_on_match and $first != -1) 0 0 0
Condition50%9/18
$break_on_match and $first == -1 or not $break_on_match and $first != -1
l!l&&r!l&&!r
93200105782164631
$break_on_match and $first == -1
!ll&&!rl&&r
270407693200
not $break_on_match and $first != -1
!ll&&!rl&&r
6164625105782
$break_on_match and $first == -1 or not $break_on_match and $first != -1
l!l&&r!l&&!r
000
$break_on_match and $first == -1
!ll&&!rl&&r
000
not $break_on_match and $first != -1
!ll&&!rl&&r
000
MC/DC75%3/4
$break_on_match and $first == -1 or not $break_on_match and $first != -1
$break_on_match $first == -1 not $break_on_match $first != -1
MC/DC0%0/4
$break_on_match and $first == -1 or not $break_on_match and $first != -1
$break_on_match $first == -1 not $break_on_match $first != -1
Truth table57%4/7
A and B or not(C) and D
A= $break_on_matchB= $first == -1C= not $break_on_matchD= $first != -1
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
Truth table0%0/7
A and B or not(C) and D
A= $break_on_matchB= $first == -1C= not $break_on_matchD= $first != -1
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
441 || ( !$break_on_match && $first != -1 ) )
442 {
443 0 my $t = $self->current;
444 0 $self->forward;
445  
446 # And pull out all the chars one at a time until we hit a
447 # breaking char
448 0 my $idx = CORE::index( $char_string, $self->current );
449  
450 0 ▶ while ( ( ( $break_on_match && $idx == -1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished) 169301 198982 368283
if ($break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished) 0 0 0
Condition45%11/24
$break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished
!ll&&!rl&&r
1989820169301
$break_on_match and $idx == -1 or not $break_on_match and $idx != -1
l!l&&r!l&&!r
169119182198982
$break_on_match and $idx == -1
!ll&&!rl&&r
10596493200169119
not $break_on_match and $idx != -1
!ll&&!rl&&r
93200105782182
$break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished
!ll&&!rl&&r
000
$break_on_match and $idx == -1 or not $break_on_match and $idx != -1
l!l&&r!l&&!r
000
$break_on_match and $idx == -1
!ll&&!rl&&r
000
not $break_on_match and $idx != -1
!ll&&!rl&&r
000
MC/DC60%3/5
$break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished
$break_on_match $idx == -1 not $break_on_match $idx != -1 not $self->is_finished
MC/DC0%0/5
$break_on_match && $idx == -1 || !$break_on_match && $idx != -1 and not $self->is_finished
$break_on_match $idx == -1 not $break_on_match $idx != -1 not $self->is_finished
Truth table40%4/10
A and B or not(C) and D and not(E)
A= $break_on_matchB= $idx == -1C= not $break_on_matchD= $idx != -1E= not $self->is_finished
ABCDEresult
0X0XX0
0X10X0
100XX0
1010X0
11XX00
0X1100
101100
11XX11
0X1111
101111
Truth table0%0/10
A and B or not(C) and D and not(E)
A= $break_on_matchB= $idx == -1C= not $break_on_matchD= $idx != -1E= not $self->is_finished
ABCDEresult
0X0XX0
0X10X0
100XX0
1010X0
11XX00
0X1100
101100
11XX11
0X1111
101111
451 || ( !$break_on_match && $idx != -1 ) )
452 && !$self->is_finished )
453 {
454 0 $t .= $self->current;
455 0 $self->forward;
456  
457 0 $idx = CORE::index( $char_string, $self->current );
458 }
459  
460 0 return $t;
461 }
462  
463 0 return '';
464 }
465  
466 78 ▶sub _extract_regex {
Subroutine _extract_regex: called CC=2 SCAR=10.1
467 0 ▶ my ( $self, $regex ) = @_;
Subroutine _extract_regex: not called CC=2 SCAR=10.1
468 # NOTE: unused in the lexer, exactly as upstream -- _extractRegex and its
469 # only caller currentStr are dead code in lexer.js, kept here for a 1:1
470 # image of the file. JS String.match(regex) returns the match array whose
471 # [0] is the whole match; modelled with $& plus captures.
472 0 ▶ return undef unless $self->current_str =~ /$regex/;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->current_str =~ /$regex/ 0 78 78
unless $self->current_str =~ /$regex/ 0 0 0
473 0 my @matches = ( $&, @{^CAPTURE} );
474  
475 # Move forward whatever was matched
476 0 $self->forward_n( length $matches[0] );
477  
478 0 return \@matches;
479 }
480  
481 2507273 ▶sub is_finished {
Subroutine is_finished: called CC=1 SCAR=1.2
Pod undocumented
482 0 ▶ my ($self) = @_;
Subroutine is_finished: not called CC=1 SCAR=1.2
Pod undocumented
483 0 return $self->index >= $self->len;
484 }
485  
486 65368 ▶sub forward_n {
Subroutine forward_n: called CC=2 SCAR=9.2
Pod undocumented
487 0 ▶ my ( $self, $n ) = @_;
Subroutine forward_n: not called CC=2 SCAR=9.2
Pod undocumented
488 0 ▶ for ( my $i = 0; $i < $n; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $n) 132143 65368 197511
if ($i < $n) 0 0 0
489 0 $self->forward;
490 }
491 }
492  
493 630491 ▶sub forward {
Subroutine forward: called CC=2 SCAR=9.2
Pod undocumented
494 0 ▶ my ($self) = @_;
Subroutine forward: not called CC=2 SCAR=9.2
Pod undocumented
495 0 $self->index( $self->index + 1 );
496  
497 0 ▶ if ( $self->previous eq "\n" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->previous eq "\n") { } 464 630027 630491
if ($self->previous eq "\n") { } 0 0 0
498 0 $self->lineno( $self->lineno + 1 );
499 0 $self->colno(0);
500 }
501 else {
502 0 $self->colno( $self->colno + 1 );
503 }
504 }
505  
506 35 ▶sub back_n {
Subroutine back_n: called CC=2 SCAR=9.2
Pod undocumented
507 0 ▶ my ( $self, $n ) = @_;
Subroutine back_n: not called CC=2 SCAR=9.2
Pod undocumented
508 0 ▶ for ( my $i = 0; $i < $n; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $n) 485 35 520
if ($i < $n) 0 0 0
509 0 $self->back;
510 }
511 }
512  
513 489 ▶sub back {
Subroutine back: called CC=3 SCAR=20.5
Pod undocumented
514 0 ▶ my ($self) = @_;
Subroutine back: not called CC=3 SCAR=20.5
Pod undocumented
515 0 $self->index( $self->index - 1 );
516  
517 0 ▶ if ( $self->current eq "\n" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->current eq "\n") { } 0 489 489
if ($self->current eq "\n") { } 0 0 0
518 0 $self->lineno( $self->lineno - 1 );
519  
520 # NOTE: upstream typo -- `this.src` is undefined (the field is `this.str`).
521 # Unreachable: back() is only ever called after a '\r' (the trimBlocks
522 # CRLF path), so current() here is '\r', never '\n'. Preserved 1:1 by
523 # reading the same nonexistent field; see the k10 report.
524 0 my $idx = rindex( $self->{src}, "\n", $self->index - 1 );
525 0 ▶ if ( $idx == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx == -1) { } 0 0 0
if ($idx == -1) { } 0 0 0
526 0 $self->colno( $self->index );
527 }
528 else {
529 0 $self->colno( $self->index - $idx );
530 }
531 }
532 else {
533 0 $self->colno( $self->colno - 1 );
534 }
535 }
536  
537 # current returns current character
538 1567351 ▶sub current {
Subroutine current: called CC=2 SCAR=9.2
Pod undocumented
539 0 ▶ my ($self) = @_;
Subroutine current: not called CC=2 SCAR=9.2
Pod undocumented
540 0 ▶ if ( !$self->is_finished ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->is_finished) 1559827 7524 1567351
unless ($self->is_finished) 0 0 0
541 0 return $self->_char_at( $self->str, $self->index );
542 }
543 0 return '';
544 }
545  
546 # currentStr returns what's left of the unparsed string
547 78 ▶sub current_str {
Subroutine current_str: called CC=2 SCAR=11.6
Pod undocumented
548 0 ▶ my ($self) = @_;
Subroutine current_str: not called CC=2 SCAR=11.6
Pod undocumented
549 0 ▶ if ( !$self->is_finished ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->is_finished) 78 0 78
unless ($self->is_finished) 0 0 0
550 0 return substr( $self->str, $self->index );
551 }
552 0 return '';
553 }
554  
555 630663 ▶sub previous {
Subroutine previous: called CC=1 SCAR=1.2
Pod undocumented
556 0 ▶ my ($self) = @_;
Subroutine previous: not called CC=1 SCAR=1.2
Pod undocumented
557 0 return $self->_char_at( $self->str, $self->index - 1 );
558 }
559  
560 # JS String.prototype.charAt: '' for an index outside [0, length). Perl's
561 # substr differs there (negative offsets wrap, past-end warns), so charAt is
562 # kept as a helper rather than an inline substr. Upstream calls this.str.charAt
563 # directly in current(), previous() and the regex look-ahead.
564 2192446 ▶sub _char_at {
Subroutine _char_at: called CC=3 SCAR=15.3
565 0 ▶ my ( $self, $str, $i ) = @_;
Subroutine _char_at: not called CC=3 SCAR=15.3
566 0 ▶ return '' if $i < 0 || $i >= length($str);
Branch: 2 branches
BranchTrueFalseTotal
if $i < 0 or $i >= length $str 3702 2188744 2192446
if $i < 0 or $i >= length $str 0 0 0
Condition33%2/6
$i < 0 or $i >= length $str
l!l&&r!l&&!r
037022188744
$i < 0 or $i >= length $str
l!l&&r!l&&!r
000
MC/DC50%1/2
$i < 0 or $i >= length $str
$i < 0 $i >= length $str
MC/DC0%0/2
$i < 0 or $i >= length $str
$i < 0 $i >= length $str
Truth table66%2/3
A or B
A= $i < 0B= $i >= length $str
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $i < 0B= $i >= length $str
ABresult
1X1
011
000
567 0 return substr( $str, $i, 1 );
568 }
569  
570 21 1;
571  
572 __END__
↑ Summary blib/lib/Text/Nunjucks/Nodes.pm »