Home | History | Annotate | Download | only in Runtime
      1 package ANTLR::Runtime::CommonToken;
      2 
      3 use Readonly;
      4 
      5 use Moose;
      6 
      7 use overload
      8     'bool' => \&not_eof,
      9     fallback => 1,
     10     ;
     11 
     12 with 'ANTLR::Runtime::Token';
     13 
     14 has 'type' => (
     15     is  => 'rw',
     16     isa => 'Int',
     17     required => 1,
     18 );
     19 
     20 has 'line' => (
     21     is  => 'rw',
     22     isa => 'Int',
     23     default => 0,
     24 );
     25 
     26 has 'char_position_in_line' => (
     27     is  => 'rw',
     28     isa => 'Int',
     29     # set to invalid position
     30     default => -1,
     31 );
     32 
     33 has 'channel' => (
     34     is  => 'rw',
     35     isa => 'Int',
     36     default => sub { ANTLR::Runtime::Token->DEFAULT_CHANNEL }
     37 );
     38 
     39 has 'input' => (
     40     is  => 'rw',
     41     isa => 'Maybe[ANTLR::Runtime::CharStream]',
     42 );
     43 
     44 # We need to be able to change the text once in a while.  If
     45 # this is non-null, then getText should return this.  Note that
     46 # start/stop are not affected by changing this.
     47 has 'text' => (
     48     is  => 'rw',
     49     isa => 'Maybe[Str]',
     50 );
     51 
     52 # What token number is this from 0..n-1 tokens; < 0 implies invalid index
     53 has 'index' => (
     54     is  => 'rw',
     55     isa => 'Int',
     56     default => -1,
     57 );
     58 
     59 # The char position into the input buffer where this token starts
     60 has 'start' => (
     61     is  => 'rw',
     62     isa => 'Int',
     63 );
     64 
     65 # The char position into the input buffer where this token stops
     66 has 'stop' => (
     67     is  => 'rw',
     68     isa => 'Int',
     69 );
     70 
     71 sub BUILD {
     72     my ($self, $arg_ref) = @_;
     73 
     74     if (exists $arg_ref->{token}) {
     75         my $token = $arg_ref->{token};
     76         $self->text($token->get_text());
     77         $self->type($token->get_type());
     78         $self->line($token->get_line());
     79         $self->index($token->get_token_index());
     80         $self->char_position_in_line($token->get_char_position_in_line());
     81         $self->channel($token->get_channel());
     82     }
     83     return;
     84 }
     85 
     86 sub get_type {
     87     my ($self) = @_;
     88     return $self->type;
     89 }
     90 
     91 sub set_type {
     92     my ($self, $value) = @_;
     93     $self->value($value);
     94     return;
     95 }
     96 
     97 
     98 sub get_line {
     99     my ($self) = @_;
    100     return $self->line;
    101 }
    102 
    103 sub set_line {
    104     my ($self, $line) = @_;
    105     $self->line($line);
    106     return;
    107 }
    108 
    109 sub get_text {
    110     my ($self) = @_;
    111 
    112     if (defined $self->text) {
    113         return $self->text;
    114     }
    115     if (!defined $self->input) {
    116         return undef;
    117     }
    118     $self->text($self->input->substring($self->start, $self->stop));
    119     return $self->text;
    120 }
    121 
    122 sub set_text {
    123     my ($self, $text) = @_;
    124     $self->text($text);
    125     return;
    126 }
    127 
    128 sub get_char_position_in_line {
    129     my ($self) = @_;
    130     return $self->char_position_in_line;
    131 }
    132 
    133 sub set_char_position_in_line {
    134     my ($self, $pos) = @_;
    135     $self->char_position_in_line($pos);
    136     return;
    137 }
    138 
    139 sub get_channel {
    140     my ($self) = @_;
    141     return $self->channel;
    142 }
    143 
    144 sub set_channel {
    145     my ($self, $channel) = @_;
    146     $self->channel($channel);
    147     return;
    148 }
    149 
    150 sub get_start_index {
    151     my ($self) = @_;
    152     return $self->start;
    153 }
    154 
    155 sub set_start_index {
    156     my ($self, $start) = @_;
    157     $self->start($start);
    158     return;
    159 }
    160 
    161 sub get_stop_index {
    162     my ($self) = @_;
    163     return $self->stop;
    164 }
    165 
    166 sub set_stop_index {
    167     my ($self, $stop) = @_;
    168     $self->stop($stop);
    169     return;
    170 }
    171 
    172 sub get_token_index {
    173     my ($self) = @_;
    174     return $self->index;
    175 }
    176 
    177 sub set_token_index {
    178     my ($self, $index) = @_;
    179     $self->index($index);
    180     return;
    181 }
    182 
    183 sub get_input_stream {
    184     my ($self) = @_;
    185     return $self->input;
    186 }
    187 
    188 sub set_input_stream {
    189     my ($self, $input) = @_;
    190     $self->input($input);
    191     return;
    192 }
    193 
    194 sub not_eof {
    195     my ($self) = @_;
    196     return $self->type != ANTLR::Runtime::Token->EOF;
    197 }
    198 
    199 =begin later
    200 
    201 	public String toString() {
    202 		String channelStr = "";
    203 		if ( channel>0 ) {
    204 			channelStr=",channel="+channel;
    205 		}
    206 		String txt = getText();
    207 		if ( txt!=null ) {
    208 			txt = txt.replaceAll("\n","\\\\n");
    209 			txt = txt.replaceAll("\r","\\\\r");
    210 			txt = txt.replaceAll("\t","\\\\t");
    211 		}
    212 		else {
    213 			txt = "<no text>";
    214 		}
    215 		return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
    216 	}
    217 
    218 =end later
    219 
    220 =cut
    221 
    222 no Moose;
    223 __PACKAGE__->meta->make_immutable();
    224 1;
    225