Class: Antlr4ruby::CommonToken

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

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, source: nil, channel: nil, start: nil, stop: nil, text: nil) ⇒ CommonToken

使用关键字参数来定义



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/antlr4ruby/common_token.rb', line 5

def initialize(type: nil, source: nil,
               channel:nil, start: nil, stop: nil, text: nil )
  @char_position_in_line = -1
  @channel = channel || Token::DEFAULT_CHANNEL
  @index = -1
  @text = text || ''
  @type = type || -1
  @start = start || 0
  @stop = stop || 0

  if source&.first
    @line = source.first.get_line
    @char_position_in_line = source.first.get_char_position_in_line
  else
    @source = EMPTY_SOURCE
  end

end

Instance Method Details

#get_channelObject



75
76
77
# File 'lib/antlr4ruby/common_token.rb', line 75

def get_channel
  @channel
end

#get_char_position_in_lineObject



67
68
69
# File 'lib/antlr4ruby/common_token.rb', line 67

def get_char_position_in_line
  @char_position_in_line
end

#get_input_streamObject



105
106
107
# File 'lib/antlr4ruby/common_token.rb', line 105

def get_input_stream
  source.second
end

#get_lineObject



63
64
65
# File 'lib/antlr4ruby/common_token.rb', line 63

def get_line
  @line
end

#get_start_indexObject



83
84
85
# File 'lib/antlr4ruby/common_token.rb', line 83

def get_start_index
  @start
end

#get_stop_indexObject



89
90
91
# File 'lib/antlr4ruby/common_token.rb', line 89

def get_stop_index
  @stop
end

#get_textObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/antlr4ruby/common_token.rb', line 47

def get_text
  return @text if @text
  input = get_input_stream
  return "" unless  input
  n = input.size
  if start < n && stop < n
    input.get_text(start..stop)
  else
    '<EOF>'
  end
end

#get_token_indexObject



95
96
97
# File 'lib/antlr4ruby/common_token.rb', line 95

def get_token_index
  @index
end

#get_token_sourceObject



101
102
103
# File 'lib/antlr4ruby/common_token.rb', line 101

def get_token_source
  source.first
end

#get_typeObject



39
40
41
# File 'lib/antlr4ruby/common_token.rb', line 39

def get_type
  @type
end

#set_channel(channel) ⇒ Object



79
# File 'lib/antlr4ruby/common_token.rb', line 79

def set_channel(channel) @channel = channel; end

#set_char_position_in_line(position) ⇒ Object



71
72
73
# File 'lib/antlr4ruby/common_token.rb', line 71

def set_char_position_in_line(position)
  @char_position_in_line = position
end

#set_line(line) ⇒ Object



43
44
45
# File 'lib/antlr4ruby/common_token.rb', line 43

def set_line(line)
  @line = line
end

#set_start_index(start) ⇒ Object



87
# File 'lib/antlr4ruby/common_token.rb', line 87

def set_start_index(start) @start = start; end

#set_stop_index(stop) ⇒ Object



93
# File 'lib/antlr4ruby/common_token.rb', line 93

def set_stop_index(stop) @stop = stop; end

#set_text(text) ⇒ Object



59
60
61
# File 'lib/antlr4ruby/common_token.rb', line 59

def set_text(text)
  @text = text
end

#set_token_index(index) ⇒ Object



99
# File 'lib/antlr4ruby/common_token.rb', line 99

def set_token_index(index) @index = index; end

#set_type(ty) ⇒ Object



81
# File 'lib/antlr4ruby/common_token.rb', line 81

def set_type(ty) @type = ty; end

#to_s(recognizer) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/antlr4ruby/common_token.rb', line 30

def to_s(recognizer)
  channel_str = channel == 0 ? "":", channel=#{channel}"
  txt = get_text
  txt = '<no text>' unless txt
  type_string = type.to_s
  type_string = recognizer.get_vocabulary.get_display_name(type) if recognizer
  "[@#{get_token_index}, #{start}:#{stop}='#{txt}',<#{type_string}>, #{line}:#{char_position_in_line}]"
end