lib/LaTeXML/Core/Tokens.pm

filter: Statementstmt 23.3% 29 / 124 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 39.1% 9 / 23 Podpod 42.8% 6 / 14 totaltotal 14.8% 44 / 297 CC 48 SCAR 47.7
CC
48
Cov
12%
stripBraces
272.0
equals
210.0
packParameters
72.0
CRAP 118.3
« lib/LaTeXML/Common/Model/RelaxNG.pm ↑ Summary lib/LaTeXML/Core/KeyVals.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Tokens | #
3 # | A list of Token(s) | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Core::Tokens;
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use base qw(LaTeXML::Common::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 our @EXPORT = ( # Global STATE; This gets bound by LaTeXML.pm
22 qw(&Tokens &TokensI),
23 );
24  
25 #======================================================================
26 # Token List constructors.
27  
28 # Return a LaTeXML::Core::Tokens made from the arguments (tokens)
29 0 ▶sub Tokens {
Subroutine Tokens: not called CC=4 SCAR=30.0
Pod documented
30 0 my (@tokens) = @_;
31 0 my $r;
32 # faster than foreach
33 0 ▶ @tokens = map { (($r = ref $_) eq 'LaTeXML::Core::Token' ? $_
Branch: 2 branches
BranchTrueFalseTotal
($r = ref $_) eq 'LaTeXML::Core::Token' ? : 0 0 0
$r eq 'LaTeXML::Core::Tokens' ? : 0 0 0
Condition0%0/3
Error("misdefined", $r, undef, 'Expected a Token, got ' . Stringify($_)) or T_OTHER(Stringify($_))
l!l&&r!l&&!r
000
MC/DC0%0/2
Error("misdefined", $r, undef, 'Expected a Token, got ' . Stringify($_)) or T_OTHER(Stringify($_))
Error("misdefined", $r, undef, 'Expected a Token, got ' . Stringify($_)) T_OTHER(Stringify($_))
Truth table0%0/3
A or B
A= Error("misdefined", $r, undef, 'Expected a Token, got ' . Stringify($_))B= T_OTHER(Stringify($_))
ABresult
1X1
011
000
34 : ($r eq 'LaTeXML::Core::Tokens' ? @$_
35 : Error('misdefined', $r, undef, "Expected a Token, got " . Stringify($_)) || T_OTHER(Stringify($_)))) }
36 @tokens;
37 0 return bless [@tokens], 'LaTeXML::Core::Tokens'; }
38  
39 0 ▶sub TokensI {
Subroutine TokensI: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my (@tokens) = @_;
41 0 return bless [@tokens], 'LaTeXML::Core::Tokens'; }
42  
43 #======================================================================
44 # Return a list of the tokens making up this Tokens
45 0 ▶sub unlist {
Subroutine unlist: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my ($self) = @_;
47 0 return @$self; }
48  
49 # Return a shallow copy of the Tokens
50 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
51 0 my ($self) = @_;
52 0 return bless [@$self], ref $self; }
53  
54 # Return a string containing the TeX form of the Tokens
55 0 ▶sub revert {
Subroutine revert: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my ($self) = @_;
57 0 return @$self; }
58  
59 # toString is used often, and for more keyword-like reasons,
60 # NOT for creating valid TeX (use revert or UnTeX for that!)
61 0 ▶sub toString {
Subroutine toString: not called CC=2 SCAR=17.9
Pod documented
62 0 my ($self) = @_;
63 0 ▶ return join('', map { ($$_[1] == CC_COMMENT ? '' : $_->toString) } @$self); }
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] == 14 ? : 0 0 0
64  
65 # Methods for overloaded ops.
66  
67 # Compare two Tokens lists, ignoring comments & markers
68 0 ▶sub equals {
Subroutine equals: not called CC=14 SCAR=53.5
Pod documented
69 0 my ($a, $b) = @_;
70 0 ▶ return 0 unless defined $b && (ref $a) eq (ref $b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b and ref $a eq ref $b 0 0 0
Condition0%0/3
defined $b and ref $a eq ref $b
!ll&&!rl&&r
000
MC/DC0%0/2
defined $b and ref $a eq ref $b
defined $b ref $a eq ref $b
Truth table0%0/3
A and B
A= defined $bB= ref $a eq ref $b
ABresult
0X0
100
111
71 0 my @a = @$a;
72 0 my @b = @$b;
73 0 ▶ while (@a || @b) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a or @b) 0 0 0
Condition0%0/3
@a or @b
l!l&&r!l&&!r
000
MC/DC0%0/2
@a or @b
@a @b
Truth table0%0/3
A or B
A= @aB= @b
ABresult
1X1
011
000
74 0 ▶ if (@a && (($a[0]->[1] == CC_COMMENT) || ($a[0]->[1] == CC_MARKER))) { shift(@a); next; }
Branch: 1 branch
BranchTrueFalseTotal
if (@a and $a[0][1] == 14 || $a[0][1] == 17) 0 0 0
Condition0%0/6
@a and $a[0][1] == 14 || $a[0][1] == 17
!ll&&!rl&&r
000
$a[0][1] == 14 or $a[0][1] == 17
l!l&&r!l&&!r
000
MC/DC0%0/3
@a and $a[0][1] == 14 || $a[0][1] == 17
@a $a[0][1] == 14 $a[0][1] == 17
Truth table0%0/4
A and B or C
A= @aB= $a[0][1] == 14C= $a[0][1] == 17
ABCresult
0XX0
1000
11X1
1011
75 0 ▶ if (@b && (($b[0]->[1] == CC_COMMENT) || ($b[0]->[1] == CC_MARKER))) { shift(@b); next; }
Branch: 1 branch
BranchTrueFalseTotal
if (@b and $b[0][1] == 14 || $b[0][1] == 17) 0 0 0
Condition0%0/6
@b and $b[0][1] == 14 || $b[0][1] == 17
!ll&&!rl&&r
000
$b[0][1] == 14 or $b[0][1] == 17
l!l&&r!l&&!r
000
MC/DC0%0/3
@b and $b[0][1] == 14 || $b[0][1] == 17
@b $b[0][1] == 14 $b[0][1] == 17
Truth table0%0/4
A and B or C
A= @bB= $b[0][1] == 14C= $b[0][1] == 17
ABCresult
0XX0
1000
11X1
1011
76 0 ▶ return unless @a && @b && shift(@a)->equals(shift(@b)); }
Branch: 1 branch
BranchTrueFalseTotal
unless @a and @b and shift(@a)->equals(shift @b) 0 0 0
Condition0%0/6
@a and @b and shift(@a)->equals(shift @b)
!ll&&!rl&&r
000
@a and @b
!ll&&!rl&&r
000
MC/DC0%0/3
@a and @b and shift(@a)->equals(shift @b)
@a @b shift(@a)->equals(shift @b)
Truth table0%0/4
A and B and C
A= @aB= @bC= shift(@a)->equals(shift @b)
ABCresult
0XX0
10X0
1100
1111
77 0 return 1; }
78  
79 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self) = @_;
81 0 return "Tokens[" . join(',', map { $_->toString } @$self) . "]"; }
82  
83 0 ▶sub beDigested {
Subroutine beDigested: not called CC=1 SCAR=0.6
Pod documented
84 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 0 my ($self, $stomach) = @_;
86 0 return $stomach->digest($self); }
87  
88 0 ▶sub neutralize {
Subroutine neutralize: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ($self, @extraspecials) = @_;
90 0 return Tokens(map { $_->neutralize(@extraspecials) } @$self); }
91  
92 0 ▶sub isBalanced {
Subroutine isBalanced: not called CC=5 SCAR=34.0
Pod undocumented
93 0 my ($self) = @_;
94 0 my $level = 0;
95 0 foreach my $t (@$self) {
96 0 my $cc = $$t[1]; # INLINE
97 0 ▶ $level++ if $cc == CC_BEGIN;
Branch: 1 branch
BranchTrueFalseTotal
if $cc == 1 0 0 0
98 0 ▶ if ($cc == CC_END) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc == 2) 0 0 0
99 0 $level--;
100 # Note that '{ }} {' is still unbalanced
101 # even though the left and right braces match in count.
102 0 ▶ last if $level < 0; } }
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 0 0 0
103 0 return $level == 0; }
104  
105 # NOTE: Assumes each arg either undef or also Tokens
106 # Using inline accessors on those assumptions
107 0 ▶sub substituteParameters {
Subroutine substituteParameters: not called CC=5 SCAR=34.0
Pod undocumented
108 0 my ($self, @args) = @_;
109 0 my @in = @{$self}; # ->unlist
110 0 my @result = ();
111 0 ▶ while (my $token = shift(@in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @in) 0 0 0
112 0 ▶ if ($$token[1] != CC_ARG) { # Non-match; copy it
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[1] != 18) { } 0 0 0
113 0 push(@result, $token); }
114 else {
115 0 ▶ if (my $arg = $args[ord($$token[0]) - ord("0") - 1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = $args[ord($token->[0]) - 48 - 1]) 0 0 0
116 0 ▶ push(@result, (ref $arg eq 'LaTeXML::Core::Token' ? $arg : @$arg)); } } } # ->unlist
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq 'LaTeXML::Core::Token' ? : 0 0 0
117 0 return bless [@result], 'LaTeXML::Core::Tokens'; }
118  
119 # Packs repeated CC_PARAM tokens into CC_ARG tokens for use as a macro body (and other token lists)
120 # Also unwraps \noexpand tokens, since that is also needed for macro bodies
121 # (but not strictly part of packing parameters)
122 0 ▶sub packParameters {
Subroutine packParameters: not called CC=8 SCAR=42.8
Pod undocumented
123 0 my ($self) = @_;
124 0 my @rescanned = ();
125 0 my @toks = @$self;
126 0 my $repacked = 0;
127 0 ▶ while (my $t = shift @toks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = shift @toks) 0 0 0
128 0 ▶ if ($$t[1] == CC_PARAM && @toks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[1] == 6 and @toks) { } 0 0 0
Condition0%0/3
$t->[1] == 6 and @toks
!ll&&!rl&&r
000
MC/DC0%0/2
$t->[1] == 6 and @toks
$t->[1] == 6 @toks
Truth table0%0/3
A and B
A= $t->[1] == 6B= @toks
ABresult
0X0
100
111
129 0 $repacked = 1;
130 0 my $next_t = shift @toks;
131 0 ▶ my $next_cc = $next_t && $$next_t[1];
Condition0%0/3
$next_t && $next_t->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$next_t && $next_t->[1]
$next_t $next_t->[1]
Truth table0%0/3
A && B
A= $next_tB= $next_t->[1]
ABresult
0X0
100
111
132 0 ▶ if ($next_cc == CC_OTHER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_cc == 12) { } 0 0 0
elsif ($next_cc == 6) { } 0 0 0
133 # only group clear match token cases
134 0 push(@rescanned, T_ARG($next_t)); }
135 elsif ($next_cc == CC_PARAM) {
136 0 push(@rescanned, $t); }
137 else { # any other case, preserve as-is, let the higher level call resolve any errors
138 # e.g. \detokenize{#,} is legal, while \textbf{#,} is not
139 0 Error('misdefined', 'expansion', undef, "Parameter has a malformed arg, should be #1-#9 or ##. ",
140 "In expansion " . ToString($self)); } }
141 else {
142 0 push(@rescanned, $t); } }
143 0 ▶ return ($repacked ? bless [@rescanned], 'LaTeXML::Core::Tokens' : $self); }
Branch: 1 branch
BranchTrueFalseTotal
$repacked ? : 0 0 0
144  
145 # Trims outer braces (if they balance each other)
146 # Should this also trim whitespace? or only if there are braces?
147 0 ▶sub stripBraces {
Subroutine stripBraces: not called CC=16 SCAR=56.1
Pod undocumented
148 0 my ($self) = @_;
149 0 my $n = 1 + $#$self;
150 0 my $i0 = 0;
151 0 my $i1 = $n;
152 # skip past spaces at ends.
153 0 ▶ while (($i0 < $n) && ($$self[$i0]->getCatcode == CC_SPACE)) { $i0++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i0 < $n and $self->[$i0]->getCatcode == 10) 0 0 0
Condition0%0/3
$i0 < $n and $self->[$i0]->getCatcode == 10
!ll&&!rl&&r
000
MC/DC0%0/2
$i0 < $n and $self->[$i0]->getCatcode == 10
$i0 < $n $self->[$i0]->getCatcode == 10
Truth table0%0/3
A and B
A= $i0 < $nB= $self->[$i0]->getCatcode == 10
ABresult
0X0
100
111
154 0 ▶ while (($i1 > 0) && ($$self[$i1 - 1]->getCatcode == CC_SPACE)) { $i1--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i1 > 0 and $$self[$i1 - 1]->getCatcode == 10) 0 0 0
Condition0%0/3
$i1 > 0 and $$self[$i1 - 1]->getCatcode == 10
!ll&&!rl&&r
000
MC/DC0%0/2
$i1 > 0 and $$self[$i1 - 1]->getCatcode == 10
$i1 > 0 $$self[$i1 - 1]->getCatcode == 10
Truth table0%0/3
A and B
A= $i1 > 0B= $$self[$i1 - 1]->getCatcode == 10
ABresult
0X0
100
111
155 0 my (@o, @p);
156 # Collect balanced pairs.
157 0 ▶ for (my $i = $i0 ; $i < $i1 ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $i1) 0 0 0
158 0 my $cc = $$self[$i]->getCatcode;
159 0 ▶ if ($cc == CC_BEGIN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cc == 1) { } 0 0 0
elsif ($cc == 2) { } 0 0 0
160 0 push(@o, $i); }
161 elsif ($cc == CC_END) {
162 0 ▶ if (@o) {
Branch: 1 branch
BranchTrueFalseTotal
if (@o) { } 0 0 0
163 0 push(@p, pop(@o), $i); }
164 else {
165 0 return $self; } } } # Unbalanced: Too many }
166 0 ▶ return $self if @o; # Unbalanced: Too many {
Branch: 1 branch
BranchTrueFalseTotal
if @o 0 0 0
167 ## COULD strip multiple pairs of braces by checking more @p pairs
168 0 ▶ if (@p) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 0 0 0
169 0 my $j1 = pop(@p);
170 0 my $j0 = pop(@p);
171 0 ▶ if (($j0 == $i0) && ($j1 == $i1 - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j0 == $i0 and $j1 == $i1 - 1) 0 0 0
Condition0%0/3
$j0 == $i0 and $j1 == $i1 - 1
!ll&&!rl&&r
000
MC/DC0%0/2
$j0 == $i0 and $j1 == $i1 - 1
$j0 == $i0 $j1 == $i1 - 1
Truth table0%0/3
A and B
A= $j0 == $i0B= $j1 == $i1 - 1
ABresult
0X0
100
111
172 0 $i0++; $i1--; } }
173 0 ▶ return (($i0 < $i1) && (($i0 > 0) || ($i1 < $n))
Branch: 1 branch
BranchTrueFalseTotal
$i0 < $i1 && ($i0 > 0 || $i1 < $n) ? : 0 0 0
Condition0%0/6
$i0 < $i1 and $i0 > 0 || $i1 < $n
!ll&&!rl&&r
000
$i0 > 0 or $i1 < $n
l!l&&r!l&&!r
000
MC/DC0%0/3
$i0 < $i1 and $i0 > 0 || $i1 < $n
$i0 < $i1 $i0 > 0 $i1 < $n
Truth table0%0/4
A and B or C
A= $i0 < $i1B= $i0 > 0C= $i1 < $n
ABCresult
0XX0
1000
11X1
1011
174 ? bless [@$self[$i0 .. $i1 - 1]], 'LaTeXML::Core::Tokens'
175 : $self); }
176  
177 #======================================================================
178  
179 5 1;
180  
181 __END__
« lib/LaTeXML/Common/Model/RelaxNG.pm ↑ Summary lib/LaTeXML/Core/KeyVals.pm »