lib/Lingy/Reader.pm

filter: Statementstmt 8.6% 13 / 151 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 18.7% 3 / 16 Podpod 0.0% 0 / 13 totaltotal 5.5% 16 / 287 CC 50 SCAR 44.0
CC
50
Cov
5%
read_scalar
182.0
read_form
156.0
read_hash_map
56.0
CRAP 81.3
« lib/Lingy/Lang/Numbers.pm ↑ Summary lib/Lingy/ClojureREPL.pm »
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::Reader;
3  
4 1 ▶use Lingy::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 my $tokenize_re = qr/
7 (?: # Ignore:
8 \#\!.* | # hashbang line
9 [\s,] | # whitespace, commas,
10 ;.* # comments
11 )*
12 ( # Capture all these tokens:
13 \#\( | # Lambda
14 ~@ | # Unquote-splice token
15 [\[\]{}()'`~^@] | # Single character tokens
16 \#? # Possibly a regex
17 "(?: # Quoted string
18 \\. | # Escaped char
19 [^\\"] # Any other char
20 )*"? | # Match if missing ending quote
21 # Other tokens
22 [^\s\[\]\{\}\(\)\'\"\`\,\;]*
23 )
24 /xo;
25  
26 sub new {
27 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 bless {
29 tokens => [],
30 lambda => [],
31 @_,
32 }, $class;
33 }
34  
35 sub tokenize {
36 [
37 0 ▶ grep length,
Subroutine tokenize: not called CC=1 SCAR=6.9
Pod undocumented
38 $_[0] =~ /$tokenize_re/g
39 ];
40 }
41  
42 sub read_str {
43 0 ▶ my ($self, $str, $repl) = @_;
Subroutine read_str: not called CC=3 SCAR=24.8
Pod undocumented
44 0 local $self->{repl} = $repl;
45 0 my $tokens = $self->{tokens} = tokenize($str);
46 0 my @forms;
47 0 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 0 0 0
48 0 my $form = $self->read_form;
49 0 ▶ if (defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) 0 0 0
50 0 push @forms, $form;
51 }
52 }
53 0 return @forms;
54 }
55  
56 sub read_form {
57 0 ▶ my ($self) = @_;
Subroutine read_form: not called CC=12 SCAR=50.5
Pod undocumented
58 0 local $_ = $self->{tokens}[0];
59 0 ▶ /^\($/ ? $self->read_list(LIST, ')') :
Branch: 11 branches
BranchTrueFalseTotal
/^\($/ ? : 0 0 0
/^\[$/ ? : 0 0 0
/^\{$/ ? : 0 0 0
/^'$/ ? : 0 0 0
/^`$/ ? : 0 0 0
/^~$/ ? : 0 0 0
/^~\@$/ ? : 0 0 0
/^\@$/ ? : 0 0 0
/^\^$/ ? : 0 0 0
/^#\(/ ? : 0 0 0
/^%\d*$/ ? : 0 0 0
60 /^\[$/ ? $self->read_list(VECTOR, ']') :
61 /^\{$/ ? $self->read_hash_map(HASHMAP, '}') :
62 /^'$/ ? $self->read_quote('quote') :
63 /^`$/ ? $self->read_quote('quasiquote') :
64 /^~$/ ? $self->read_quote('unquote') :
65 /^~\@$/ ? $self->read_quote('splice-unquote') :
66 /^\@$/ ? $self->read_quote('deref') :
67 /^\^$/ ? $self->with_meta :
68 /^#\(/ ? $self->read_lambda :
69 /^%\d*$/ ? $self->read_lambda_symbol :
70 $self->read_scalar;
71 }
72  
73 sub read_more {
74 0 ▶ my ($self) = @_;
Subroutine read_more: not called CC=3 SCAR=24.8
Pod undocumented
75 0 ▶ if ($self->{repl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"repl"}) 0 0 0
76 0 my $line = Lingy::ReadLine::readline(1);
77 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
78 0 push @{$self->{tokens}}, @{tokenize($line)};
79 0 return 1;
80 }
81 }
82 0 return;
83 }
84  
85 sub read_list {
86 0 ▶ my ($self, $type, $end) = @_;
Subroutine read_list: not called CC=4 SCAR=30.0
Pod undocumented
87 0 my $tokens = $self->{tokens};
88 0 shift @$tokens;
89 0 my $list = $type->new([]);
90 0 while (1) {
91 0 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 0 0 0
92 0 ▶ if ($tokens->[0] eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0] eq $end) 0 0 0
93 0 shift @$tokens;
94 0 return $list;
95 }
96 0 push @$list, $self->read_form;
97 }
98 0 ▶ $self->read_more and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->read_more and next 0 0 0
99 0 err "Reached end of input in 'read_list'";
100 }
101 }
102  
103 sub read_lambda {
104 0 ▶ my ($self) = @_;
Subroutine read_lambda: not called CC=6 SCAR=37.4
Pod undocumented
105 0 my $tokens = $self->{tokens};
106 0 shift @$tokens;
107 0 push @{$self->{lambda}}, {
108 sym => {},
109 max => 0,
110 };
111 0 my $list = [];
112 0 outer: while (1) {
113 0 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 0 0 0
114 0 ▶ if ($tokens->[0] eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0] eq ")") 0 0 0
115 0 shift @$tokens;
116 0 last outer;
117 }
118 0 push @$list, $self->read_form;
119 }
120 0 ▶ $self->read_more and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->read_more and next 0 0 0
121 0 err "Reached end of input in 'read_list'";
122 }
123 0 my $lambda = pop @{$self->{lambda}};
124 0 my ($sym, $max) = @{$lambda}{'sym', 'max'};
125 0 my $syms = [];
126 0 ▶ for (my $i = 1; $i <= $lambda->{max}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $lambda->{"max"}) 0 0 0
127 0 ▶ push @$syms, $sym->{"%$i"} // symbol("p${i}_${\RT->nextID}");
Condition0%0/3
$$sym{"%$i"} // symbol(qq[p${i}_${\"Lingy::Lang::RT"->nextID;}])
l!l&&r!l&&!r
000
MC/DC0%0/2
$$sym{"%$i"} // symbol(qq[p${i}_${\"Lingy::Lang::RT"->nextID;}])
$$sym{"%$i"} symbol(qq[p${i}_${\"Lingy::Lang::RT"->nextID;}])
Truth table0%0/3
A // B
A= $$sym{"%$i"}B= symbol(qq[p${i}_${\"Lingy::Lang::RT"->nextID;}])
ABresult
1X1
011
000
128 }
129  
130 0 LIST->new([
131 SYMBOL->new('fn*'),
132 VECTOR->new($syms),
133 LIST->new($list),
134 ]);
135 }
136  
137 sub read_lambda_symbol {
138 0 ▶ my ($self) = @_;
Subroutine read_lambda_symbol: not called CC=5 SCAR=34.0
Pod undocumented
139 0 my $token = shift @{$self->{tokens}};
140 0 ▶ return symbol($token) unless @{$self->{lambda}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"lambda"};} 0 0 0
141 0 ▶ $token = '%1' if $token eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "%" 0 0 0
142 0 my $num = substr($token, 1);
143 0 my $lambda = $self->{lambda}->[-1];
144 0 ▶ $lambda->{max} = $num if $num > $lambda->{max};
Branch: 1 branch
BranchTrueFalseTotal
if $num > $lambda->{"max"} 0 0 0
145 0 ▶ $lambda->{sym}{$token} //= symbol("p${num}_${\RT->nextID}");
Condition0%0/3
$lambda->{"sym"}{$token} //= symbol(qq[p${num}_${\"Lingy::Lang::RT"->nextID;}])
l!l&&r!l&&!r
000
MC/DC0%0/2
$lambda->{"sym"}{$token} //= symbol(qq[p${num}_${\"Lingy::Lang::RT"->nextID;}])
$lambda->{"sym"}{$token} symbol(qq[p${num}_${\"Lingy::Lang::RT"->nextID;}])
Truth table0%0/3
A //= B
A= $lambda->{"sym"}{$token}B= symbol(qq[p${num}_${\"Lingy::Lang::RT"->nextID;}])
ABresult
1X1
011
000
146 }
147  
148 sub read_hash_map {
149 0 ▶ my ($self, $type, $end) = @_;
Subroutine read_hash_map: not called CC=7 SCAR=40.3
Pod undocumented
150 0 my $tokens = $self->{tokens};
151 0 shift @$tokens;
152 0 my $hash = $type->new([]);
153 0 my $i = 0;
154 0 my $key;
155 0 while (1) {
156 0 ▶ while (@$tokens > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens > 0) 0 0 0
157 0 ▶ if ($tokens->[0] eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0] eq $end) 0 0 0
158 0 shift @$tokens;
159 0 ▶ err "Map literal must contain an even number of forms"
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
160 if defined $key;
161 0 return $hash;
162 }
163 0 $i++;
164 0 ▶ if ($i % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 0 0 0
165 0 $key = $self->read_form;
166 } else {
167 0 my $key_str = $type->_get_key($key);
168 err "Duplicate key: '$key'"
169 0 ▶ if exists $hash->{$key_str};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$key_str} 0 0 0
170 0 my $val= $self->read_form;
171 0 $hash->{$key_str} = $val;
172 0 undef $key;
173 }
174 }
175 0 ▶ $self->read_more and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->read_more and next 0 0 0
176 0 err "Reached end of input in 'read_hash_map'";
177 }
178 }
179  
180 1 my $string_re = qr/#?"((?:\\.|[^\\"])*)"/;
181 1 my $unescape = {
182 'n' => "\n",
183 't' => "\t",
184 '"' => '"',
185 '\\' => "\\",
186 };
187 sub read_scalar {
188 0 ▶ my ($self) = @_;
Subroutine read_scalar: not called CC=13 SCAR=52.0
Pod undocumented
189 0 my $scalar = local $_ = shift @{$self->{tokens}};
190  
191 0 ▶ while (/^#?"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^#?"/) 0 0 0
192 0 ▶ if (/^$string_re$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$string_re$/) 0 0 0
193 0 my $is_regex = /^#/;
194 0 s/^$string_re$/$1/;
195 0 s/\\([nt\"\\])/$unescape->{$1}/ge;
196 0 ▶ return $is_regex ? REGEX->new($_) : string($_);
Branch: 1 branch
BranchTrueFalseTotal
$is_regex ? : 0 0 0
197 }
198 0 ▶ if ($self->{repl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"repl"}) 0 0 0
199 0 my $line = Lingy::ReadLine::readline(1);
200 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
201 0 $_ .= "\n$line";
202 0 next;
203 }
204 }
205 0 err "Reached end of input looking for '\"'";
206 }
207 0 ▶ return nil if $_ eq 'nil';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "nil" 0 0 0
208 0 ▶ return true if $_ eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "true" 0 0 0
209 0 ▶ return false if $_ eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "false" 0 0 0
210 0 ▶ return KEYWORD->new($_) if /^:/;
Branch: 1 branch
BranchTrueFalseTotal
if /^:/ 0 0 0
211 0 ▶ return NUMBER->new($_) if /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^-?\d+$/ 0 0 0
212 0 ▶ return CHARACTER->read($_) if /^\\/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\\/ 0 0 0
213 0 ▶ err "Unmatched delimiter: '$_'" if /^[\)\]\}]$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[\)\]\}]$/ 0 0 0
214 0 return $self->read_symbol($_);
215 }
216  
217 # Defined separately to allow subclassing:
218 sub read_symbol {
219 0 ▶ my ($self, $symbol) = @_;
Subroutine read_symbol: not called CC=4 SCAR=30.0
Pod undocumented
220 0 ▶ if (my $ids = $self->{autogensym}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ids = $self->{"autogensym"}) 0 0 0
221 0 ▶ if ($symbol =~ m{^([^/]+)#$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol =~ m[^([^/]+)#$]) 0 0 0
222 0 ▶ my $id = $ids->{$1} //= RT->nextID();
Condition0%0/3
$ids->{$1} //= "Lingy::Lang::RT"->nextID
l!l&&r!l&&!r
000
MC/DC0%0/2
$ids->{$1} //= "Lingy::Lang::RT"->nextID
$ids->{$1} "Lingy::Lang::RT"->nextID
Truth table0%0/3
A //= B
A= $ids->{$1}B= "Lingy::Lang::RT"->nextID
ABresult
1X1
011
000
223 0 $symbol =~ s/#$/__${id}__auto__/;
224 }
225 }
226 0 symbol($symbol);
227 }
228  
229 sub read_quote {
230 0 ▶ my ($self, $quote) = @_;
Subroutine read_quote: not called CC=2 SCAR=17.9
Pod undocumented
231 0 shift @{$self->{tokens}};
232 0 my $form;
233 0 ▶ if ($quote eq 'quasiquote') {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq 'quasiquote') { } 0 0 0
234 0 $self->{autogensym} = {};
235 0 $form = $self->read_form;
236 0 delete $self->{autogensym};
237 } else {
238 0 $form = $self->read_form;
239 }
240 0 return list([symbol($quote), $form]);
241 }
242  
243 sub with_meta {
244 0 ▶ my ($self, $quote) = @_;
Subroutine with_meta: not called CC=1 SCAR=6.9
Pod undocumented
245 0 shift @{$self->{tokens}};
246  
247 0 my $meta = $self->read_form;
248 0 my $form = $self->read_form;
249  
250 0 bless [
251 symbol('with-meta'),
252 $form,
253 $meta,
254 ], LIST;
255 }
256  
257 1 1;
« lib/Lingy/Lang/Numbers.pm ↑ Summary lib/Lingy/ClojureREPL.pm »