• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package ANTLR::Runtime::ANTLRFileStream;
2
3use Carp;
4use Readonly;
5
6use Moose;
7
8extends 'ANTLR::Runtime::ANTLRStringStream';
9
10has 'file_name' => (
11    is  => 'ro',
12    isa => 'Str',
13    required => 1,
14);
15
16sub BUILDARGS {
17    my ($class, @args) = @_;
18    my $args = $class->SUPER::BUILDARGS(@args);
19
20    my $file_name = $args->{file_name};
21    if (!defined $file_name) {
22        return;
23    }
24
25    my $fh;
26    my $encoding = $args->{encoding};
27    if (defined $encoding) {
28        open $fh, "<:encoding($encoding)", $file_name
29            or croak "Can't open $file_name: $!";
30    }
31    else {
32        open $fh, '<', $file_name
33            or croak "Can't open $file_name: $!";
34    }
35
36    my $content;
37    {
38        local $/;
39        $content = <$fh>;
40    }
41    close $fh or carp "Can't close $fh: $!";
42
43    $args->{input} = $content;
44
45    return $args;
46}
47
48sub load {
49    my ($self, $file_name, $encoding) = @_;
50
51    if (!defined $file_name) {
52        return;
53    }
54
55    my $fh;
56    if (defined $encoding) {
57        open $fh, "<:encoding($encoding)", $file_name
58            or croak "Can't open $file_name: $!";
59    }
60    else {
61        open $fh, '<', $file_name
62            or croak "Can't open $file_name: $!";
63    }
64
65    my $content;
66    {
67        local $/;
68        $content = <$fh>;
69    }
70    close $fh or carp "Can't close $fh: $!";
71
72    $self->input($content);
73    return;
74}
75
76sub get_source_name {
77    my ($self) = @_;
78    return $self->file_name;
79}
80
81no Moose;
82__PACKAGE__->meta->make_immutable();
831;
84__END__
85