ATN.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  2. # Use of this file is governed by the BSD 3-clause license that
  3. # can be found in the LICENSE.txt file in the project root.
  4. #/
  5. from antlr4.IntervalSet import IntervalSet
  6. from antlr4.RuleContext import RuleContext
  7. from antlr4.Token import Token
  8. from antlr4.atn.ATNType import ATNType
  9. from antlr4.atn.ATNState import ATNState, DecisionState
  10. class ATN(object):
  11. __slots__ = (
  12. 'grammarType', 'maxTokenType', 'states', 'decisionToState',
  13. 'ruleToStartState', 'ruleToStopState', 'modeNameToStartState',
  14. 'ruleToTokenType', 'lexerActions', 'modeToStartState'
  15. )
  16. INVALID_ALT_NUMBER = 0
  17. # Used for runtime deserialization of ATNs from strings#/
  18. def __init__(self, grammarType:ATNType , maxTokenType:int ):
  19. # The type of the ATN.
  20. self.grammarType = grammarType
  21. # The maximum value for any symbol recognized by a transition in the ATN.
  22. self.maxTokenType = maxTokenType
  23. self.states = []
  24. # Each subrule/rule is a decision point and we must track them so we
  25. # can go back later and build DFA predictors for them. This includes
  26. # all the rules, subrules, optional blocks, ()+, ()* etc...
  27. self.decisionToState = []
  28. # Maps from rule index to starting state number.
  29. self.ruleToStartState = []
  30. # Maps from rule index to stop state number.
  31. self.ruleToStopState = None
  32. self.modeNameToStartState = dict()
  33. # For lexer ATNs, this maps the rule index to the resulting token type.
  34. # For parser ATNs, this maps the rule index to the generated bypass token
  35. # type if the
  36. # {@link ATNDeserializationOptions#isGenerateRuleBypassTransitions}
  37. # deserialization option was specified; otherwise, this is {@code null}.
  38. self.ruleToTokenType = None
  39. # For lexer ATNs, this is an array of {@link LexerAction} objects which may
  40. # be referenced by action transitions in the ATN.
  41. self.lexerActions = None
  42. self.modeToStartState = []
  43. # Compute the set of valid tokens that can occur starting in state {@code s}.
  44. # If {@code ctx} is null, the set of tokens will not include what can follow
  45. # the rule surrounding {@code s}. In other words, the set will be
  46. # restricted to tokens reachable staying within {@code s}'s rule.
  47. def nextTokensInContext(self, s:ATNState, ctx:RuleContext):
  48. from antlr4.LL1Analyzer import LL1Analyzer
  49. anal = LL1Analyzer(self)
  50. return anal.LOOK(s, ctx=ctx)
  51. # Compute the set of valid tokens that can occur starting in {@code s} and
  52. # staying in same rule. {@link Token#EPSILON} is in set if we reach end of
  53. # rule.
  54. def nextTokensNoContext(self, s:ATNState):
  55. if s.nextTokenWithinRule is not None:
  56. return s.nextTokenWithinRule
  57. s.nextTokenWithinRule = self.nextTokensInContext(s, None)
  58. s.nextTokenWithinRule.readonly = True
  59. return s.nextTokenWithinRule
  60. def nextTokens(self, s:ATNState, ctx:RuleContext = None):
  61. if ctx==None:
  62. return self.nextTokensNoContext(s)
  63. else:
  64. return self.nextTokensInContext(s, ctx)
  65. def addState(self, state:ATNState):
  66. if state is not None:
  67. state.atn = self
  68. state.stateNumber = len(self.states)
  69. self.states.append(state)
  70. def removeState(self, state:ATNState):
  71. self.states[state.stateNumber] = None # just free mem, don't shift states in list
  72. def defineDecisionState(self, s:DecisionState):
  73. self.decisionToState.append(s)
  74. s.decision = len(self.decisionToState)-1
  75. return s.decision
  76. def getDecisionState(self, decision:int):
  77. if len(self.decisionToState)==0:
  78. return None
  79. else:
  80. return self.decisionToState[decision]
  81. # Computes the set of input symbols which could follow ATN state number
  82. # {@code stateNumber} in the specified full {@code context}. This method
  83. # considers the complete parser context, but does not evaluate semantic
  84. # predicates (i.e. all predicates encountered during the calculation are
  85. # assumed true). If a path in the ATN exists from the starting state to the
  86. # {@link RuleStopState} of the outermost context without matching any
  87. # symbols, {@link Token#EOF} is added to the returned set.
  88. #
  89. # <p>If {@code context} is {@code null}, it is treated as
  90. # {@link ParserRuleContext#EMPTY}.</p>
  91. #
  92. # @param stateNumber the ATN state number
  93. # @param context the full parse context
  94. # @return The set of potentially valid input symbols which could follow the
  95. # specified state in the specified context.
  96. # @throws IllegalArgumentException if the ATN does not contain a state with
  97. # number {@code stateNumber}
  98. #/
  99. def getExpectedTokens(self, stateNumber:int, ctx:RuleContext ):
  100. if stateNumber < 0 or stateNumber >= len(self.states):
  101. raise Exception("Invalid state number.")
  102. s = self.states[stateNumber]
  103. following = self.nextTokens(s)
  104. if Token.EPSILON not in following:
  105. return following
  106. expected = IntervalSet()
  107. expected.addSet(following)
  108. expected.removeOne(Token.EPSILON)
  109. while (ctx != None and ctx.invokingState >= 0 and Token.EPSILON in following):
  110. invokingState = self.states[ctx.invokingState]
  111. rt = invokingState.transitions[0]
  112. following = self.nextTokens(rt.followState)
  113. expected.addSet(following)
  114. expected.removeOne(Token.EPSILON)
  115. ctx = ctx.parentCtx
  116. if Token.EPSILON in following:
  117. expected.addOne(Token.EOF)
  118. return expected