Home | History | Annotate | Download | only in Runtime
      1 package ANTLR::Runtime::RecognitionException;
      2 
      3 use Carp;
      4 use Readonly;
      5 
      6 use Moose;
      7 use Moose::Util::TypeConstraints;
      8 
      9 extends 'ANTLR::Runtime::Exception';
     10 
     11 has 'input' => (
     12     is   => 'ro',
     13     does => 'ANTLR::Runtime::IntStream',
     14     required => 1,
     15 );
     16 
     17 has 'index' => (
     18     is  => 'ro',
     19     isa => 'Int',
     20     default => 0,
     21 );
     22 
     23 has 'token' => (
     24     is   => 'ro',
     25     does => 'ANTLR::Runtime::Token',
     26 );
     27 
     28 has 'node' => (
     29     is  => 'ro',
     30     isa => 'Any',
     31 );
     32 
     33 subtype 'Char'
     34     => as 'Str'
     35     => where { $_ eq '-1' || length == 1 };
     36 
     37 has 'c' => (
     38     is  => 'ro',
     39     isa => 'Maybe[Char]',
     40 );
     41 
     42 has 'line' => (
     43     is  => 'ro',
     44     isa => 'Int',
     45     default => 0,
     46 );
     47 
     48 has 'char_position_in_line' => (
     49     is  => 'ro',
     50     isa => 'Int',
     51     default => 0,
     52 );
     53 
     54 has 'approximate_line_info' => (
     55     is  => 'rw',
     56     isa => 'Bool',
     57 );
     58 
     59 sub BUILDARGS {
     60     my ($class, @args) = @_;
     61     my $args = $class->SUPER::BUILDARGS(@args);
     62 
     63     my $new_args = { %$args };
     64     my $input = $args->{input};
     65     $new_args->{input} = $input;
     66     $new_args->{index} = $input->index();
     67 
     68     if ($input->does('ANTLR::Runtime::TokenStream')) {
     69         my $token = $input->LT(1);
     70         $new_args->{token} = $token;
     71         $new_args->{line} = $token->get_line();
     72         $new_args->{char_position_in_line} = $token->get_char_position_in_line();
     73     }
     74 
     75     if ($input->does('ANTLR::Runtime::TreeNodeStream')) {
     76         # extract_information_from_tree_node_stream($input);
     77     }
     78     elsif ($input->does('ANTLR::Runtime::CharStream')) {
     79         $new_args->{c} = $input->LA(1);
     80         $new_args->{line} = $input->get_line();
     81         $new_args->{char_position_in_line} = $input->get_char_position_in_line();
     82     }
     83     else {
     84         $new_args->{c} = $input->LA(1);
     85     }
     86 
     87     return $new_args;
     88 }
     89 
     90 sub get_unexpected_type {
     91     my ($self) = @_;
     92 
     93     if ($self->input->isa('ANTLR::Runtime::TokenStream')) {
     94         return $self->token->get_type();
     95     } else {
     96         return $self->c;
     97     }
     98 }
     99 
    100 sub get_c {
    101     my ($self) = @_;
    102     return $self->c;
    103 }
    104 
    105 sub get_line {
    106     my ($self) = @_;
    107     return $self->line;
    108 }
    109 
    110 sub get_char_position_in_line {
    111     my ($self) = @_;
    112     return $self->char_position_in_line;
    113 }
    114 
    115 sub get_token {
    116     my ($self) = @_;
    117     return $self->token;
    118 }
    119 
    120 no Moose;
    121 __PACKAGE__->meta->make_immutable();
    122 1;
    123