Tea/Match2AST.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 47 CC 5 SCAR 24.8
CC
5
Cov
0%
match2ast
20.0
resolve_line
6.0
BEGIN
2.0
CRAP 12.0
1 package Language::Tea::Match2AST;
2  
3 use strict;
4 use warnings;
5  
6 sub match2ast {
7 my $root = shift;
8 my ( $filename, $source_lines ) = @_;
9  
10 my $visitor_ast;
11 $visitor_ast = sub {
12 my ($root) = shift;
13 if ( ref($root) eq 'Pugs::Runtime::Match' ) {
14  
15 #print "visit ",ref($root), "\n";
16  
17 my $h = {};
18 my @keys = sort keys %$root;
19 return "" . $root unless @keys;
20 for my $key (@keys) {
21 my $res =
22 Language::Tea::Traverse::visit_prefix( $root->{$key},
23 $visitor_ast, @_ );
24 if ( defined $res ) {
25 $h->{$key} = $res;
26 }
27 else {
28 $h->{$key} = $root->{$key};
29 }
30 }
31  
32 $h->{info} = {
33 from => $root->from,
34 to => $root->to,
35 file => $filename,
36 resolve_line( $root->from, $source_lines ),
37 };
38  
39 return $h;
40 }
41 };
42  
43 return Language::Tea::Traverse::visit_prefix( $root, $visitor_ast );
44  
45 }
46  
47 sub resolve_line {
48 my $from = shift;
49 my $source_lines = shift;
50 my $cnt_lines = 0;
51 my $cnt_chars = 0;
52 my $col = 0;
53 while ( $cnt_chars <= $from ) {
54 $col = 1 + $from - $cnt_chars;
55 $cnt_chars += length( $source_lines->[ $cnt_lines++ ] );
56 last unless exists $source_lines->[$cnt_lines];
57 }
58 return ( line => $cnt_lines, col => $col );
59 }
60  
61 1;