Arithmetic/NoHash.pm

filter: Statementstmt 100% 19 / 19 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.3% 26 / 27 CC 2 SCAR 3.0
CC
2
Cov
95%
parse_arithmetic
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« Arithmetic.pm ↑ Summary Arithmetic/Marpa.pm »
1 package PERLANCAR::Parse::Arithmetic::NoHash;
2  
3 2 our $DATE = '2016-06-18'; # DATE
4 2 our $VERSION = '0.004'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my ($match_top,
11 $match_add, $match_op_add,
12 $match_mult, $match_op_mult,
13 $match_pow,
14 );
15  
16 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our @EXPORT_OK = qw(parse_arithmetic);
18  
19 3 ▶sub parse_arithmetic {
Subroutine parse_arithmetic: called CC=2 SCAR=7.1
Pod documented
20 state $RE =
21 qr{
22 (?&TOP)
23 (?{
24 $match_top = $^R;
25 })
26  
27 (?(DEFINE)
28  
29 (?<TOP>
30 ^\s* (?&EXPR) \s*$
31 )
32  
33 (?<EXPR>
34 (?&MULT_EXPR)
35 (?{
36 $match_add = $^R
37 })
38 (?: \s* ([+-])
39 (?{
40 $match_op_add = $^N;
41 })
42 \s* (?&MULT_EXPR)
43 (?{
44 $match_add = $match_op_add eq '+' ? $match_add + $^R : $match_add - $^R;
45 })
46 )*
47 )
48  
49 (?<MULT_EXPR>
50 (?&POW_EXPR)
51 (?{
52 $match_mult = $^R;
53 })
54 (?: \s* ([*/])
55 (?{
56 $match_op_mult = $^N;
57 }) \s*
58 (?&POW_EXPR)
59 (?{
60 $match_mult = $match_op_mult eq '*' ? $match_mult * $^R : $match_mult / $^R;
61 })
62 )*
63 )
64  
65 (?<POW_EXPR>
66 (?&TERM)
67 (?{
68 $match_pow = [$^R];
69 })
70 (?: \s* \*\* \s* (?&TERM)
71 (?{
72 unshift @$match_pow, $^R;
73 })
74 )*
75 (?{
76 # because ** is right-to-left, we collect first then
77 # apply from right to left
78 my $res = $match_pow->[0];
79 for (1..$#{$match_pow}) {
80 $res = $match_pow->[$_] ** $res;
81 }
82 $res;
83 })
84 )
85  
86 (?<TERM>
87 \( \s* (?&EXPR)
88 (?{
89 $^R;
90 })
91 \s* \)
92 | (?&LITERAL)
93 (?{
94 $^R;
95 })
96 )
97  
98 (?<LITERAL>
99 (-?(?:\d+|\d*\.\d+))
100 (?{
101 $^N;
102 })
103 )
104 )
105 3 }x;
106 3 ▶ $_[0] =~ $RE or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$RE/ or return undef 3 0 3
107 3 $match_top;
108 }
109  
110 2 1;
111 # ABSTRACT: Parse arithmetic expmatchsion
112  
113 __END__
« Arithmetic.pm ↑ Summary Arithmetic/Marpa.pm »