File Coverage

blib/lib/Language/Expr/Compiler/Base.pm
Criterion Covered Total %
statement 19 28 67.8
branch n/a
condition n/a
subroutine 6 8 75.0
pod 3 3 100.0
total 28 39 71.7


line stmt bran cond sub pod time code
1             package Language::Expr::Compiler::Base;
2              
3             our $DATE = '2016-06-29'; # DATE
4             our $VERSION = '0.27'; # VERSION
5              
6 2     2   803 use 5.010;
  2         5  
7 2     2   6 use strict;
  2         3  
  2         31  
8 2     2   6 use warnings;
  2         3  
  2         44  
9              
10 2     2   796 use UUID::Tiny ':std';
  2         26748  
  2         344  
11              
12 2     2   684 use Mo qw(build default);
  2         726  
  2         10  
13              
14             # [[type, uuid, data], ...]
15             has markers => (is => 'rw', default => sub { [] });
16             has func_mapping => (is => 'rw', default => sub { {} });
17             has hook_var => (is => 'rw');
18             has hook_func => (is => 'rw');
19              
20             sub new_marker {
21 31     31 1 54 my ($self, $type, $data) = @_;
22 31         114 my $uuid = UUID::Tiny::create_uuid_as_string(UUID_V4);
23             #my $uuid = int(9000*rand()+1000);
24             #print "DEBUG: Creating new marker: type=$type, uuid=$uuid, data=", ($data // "undef"), "\n\n";
25 31         2784 push @{ $self->markers }, [$type, $uuid, $data];
  31         106  
26 31         311 $uuid;
27             }
28              
29             sub marker_ids {
30 0     0 1   my ($self) = @_;
31 0           map {$_->[1]} @{ $self->markers };
  0            
  0            
32             }
33              
34             sub marker_ids_re {
35 0     0 1   my ($self) = @_;
36 0           my $re = "(?:" . join("|", map {$_->[1]} @{ $self->markers }) . ")";
  0            
  0            
37 0           qr/$re/;
38             }
39              
40             1;
41             # ABSTRACT: Base class for Expr compilers
42              
43             __END__
44              
45             =pod
46              
47             =encoding UTF-8
48              
49             =head1 NAME
50              
51             Language::Expr::Compiler::Base - Base class for Expr compilers
52              
53             =head1 VERSION
54              
55             This document describes version 0.27 of Language::Expr::Compiler::Base (from Perl distribution Language-Expr), released on 2016-06-29.
56              
57             =head1 ATTRIBUTES
58              
59             =head2 markers => ARRAYREF
60              
61             Used to mark compile output string with various unique strings, and later on
62             revisit these markers and substitute for other, final values. This technique is
63             kind of a hack, used for subexpression, inserting PHP use() statement (because
64             they must be processed outward to inward), etc.
65              
66             =head2 func_mapping => HASHREF
67              
68             Map Expr function to target language's function/method/property.
69              
70             =head2 hook_var
71              
72             Can be set to a coderef that will be called during parsing whenever variable is
73             encountered. The coderef is called with variable name as argument, and expected
74             to return target language code to handle the variable. By default, if this
75             attribute is not set, variable in expression is returned as is (e.g. '$foo'
76             becomes '$foo' in Perl), which means some will result in error (e.g. '${name
77             that contains some symbols that makes it invalid Perl}').
78              
79             If the coderef returns undef, the default behaviour is used.
80              
81             Note that due to current limitation of Perl regex and/or Regexp::Grammars, you
82             cannot use any regex in your hook_var.
83              
84             =head2 hook_func
85              
86             Can be set to a coderef that will be called during parsing whenever a function
87             call is encountered. The coderef is called as its arguments function name and
88             list of arguments and expected to return target language code to handle the
89             function call. By default, if this attribute is not set, variable in expression
90             is returned as is (e.g. 'foo(1, 2, 3)' becomes 'foo(1, 2, 3)' in Perl).
91              
92             If the coderef returns undef, the default behaviour is used.
93              
94             Note that due to current limitation of Perl regex and/or Regexp::Grammars, you
95             cannot use any regex in your hook_var.
96              
97             =head1 METHODS
98              
99             =head2 new_marker(TYPE[, DATA]) => UUID
100              
101             Create a new marker. Return a unique ID to be placed in compiled
102             output.
103              
104             =head2 marker_ids() => ARRAY
105              
106             Return an array of all marker IDs.
107              
108             =head2 marker_ids_re() => STRING
109              
110             Return a regex that matches marker IDs.
111              
112             =head1 HOMEPAGE
113              
114             Please visit the project's homepage at L<https://metacpan.org/release/Language-Expr>.
115              
116             =head1 SOURCE
117              
118             Source repository is at L<https://github.com/perlancar/perl-Language-Expr>.
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Language-Expr>
123              
124             When submitting a bug or request, please include a test-file or a
125             patch to an existing test-file that illustrates the bug or desired
126             feature.
127              
128             =head1 AUTHOR
129              
130             perlancar <perlancar@cpan.org>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2016 by perlancar@cpan.org.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut