Class: Antlr4::Runtime::CommonToken

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/common_token.rb

Direct Known Subclasses

TokenTagToken

Constant Summary collapse

@@EMPTY_SOURCE =
OpenStruct.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil) ⇒ CommonToken

Returns a new instance of CommonToken



19
20
21
22
23
24
25
26
# File 'lib/antlr4/runtime/common_token.rb', line 19

def initialize(type = nil)
  @char_position_in_line = -1
  @channel = Token::DEFAULT_CHANNEL
  @index = -1
  @type = type
  @source = @@EMPTY_SOURCE
  @_text = nil
end

Instance Attribute Details

#_textObject

Returns the value of attribute _text



17
18
19
# File 'lib/antlr4/runtime/common_token.rb', line 17

def _text
  @_text
end

#channelObject

Returns the value of attribute channel



12
13
14
# File 'lib/antlr4/runtime/common_token.rb', line 12

def channel
  @channel
end

#char_position_in_lineObject

Returns the value of attribute char_position_in_line



11
12
13
# File 'lib/antlr4/runtime/common_token.rb', line 11

def char_position_in_line
  @char_position_in_line
end

#indexObject

Returns the value of attribute index



14
15
16
# File 'lib/antlr4/runtime/common_token.rb', line 14

def index
  @index
end

#lineObject

Returns the value of attribute line



10
11
12
# File 'lib/antlr4/runtime/common_token.rb', line 10

def line
  @line
end

#sourceObject

Returns the value of attribute source



13
14
15
# File 'lib/antlr4/runtime/common_token.rb', line 13

def source
  @source
end

#startObject

Returns the value of attribute start



15
16
17
# File 'lib/antlr4/runtime/common_token.rb', line 15

def start
  @start
end

#stopObject

Returns the value of attribute stop



16
17
18
# File 'lib/antlr4/runtime/common_token.rb', line 16

def stop
  @stop
end

#typeObject

Returns the value of attribute type



9
10
11
# File 'lib/antlr4/runtime/common_token.rb', line 9

def type
  @type
end

Class Method Details

.create1(source, type, channel, start, stop) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/antlr4/runtime/common_token.rb', line 28

def self.create1(source, type, channel, start, stop)
  result = CommonToken.new(type)
  result.source = source
  result.channel = channel
  result.start = start
  result.stop = stop
  unless source.a.nil?
    result.line = source.a.line
    result.char_position_in_line = source.a.char_position_in_line
  end
  result
end

.create2(type, text) ⇒ Object



41
42
43
44
45
# File 'lib/antlr4/runtime/common_token.rb', line 41

def self.create2(type, text)
  result = CommonToken.new(type)
  result._text = text
  result
end

Instance Method Details

#create3(old_token) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/antlr4/runtime/common_token.rb', line 47

def create3(old_token)
  result = CommonToken.new(old_token.type)

  result.line = old_token.line
  result.index = old_token.token_index
  result.char_position_in_line = old_token.char_position_in_line
  result.channel = old_token.channel
  result.start = old_token.start_index
  result.stop = old_token.stop_index

  if old_token.is_a? CommonToken
    result._text = old_token.text
    result.source = old_token.source
  else
    result._text = old_token.text
    result.source = OpenStruct.new
    result.source.a = old_token.token_source
    result.source.b = old_token.input_stream
  end
  result
end

#input_streamObject



69
70
71
# File 'lib/antlr4/runtime/common_token.rb', line 69

def input_stream
  @source.b
end

#textObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/antlr4/runtime/common_token.rb', line 73

def text
  return @_text unless @_text.nil?

  input = input_stream
  return nil if input.nil?

  n = input.size
  if @start < n && @stop < n
    input.text(Interval.of(@start, @stop))
  else
    '<EOF>'
  end
end

#to_sObject



104
105
106
# File 'lib/antlr4/runtime/common_token.rb', line 104

def to_s
  '[@ ' << @start.to_s << ':' << @stop.to_s << ',' << @line.to_s << ':' << ']'
end

#to_s_oldObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/antlr4/runtime/common_token.rb', line 108

def to_s_old
  channel_str = ''
  channel_str = ',channel=' + @channel.to_s if @channel > 0
  txt = @_text
  if !txt.nil?
    txt = txt.sub("\n", '\\n')
    txt = txt.sub("\r", '\\r')
    txt = txt.sub("\t", '\\t')
  else
    txt = '<no text>'
  end

  type_string = type.to_s

  '[@' << token_index.to_s << ',' << @start.to_s << ':' << @stop.to_s << "='" << txt << "',<" << type_string << '>' << channel_str << ',' << @line.to_s << ':' << char_position_in_line.to_s << ']'
end

#to_s_recog(r = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/antlr4/runtime/common_token.rb', line 87

def to_s_recog(r = nil)
  channel_str = ''
  channel_str = ',channel=' + @channel.to_s if @channel > 0
  txt = @_text
  if !txt.nil?
    txt = txt.sub("\n", '\\n')
    txt = txt.sub("\r", '\\r')
    txt = txt.sub("\t", '\\t')
  else
    txt = '<no text>'
  end

  type_string = type.to_s
  type_string = r.get_vocabulary.display_name(@type) unless r.nil?
  '[@' << token_index.to_s << ',' << @start.to_s << ':' << @stop.to_s << "='" << txt << "',<" << type_string << '>' << channel_str << ',' << @line.to_s << ':' << char_position_in_line.to_s << ']'
end