PageRenderTime 542ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/antlr/antlr4
Python | 252 lines | 170 code | 60 blank | 22 comment | 7 complexity | f730d7cf1746d67f495fbfd68a014ebc 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 __builtin__ import unicode
  19. from antlr4.IntervalSet import IntervalSet, Interval
  20. from antlr4.Token import Token
  21. from antlr4.atn.SemanticContext import Predicate, PrecedencePredicate
  22. class Transition (object):
  23. # constants for serialization
  24. EPSILON = 1
  25. RANGE = 2
  26. RULE = 3
  27. PREDICATE = 4 # e.g., {isType(input.LT(1))}?
  28. ATOM = 5
  29. ACTION = 6
  30. SET = 7 # ~(A|B) or ~atom, wildcard, which convert to next 2
  31. NOT_SET = 8
  32. WILDCARD = 9
  33. PRECEDENCE = 10
  34. serializationNames = [
  35. u"INVALID",
  36. u"EPSILON",
  37. u"RANGE",
  38. u"RULE",
  39. u"PREDICATE",
  40. u"ATOM",
  41. u"ACTION",
  42. u"SET",
  43. u"NOT_SET",
  44. u"WILDCARD",
  45. u"PRECEDENCE"
  46. ]
  47. serializationTypes = dict()
  48. def __init__(self, target):
  49. # The target of this transition.
  50. if target is None:
  51. raise Exception("target cannot be null.")
  52. self.target = target
  53. # Are we epsilon, action, sempred?
  54. self.isEpsilon = False
  55. self.label = None
  56. def __str__(self):
  57. return unicode(self)
  58. # TODO: make all transitions sets? no, should remove set edges
  59. class AtomTransition(Transition):
  60. def __init__(self, target, label):
  61. super(AtomTransition, self).__init__(target)
  62. self.label_ = label # The token type or character value; or, signifies special label.
  63. self.label = self.makeLabel()
  64. self.serializationType = self.ATOM
  65. def makeLabel(self):
  66. s = IntervalSet()
  67. s.addOne(self.label_)
  68. return s
  69. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  70. return self.label_ == symbol
  71. def __unicode__(self):
  72. return unicode(self.label_)
  73. class RuleTransition(Transition):
  74. def __init__(self, ruleStart, ruleIndex, precedence, followState):
  75. super(RuleTransition, self).__init__(ruleStart)
  76. self.ruleIndex = ruleIndex # ptr to the rule definition object for this rule ref
  77. self.precedence = precedence
  78. self.followState = followState # what node to begin computations following ref to rule
  79. self.serializationType = self.RULE
  80. self.isEpsilon = True
  81. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  82. return False
  83. class EpsilonTransition(Transition):
  84. def __init__(self, target, outermostPrecedenceReturn=-1):
  85. super(EpsilonTransition, self).__init__(target)
  86. self.serializationType = self.EPSILON
  87. self.isEpsilon = True
  88. self.outermostPrecedenceReturn = outermostPrecedenceReturn
  89. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  90. return False
  91. def __unicode__(self):
  92. return "epsilon"
  93. class RangeTransition(Transition):
  94. def __init__(self, target, start, stop):
  95. super(RangeTransition, self).__init__(target)
  96. self.serializationType = self.RANGE
  97. self.start = start
  98. self.stop = stop
  99. self.label = self.makeLabel()
  100. def makeLabel(self):
  101. s = IntervalSet()
  102. s.addRange(Interval(self.start, self.stop + 1))
  103. return s
  104. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  105. return symbol >= self.start and symbol <= self.stop
  106. def __unicode__(self):
  107. return "'" + chr(self.start) + "'..'" + chr(self.stop) + "'"
  108. class AbstractPredicateTransition(Transition):
  109. def __init__(self, target):
  110. super(AbstractPredicateTransition, self).__init__(target)
  111. class PredicateTransition(AbstractPredicateTransition):
  112. def __init__(self, target, ruleIndex, predIndex, isCtxDependent):
  113. super(PredicateTransition, self).__init__(target)
  114. self.serializationType = self.PREDICATE
  115. self.ruleIndex = ruleIndex
  116. self.predIndex = predIndex
  117. self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
  118. self.isEpsilon = True
  119. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  120. return False
  121. def getPredicate(self):
  122. return Predicate(self.ruleIndex, self.predIndex, self.isCtxDependent)
  123. def __unicode__(self):
  124. return u"pred_" + unicode(self.ruleIndex) + u":" + unicode(self.predIndex)
  125. class ActionTransition(Transition):
  126. def __init__(self, target, ruleIndex, actionIndex=-1, isCtxDependent=False):
  127. super(ActionTransition, self).__init__(target)
  128. self.serializationType = self.ACTION
  129. self.ruleIndex = ruleIndex
  130. self.actionIndex = actionIndex
  131. self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
  132. self.isEpsilon = True
  133. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  134. return False
  135. def __unicode__(self):
  136. return u"action_" + unicode(self.ruleIndex) + u":" + unicode(self.actionIndex)
  137. # A transition containing a set of values.
  138. class SetTransition(Transition):
  139. def __init__(self, target, set):
  140. super(SetTransition, self).__init__(target)
  141. self.serializationType = self.SET
  142. if set is not None:
  143. self.label = set
  144. else:
  145. self.label = IntervalSet()
  146. self.label.addRange(Interval(Token.INVALID_TYPE, Token.INVALID_TYPE + 1))
  147. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  148. return symbol in self.label
  149. def __unicode__(self):
  150. return unicode(self.label)
  151. class NotSetTransition(SetTransition):
  152. def __init__(self, target, set):
  153. super(NotSetTransition, self).__init__(target, set)
  154. self.serializationType = self.NOT_SET
  155. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  156. return symbol >= minVocabSymbol \
  157. and symbol <= maxVocabSymbol \
  158. and not super(type(self), self).matches(symbol, minVocabSymbol, maxVocabSymbol)
  159. def __unicode__(self):
  160. return u'~' + super(type(self), self).__unicode__()
  161. class WildcardTransition(Transition):
  162. def __init__(self, target):
  163. super(WildcardTransition, self).__init__(target)
  164. self.serializationType = self.WILDCARD
  165. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  166. return symbol >= minVocabSymbol and symbol <= maxVocabSymbol
  167. def __unicode__(self):
  168. return u"."
  169. class PrecedencePredicateTransition(AbstractPredicateTransition):
  170. def __init__(self, target, precedence):
  171. super(PrecedencePredicateTransition, self).__init__(target)
  172. self.serializationType = self.PRECEDENCE
  173. self.precedence = precedence
  174. self.isEpsilon = True
  175. def matches( self, symbol, minVocabSymbol, maxVocabSymbol):
  176. return False
  177. def getPredicate(self):
  178. return PrecedencePredicate(self.precedence)
  179. def __unicode__(self):
  180. return self.precedence + " >= _p"
  181. Transition.serializationTypes = {
  182. EpsilonTransition: Transition.EPSILON,
  183. RangeTransition: Transition.RANGE,
  184. RuleTransition: Transition.RULE,
  185. PredicateTransition: Transition.PREDICATE,
  186. AtomTransition: Transition.ATOM,
  187. ActionTransition: Transition.ACTION,
  188. SetTransition: Transition.SET,
  189. NotSetTransition: Transition.NOT_SET,
  190. WildcardTransition: Transition.WILDCARD,
  191. PrecedencePredicateTransition: Transition.PRECEDENCE
  192. }