Class: CommonTokenFactory

Inherits:
TokenFactory show all
Defined in:
lib/antlr4/CommonTokenFactory.rb

Overview

This default implementation of TokenFactory creates CommonToken objects.

Constant Summary collapse

@@default =

The default CommonTokenFactory instance.

<p> This token factory does not explicitly copy token text when constructing tokens.</p>

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_copyText = false) ⇒ CommonTokenFactory

Returns a new instance of CommonTokenFactory.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/antlr4/CommonTokenFactory.rb', line 19

def initialize(_copyText=false)
    # Indicates whether {@link CommonToken#setText} should be called after
    # constructing tokens to explicitly set the text. This is useful for cases
    # where the input stream might not be able to provide arbitrary substrings
    # of text from the input after the lexer creates a token (e.g. the
    # implementation of {@link CharStream#getText} in
    # {@link UnbufferedCharStream} throws an
    # {@link UnsupportedOperationException}). Explicitly setting the token text
    # allows {@link Token#getText} to be called at any time regardless of the
    # input stream implementation.
    #
    # <p>
    # The default value is {@code false} to avoid the performance and memory
    # overhead of copying text for every token unless explicitly requested.</p>
    #
    @copyText = _copyText
end

Instance Attribute Details

#copyTextObject

Returns the value of attribute copyText.



18
19
20
# File 'lib/antlr4/CommonTokenFactory.rb', line 18

def copyText
  @copyText
end

Class Method Details

.DEFAULTObject



13
14
15
16
# File 'lib/antlr4/CommonTokenFactory.rb', line 13

def self.DEFAULT 
  @@default = new() if @@default.nil?
  @@default
end

Instance Method Details

#create(source, type, text, channel, start, stop, line, column) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/antlr4/CommonTokenFactory.rb', line 36

def create(source, type, text, channel, start, stop, line, column)
    t = CommonToken.new(source, type, channel, start, stop)
    t.line = line
    t.column = column
    if not text.nil? then
        t.text = text
    elsif self.copyText and not source[1].nil? then
        t.text = source[1].getText(start,stop)
    end
    return t
end

#createThin(type, text) ⇒ Object



48
49
50
51
52
# File 'lib/antlr4/CommonTokenFactory.rb', line 48

def createThin(type, text)
    t = CommonToken.new(type)
    t.text = text
    return t
end