File Coverage

blib/lib/MarpaX/Languages/IDL/AST/Util.pm
Criterion Covered Total %
statement 6 26 23.0
branch 0 10 0.0
condition n/a
subroutine 2 3 66.6
pod 1 1 100.0
total 9 40 22.5


line stmt bran cond sub pod time code
1 1     1   16 use strict;
  1         2  
  1         45  
2 1     1   6 use warnings FATAL => 'all';
  1         1  
  1         572  
3              
4             package MarpaX::Languages::IDL::AST::Util;
5              
6             # ABSTRACT: Translate an IDL source to an AST - Tools
7              
8             our $VERSION = '0.005'; # VERSION
9              
10             # Marpa follows Unicode recommendation, i.e. perl's \R, that cannot be in a character class
11             our $NEWLINE_REGEXP = qr/(?>\x0D\x0A|\v)/;
12              
13              
14              
15             sub showLineAndCol {
16 0     0 1   my ($line, $col, $source) = @_;
17              
18 0 0         my $pointer = ($col > 0 ? '-' x ($col-1) : '') . '^';
19 0           my $content = '';
20              
21 0           my $prevpos = pos($source);
22 0           pos($source) = undef;
23 0           my $thisline = 0;
24 0           my $nbnewlines = 0;
25 0           my $eos = 0;
26 0           while ($source =~ m/\G(.*?)($NEWLINE_REGEXP|\Z)/scmg) {
27 0 0         if (++$thisline == $line) {
28 0           $content = substr($source, $-[1], $+[1] - $-[1]);
29 0 0         $eos = (($+[2] - $-[2]) > 0) ? 0 : 1;
30 0           last;
31             }
32             }
33 0           $content =~ s/\t/ /g;
34 0 0         if ($content) {
35 0           $nbnewlines = (substr($source, 0, pos($source)) =~ tr/\n//);
36 0 0         if ($eos) {
37 0           ++$nbnewlines; # End of string instead of $NEWLINE_REGEXP
38             }
39             }
40 0           pos($source) = $prevpos;
41              
42             # return "line:column $line:$col (Unicode newline count) $nbnewlines:$col (\\n count)\n\n$content\n$pointer";
43 0           return "line:column $nbnewlines:$col\n\n$content\n$pointer";
44             }
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             MarpaX::Languages::IDL::AST::Util - Translate an IDL source to an AST - Tools
57              
58             =head1 VERSION
59              
60             version 0.005
61              
62             =head1 SYNOPSIS
63              
64             use MarpaX::Languages::IDL::AST::Util;
65              
66             =head2 showLineAndCol($line, $col, $source)
67              
68             Pretty-printing of line No $line, column No $col in $source.
69              
70             =head1 DESCRIPTION
71              
72             This module contain some tools used by IDL to AST
73              
74             =head1 AUTHOR
75              
76             Jean-Damien Durand <jeandamiendurand@free.fr>
77              
78             =head1 COPYRIGHT AND LICENSE
79              
80             This software is copyright (c) 2014 by Jean-Damien Durand.
81              
82             This is free software; you can redistribute it and/or modify it under
83             the same terms as the Perl 5 programming language system itself.
84              
85             =cut