PageRenderTime 659ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/Python3/src/antlr4/atn/Transition.py

https://github.com/antlr/antlr4
Python | 268 lines | 182 code | 63 blank | 23 comment | 7 complexity | 6087166d77d612a3aef940dacf60163d MD5 | raw file
Possible License(s): BSD-3-Clause
  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. # An ATN transition between any two ATN states. Subclasses define
  7. # atom, set, epsilon, action, predicate, rule transitions.
  8. #
  9. # <p>This is a one way link. It emanates from a state (usually via a list of
  10. # transitions) and has a target state.</p>
  11. #
  12. # <p>Since we never have to change the ATN transitions once we construct it,
  13. # we can fix these transitions as specific classes. The DFA transitions
  14. # on the other hand need to update the labels as it adds transitions to
  15. # the states. We'll use the term Edge for the DFA to distinguish them from
  16. # ATN transitions.</p>
  17. #
  18. from antlr4.IntervalSet import IntervalSet
  19. from antlr4.Token import Token
  20. # need forward declarations
  21. from antlr4.atn.SemanticContext import Predicate, PrecedencePredicate
  22. ATNState = None
  23. RuleStartState = None
  24. class Transition (object):
  25. __slots__ = ('target','isEpsilon','label')
  26. # constants for serialization
  27. EPSILON = 1
  28. RANGE = 2
  29. RULE = 3
  30. PREDICATE = 4 # e.g., {isType(input.LT(1))}?
  31. ATOM = 5
  32. ACTION = 6
  33. SET = 7 # ~(A|B) or ~atom, wildcard, which convert to next 2
  34. NOT_SET = 8
  35. WILDCARD = 9
  36. PRECEDENCE = 10
  37. serializationNames = [
  38. "INVALID",
  39. "EPSILON",
  40. "RANGE",
  41. "RULE",
  42. "PREDICATE",
  43. "ATOM",
  44. "ACTION",
  45. "SET",
  46. "NOT_SET",
  47. "WILDCARD",
  48. "PRECEDENCE"
  49. ]
  50. serializationTypes = dict()
  51. def __init__(self, target:ATNState):
  52. # The target of this transition.
  53. if target is None:
  54. raise Exception("target cannot be null.")
  55. self.target = target
  56. # Are we epsilon, action, sempred?
  57. self.isEpsilon = False
  58. self.label = None
  59. # TODO: make all transitions sets? no, should remove set edges
  60. class AtomTransition(Transition):
  61. __slots__ = ('label_', 'serializationType')
  62. def __init__(self, target:ATNState, label:int):
  63. super().__init__(target)
  64. self.label_ = label # The token type or character value; or, signifies special label.
  65. self.label = self.makeLabel()
  66. self.serializationType = self.ATOM
  67. def makeLabel(self):
  68. s = IntervalSet()
  69. s.addOne(self.label_)
  70. return s
  71. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  72. return self.label_ == symbol
  73. def __str__(self):
  74. return str(self.label_)
  75. class RuleTransition(Transition):
  76. __slots__ = ('ruleIndex', 'precedence', 'followState', 'serializationType')
  77. def __init__(self, ruleStart:RuleStartState, ruleIndex:int, precedence:int, followState:ATNState):
  78. super().__init__(ruleStart)
  79. self.ruleIndex = ruleIndex # ptr to the rule definition object for this rule ref
  80. self.precedence = precedence
  81. self.followState = followState # what node to begin computations following ref to rule
  82. self.serializationType = self.RULE
  83. self.isEpsilon = True
  84. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  85. return False
  86. class EpsilonTransition(Transition):
  87. __slots__ = ('serializationType', 'outermostPrecedenceReturn')
  88. def __init__(self, target, outermostPrecedenceReturn=-1):
  89. super(EpsilonTransition, self).__init__(target)
  90. self.serializationType = self.EPSILON
  91. self.isEpsilon = True
  92. self.outermostPrecedenceReturn = outermostPrecedenceReturn
  93. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  94. return False
  95. def __str__(self):
  96. return "epsilon"
  97. class RangeTransition(Transition):
  98. __slots__ = ('serializationType', 'start', 'stop')
  99. def __init__(self, target:ATNState, start:int, stop:int):
  100. super().__init__(target)
  101. self.serializationType = self.RANGE
  102. self.start = start
  103. self.stop = stop
  104. self.label = self.makeLabel()
  105. def makeLabel(self):
  106. s = IntervalSet()
  107. s.addRange(range(self.start, self.stop + 1))
  108. return s
  109. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  110. return symbol >= self.start and symbol <= self.stop
  111. def __str__(self):
  112. return "'" + chr(self.start) + "'..'" + chr(self.stop) + "'"
  113. class AbstractPredicateTransition(Transition):
  114. def __init__(self, target:ATNState):
  115. super().__init__(target)
  116. class PredicateTransition(AbstractPredicateTransition):
  117. __slots__ = ('serializationType', 'ruleIndex', 'predIndex', 'isCtxDependent')
  118. def __init__(self, target:ATNState, ruleIndex:int, predIndex:int, isCtxDependent:bool):
  119. super().__init__(target)
  120. self.serializationType = self.PREDICATE
  121. self.ruleIndex = ruleIndex
  122. self.predIndex = predIndex
  123. self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
  124. self.isEpsilon = True
  125. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  126. return False
  127. def getPredicate(self):
  128. return Predicate(self.ruleIndex, self.predIndex, self.isCtxDependent)
  129. def __str__(self):
  130. return "pred_" + str(self.ruleIndex) + ":" + str(self.predIndex)
  131. class ActionTransition(Transition):
  132. __slots__ = ('serializationType', 'ruleIndex', 'actionIndex', 'isCtxDependent')
  133. def __init__(self, target:ATNState, ruleIndex:int, actionIndex:int=-1, isCtxDependent:bool=False):
  134. super().__init__(target)
  135. self.serializationType = self.ACTION
  136. self.ruleIndex = ruleIndex
  137. self.actionIndex = actionIndex
  138. self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
  139. self.isEpsilon = True
  140. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  141. return False
  142. def __str__(self):
  143. return "action_"+self.ruleIndex+":"+self.actionIndex
  144. # A transition containing a set of values.
  145. class SetTransition(Transition):
  146. __slots__ = 'serializationType'
  147. def __init__(self, target:ATNState, set:IntervalSet):
  148. super().__init__(target)
  149. self.serializationType = self.SET
  150. if set is not None:
  151. self.label = set
  152. else:
  153. self.label = IntervalSet()
  154. self.label.addRange(range(Token.INVALID_TYPE, Token.INVALID_TYPE + 1))
  155. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  156. return symbol in self.label
  157. def __str__(self):
  158. return str(self.label)
  159. class NotSetTransition(SetTransition):
  160. def __init__(self, target:ATNState, set:IntervalSet):
  161. super().__init__(target, set)
  162. self.serializationType = self.NOT_SET
  163. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  164. return symbol >= minVocabSymbol \
  165. and symbol <= maxVocabSymbol \
  166. and not super(type(self), self).matches(symbol, minVocabSymbol, maxVocabSymbol)
  167. def __str__(self):
  168. return '~' + super(type(self), self).__str__()
  169. class WildcardTransition(Transition):
  170. __slots__ = 'serializationType'
  171. def __init__(self, target:ATNState):
  172. super().__init__(target)
  173. self.serializationType = self.WILDCARD
  174. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  175. return symbol >= minVocabSymbol and symbol <= maxVocabSymbol
  176. def __str__(self):
  177. return "."
  178. class PrecedencePredicateTransition(AbstractPredicateTransition):
  179. __slots__ = ('serializationType', 'precedence')
  180. def __init__(self, target:ATNState, precedence:int):
  181. super().__init__(target)
  182. self.serializationType = self.PRECEDENCE
  183. self.precedence = precedence
  184. self.isEpsilon = True
  185. def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
  186. return False
  187. def getPredicate(self):
  188. return PrecedencePredicate(self.precedence)
  189. def __str__(self):
  190. return self.precedence + " >= _p"
  191. Transition.serializationTypes = {
  192. EpsilonTransition: Transition.EPSILON,
  193. RangeTransition: Transition.RANGE,
  194. RuleTransition: Transition.RULE,
  195. PredicateTransition: Transition.PREDICATE,
  196. AtomTransition: Transition.ATOM,
  197. ActionTransition: Transition.ACTION,
  198. SetTransition: Transition.SET,
  199. NotSetTransition: Transition.NOT_SET,
  200. WildcardTransition: Transition.WILDCARD,
  201. PrecedencePredicateTransition: Transition.PRECEDENCE
  202. }
  203. del ATNState
  204. del RuleStartState
  205. from antlr4.atn.ATNState import *