SemanticContext.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 tree structure used to record the semantic context in which
  7. # an ATN configuration is valid. It's either a single predicate,
  8. # a conjunction {@code p1&&p2}, or a sum of products {@code p1||p2}.
  9. #
  10. # <p>I have scoped the {@link AND}, {@link OR}, and {@link Predicate} subclasses of
  11. # {@link SemanticContext} within the scope of this outer class.</p>
  12. #
  13. from antlr4.Recognizer import Recognizer
  14. from antlr4.RuleContext import RuleContext
  15. from io import StringIO
  16. class SemanticContext(object):
  17. #
  18. # The default {@link SemanticContext}, which is semantically equivalent to
  19. # a predicate of the form {@code {true}?}.
  20. #
  21. NONE = None
  22. #
  23. # For context independent predicates, we evaluate them without a local
  24. # context (i.e., null context). That way, we can evaluate them without
  25. # having to create proper rule-specific context during prediction (as
  26. # opposed to the parser, which creates them naturally). In a practical
  27. # sense, this avoids a cast exception from RuleContext to myruleContext.
  28. #
  29. # <p>For context dependent predicates, we must pass in a local context so that
  30. # references such as $arg evaluate properly as _localctx.arg. We only
  31. # capture context dependent predicates in the context in which we begin
  32. # prediction, so we passed in the outer context here in case of context
  33. # dependent predicate evaluation.</p>
  34. #
  35. def eval(self, parser:Recognizer , outerContext:RuleContext ):
  36. pass
  37. #
  38. # Evaluate the precedence predicates for the context and reduce the result.
  39. #
  40. # @param parser The parser instance.
  41. # @param outerContext The current parser context object.
  42. # @return The simplified semantic context after precedence predicates are
  43. # evaluated, which will be one of the following values.
  44. # <ul>
  45. # <li>{@link #NONE}: if the predicate simplifies to {@code true} after
  46. # precedence predicates are evaluated.</li>
  47. # <li>{@code null}: if the predicate simplifies to {@code false} after
  48. # precedence predicates are evaluated.</li>
  49. # <li>{@code this}: if the semantic context is not changed as a result of
  50. # precedence predicate evaluation.</li>
  51. # <li>A non-{@code null} {@link SemanticContext}: the new simplified
  52. # semantic context after precedence predicates are evaluated.</li>
  53. # </ul>
  54. #
  55. def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
  56. return self
  57. # need forward declaration
  58. AND = None
  59. def andContext(a:SemanticContext, b:SemanticContext):
  60. if a is None or a is SemanticContext.NONE:
  61. return b
  62. if b is None or b is SemanticContext.NONE:
  63. return a
  64. result = AND(a, b)
  65. if len(result.opnds) == 1:
  66. return result.opnds[0]
  67. else:
  68. return result
  69. # need forward declaration
  70. OR = None
  71. def orContext(a:SemanticContext, b:SemanticContext):
  72. if a is None:
  73. return b
  74. if b is None:
  75. return a
  76. if a is SemanticContext.NONE or b is SemanticContext.NONE:
  77. return SemanticContext.NONE
  78. result = OR(a, b)
  79. if len(result.opnds) == 1:
  80. return result.opnds[0]
  81. else:
  82. return result
  83. def filterPrecedencePredicates(collection:set):
  84. return [context for context in collection if isinstance(context, PrecedencePredicate)]
  85. class Predicate(SemanticContext):
  86. __slots__ = ('ruleIndex', 'predIndex', 'isCtxDependent')
  87. def __init__(self, ruleIndex:int=-1, predIndex:int=-1, isCtxDependent:bool=False):
  88. self.ruleIndex = ruleIndex
  89. self.predIndex = predIndex
  90. self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
  91. def eval(self, parser:Recognizer , outerContext:RuleContext ):
  92. localctx = outerContext if self.isCtxDependent else None
  93. return parser.sempred(localctx, self.ruleIndex, self.predIndex)
  94. def __hash__(self):
  95. return hash((self.ruleIndex, self.predIndex, self.isCtxDependent))
  96. def __eq__(self, other):
  97. if self is other:
  98. return True
  99. elif not isinstance(other, Predicate):
  100. return False
  101. return self.ruleIndex == other.ruleIndex and \
  102. self.predIndex == other.predIndex and \
  103. self.isCtxDependent == other.isCtxDependent
  104. def __str__(self):
  105. return "{" + str(self.ruleIndex) + ":" + str(self.predIndex) + "}?"
  106. class PrecedencePredicate(SemanticContext):
  107. def __init__(self, precedence:int=0):
  108. self.precedence = precedence
  109. def eval(self, parser:Recognizer , outerContext:RuleContext ):
  110. return parser.precpred(outerContext, self.precedence)
  111. def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
  112. if parser.precpred(outerContext, self.precedence):
  113. return SemanticContext.NONE
  114. else:
  115. return None
  116. def __lt__(self, other):
  117. return self.precedence < other.precedence
  118. def __hash__(self):
  119. return 31
  120. def __eq__(self, other):
  121. if self is other:
  122. return True
  123. elif not isinstance(other, PrecedencePredicate):
  124. return False
  125. else:
  126. return self.precedence == other.precedence
  127. # A semantic context which is true whenever none of the contained contexts
  128. # is false.
  129. del AND
  130. class AND(SemanticContext):
  131. __slots__ = 'opnds'
  132. def __init__(self, a:SemanticContext, b:SemanticContext):
  133. operands = set()
  134. if isinstance( a, AND ):
  135. operands.update(a.opnds)
  136. else:
  137. operands.add(a)
  138. if isinstance( b, AND ):
  139. operands.update(b.opnds)
  140. else:
  141. operands.add(b)
  142. precedencePredicates = filterPrecedencePredicates(operands)
  143. if len(precedencePredicates)>0:
  144. # interested in the transition with the lowest precedence
  145. reduced = min(precedencePredicates)
  146. operands.add(reduced)
  147. self.opnds = list(operands)
  148. def __eq__(self, other):
  149. if self is other:
  150. return True
  151. elif not isinstance(other, AND):
  152. return False
  153. else:
  154. return self.opnds == other.opnds
  155. def __hash__(self):
  156. h = 0
  157. for o in self.opnds:
  158. h = hash((h, o))
  159. return hash((h, "AND"))
  160. #
  161. # {@inheritDoc}
  162. #
  163. # <p>
  164. # The evaluation of predicates by this context is short-circuiting, but
  165. # unordered.</p>
  166. #
  167. def eval(self, parser:Recognizer, outerContext:RuleContext):
  168. return all(opnd.eval(parser, outerContext) for opnd in self.opnds)
  169. def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
  170. differs = False
  171. operands = []
  172. for context in self.opnds:
  173. evaluated = context.evalPrecedence(parser, outerContext)
  174. differs |= evaluated is not context
  175. if evaluated is None:
  176. # The AND context is false if any element is false
  177. return None
  178. elif evaluated is not SemanticContext.NONE:
  179. # Reduce the result by skipping true elements
  180. operands.append(evaluated)
  181. if not differs:
  182. return self
  183. if len(operands)==0:
  184. # all elements were true, so the AND context is true
  185. return SemanticContext.NONE
  186. result = None
  187. for o in operands:
  188. result = o if result is None else andContext(result, o)
  189. return result
  190. def __str__(self):
  191. with StringIO() as buf:
  192. first = True
  193. for o in self.opnds:
  194. if not first:
  195. buf.write("&&")
  196. buf.write(str(o))
  197. first = False
  198. return buf.getvalue()
  199. #
  200. # A semantic context which is true whenever at least one of the contained
  201. # contexts is true.
  202. del OR
  203. class OR (SemanticContext):
  204. __slots__ = 'opnds'
  205. def __init__(self, a:SemanticContext, b:SemanticContext):
  206. operands = set()
  207. if isinstance( a, OR ):
  208. operands.update(a.opnds)
  209. else:
  210. operands.add(a)
  211. if isinstance( b, OR ):
  212. operands.update(b.opnds)
  213. else:
  214. operands.add(b)
  215. precedencePredicates = filterPrecedencePredicates(operands)
  216. if len(precedencePredicates)>0:
  217. # interested in the transition with the highest precedence
  218. s = sorted(precedencePredicates)
  219. reduced = s[-1]
  220. operands.add(reduced)
  221. self.opnds = list(operands)
  222. def __eq__(self, other):
  223. if self is other:
  224. return True
  225. elif not isinstance(other, OR):
  226. return False
  227. else:
  228. return self.opnds == other.opnds
  229. def __hash__(self):
  230. h = 0
  231. for o in self.opnds:
  232. h = hash((h, o))
  233. return hash((h, "OR"))
  234. # <p>
  235. # The evaluation of predicates by this context is short-circuiting, but
  236. # unordered.</p>
  237. #
  238. def eval(self, parser:Recognizer, outerContext:RuleContext):
  239. return any(opnd.eval(parser, outerContext) for opnd in self.opnds)
  240. def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
  241. differs = False
  242. operands = []
  243. for context in self.opnds:
  244. evaluated = context.evalPrecedence(parser, outerContext)
  245. differs |= evaluated is not context
  246. if evaluated is SemanticContext.NONE:
  247. # The OR context is true if any element is true
  248. return SemanticContext.NONE
  249. elif evaluated is not None:
  250. # Reduce the result by skipping false elements
  251. operands.append(evaluated)
  252. if not differs:
  253. return self
  254. if len(operands)==0:
  255. # all elements were false, so the OR context is false
  256. return None
  257. result = None
  258. for o in operands:
  259. result = o if result is None else orContext(result, o)
  260. return result
  261. def __str__(self):
  262. with StringIO() as buf:
  263. first = True
  264. for o in self.opnds:
  265. if not first:
  266. buf.write("||")
  267. buf.write(str(o))
  268. first = False
  269. return buf.getvalue()
  270. SemanticContext.NONE = Predicate()