line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Parse::Highlife::Transformer; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
4
|
1
|
|
|
1
|
|
4
|
use Parse::Highlife::Utils qw(params); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
5
|
1
|
|
|
1
|
|
6
|
use Data::Dump qw(dump); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
576
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new |
8
|
|
|
|
|
|
|
{ |
9
|
0
|
|
|
0
|
0
|
|
my( $class, @args ) = @_; |
10
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
11
|
0
|
|
|
|
|
|
return $self -> _init( @args ); |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _init |
15
|
|
|
|
|
|
|
{ |
16
|
0
|
|
|
0
|
|
|
my( $self, @args ) = @_; |
17
|
0
|
|
|
|
|
|
$self->{'transformers'} = {}; |
18
|
0
|
|
|
|
|
|
$self->{'stringifiers'} = {}; |
19
|
0
|
|
|
|
|
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub transformer |
23
|
|
|
|
|
|
|
{ |
24
|
|
|
|
|
|
|
my( $self, $name, $coderef ) |
25
|
|
|
|
|
|
|
= params( \@_, |
26
|
|
|
|
|
|
|
-rule => '', |
27
|
0
|
|
|
0
|
|
|
-fn => sub {}, |
28
|
0
|
|
|
0
|
0
|
|
); |
29
|
0
|
|
|
|
|
|
$self->{'transformers'}->{$name} = $coderef; |
30
|
0
|
|
|
|
|
|
return 1; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub stringifier |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
my( $self, $name, $coderef ) |
36
|
|
|
|
|
|
|
= params( \@_, |
37
|
|
|
|
|
|
|
-rule => '', |
38
|
0
|
|
|
0
|
|
|
-fn => sub {}, |
39
|
0
|
|
|
0
|
0
|
|
); |
40
|
0
|
|
|
|
|
|
$self->{'stringifiers'}->{$name} = $coderef; |
41
|
0
|
|
|
|
|
|
return 1; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub transform |
45
|
|
|
|
|
|
|
{ |
46
|
0
|
|
|
0
|
0
|
|
my( $self, $ast, @params ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $transformer_name = $ast->{'rulename'}; |
49
|
|
|
|
|
|
|
#print "-- TRANSFORM $transformer_name --\n"; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if( exists $self->{'transformers'}->{$transformer_name} ) { |
52
|
0
|
|
|
|
|
|
my $new_ast = $self->{'transformers'}->{$transformer_name}->( $self, $ast, @params ); |
53
|
0
|
0
|
0
|
|
|
|
return ( defined $new_ast && ref $new_ast eq 'Parse::Highlife::AST' ? $new_ast : $ast ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
0
|
|
|
|
|
|
return $self->transform_children( $ast, @params ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub transform_children |
61
|
|
|
|
|
|
|
{ |
62
|
0
|
|
|
0
|
0
|
|
my( $self, $ast, @params ) = @_; |
63
|
0
|
0
|
|
|
|
|
if( $ast->{'category'} eq 'group' ) { |
64
|
0
|
|
|
|
|
|
$ast->{'children'} = |
65
|
|
|
|
|
|
|
[ |
66
|
0
|
|
|
|
|
|
map { $self->transform( $_, @params ) } @{$ast->{'children'}} |
|
0
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
]; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
# leaf's are not transformed by default |
70
|
0
|
|
|
|
|
|
return $ast; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub stringify |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
0
|
0
|
|
my( $self, $ast, @params ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $stringifier_name = $ast->{'rulename'}; |
78
|
|
|
|
|
|
|
#print "-- STRINGIFY $stringifier_name --\n"; |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if( exists $self->{'stringifiers'}->{$stringifier_name} ) { |
81
|
0
|
|
|
|
|
|
my $string = $self->{'stringifiers'}->{$stringifier_name}->( $self, $ast, @params ); |
82
|
0
|
0
|
0
|
|
|
|
return ( defined $string && ! ref $string ? $string : '?' ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
0
|
|
|
|
|
|
return $self->stringify_children( $ast, @params ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub stringify_children |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
|
|
0
|
0
|
|
my( $self, $ast, @params ) = @_; |
92
|
0
|
0
|
|
|
|
|
if( $ast->{'category'} eq 'group' ) { |
93
|
0
|
|
|
|
|
|
return join '', map { $self->stringify( $_, @params ) } @{$ast->{'children'}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
# leaf's are not stringified by default |
96
|
0
|
|
|
|
|
|
return $ast->{'children'}; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|