1 package ANTLR::Runtime::Parser; 2 3 use Readonly; 4 use Carp; 5 6 use Moose; 7 8 extends 'ANTLR::Runtime::BaseRecognizer'; 9 10 has 'input' => ( 11 is => 'rw', 12 does => 'ANTLR::Runtime::TokenStream' 13 ); 14 15 sub reset { 16 my ($self) = @_; 17 18 $self->SUPER::reset(); # reset all recognizer state variables 19 if (defined $self->input) { 20 $self->input->seek(0); # rewind the input 21 } 22 } 23 24 sub get_current_input_symbol { 25 my ($self, $input) = @_; 26 return $self->input->LT(1); 27 } 28 29 sub get_missing_symbol { 30 my ($self, $arg_ref) = @_; 31 my $input = $arg_ref->{input}; 32 my $exception = $arg_ref->{exception}; 33 my $expected_token_type = $arg_ref->{expected_token_type}; 34 my $follow = $arg_ref->{follow}; 35 36 my $token_text; 37 if ($expected_token_type == ANTLR::Runtime::Token->EOF) { 38 $token_text = '<missing EOF>'; 39 } 40 else { 41 $token_text = '<missing ' . $self->get_token_names()->[$expected_token_type] . '>'; 42 } 43 44 my $t = ANTLR::Runtime::CommonToken->new({ 45 type => $expected_token_type, 46 text => $token_text 47 }); 48 my $current = $input->LT(1); 49 if ($current->get_type() == ANTLR::Runtime::Token->EOF) { 50 $current = $input->LT(-1); 51 } 52 $t->set_line($current->get_line()); 53 $t->set_char_position_in_line($current->get_char_position_in_line()); 54 $t->set_channel($self->DEFAULT_TOKEN_CHANNEL); 55 56 return $t; 57 } 58 59 sub set_token_stream { 60 my ($self, $input) = @_; 61 62 $self->input(undef); 63 $self->reset(); 64 $self->input($input); 65 } 66 67 sub get_token_stream { 68 my ($self) = @_; 69 70 return $self->input; 71 } 72 73 sub get_source_name { 74 my ($self) = @_; 75 return $self->input->get_source_name(); 76 } 77 78 sub trace_in { 79 my ($self, $rule_name, $rule_index) = @_; 80 81 $self->SUPER::trace_in($rule_name, $rule_index, $self->input->LT(1)); 82 } 83 84 sub trace_out { 85 my ($self, $rule_name, $rule_index) = @_; 86 87 $self->SUPER::trace_out($rule_name, $rule_index, $self->input->LT(1)); 88 } 89 90 no Moose; 91 __PACKAGE__->meta->make_immutable(); 92 1; 93 __END__ 94