Class: LexerActionExecutor

Inherits:
Object show all
Defined in:
lib/antlr4/atn/LexerActionExecutor.rb

Overview

Represents an executor for a sequence of lexer actions which traversed during the matching operation of a lexer rule (token).

<p>The executor tracks position information for position-dependent lexer actions efficiently, ensuring that actions appearing only at the end of the rule do not cause bloating of the DFA created for the lexer.</p>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_lexerActions = Array.new) ⇒ LexerActionExecutor

Returns a new instance of LexerActionExecutor.



11
12
13
14
15
16
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 11

def initialize(_lexerActions=Array.new)
    @lexerActions = _lexerActions
    # Caches the result of {@link #hashCode} since the hash code is an element
    # of the performance-critical {@link LexerATNConfig#hashCode} operation.
    @hashCode = self.lexerActions.map(&:to_s).join('').hash
end

Instance Attribute Details

#hashCodeObject

Returns the value of attribute hashCode.



10
11
12
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 10

def hashCode
  @hashCode
end

#lexerActionsObject

Returns the value of attribute lexerActions.



10
11
12
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 10

def lexerActions
  @lexerActions
end

Class Method Details

.append(lexerActionExecutor, lexerAction) ⇒ Object

Creates a LexerActionExecutor which executes the actions for the input lexerActionExecutor followed by a specified lexerAction.

the lexer while matching a token within a particular LexerATNConfig. If this is null, the method behaves as though it were an empty executor. specified in lexerActionExecutor.

of lexerActionExecutor and lexerAction.

Parameters:

  • lexerActionExecutor

    The executor for actions already traversed by

  • lexerAction

    The lexer action to execute after the actions



31
32
33
34
35
36
37
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 31

def self.append(lexerActionExecutor, lexerAction)
    if lexerActionExecutor.nil? 
        return LexerActionExecutor.new([ lexerAction ])
    end
    lexerActions = lexerActionExecutor.lexerActions.concat(lexerAction )
    return LexerActionExecutor.new(lexerActions)
end

Instance Method Details

#==(other) ⇒ Object



130
131
132
133
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 130

def ==( other)
   self.equal?(other) or (other.kind_of?(LexerActionExecutor) and 
       self.hashCode == other.hashCode and self.lexerActions == other.lexerActions)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 127

def eql?(other)
    self == other
end

#execute(lexer, input, startIndex) ⇒ Object

Execute the actions encapsulated by this executor within the context of a particular Lexer.

<p>This method calls IntStream#seek to set the position of the input CharStream prior to calling LexerAction#execute on a position-dependent action. Before the method returns, the input position will be restored to the same position it was in when the method was invoked.</p>

When this method is called, the current IntStream#index for input should be the start of the following token, i.e. 1 character past the end of the current token. IntStream#seek to set the input position to the beginning of the token. /

Parameters:

  • lexer

    The lexer instance.

  • input

    The input stream which is the source for the current token.

  • startIndex

    The token start index. This value may be passed to



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 103

def execute(lexer, input, startIndex)
    requiresSeek = false
    stopIndex = input.index
    begin
        self.lexerActions.each { |lexerAction|
            if lexerAction.kind_of? LexerIndexedCustomAction
                offset = lexerAction.offset
                input.seek(startIndex + offset)
                lexerAction = lexerAction.action
                requiresSeek = (startIndex + offset) != stopIndex
            elsif lexerAction.isPositionDependent
                input.seek(stopIndex)
                requiresSeek = false
            end
            lexerAction.execute(lexer)
        }
    ensure 
        input.seek(stopIndex) if requiresSeek
    end
end

#fixOffsetBeforeMatch(offset) ⇒ Object

Creates a LexerActionExecutor which encodes the current offset for position-dependent lexer actions.

<p>Normally, when the executor encounters lexer actions where LexerAction#isPositionDependent returns true, it calls IntStream#seek on the input CharStream to set the input position to the end of the current token. This behavior provides for efficient DFA representation of lexer actions which appear at the end of a lexer rule, even when the lexer rule matches a variable number of characters.</p>

<p>Prior to traversing a match transition in the ATN, the current offset from the token start index is assigned to all position-dependent lexer actions which have not already been assigned a fixed offset. By storing the offsets relative to the token start index, the DFA representation of lexer actions which appear in the middle of tokens remains efficient due to sharing among tokens of the same length, regardless of their absolute position in the input stream.</p>

<p>If the current executor already has offsets assigned to all position-dependent lexer actions, the method returns this.</p>

lexer actions which do not already have offsets assigned.

for all position-dependent lexer actions. /

Parameters:

  • offset

    The current offset to assign to all position-dependent



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 67

def fixOffsetBeforeMatch(offset)
    updatedLexerActions = nil
    @lexerActions.each_index {|i|
        if @lexerActions[i].isPositionDependent and not @lexerActions[i].kind_of?(LexerIndexedCustomAction) then
            if updatedLexerActions.nil? then
                updatedLexerActions = @lexerActions.map{|x| x} 
            end
            updatedLexerActions[i] = LexerIndexedCustomAction.new(offset, @lexerActions[i])
        end
    }
    if updatedLexerActions.nil? 
        return self
    else
        return LexerActionExecutor.new(updatedLexerActions)
    end
end

#hashObject



123
124
125
# File 'lib/antlr4/atn/LexerActionExecutor.rb', line 123

def hash
    return self.hashCode
end