Class: ATN

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

Constant Summary collapse

INVALID_ALT_NUMBER =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammarType, maxTokenType) ⇒ ATN

Returns a new instance of ATN.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/antlr4/atn/ATN.rb', line 11

def initialize(grammarType, maxTokenType)
    # The type of the ATN.
    @grammarType = grammarType
    # The maximum value for any symbol recognized by a transition in the ATN.
    @maxTokenType = maxTokenType
    @states = Array.new
    # Each subrule/rule is a decision point and we must track them so we
    #  can go back later and build DFA predictors for them.  This includes
    #  all the rules, subrules, optional blocks, ()+, ()* etc...
    @decisionToState = Array.new
    # Maps from rule index to starting state number.
    @ruleToStartState = Array.new
    # Maps from rule index to stop state number.
    @ruleToStopState = nil
    @modeNameToStartState = Hash.new
    # For lexer ATNs, this maps the rule index to the resulting token type.
    # For parser ATNs, this maps the rule index to the generated bypass token
    # type if the
    # {@link ATNDeserializationOptions#isGenerateRuleBypassTransitions}
    # deserialization option was specified; otherwise, this is {@code null}.
    @ruleToTokenType = nil
    # For lexer ATNs, this is an array of {@link LexerAction} objects which may
    # be referenced by action transitions in the ATN.
    @lexerActions = nil
    @modeToStartState = Array.new
end

Instance Attribute Details

#decisionToStateObject

Used for runtime deserialization of ATNs from strings#/



8
9
10
# File 'lib/antlr4/atn/ATN.rb', line 8

def decisionToState
  @decisionToState
end

#grammarTypeObject

Used for runtime deserialization of ATNs from strings#/



8
9
10
# File 'lib/antlr4/atn/ATN.rb', line 8

def grammarType
  @grammarType
end

#lexerActionsObject

Returns the value of attribute lexerActions.



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

def lexerActions
  @lexerActions
end

#maxTokenTypeObject

Used for runtime deserialization of ATNs from strings#/



8
9
10
# File 'lib/antlr4/atn/ATN.rb', line 8

def maxTokenType
  @maxTokenType
end

#modeNameToStartStateObject

Returns the value of attribute modeNameToStartState.



9
10
11
# File 'lib/antlr4/atn/ATN.rb', line 9

def modeNameToStartState
  @modeNameToStartState
end

#modeToStartStateObject

Returns the value of attribute modeToStartState.



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

def modeToStartState
  @modeToStartState
end

#ruleToStartStateObject

Returns the value of attribute ruleToStartState.



9
10
11
# File 'lib/antlr4/atn/ATN.rb', line 9

def ruleToStartState
  @ruleToStartState
end

#ruleToStopStateObject

Returns the value of attribute ruleToStopState.



9
10
11
# File 'lib/antlr4/atn/ATN.rb', line 9

def ruleToStopState
  @ruleToStopState
end

#ruleToTokenTypeObject

Returns the value of attribute ruleToTokenType.



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

def ruleToTokenType
  @ruleToTokenType
end

#statesObject

Used for runtime deserialization of ATNs from strings#/



8
9
10
# File 'lib/antlr4/atn/ATN.rb', line 8

def states
  @states
end

Class Method Details

.INVALID_ALT_NUMBERObject



4
5
6
# File 'lib/antlr4/atn/ATN.rb', line 4

def self.INVALID_ALT_NUMBER 
  ATN::INVALID_ALT_NUMBER 
end

Instance Method Details

#addState(state) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/antlr4/atn/ATN.rb', line 65

def addState(state)
    if not state.nil? 
        state.atn = self
        state.stateNumber = self.states.length()
    end
    self.states.push(state)
end

#defineDecisionState(s) ⇒ Object



76
77
78
79
80
# File 'lib/antlr4/atn/ATN.rb', line 76

def defineDecisionState(s)
    self.decisionToState.push(s)
    s.decision = self.decisionToState.length()-1
    return s.decision
end

#getDecisionState(decision) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/antlr4/atn/ATN.rb', line 81

def getDecisionState(decision)
    if self.decisionToState.length==0
        return  nil
    else
        return self.decisionToState[decision]
    end
end

#getExpectedTokens(stateNumber, ctx) ⇒ Object

Computes the set of input symbols which could follow ATN state number stateNumber in the specified full context. This method considers the complete parser context, but does not evaluate semantic predicates (i.e. all predicates encountered during the calculation are assumed true). If a path in the ATN exists from the starting state to the RuleStopState of the outermost context without matching any symbols, Token#EOF is added to the returned set.

<p>If context is null, it is treated as ParserRuleContext#EMPTY.</p>

specified state in the specified context. number stateNumber /

Parameters:

  • stateNumber

    the ATN state number

  • context

    the full parse context

Returns:

  • The set of potentially valid input symbols which could follow the



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/antlr4/atn/ATN.rb', line 106

def getExpectedTokens(stateNumber, ctx)
    if stateNumber < 0 or stateNumber >= self.states.length
        raise Exception.new("Invalid state number: #{stateNumber}.")
    end
    s = self.states[stateNumber]
    following = self.nextTokens(s)
    if not following.member? Token::EPSILON 
        return following
    end
    expected = IntervalSet.new()
    expected.addSet(following)
    #puts expected.to_s
    expected.remove(Token::EPSILON)
    #puts expected.to_s
    while ( ctx and ctx.invokingState >= 0 and following.member?(Token::EPSILON) ) do
        invokingState = self.states[ctx.invokingState]
        rt = invokingState.transitions[0]
        following = self.nextTokens(rt.followState)
        expected.addSet(following)
        expected.remove(Token::EPSILON)
        ctx = ctx.parentCtx
    end
    if following.member?(Token::EPSILON) then
        expected.addOne(Token::EOF)
    end
    return expected
end

#nextTokens(s, ctx = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/antlr4/atn/ATN.rb', line 58

def nextTokens(s, ctx= nil) 
    if ctx.nil? 
        return self.nextTokensNoContext(s)
    else
        return self.nextTokensInContext(s, ctx)
    end
end

#nextTokensInContext(s, ctx) ⇒ Object

Compute the set of valid tokens that can occur starting in state s.

If {@code ctx} is null, the set of tokens will not include what can follow
the rule surrounding {@code s}. In other words, the set will be
restricted to tokens reachable staying within {@code s}'s rule.


41
42
43
44
45
# File 'lib/antlr4/atn/ATN.rb', line 41

def nextTokensInContext(s, ctx)
    require 'antlr4/LL1Analyzer'
    anal = LL1Analyzer.new(self)
    return anal.LOOK(s, ctx=ctx)
end

#nextTokensNoContext(s) ⇒ Object

Compute the set of valid tokens that can occur starting in s and staying in same rule. Token#EPSILON is in set if we reach end of rule.



50
51
52
53
54
55
56
57
# File 'lib/antlr4/atn/ATN.rb', line 50

def nextTokensNoContext(s)
    if not s.nextTokenWithinRule.nil? 
      return s.nextTokenWithinRule 
    end
    s.nextTokenWithinRule = self.nextTokensInContext(s,nil )
    s.nextTokenWithinRule.readonly = true
    return s.nextTokenWithinRule
end

#removeState(state) ⇒ Object



73
74
75
# File 'lib/antlr4/atn/ATN.rb', line 73

def removeState(state)
    self.states[state.stateNumber] = nil 
end