blib/lib/SExpression/Decode/Marpa.pm

filter: Statementstmt 20.8% 20 / 96 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 17.3% 4 / 23 Podpod 100% 1 / 1 totaltotal 12.8% 25 / 195 CC 36 SCAR 43.9
CC
36
Cov
12%
__ANON__
210.0
__ANON__
132.0
__ANON__
20.0
CRAP 80.8
↑ Summary
1 package SExpression::Decode::Marpa;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-04-10'; # DATE
5 1 our $DIST = 'SExpression-Decode-Marpa'; # DIST
6 1 our $VERSION = '0.002'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use MarpaX::Simple qw(gen_parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15 1 our @ISA = qw(Exporter);
16 1 our @EXPORT_OK = qw(from_sexp);
17  
18 my $parser = gen_parser(
19 grammar => <<'EOF',
20 :default ::= action => do_array
21  
22 :start ::= value
23  
24 value ::= number_int_radix action => do_number_int_radix
25 | number_int_hex action => do_number_int_hex
26 | number_int_oct action => do_number_int_oct
27 | number_int_bin action => do_number_int_bin
28 | number_inf_nan action => do_number_inf_nan
29 | number_int action => do_first
30 | number_float action => do_first
31 | char_unescaped action => do_char_unescaped
32 | char_escaped_1char action => do_char_escaped_1char
33 | char_escaped_ctrl action => do_char_escaped_ctrl
34 | string action => do_first
35 | list action => do_first
36 | vector action => do_first
37 | 't' action => do_true
38 | 'nil' action => do_undef
39 | atom action => do_first
40  
41 opt_dec_digits ~ [0-9]*
42 dec_digits ~ [0-9]+
43 hex_digits ~ [0-9A-Fa-f]+
44 oct_digits ~ [0-7]+
45 bin_digits ~ [01]+
46 alphanums ~ [0-9A-Za-z]+
47 sign ~ [+-]
48 r ~ [rR]
49 x ~ [xX]
50 o ~ [oO]
51 b ~ [bB]
52 e ~ [eE]
53  
54 number_int_radix ~ '#' dec_digits r sign alphanums
55 | '#' dec_digits r alphanums
56  
57 number_int_hex ~ '#' x sign hex_digits
58 | '#' x hex_digits
59  
60 number_int_oct ~ '#' o sign oct_digits
61 | '#' o oct_digits
62  
63 number_int_bin ~ '#' b sign bin_digits
64 | '#' b bin_digits
65  
66 number_int ~ sign dec_digits
67 | dec_digits
68  
69 unsigned_decimal ~ opt_dec_digits '.' dec_digits
70 | dec_digits '.' opt_dec_digits
71 decimal ~ sign unsigned_decimal
72 | unsigned_decimal
73 inf ~ 'INF'
74 nan ~ 'NaN'
75  
76 number_inf_nan ~ decimal e '+' inf
77 | decimal e '+' nan
78  
79 exp ~ e sign dec_digits
80 | e dec_digits
81  
82 number_float ~ decimal
83 | decimal exp
84  
85 ch1 ~ [^\\\(]
86 char_unescaped ~ '?' ch1
87  
88 ch2 ~ [^\^C]
89 char_escaped_1char ~ '?\' ch2
90  
91 alpha ~ [A-Za-z]
92 ch3 ~ 'C-'
93 | '^'
94 char_escaped_ctrl ~ '?\' ch3 alpha
95  
96 string ::= string_lexeme action => do_string
97 string_lexeme ~ quote in_string quote
98 quote ~ ["]
99 in_string ~ in_string_char*
100 in_string_char ~ [^"\\]
101 | '\' [\d\D]
102  
103 atom ~ [^\\\[\]\(\)\s".#]+
104  
105 vector ::= ('[' ']')
106 | ('[') list_elems_dot (']') action => do_first
107 | ('[') list_elems_nodot (']') action => do_first
108  
109 list ::= ('(' ')')
110 | ('(') list_elems_dot (')') action => do_first
111 | ('(') list_elems_nodot (')') action => do_first
112  
113 list_elems_nodot ::= value+
114 list_elems_dot ::= list_elems_nodot ('.') value action => do_list_elems_dot
115  
116 whitespace ~ [\s]+
117 :discard ~ whitespace
118  
119 EOF
120 actions => {
121 0 ▶ do_array => sub { shift; [@_] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 ▶ do_join => sub { shift; join "", @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 ▶ do_hash => sub { shift; +{map {@$_} @{ $_[0] } } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 ▶ do_first => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ do_second => sub { $_[2] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ do_number_int_radix => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
127 0 require Math::NumberBase;
128  
129 0 ▶ my ($base, $num) = $_[1] =~ /\A#([0-9]+)[rR](\w+)/ or die;
Branch: 1 branch
BranchTrueFalseTotal
() = $_[1] =~ /\A#([0-9]+)[rR](\w+)/ or die 0 0 0
130 0 $_[1] =~ s/\A([+-]?)//;
131 0 ▶ my $sign = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
132 0 ▶ ($sign eq '-' ? -1:1) * Math::NumberBase->new($base)->to_decimal(lc $num);
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
133 },
134 0 ▶ do_number_int_hex => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
135 0 my $str = $_[1];
136 0 $str =~ s/\A#[xX]//;
137 0 $str =~ s/\A([+-]?)//;
138 0 ▶ my $sign = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
139 0 ▶ ($sign eq '-' ? -1:1) * hex($str);
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
140 },
141 0 ▶ do_number_int_oct => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
142 0 my $str = $_[1];
143 0 $str =~ s/\A#[oO]//;
144 0 $str =~ s/\A([+-]?)//;
145 0 ▶ my $sign = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
146 0 ▶ ($sign eq '-' ? -1:1) * oct($str);
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
147 },
148 0 ▶ do_number_int_bin => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
149 0 my $str = $_[1];
150 0 $str =~ s/\A#[bB]//;
151 0 $str =~ s/\A([+-]?)//;
152 0 ▶ my $sign = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
153 0 ▶ ($sign eq '-' ? -1:1) * oct("0b$str");
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
154 },
155 0 ▶ do_number_inf_nan => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
156 0 my $str = $_[1];
157 0 $str =~ s/\A([+-]?)//;
158 0 ▶ my $sign = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
159 0 ▶ $str =~ /NaN/ ? "NaN" : ($sign eq '-' ? -1:1) * "Inf";
Branch: 2 branches
BranchTrueFalseTotal
$str =~ /NaN/ ? : 0 0 0
$sign eq '-' ? : 0 0 0
160 },
161 0 ▶ do_char_unescaped => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 substr($_[1], -1, 1);
163 },
164 0 ▶ do_char_escaped_1char => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
165 0 my $char = substr($_[1], -1, 1);
166 0 ▶ if ($char eq 'a') { return chr(7) }
Branch: 10 branches
BranchTrueFalseTotal
if ($char eq 'a') { } 0 0 0
elsif ($char eq 'b') { } 0 0 0
elsif ($char eq 't') { } 0 0 0
elsif ($char eq 'n') { } 0 0 0
elsif ($char eq 'v') { } 0 0 0
elsif ($char eq 'f') { } 0 0 0
elsif ($char eq 'r') { } 0 0 0
elsif ($char eq 'e') { } 0 0 0
elsif ($char eq 's') { } 0 0 0
elsif ($char eq 'd') { } 0 0 0
167 0 elsif ($char eq 'b') { return chr(8) }
168 0 elsif ($char eq 't') { return chr(9) }
169 0 elsif ($char eq 'n') { return chr(10) }
170 0 elsif ($char eq 'v') { return chr(11) }
171 0 elsif ($char eq 'f') { return chr(12) }
172 0 elsif ($char eq 'r') { return chr(13) }
173 0 elsif ($char eq 'e') { return chr(27) }
174 0 elsif ($char eq 's') { return chr(32) }
175 0 elsif ($char eq 'd') { return chr(127) }
176 0 else { return $char }
177 },
178 0 ▶ do_char_escaped_ctrl => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 0 my $char = lc substr($_[1], -1, 1);
180 0 return chr(ord($char) - 97+1);
181 },
182 0 ▶ do_string => sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
183 0 my $str0 = substr($_[1], 1, length($_[1])-2);
184 # XXX support \x... and \... octal
185 0 $str0 =~ s{\\(C-|^)([A-Za-z]) | # 1 2 control
186 \\x([0-91-f]{1,2}) | # 3 hex
187 \\([0-7]{1,3}) | # 4 octal
188 \\([^C\^])} # 5 other single char
189 {
190 0 ▶ if (defined $1) { chr(ord(lc $2) - 97+1) }
Branch: 3 branches
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
elsif (defined $3) { } 0 0 0
elsif (defined $4) { } 0 0 0
191 0 elsif (defined $3) { chr(hex $3) }
192 0 elsif (defined $4) { chr(oct $4) }
193 else {
194 0 my $c = $5;
195 0 ▶ if ($c eq 'a') { return chr(7) }
Branch: 10 branches
BranchTrueFalseTotal
if ($c eq 'a') { } 0 0 0
elsif ($c eq 'b') { } 0 0 0
elsif ($c eq 't') { } 0 0 0
elsif ($c eq 'n') { } 0 0 0
elsif ($c eq 'v') { } 0 0 0
elsif ($c eq 'f') { } 0 0 0
elsif ($c eq 'r') { } 0 0 0
elsif ($c eq 'e') { } 0 0 0
elsif ($c eq 's') { } 0 0 0
elsif ($c eq 'd') { } 0 0 0
196 0 elsif ($c eq 'b') { return chr(8) }
197 0 elsif ($c eq 't') { return chr(9) }
198 0 elsif ($c eq 'n') { return chr(10) }
199 0 elsif ($c eq 'v') { return chr(11) }
200 0 elsif ($c eq 'f') { return chr(12) }
201 0 elsif ($c eq 'r') { return chr(13) }
202 0 elsif ($c eq 'e') { return chr(27) }
203 0 elsif ($c eq 's') { return chr(32) }
204 0 elsif ($c eq 'd') { return chr(127) }
205 0 else { $c }
206 }
207 }egx;
208 0 $str0;
209 },
210 0 ▶ do_list_elems_dot => sub { [@{ $_[1] }, $_[2]] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ do_undef => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 ▶ do_true => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 ▶ do_empty_string => sub { '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 },
215 1 );
216  
217 0 ▶sub from_sexp {
Subroutine from_sexp: not called CC=1 SCAR=6.9
Pod documented
218 0 $parser->(shift);
219 }
220  
221 1 1;
222 # ABSTRACT: S-expression parser using Marpa
223  
224 __END__
↑ Summary