Class: DFA

Inherits:
Object show all
Defined in:
lib/antlr4/dfa/DFA.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_atnStartState, _decision = 0) ⇒ DFA

Returns a new instance of DFA.

Raises:

  • (Exception)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/antlr4/dfa/DFA.rb', line 4

def initialize(_atnStartState, _decision=0)
    raise Exception.new("atnStartState is nil") if _atnStartState.nil?
    type_check(_decision, Fixnum)
    type_check(_atnStartState, ATNState)
    # From which ATN state did we create this DFA?
    @atnStartState = _atnStartState
    @decision = _decision
    # A set of all DFA states. Use {@link Map} so we can get old state back
    #  ({@link Set} only allows you to see if it's there).
    @_states = Hash.new
    @s0 = nil
    # {@code true} if this DFA is for a precedence decision; otherwise,
    # {@code false}. This is the backing field for {@link #isPrecedenceDfa},
    # {@link #setPrecedenceDfa}.
    @precedenceDfa = false
end

Instance Attribute Details

#_statesObject

Returns the value of attribute _states.



3
4
5
# File 'lib/antlr4/dfa/DFA.rb', line 3

def _states
  @_states
end

#atnStartStateObject

Returns the value of attribute atnStartState.



3
4
5
# File 'lib/antlr4/dfa/DFA.rb', line 3

def atnStartState
  @atnStartState
end

#decisionObject

Returns the value of attribute decision.



3
4
5
# File 'lib/antlr4/dfa/DFA.rb', line 3

def decision
  @decision
end

#precedenceDfaObject

Returns the value of attribute precedenceDfa.



3
4
5
# File 'lib/antlr4/dfa/DFA.rb', line 3

def precedenceDfa
  @precedenceDfa
end

#s0Object

Returns the value of attribute s0.



3
4
5
# File 'lib/antlr4/dfa/DFA.rb', line 3

def s0
  @s0
end

Instance Method Details

#getPrecedenceStartState(precedence) ⇒ Object

Get the start state for a specific precedence value.

null if no start state exists for the specified precedence.

Parameters:

  • precedence

    The current precedence.

Returns:

  • The start state corresponding to the specified precedence, or

See Also:

  • #isPrecedenceDfa()


32
33
34
35
36
37
38
39
40
41
# File 'lib/antlr4/dfa/DFA.rb', line 32

def getPrecedenceStartState(precedence)
    if not self.precedenceDfa then
        raise IllegalStateException.new("Only precedence DFAs may contain a precedence start state.")
    end
    # s0.edges is never null for a precedence DFA
    if precedence < 0 or precedence >= self.s0.edges.length then
        return nil
    end
    return self.s0.edges[precedence]
end

#inspectObject



109
110
111
# File 'lib/antlr4/dfa/DFA.rb', line 109

def inspect
    "<DFA #{atnStartState.inspect} decision(#{@decision}) states(#{states.length}) >"
end

#isPrecedenceDfaObject



20
21
22
# File 'lib/antlr4/dfa/DFA.rb', line 20

def isPrecedenceDfa
    @precedenceDfa
end

#setPrecedenceDfa(_precedenceDfa) ⇒ Object

Sets whether this is a precedence DFA. If the specified value differs from the current DFA configuration, the following actions are taken; otherwise no changes are made to the current DFA.

<ul> <li>The #states map is cleared</li> <li>If precedenceDfa is false, the initial state #s0 is set to null; otherwise, it is initialized to a new DFAState with an empty outgoing DFAState#edges array to store the start states for individual precedence values.</li> <li>The #precedenceDfa field is updated</li> </ul>

false

Parameters:

  • precedenceDfa (@code true)

    if this is a precedence DFA; otherwise,



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/antlr4/dfa/DFA.rb', line 81

def setPrecedenceDfa(_precedenceDfa)
    if self.precedenceDfa != _precedenceDfa then
        self._states = Hash.new
        if _precedenceDfa then
            precedenceState = DFAState.new(nil, ATNConfigSet.new())
            precedenceState.edges = Array.new
            precedenceState.isAcceptState = false
            precedenceState.requiresFullContext = false
            self.s0 = precedenceState
        else
            self.s0 = nil
        end
        self.precedenceDfa = _precedenceDfa
    end
end

#setPrecedenceStartState(precedence, startState) ⇒ Object

Set the start state for a specific precedence value.

precedence.

Parameters:

  • precedence

    The current precedence.

  • startState

    The start state corresponding to the specified

See Also:

  • #isPrecedenceDfa()


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/antlr4/dfa/DFA.rb', line 52

def setPrecedenceStartState(precedence, startState)
    if not self.precedenceDfa then
        raise IllegalStateException.new("Only precedence DFAs may contain a precedence start state.")
    end
    if precedence < 0
        return
    end
    # synchronization on s0 here is ok. when the DFA is turned into a
    # precedence DFA, s0 will be initialized once and not updated again
    # s0.edges is never null for a precedence DFA
    self.s0.edges[precedence] = startState
end

#sortedStatesObject

Return a list of all states in this DFA, ordered by state number.



102
103
104
# File 'lib/antlr4/dfa/DFA.rb', line 102

def sortedStates()
    return self._states.keys().sort {|a,b| a.stateNumber <=> b.stateNumber}
end

#statesObject



97
98
99
# File 'lib/antlr4/dfa/DFA.rb', line 97

def states()
    self._states
end

#to_sObject



106
107
108
# File 'lib/antlr4/dfa/DFA.rb', line 106

def to_s
   toString()
end

#toLexerStringObject



121
122
123
124
125
126
127
# File 'lib/antlr4/dfa/DFA.rb', line 121

def toLexerString
    if self.s0.nil? then
        return ''
    end
    serializer = LexerDFASerializer.new(self)
    return serializer.to_s
end

#toString(tokenNames = nil) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/antlr4/dfa/DFA.rb', line 113

def toString(tokenNames=nil)
    if self.s0.nil? then
        return ''
    end
    serializer = DFASerializer.new(self,tokenNames)
    return serializer.to_s
end