ATNConfig.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #
  2. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  3. # Use of this file is governed by the BSD 3-clause license that
  4. # can be found in the LICENSE.txt file in the project root.
  5. #/
  6. # A tuple: (ATN state, predicted alt, syntactic, semantic context).
  7. # The syntactic context is a graph-structured stack node whose
  8. # path(s) to the root is the rule invocation(s)
  9. # chain used to arrive at the state. The semantic context is
  10. # the tree of semantic predicates encountered before reaching
  11. # an ATN state.
  12. #/
  13. from io import StringIO
  14. from antlr4.PredictionContext import PredictionContext
  15. from antlr4.atn.ATNState import ATNState, DecisionState
  16. from antlr4.atn.LexerActionExecutor import LexerActionExecutor
  17. from antlr4.atn.SemanticContext import SemanticContext
  18. # need a forward declaration
  19. ATNConfig = None
  20. class ATNConfig(object):
  21. __slots__ = (
  22. 'state', 'alt', 'context', 'semanticContext', 'reachesIntoOuterContext',
  23. 'precedenceFilterSuppressed'
  24. )
  25. def __init__(self, state:ATNState=None, alt:int=None, context:PredictionContext=None, semantic:SemanticContext=None, config:ATNConfig=None):
  26. if config is not None:
  27. if state is None:
  28. state = config.state
  29. if alt is None:
  30. alt = config.alt
  31. if context is None:
  32. context = config.context
  33. if semantic is None:
  34. semantic = config.semanticContext
  35. if semantic is None:
  36. semantic = SemanticContext.NONE
  37. # The ATN state associated with this configuration#/
  38. self.state = state
  39. # What alt (or lexer rule) is predicted by this configuration#/
  40. self.alt = alt
  41. # The stack of invoking states leading to the rule/states associated
  42. # with this config. We track only those contexts pushed during
  43. # execution of the ATN simulator.
  44. self.context = context
  45. self.semanticContext = semantic
  46. # We cannot execute predicates dependent upon local context unless
  47. # we know for sure we are in the correct context. Because there is
  48. # no way to do this efficiently, we simply cannot evaluate
  49. # dependent predicates unless we are in the rule that initially
  50. # invokes the ATN simulator.
  51. #
  52. # closure() tracks the depth of how far we dip into the
  53. # outer context: depth > 0. Note that it may not be totally
  54. # accurate depth since I don't ever decrement. TODO: make it a boolean then
  55. self.reachesIntoOuterContext = 0 if config is None else config.reachesIntoOuterContext
  56. self.precedenceFilterSuppressed = False if config is None else config.precedenceFilterSuppressed
  57. # An ATN configuration is equal to another if both have
  58. # the same state, they predict the same alternative, and
  59. # syntactic/semantic contexts are the same.
  60. #/
  61. def __eq__(self, other):
  62. if self is other:
  63. return True
  64. elif not isinstance(other, ATNConfig):
  65. return False
  66. else:
  67. return self.state.stateNumber==other.state.stateNumber \
  68. and self.alt==other.alt \
  69. and ((self.context is other.context) or (self.context==other.context)) \
  70. and self.semanticContext==other.semanticContext \
  71. and self.precedenceFilterSuppressed==other.precedenceFilterSuppressed
  72. def __hash__(self):
  73. return hash((self.state.stateNumber, self.alt, self.context, self.semanticContext))
  74. def hashCodeForConfigSet(self):
  75. return hash((self.state.stateNumber, self.alt, hash(self.semanticContext)))
  76. def equalsForConfigSet(self, other):
  77. if self is other:
  78. return True
  79. elif not isinstance(other, ATNConfig):
  80. return False
  81. else:
  82. return self.state.stateNumber==other.state.stateNumber \
  83. and self.alt==other.alt \
  84. and self.semanticContext==other.semanticContext
  85. def __str__(self):
  86. with StringIO() as buf:
  87. buf.write('(')
  88. buf.write(str(self.state))
  89. buf.write(",")
  90. buf.write(str(self.alt))
  91. if self.context is not None:
  92. buf.write(",[")
  93. buf.write(str(self.context))
  94. buf.write("]")
  95. if self.semanticContext is not None and self.semanticContext is not SemanticContext.NONE:
  96. buf.write(",")
  97. buf.write(str(self.semanticContext))
  98. if self.reachesIntoOuterContext>0:
  99. buf.write(",up=")
  100. buf.write(str(self.reachesIntoOuterContext))
  101. buf.write(')')
  102. return buf.getvalue()
  103. # need a forward declaration
  104. LexerATNConfig = None
  105. class LexerATNConfig(ATNConfig):
  106. __slots__ = ('lexerActionExecutor', 'passedThroughNonGreedyDecision')
  107. def __init__(self, state:ATNState, alt:int=None, context:PredictionContext=None, semantic:SemanticContext=SemanticContext.NONE,
  108. lexerActionExecutor:LexerActionExecutor=None, config:LexerATNConfig=None):
  109. super().__init__(state=state, alt=alt, context=context, semantic=semantic, config=config)
  110. if config is not None:
  111. if lexerActionExecutor is None:
  112. lexerActionExecutor = config.lexerActionExecutor
  113. # This is the backing field for {@link #getLexerActionExecutor}.
  114. self.lexerActionExecutor = lexerActionExecutor
  115. self.passedThroughNonGreedyDecision = False if config is None else self.checkNonGreedyDecision(config, state)
  116. def __hash__(self):
  117. return hash((self.state.stateNumber, self.alt, self.context,
  118. self.semanticContext, self.passedThroughNonGreedyDecision,
  119. self.lexerActionExecutor))
  120. def __eq__(self, other):
  121. if self is other:
  122. return True
  123. elif not isinstance(other, LexerATNConfig):
  124. return False
  125. if self.passedThroughNonGreedyDecision != other.passedThroughNonGreedyDecision:
  126. return False
  127. if not(self.lexerActionExecutor == other.lexerActionExecutor):
  128. return False
  129. return super().__eq__(other)
  130. def hashCodeForConfigSet(self):
  131. return hash(self)
  132. def equalsForConfigSet(self, other):
  133. return self==other
  134. def checkNonGreedyDecision(self, source:LexerATNConfig, target:ATNState):
  135. return source.passedThroughNonGreedyDecision \
  136. or isinstance(target, DecisionState) and target.nonGreedy