File Coverage

blib/lib/Language/MinCaml/Code.pm
Criterion Covered Total %
statement 49 49 100.0
branch 11 12 91.6
condition n/a
subroutine 11 11 100.0
pod 1 5 20.0
total 72 77 93.5


line stmt bran cond sub pod time code
1             package Language::MinCaml::Code;
2 5     5   1245 use strict;
  5         8  
  5         168  
3 5     5   25 use base qw(Class::Accessor::Fast);
  5         10  
  5         4813  
4 5     5   18819 use Carp;
  5         10  
  5         494  
5 5     5   4828 use IO::File;
  5         73934  
  5         3732  
6              
7             __PACKAGE__->mk_ro_accessors(qw(line column));
8              
9             sub new {
10 11     11 1 74 bless { buffer => q{},
11             line => 0,
12             column => 0,
13             next_line => undef
14             }, __PACKAGE__;
15             }
16              
17             sub from_string {
18 8     8 0 68 my($class, $string) = @_;
19 8         60 my @lines = split(/\n/, $string);
20 8         30 my $self = $class->new;
21              
22             $self->{next_line} = sub {
23 9 100   9   21 if (@lines) {
24 6         15 $self->{buffer} = shift @lines;
25 6         12 $self->{line}++;
26 6         10 $self->{column} = 1;
27             }
28             else {
29 3         7 $self->{buffer} = q{};
30 3         4 $self->{line} = 0;
31 3         6 $self->{column} = 0;
32             }
33 9         14 return;
34 8         50 };
35              
36 8         21 $self->{next_line}->();
37 8         30 $self;
38             }
39              
40             sub from_file {
41 2     2 0 16 my($class, $file_path) = @_;
42 2 50       13 my $handler = IO::File->new($file_path, 'r')
43             or croak "Can't open '$file_path'.";
44 2         269 my $self = $class->new;
45              
46             $self->{next_line} = sub {
47 2 100   2   12 if ($handler->eof) {
48 1         33 $handler->close;
49 1         20 $self->{buffer} = q{};
50 1         3 $self->{line} = 0;
51 1         2 $self->{column} = 0;
52             }
53             else {
54 1         64 $self->{buffer} = $handler->getline;
55 1         35 chomp $self->{buffer};
56 1         2 $self->{line}++;
57 1         2 $self->{column} = 1;
58             }
59 2         12 return;
60 2         13 };
61              
62 2         6 $self->{next_line}->();
63 2         6 $self;
64             }
65              
66             sub buffer {
67 4     4 0 11 my $self = shift;
68 4 100       26 $self->{buffer} ? substr($self->{buffer}, $self->{column} - 1) : q{};
69             }
70              
71             sub forward {
72 3     3 0 12 my($self, $number) = @_;
73 3 100       8 $self->{column} += $number if $self->{buffer};
74 3 100       10 $self->{next_line}->() if $self->{column} > length($self->{buffer});
75 3         5 return;
76             }
77              
78             1;