File Coverage

blib/lib/Language/Expr/EvaluatorRole.pm
Criterion Covered Total %
statement 60 63 95.2
branch 40 46 86.9
condition 17 24 70.8
subroutine 8 8 100.0
pod 2 2 100.0
total 127 143 88.8


line stmt bran cond sub pod time code
1             package Language::Expr::EvaluatorRole;
2              
3             our $DATE = '2016-07-03'; # DATE
4             our $VERSION = '0.29'; # VERSION
5              
6 5     5   2183 use 5.010;
  5         12  
7 5     5   21 use strict;
  5         7  
  5         83  
8 5     5   15 use warnings;
  5         10  
  5         95  
9              
10 5     5   15 use Role::Tiny;
  5         5  
  5         28  
11              
12             requires 'rule_pair_simple';
13             requires 'rule_pair_string';
14             requires 'rule_or_xor';
15             requires 'rule_ternary';
16             requires 'rule_and';
17             requires 'rule_bit_or_xor';
18             requires 'rule_bit_and';
19             requires 'rule_comparison3';
20             requires 'rule_comparison';
21             requires 'rule_bit_shift';
22             requires 'rule_add';
23             requires 'rule_mult';
24             requires 'rule_unary';
25             requires 'rule_power';
26             requires 'rule_subscripting_var';
27             requires 'rule_subscripting_expr';
28             requires 'rule_array';
29             requires 'rule_hash';
30             requires 'rule_undef';
31             requires 'rule_squotestr';
32             requires 'rule_dquotestr';
33             requires 'rule_var';
34             requires 'rule_func';
35             requires 'rule_func_map';
36             requires 'rule_func_grep';
37             requires 'rule_func_usort';
38             requires 'rule_bool';
39             requires 'rule_num';
40             requires 'rule_parenthesis';
41             requires 'expr_preprocess';
42             requires 'expr_postprocess';
43              
44             sub parse_dquotestr {
45 114     114 1 141 my ($self, @parts) = @_;
46 114 100       314 if (ref($parts[0]) eq 'ARRAY') { splice @parts, 0, 1, @{ $parts[0] } }
  110         144  
  110         256  
47 114         126 my @res;
48             my @sbuf;
49              
50             #say "D:parse_dquotestr:parts = ", join(", ", @parts);
51             #for (grep {defined} @parts) {
52 114         196 for (@parts) {
53 5     5   1212 no warnings;
  5         10  
  5         2258  
54 114         196 my $s01 = substr($_, 0, 1);
55 114         133 my $s02 = substr($_, 0, 2);
56 114         117 my $l = length();
57 114 100 100     1816 if ($_ eq "\\'" ) { push @sbuf, "'" }
  2 100 66     5  
    100 33        
    100 33        
    100 100        
    100 66        
    100 100        
    100 66        
    100          
    100          
    50          
    50          
    50          
    100          
    100          
58 2         5 elsif ($_ eq "\\\"") { push @sbuf, '"' }
59 2         6 elsif ($_ eq "\\\\") { push @sbuf, "\\" }
60 2         6 elsif ($_ eq "\\\$") { push @sbuf, '$' }
61 2         7 elsif ($_ eq "\\t" ) { push @sbuf, "\t" }
62 2         6 elsif ($_ eq "\\n" ) { push @sbuf, "\n" }
63 2         6 elsif ($_ eq "\\f" ) { push @sbuf, "\f" }
64 2         6 elsif ($_ eq "\\b" ) { push @sbuf, "\b" }
65 2         6 elsif ($_ eq "\\a" ) { push @sbuf, "\a" }
66 2         5 elsif ($_ eq "\\e" ) { push @sbuf, "\e" }
67             elsif ($l >= 2 && $l <= 4 && $s01 eq "\\" &&
68             substr($_, 1, 1) >= "0" && substr($_, 1, 1) <= "7") {
69             # \000 octal escape
70 0         0 push @sbuf, chr(oct(substr($_, 1))) }
71             elsif ($l >= 3 && $l <= 4 && $s02 eq "\\x") {
72             # \xFF hex escape
73 0         0 push @sbuf, chr(hex(substr($_, 1))) }
74             elsif ($l >= 5 && $l <= 8 && substr($_, 0, 3) eq "\\x{") {
75             # \x{1234} wide hex escape
76 0         0 push @sbuf, chr(hex(substr($_, 3, length()-4))) }
77             elsif ($s02 eq '${') {
78             # ${var}
79 2 50       6 push @res, {type=>"STR", value=>join("", @sbuf)} if @sbuf; @sbuf=();
  2         5  
80 2         11 push @res, {type=>"VAR", value=>substr($_, 2, length()-3)} }
81             elsif ($s01 eq '$') {
82             # $var
83 2 50       10 push @res, {type=>"STR", value=>join("", @sbuf)} if @sbuf; @sbuf=();
  2         5  
84 2         12 push @res, {type=>"VAR", value=>substr($_, 1, length()-1)} }
85             else {
86 90         202 push @sbuf, $_;
87             }
88             }
89 114 100       306 push @res, {type=>"STR", value=>join("", grep {defined} @sbuf)} if @sbuf;
  110         459  
90 114         330 \@res;
91             }
92              
93             sub parse_squotestr {
94 20     20 1 29 my ($self, @parts) = @_;
95 20 100       58 if (ref($parts[0]) eq 'ARRAY') { splice @parts, 0, 1, @{ $parts[0] } }
  18         19  
  18         42  
96 20         18 my @res;
97             my @sbuf;
98              
99             #say "D:parse_dquotestr:parts = ", join(", ", @parts);
100             #for (grep {defined} @parts) {
101 20         37 for (@parts) {
102 5     5   23 no warnings;
  5         6  
  5         628  
103 24 100       63 if ($_ eq "\\'" ) { push @sbuf, "'" }
  2 100       4  
104 2         6 elsif ($_ eq "\\\\") { push @sbuf, "\\" }
105 20         36 else { push @sbuf, $_ }
106             }
107 20 50       51 push @res, {type=>"STR", value=>join("", grep {defined} @sbuf)} if @sbuf;
  24         80  
108 20         55 \@res;
109             }
110              
111             1;
112             # ABSTRACT: Specification for Language::Expr interpreter/compiler
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Language::Expr::EvaluatorRole - Specification for Language::Expr interpreter/compiler
123              
124             =head1 VERSION
125              
126             This document describes version 0.29 of Language::Expr::EvaluatorRole (from Perl distribution Language-Expr), released on 2016-07-03.
127              
128             =head1 METHODS
129              
130             =head2 parse_dquotestr($raw_parts) -> [{type=>"STR"|"VAR"}, value=>...}, ...]
131              
132             Instead of parsing parts themselves, consumers can use this method (typically in
133             their rule_dquotestr). This method converts each Expr escapes into Perl string
134             and variables. For example:
135              
136             parse_dquotestr('abc', "\\t", '\\\\', '$foo', ' ', '${bar baz}') -> (
137             {type=>"STR", value=>'abc\t\\'},
138             {type=>"VAR", value=>'foo'},
139             {type=>"STR", value=>' '},
140             {type=>"VAR", value=>'bar baz'},
141             )
142              
143             =head2 parse_squotestr($raw_parts) => [{type=>STR, value=>...}, ...]
144              
145             Instead of parsing parts themselves, consumers can use this method (typically in
146             their rule_squotestr). This method converts Expr single quoted string into Perl
147             string.
148              
149             parse_dquotestr('abc', "\\t", '\\\\', '$foo', ' ', '${bar baz}') -> (
150             {type=>"STR", value=>'abc\t\\$foo ${bar baz}'},
151             )
152              
153             =head1 HOMEPAGE
154              
155             Please visit the project's homepage at L<https://metacpan.org/release/Language-Expr>.
156              
157             =head1 SOURCE
158              
159             Source repository is at L<https://github.com/sharyanto/perl-Language-Expr>.
160              
161             =head1 BUGS
162              
163             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Language-Expr>
164              
165             When submitting a bug or request, please include a test-file or a
166             patch to an existing test-file that illustrates the bug or desired
167             feature.
168              
169             =head1 AUTHOR
170              
171             perlancar <perlancar@cpan.org>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2016 by perlancar@cpan.org.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut