blib/lib/Text/Nunjucks/Nodes.pm

filter: Statementstmt 73.8% 186 / 252 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 91.0% 61 / 67 Podpod 0.0% 0 / 2 totaltotal 60.9% 247 / 405 CC 39 SCAR 35.3
CC
39
Cov
58%
print_nodes
110.0
print_nodes
110.0
_print
72.0
CRAP 34.0
« blib/lib/Text/Nunjucks/Lexer/Tokenizer.pm ↑ Summary blib/lib/Text/Nunjucks/Parser/Parser.pm »
1 package Text::Nunjucks::Nodes;
2 # ABSTRACT: AST node types for Text::Nunjucks, ported 1:1 from nodes.js
3  
4 20 our $VERSION = '0.001';
5  
6 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # 1:1 port of upstream src/nodes.js. object.js has no Perl counterpart
12 # (ADR 0002): Moo is the object system. Every node class nodes.js declares --
13 # the base classes it writes with `class` (Node, Value, NodeList), the two with
14 # a custom init (FromImport, CallExtension) and every `X.extend('Name',
15 # {fields})` line -- is a hand-written Moo class in its own file under
16 # Text/Nunjucks/Nodes/ (ADR 0020), carrying the nodes.js line it ports. This
17 # module keeps nodes.js's module-level functions (print, printNodes) and loads
18 # every node class, in nodes.js declaration order, so `use Text::Nunjucks::Nodes`
19 # makes all of them available.
20 #
21 # nodes.js requires only Obj from object.js, not EmitterObj, so no on/emit role
22 # is needed here.
23  
24 20 ▶use Text::Nunjucks::Nodes::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 20 ▶use Text::Nunjucks::Nodes::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 20 ▶use Text::Nunjucks::Nodes::NodeList;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 20 ▶use Text::Nunjucks::Nodes::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 20 ▶use Text::Nunjucks::Nodes::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 20 ▶use Text::Nunjucks::Nodes::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 20 ▶use Text::Nunjucks::Nodes::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 20 ▶use Text::Nunjucks::Nodes::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 20 ▶use Text::Nunjucks::Nodes::Pair;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 20 ▶use Text::Nunjucks::Nodes::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 20 ▶use Text::Nunjucks::Nodes::LookupVal;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 20 ▶use Text::Nunjucks::Nodes::If;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 20 ▶use Text::Nunjucks::Nodes::IfAsync;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 20 ▶use Text::Nunjucks::Nodes::InlineIf;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 20 ▶use Text::Nunjucks::Nodes::For;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 20 ▶use Text::Nunjucks::Nodes::AsyncEach;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 20 ▶use Text::Nunjucks::Nodes::AsyncAll;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 20 ▶use Text::Nunjucks::Nodes::Macro;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 20 ▶use Text::Nunjucks::Nodes::Caller;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 20 ▶use Text::Nunjucks::Nodes::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 20 ▶use Text::Nunjucks::Nodes::FromImport;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 20 ▶use Text::Nunjucks::Nodes::FunCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 20 ▶use Text::Nunjucks::Nodes::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 20 ▶use Text::Nunjucks::Nodes::FilterAsync;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 20 ▶use Text::Nunjucks::Nodes::KeywordArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 20 ▶use Text::Nunjucks::Nodes::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 20 ▶use Text::Nunjucks::Nodes::Super;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 20 ▶use Text::Nunjucks::Nodes::TemplateRef;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 20 ▶use Text::Nunjucks::Nodes::Extends;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 20 ▶use Text::Nunjucks::Nodes::Include;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 20 ▶use Text::Nunjucks::Nodes::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 20 ▶use Text::Nunjucks::Nodes::Switch;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 20 ▶use Text::Nunjucks::Nodes::Case;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 20 ▶use Text::Nunjucks::Nodes::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 20 ▶use Text::Nunjucks::Nodes::Capture;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 20 ▶use Text::Nunjucks::Nodes::TemplateData;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 20 ▶use Text::Nunjucks::Nodes::UnaryOp;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 20 ▶use Text::Nunjucks::Nodes::BinOp;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 20 ▶use Text::Nunjucks::Nodes::In;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 20 ▶use Text::Nunjucks::Nodes::Is;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 20 ▶use Text::Nunjucks::Nodes::Or;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 20 ▶use Text::Nunjucks::Nodes::And;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 20 ▶use Text::Nunjucks::Nodes::Not;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 20 ▶use Text::Nunjucks::Nodes::Add;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 20 ▶use Text::Nunjucks::Nodes::Concat;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 20 ▶use Text::Nunjucks::Nodes::Sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 20 ▶use Text::Nunjucks::Nodes::Mul;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 20 ▶use Text::Nunjucks::Nodes::Div;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 20 ▶use Text::Nunjucks::Nodes::FloorDiv;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 20 ▶use Text::Nunjucks::Nodes::Mod;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 20 ▶use Text::Nunjucks::Nodes::Pow;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 20 ▶use Text::Nunjucks::Nodes::Neg;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 20 ▶use Text::Nunjucks::Nodes::Pos;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 20 ▶use Text::Nunjucks::Nodes::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 20 ▶use Text::Nunjucks::Nodes::CompareOperand;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 20 ▶use Text::Nunjucks::Nodes::CallExtension;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 20 ▶use Text::Nunjucks::Nodes::CallExtensionAsync;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 #### printNodes -- AST pretty-printer (upstream: print / printNodes)
83  
84 20 my $JSON = JSON::MaybeXS->new( canonical => 1, convert_blessed => 1 )->pretty(1);
85  
86 # upstream `print`. "This is hacky, but this is just a debugging function
87 # anyway." A non-empty line ("0" counts, unlike Perl truthiness) is prefixed
88 # with `indent` spaces; the last line gets no trailing newline. NOTE: debug
89 # helper only, not byte-asserted anywhere -- the property JSON's whitespace and
90 # key order differ from JS's JSON.stringify (Perl hashes are unordered).
91 0 ▶sub _print {
Subroutine _print: not called CC=8 SCAR=42.8
92 0 ▶ my ( $str, $indent, $inline ) = @_;
Subroutine _print: not called CC=8 SCAR=42.8
93 0 ▶ $indent = 0 unless defined $indent;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $indent 0 0 0
unless defined $indent 0 0 0
94 0 my @lines = split /\n/, $str, -1;
95 0 for my $i ( 0 .. $#lines ) {
96 0 my $line = $lines[$i];
97 0 ▶ print STDOUT ( ' ' x $indent )
Branch: 2 branches
BranchTrueFalseTotal
if length $line and $inline && $i > 0 || !$inline 0 0 0
if length $line and $inline && $i > 0 || !$inline 0 0 0
Condition0%0/18
length $line and $inline && $i > 0 || !$inline
!ll&&!rl&&r
000
$inline and $i > 0 or not $inline
l!l&&r!l&&!r
000
$inline and $i > 0
!ll&&!rl&&r
000
length $line and $inline && $i > 0 || !$inline
!ll&&!rl&&r
000
$inline and $i > 0 or not $inline
l!l&&r!l&&!r
000
$inline and $i > 0
!ll&&!rl&&r
000
MC/DC0%0/4
length $line and $inline && $i > 0 || !$inline
length $line $inline $i > 0 not $inline
MC/DC0%0/4
length $line and $inline && $i > 0 || !$inline
length $line $inline $i > 0 not $inline
Truth table0%0/6
A and B and C or not(D)
A= length $lineB= $inlineC= $i > 0D= not $inline
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
Truth table0%0/6
A and B and C or not(D)
A= length $lineB= $inlineC= $i > 0D= not $inline
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
98 if length($line) && ( ( $inline && $i > 0 ) || !$inline );
99 0 ▶ print STDOUT $line . ( $i == $#lines ? '' : "\n" );
Branch: 2 branches
BranchTrueFalseTotal
$i == $#lines ? : 0 0 0
$i == $#lines ? : 0 0 0
100 }
101 }
102  
103 # Print the AST in a nicely formatted tree format for debuggin
104 0 ▶sub print_nodes {
Subroutine print_nodes: not called CC=10 SCAR=47.0
Pod undocumented
105 0 ▶ my ( $node, $indent ) = @_;
Subroutine print_nodes: not called CC=10 SCAR=47.0
Pod undocumented
106 0 ▶ $indent = $indent || 0;
Condition0%0/4
$indent || 0
l!l
00
$indent || 0
l!l
00
MC/DC0%0/1
$indent || 0
$indent
MC/DC0%0/1
$indent || 0
$indent
Truth table0%0/2
A
A= $indent
Aresult
11
00
Truth table0%0/2
A
A= $indent
Aresult
11
00
107  
108 0 _print( $node->typename . ': ', $indent );
109  
110 0 ▶ if ( $node->isa('Text::Nunjucks::Nodes::NodeList') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($node->isa('Text::Nunjucks::Nodes::NodeList')) { } 0 0 0
elsif ($node->isa('Text::Nunjucks::Nodes::CallExtension')) { } 0 0 0
if ($node->isa('Text::Nunjucks::Nodes::NodeList')) { } 0 0 0
elsif ($node->isa('Text::Nunjucks::Nodes::CallExtension')) { } 0 0 0
111 0 _print("\n");
112 0 print_nodes( $_, $indent + 2 ) for @{ $node->children };
113 }
114 elsif ( $node->isa('Text::Nunjucks::Nodes::CallExtension') ) {
115 0 _print( $node->extName . '.' . $node->prop . "\n" );
116  
117 0 ▶ print_nodes( $node->args, $indent + 2 ) if $node->args;
Branch: 2 branches
BranchTrueFalseTotal
if $node->args 0 0 0
if $node->args 0 0 0
118  
119 0 ▶ if ( $node->contentArgs ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->contentArgs) 0 0 0
if ($node->contentArgs) 0 0 0
120 0 print_nodes( $_, $indent + 2 ) for @{ $node->contentArgs };
121 }
122 }
123 else {
124 0 my @nodes;
125 my $props;
126  
127 0 ▶ $node->iter_fields( sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
128 0 ▶ my ( $val, $field_name ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
129 0 ▶ if ( blessed($val) && $val->isa('Text::Nunjucks::Nodes::Node') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa('Text::Nunjucks::Nodes::Node')) { } 0 0 0
if (builtin::blessed($val) and $val->isa('Text::Nunjucks::Nodes::Node')) { } 0 0 0
Condition0%0/6
builtin::blessed($val) and $val->isa("Text::Nunjucks::Nodes::Node")
!ll&&!rl&&r
000
builtin::blessed($val) and $val->isa("Text::Nunjucks::Nodes::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($val) and $val->isa("Text::Nunjucks::Nodes::Node")
builtin::blessed($val) $val->isa("Text::Nunjucks::Nodes::Node")
MC/DC0%0/2
builtin::blessed($val) and $val->isa("Text::Nunjucks::Nodes::Node")
builtin::blessed($val) $val->isa("Text::Nunjucks::Nodes::Node")
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa("Text::Nunjucks::Nodes::Node")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa("Text::Nunjucks::Nodes::Node")
ABresult
0X0
100
111
130 0 push @nodes, [ $field_name, $val ];
131 }
132 else {
133 0 ▶ $props ||= {};
Condition0%0/4
$props ||= {}
l!l
00
$props ||= {}
l!l
00
MC/DC0%0/1
$props ||= {}
$props
MC/DC0%0/1
$props ||= {}
$props
Truth table0%0/2
A
A= $props
Aresult
11
00
Truth table0%0/2
A
A= $props
Aresult
11
00
134 0 $props->{$field_name} = $val;
135 }
136 0 } );
137  
138 0 ▶ if ($props) {
Branch: 2 branches
BranchTrueFalseTotal
if ($props) { } 0 0 0
if ($props) { } 0 0 0
139 0 _print( $JSON->encode($props) . "\n", undef, 1 );
140 }
141 else {
142 0 _print("\n");
143 }
144  
145 0 for my $pair (@nodes) {
146 0 my ( $field_name, $n ) = @$pair;
147 0 _print( '[' . $field_name . '] =>', $indent + 2 );
148 0 print_nodes( $n, $indent + 4 );
149 }
150 }
151 }
152  
153 20 1;
154  
155 __END__
« blib/lib/Text/Nunjucks/Lexer/Tokenizer.pm ↑ Summary blib/lib/Text/Nunjucks/Parser/Parser.pm »