Class: CommonToken

Inherits:
Token show all
Defined in:
lib/antlr4/Token.rb

Direct Known Subclasses

TokenTagToken

Constant Summary collapse

EMPTY_SOURCE =

An empty Pair which is used as the default value of #source for tokens that do not have a source.

[nil, nil]

Constants inherited from Token

Token::DEFAULT_CHANNEL, Token::EOF, Token::EPSILON, Token::HIDDEN_CHANNEL, Token::INVALID_TYPE, Token::MIN_USER_TOKEN_TYPE

Instance Attribute Summary

Attributes inherited from Token

#channel, #column, #line, #source, #start, #stop, #tokenIndex, #type

Instance Method Summary collapse

Methods inherited from Token

#getInputStream, #getTokenSource

Constructor Details

#initialize(source = EMPTY_SOURCE, type = nil, channel = Token::DEFAULT_CHANNEL, start = -1,, stop = -1)) ⇒ CommonToken

Returns a new instance of CommonToken.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/antlr4/Token.rb', line 58

def initialize(source = EMPTY_SOURCE, type = nil, channel=Token::DEFAULT_CHANNEL, start=-1, stop=-1)
    super()
    self.source = source
    self.type = type
    self.channel = channel
    self.start = start
    self.stop = stop
    self.tokenIndex = -1
    if source[0] then
        self.line = source[0].line
        self.column = source[0].column
    else
        self.column = -1
        self.line = nil
    end
end

Instance Method Details

#cloneObject

Constructs a new CommonToken as a copy of another Token.

<p> If oldToken is also a CommonToken instance, the newly constructed token will share a reference to the #text field and the Pair stored in #source. Otherwise, #text will be assigned the result of calling #getText, and #source will be constructed from the result of Token#getTokenSource and Token#getInputStream.</p>

Parameters:

  • oldToken

    The token to copy.

Raises:

  • (NotImplementedError)


86
87
88
89
90
91
92
93
94
# File 'lib/antlr4/Token.rb', line 86

def clone()
#        t = CommonToken(self.source, self.type, self.channel, self.start, self.stop)
#        t.tokenIndex = self.tokenIndex
#        t.line = self.line
#        t.column = self.column
#        t.text = self.text
#        return t
  raise NotImplementedError.new("Token.clone not implemented")
end

#inspectObject



121
122
123
# File 'lib/antlr4/Token.rb', line 121

def inspect
  to_s
end

#textObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/antlr4/Token.rb', line 95

def text()
    return @text if @text
    input = self.getInputStream()
    return nil if input.nil? 
    n = input.size
    if self.start < n and self.stop < n then
        return input.getText(self.start, self.stop)
    else
        return '<EOF>'
    end
end

#to_sObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/antlr4/Token.rb', line 107

def to_s
  txt = self.text()
  if txt.nil? then
    txt = "<no text>"
  else 
    txt = txt.gsub("\n","\\n").gsub("\r","\\r").gsub("\t","\\t")
  end
  if self.channel > 0 then
    c = ",channel=#{channel}"
  else
    c = ""
  end
  "[@#{tokenIndex},#{start}:#{stop}='#{txt}',<#{type}>#{c},#{line}:#{column}]"
end