lib/LaTeXML/Core/Tokens.pm

filter: Statementstmt 21.6% 26 / 120 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 36.3% 8 / 22 Podpod 42.8% 6 / 14 totaltotal 14.5% 40 / 275 CC 43 SCAR 45.9
CC
43
Cov
12%
stripBraces
272.0
packParameters
110.0
equals
56.0
CRAP 98.3
« lib/LaTeXML/MathParser.pm ↑ Summary lib/LaTeXML/Util/WWW.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)
23 );
24  
25 #======================================================================
26 # Token List constructors.
27  
28 # Return a LaTeXML::Core::Tokens made from the arguments (tokens)
29 sub Tokens {
30 0 ▶ my (@tokens) = @_;
Subroutine Tokens: not called CC=4 SCAR=30.0
Pod documented
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 #======================================================================
40 # Return a list of the tokens making up this Tokens
41 sub unlist {
42 0 ▶ my ($self) = @_;
Subroutine unlist: not called CC=1 SCAR=6.9
Pod undocumented
43 0 return @$self; }
44  
45 # Return a shallow copy of the Tokens
46 sub clone {
47 0 ▶ my ($self) = @_;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
48 0 return bless [@$self], ref $self; }
49  
50 # Return a string containing the TeX form of the Tokens
51 sub revert {
52 0 ▶ my ($self) = @_;
Subroutine revert: not called CC=2 SCAR=17.9
Pod undocumented
53 0 ▶ return map { ($$_[1] == CC_SMUGGLE_THE ? $$_[2] : $_); } @$self; }
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] == 19 ? : 0 0 0
54  
55 # toString is used often, and for more keyword-like reasons,
56 # NOT for creating valid TeX (use revert or UnTeX for that!)
57 sub toString {
58 0 ▶ my ($self) = @_;
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
59 0 return join('', map { $_->toString } @$self); }
60  
61 # Methods for overloaded ops.
62 sub equals {
63 0 ▶ my ($a, $b) = @_;
Subroutine equals: not called CC=7 SCAR=40.3
Pod documented
64 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
65 0 my @a = @$a;
66 0 my @b = @$b;
67 0 ▶ while (@a && @b && ($a[0]->equals($b[0]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a and @b and $a[0]->equals($b[0])) 0 0 0
Condition0%0/6
@a and @b and $a[0]->equals($b[0])
!ll&&!rl&&r
000
@a and @b
!ll&&!rl&&r
000
MC/DC0%0/3
@a and @b and $a[0]->equals($b[0])
@a @b $a[0]->equals($b[0])
Truth table0%0/4
A and B and C
A= @aB= @bC= $a[0]->equals($b[0])
ABCresult
0XX0
10X0
1100
1111
68 0 shift(@a); shift(@b); }
69 0 ▶ return !(@a || @b); }
Condition0%0/3
@a || @b
l!l&&r!l&&!r
000
MC/DC0%0/2
@a || @b
@a @b
Truth table0%0/3
A || B
A= @aB= @b
ABresult
1X1
011
000
70  
71 sub stringify {
72 0 ▶ my ($self) = @_;
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
73 0 return "Tokens[" . join(',', map { $_->toString } @$self) . "]"; }
74  
75 sub beDigested {
76 0 ▶ my ($self, $stomach) = @_;
Subroutine beDigested: not called CC=1 SCAR=6.9
Pod documented
77 0 return $stomach->digest($self); }
78  
79 sub neutralize {
80 0 ▶ my ($self, @extraspecials) = @_;
Subroutine neutralize: not called CC=1 SCAR=6.9
Pod undocumented
81 # Remove dont_expand, but preserve SMUGGLE_THE
82 0 return Tokens(map { $_->neutralize(@extraspecials) } @$self); }
83  
84 sub without_dont_expand {
85 0 ▶ my ($self) = @_;
Subroutine without_dont_expand: not called CC=1 SCAR=6.9
Pod undocumented
86 0 return Tokens(map { $_->without_dont_expand } @$self); }
87  
88 sub isBalanced {
89 0 ▶ my ($self) = @_;
Subroutine isBalanced: not called CC=4 SCAR=30.0
Pod undocumented
90 0 my $level = 0;
91 0 foreach my $t (@$self) {
92 0 my $cc = $$t[1]; # INLINE
93 0 ▶ $level++ if $cc == CC_BEGIN;
Branch: 1 branch
BranchTrueFalseTotal
if $cc == 1 0 0 0
94 0 ▶ $level-- if $cc == CC_END; }
Branch: 1 branch
BranchTrueFalseTotal
if $cc == 2 0 0 0
95 0 return $level == 0; }
96  
97 # NOTE: Assumes each arg either undef or also Tokens
98 # Using inline accessors on those assumptions
99 sub substituteParameters {
100 0 ▶ my ($self, @args) = @_;
Subroutine substituteParameters: not called CC=6 SCAR=37.4
Pod undocumented
101 0 my @in = @{$self}; # ->unlist
102 0 ▶ return $self unless grep { $$_[1] == CC_ARG; } @in;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[1] == 18;} @in 0 0 0
103 0 my @result = ();
104 0 ▶ while (my $token = shift(@in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @in) 0 0 0
105 0 ▶ if ($$token[1] != CC_ARG) { # Non-match; copy it
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[1] != 18) { } 0 0 0
106 0 push(@result, $token); }
107 else {
108 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
109 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
110 0 return bless [@result], 'LaTeXML::Core::Tokens'; }
111  
112 # Process the CC_PARAM tokens for use as a macro body (and other token lists)
113 # Groups PARAM+OTHER token pair into match tokens.
114 # Collapses PARAM+PARAM token pair into a single PARAM
115 # B book suggests running this
116 # and remove dont_expand markers.
117 sub packParameters {
118 0 ▶ my ($self) = @_;
Subroutine packParameters: not called CC=10 SCAR=47.0
Pod undocumented
119 0 my @rescanned = ();
120 0 my @toks = @$self;
121 0 my $repacked = 0;
122 0 ▶ while (my $t = shift @toks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = shift @toks) 0 0 0
123 0 ▶ if ($$t[1] == CC_PARAM && @toks) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->[1] == 6 and @toks) { } 0 0 0
elsif (my $inner = $t->[2]) { } 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
124 0 $repacked = 1;
125 # NOTE for future cleanup: Only CC_CS & CC_ACTIVE should ever get with_dont_expand!
126 0 my $next_t = shift @toks;
127 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
128 0 ▶ if ($next_cc == CC_OTHER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_cc == 12) { } 0 0 0
elsif ($next_cc == 6) { } 0 0 0
129 # only group clear match token cases
130 0 push(@rescanned, T_ARG($next_t)); }
131 elsif ($next_cc == CC_PARAM) {
132 0 push(@rescanned, $t); }
133 else { # any other case, preserve as-is, let the higher level call resolve any errors
134 # e.g. \detokenize{#,} is legal, while \textbf{#,} is not
135 0 Error('misdefined', 'expansion', undef, "Parameter has a malformed arg, should be #1-#9 or ##. ",
136 "In expansion " . ToString(Tokens(@toks))); } }
137 elsif (my $inner = $$t[2]) { # Open-coded $t->without_dont_expand
138 0 $repacked = 1;
139 0 ▶ push(@rescanned, ($$inner[2] || $inner)); }
Condition0%0/3
$inner->[2] || $inner
l!l&&r!l&&!r
000
MC/DC0%0/2
$inner->[2] || $inner
$inner->[2] $inner
Truth table0%0/3
A || B
A= $inner->[2]B= $inner
ABresult
1X1
011
000
140 else {
141 0 push(@rescanned, $t); } }
142 0 ▶ return ($repacked ? bless [@rescanned], 'LaTeXML::Core::Tokens' : $self); }
Branch: 1 branch
BranchTrueFalseTotal
$repacked ? : 0 0 0
143  
144 # Trims outer braces (if they balance each other)
145 # Should this also trim whitespace? or only if there are braces?
146 sub stripBraces {
147 0 ▶ my ($self) = @_;
Subroutine stripBraces: not called CC=16 SCAR=56.1
Pod undocumented
148 0 my $n = 1 + $#$self;
149 0 my $i0 = 0;
150 0 my $i1 = $n;
151 # skip past spaces at ends.
152 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
153 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
154 0 my (@o, @p);
155 # Collect balanced pairs.
156 0 ▶ for (my $i = $i0 ; $i < $i1 ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $i1) 0 0 0
157 0 my $cc = $$self[$i]->getCatcode;
158 0 ▶ if ($cc == CC_BEGIN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cc == 1) { } 0 0 0
elsif ($cc == 2) { } 0 0 0
159 0 push(@o, $i); }
160 elsif ($cc == CC_END) {
161 0 ▶ if (@o) {
Branch: 1 branch
BranchTrueFalseTotal
if (@o) { } 0 0 0
162 0 push(@p, pop(@o), $i); }
163 else {
164 0 return $self; } } } # Unbalanced: Too many }
165 0 ▶ return $self if @o; # Unbalanced: Too many {
Branch: 1 branch
BranchTrueFalseTotal
if @o 0 0 0
166 ## COULD strip multiple pairs of braces by checking more @p pairs
167 0 ▶ if (@p) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 0 0 0
168 0 my $j1 = pop(@p);
169 0 my $j0 = pop(@p);
170 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
171 0 $i0++; $i1--; } }
172 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
173 ? bless [@$self[$i0 .. $i1 - 1]], 'LaTeXML::Core::Tokens'
174 : $self); }
175  
176 #======================================================================
177  
178 5 1;
179  
180 __END__
« lib/LaTeXML/MathParser.pm ↑ Summary lib/LaTeXML/Util/WWW.pm »