did_parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +12 -0
  3. data/Rakefile +28 -0
  4. data/did_parser.gemspec +31 -0
  5. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRErrorListener.cpp +10 -0
  6. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRErrorListener.h +167 -0
  7. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRErrorStrategy.cpp +10 -0
  8. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRErrorStrategy.h +121 -0
  9. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRFileStream.cpp +23 -0
  10. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRFileStream.h +30 -0
  11. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRInputStream.cpp +180 -0
  12. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ANTLRInputStream.h +79 -0
  13. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BailErrorStrategy.cpp +61 -0
  14. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BailErrorStrategy.h +59 -0
  15. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BaseErrorListener.cpp +25 -0
  16. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BaseErrorListener.h +36 -0
  17. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BufferedTokenStream.cpp +414 -0
  18. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/BufferedTokenStream.h +200 -0
  19. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CharStream.cpp +11 -0
  20. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CharStream.h +37 -0
  21. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonToken.cpp +193 -0
  22. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonToken.h +158 -0
  23. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonTokenFactory.cpp +39 -0
  24. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonTokenFactory.h +74 -0
  25. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonTokenStream.cpp +78 -0
  26. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/CommonTokenStream.h +79 -0
  27. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ConsoleErrorListener.cpp +15 -0
  28. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ConsoleErrorListener.h +35 -0
  29. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp +336 -0
  30. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/DefaultErrorStrategy.h +466 -0
  31. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/DiagnosticErrorListener.cpp +84 -0
  32. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/DiagnosticErrorListener.h +80 -0
  33. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Exceptions.cpp +64 -0
  34. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Exceptions.h +99 -0
  35. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/FailedPredicateException.cpp +52 -0
  36. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/FailedPredicateException.h +32 -0
  37. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/InputMismatchException.cpp +18 -0
  38. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/InputMismatchException.h +24 -0
  39. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/IntStream.cpp +12 -0
  40. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/IntStream.h +218 -0
  41. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/InterpreterRuleContext.cpp +19 -0
  42. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/InterpreterRuleContext.h +45 -0
  43. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Lexer.cpp +294 -0
  44. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Lexer.h +196 -0
  45. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/LexerInterpreter.cpp +60 -0
  46. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/LexerInterpreter.h +46 -0
  47. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/LexerNoViableAltException.cpp +36 -0
  48. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/LexerNoViableAltException.h +31 -0
  49. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ListTokenSource.cpp +92 -0
  50. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ListTokenSource.h +88 -0
  51. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/NoViableAltException.cpp +46 -0
  52. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/NoViableAltException.h +42 -0
  53. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Parser.cpp +668 -0
  54. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Parser.h +461 -0
  55. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ParserInterpreter.cpp +294 -0
  56. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ParserInterpreter.h +173 -0
  57. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ParserRuleContext.cpp +138 -0
  58. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ParserRuleContext.h +147 -0
  59. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ProxyErrorListener.cpp +53 -0
  60. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/ProxyErrorListener.h +38 -0
  61. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RecognitionException.cpp +65 -0
  62. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RecognitionException.h +98 -0
  63. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Recognizer.cpp +156 -0
  64. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Recognizer.h +159 -0
  65. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuleContext.cpp +144 -0
  66. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuleContext.h +141 -0
  67. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuleContextWithAltNum.cpp +27 -0
  68. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuleContextWithAltNum.h +32 -0
  69. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuntimeMetaData.cpp +54 -0
  70. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/RuntimeMetaData.h +155 -0
  71. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Token.cpp +9 -0
  72. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Token.h +92 -0
  73. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenFactory.h +30 -0
  74. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenSource.cpp +9 -0
  75. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenSource.h +85 -0
  76. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenStream.cpp +11 -0
  77. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenStream.h +137 -0
  78. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenStreamRewriter.cpp +425 -0
  79. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/TokenStreamRewriter.h +295 -0
  80. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp +208 -0
  81. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/UnbufferedCharStream.h +117 -0
  82. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/UnbufferedTokenStream.cpp +270 -0
  83. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/UnbufferedTokenStream.h +115 -0
  84. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Version.h +42 -0
  85. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Vocabulary.cpp +64 -0
  86. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/Vocabulary.h +177 -0
  87. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/WritableToken.cpp +9 -0
  88. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/WritableToken.h +23 -0
  89. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/antlr4-common.h +98 -0
  90. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/antlr4-runtime.h +169 -0
  91. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATN.cpp +158 -0
  92. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATN.h +132 -0
  93. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNConfig.cpp +106 -0
  94. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNConfig.h +157 -0
  95. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp +232 -0
  96. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNConfigSet.h +156 -0
  97. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNDeserializationOptions.cpp +54 -0
  98. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNDeserializationOptions.h +48 -0
  99. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp +629 -0
  100. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNDeserializer.h +32 -0
  101. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp +33 -0
  102. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNSimulator.h +71 -0
  103. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNState.cpp +56 -0
  104. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNState.h +139 -0
  105. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNStateType.cpp +33 -0
  106. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNStateType.h +36 -0
  107. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ATNType.h +20 -0
  108. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ActionTransition.cpp +29 -0
  109. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ActionTransition.h +35 -0
  110. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp +16 -0
  111. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/AmbiguityInfo.h +68 -0
  112. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp +109 -0
  113. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h +51 -0
  114. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/AtomTransition.cpp +27 -0
  115. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/AtomTransition.h +33 -0
  116. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/BasicBlockStartState.h +24 -0
  117. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/BasicState.h +23 -0
  118. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/BlockEndState.h +26 -0
  119. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/BlockStartState.h +30 -0
  120. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.cpp +14 -0
  121. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.h +47 -0
  122. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionEventInfo.cpp +14 -0
  123. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionEventInfo.h +70 -0
  124. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionInfo.cpp +25 -0
  125. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionInfo.h +227 -0
  126. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionState.cpp +12 -0
  127. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/DecisionState.h +34 -0
  128. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp +31 -0
  129. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/EpsilonTransition.h +42 -0
  130. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ErrorInfo.cpp +15 -0
  131. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ErrorInfo.h +43 -0
  132. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp +189 -0
  133. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LL1Analyzer.h +76 -0
  134. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerATNConfig.cpp +67 -0
  135. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerATNConfig.h +44 -0
  136. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp +615 -0
  137. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerATNSimulator.h +199 -0
  138. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerAction.cpp +15 -0
  139. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerAction.h +100 -0
  140. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp +111 -0
  141. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerActionExecutor.h +128 -0
  142. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerActionType.h +57 -0
  143. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerChannelAction.cpp +43 -0
  144. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerChannelAction.h +59 -0
  145. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerCustomAction.cpp +45 -0
  146. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerCustomAction.h +75 -0
  147. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerIndexedCustomAction.cpp +57 -0
  148. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerIndexedCustomAction.h +76 -0
  149. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerModeAction.cpp +43 -0
  150. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerModeAction.h +57 -0
  151. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerMoreAction.cpp +36 -0
  152. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerMoreAction.h +53 -0
  153. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerPopModeAction.cpp +36 -0
  154. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerPopModeAction.h +53 -0
  155. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerPushModeAction.cpp +43 -0
  156. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerPushModeAction.h +57 -0
  157. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerSkipAction.cpp +36 -0
  158. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerSkipAction.h +51 -0
  159. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerTypeAction.cpp +43 -0
  160. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LexerTypeAction.h +51 -0
  161. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LookaheadEventInfo.cpp +16 -0
  162. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LookaheadEventInfo.h +42 -0
  163. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/LoopEndState.h +26 -0
  164. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/NotSetTransition.cpp +22 -0
  165. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/NotSetTransition.h +27 -0
  166. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/OrderedATNConfigSet.cpp +16 -0
  167. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/OrderedATNConfigSet.h +25 -0
  168. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ParseInfo.cpp +102 -0
  169. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ParseInfo.h +102 -0
  170. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp +1383 -0
  171. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h +911 -0
  172. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ParserATNSimulatorOptions.h +50 -0
  173. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PlusBlockStartState.h +29 -0
  174. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PlusLoopbackState.h +25 -0
  175. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.cpp +23 -0
  176. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.h +35 -0
  177. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.cpp +17 -0
  178. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.h +62 -0
  179. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredicateTransition.cpp +24 -0
  180. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredicateTransition.h +50 -0
  181. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContext.cpp +579 -0
  182. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContext.h +225 -0
  183. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextCache.cpp +56 -0
  184. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextCache.h +64 -0
  185. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextMergeCache.cpp +167 -0
  186. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextMergeCache.h +101 -0
  187. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextMergeCacheOptions.h +71 -0
  188. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionContextType.h +21 -0
  189. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionMode.cpp +202 -0
  190. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/PredictionMode.h +436 -0
  191. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.cpp +179 -0
  192. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.h +60 -0
  193. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RangeTransition.cpp +26 -0
  194. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RangeTransition.h +31 -0
  195. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RuleStartState.h +26 -0
  196. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RuleStopState.h +27 -0
  197. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RuleTransition.cpp +33 -0
  198. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/RuleTransition.h +42 -0
  199. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SemanticContext.cpp +418 -0
  200. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SemanticContext.h +233 -0
  201. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SemanticContextType.h +23 -0
  202. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SerializedATNView.h +101 -0
  203. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SetTransition.cpp +28 -0
  204. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SetTransition.h +38 -0
  205. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp +86 -0
  206. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.h +43 -0
  207. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/StarBlockStartState.h +24 -0
  208. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/StarLoopEntryState.h +37 -0
  209. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/StarLoopbackState.cpp +19 -0
  210. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/StarLoopbackState.h +25 -0
  211. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/TokensStartState.h +24 -0
  212. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/Transition.cpp +36 -0
  213. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/Transition.h +65 -0
  214. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/TransitionType.cpp +27 -0
  215. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/TransitionType.h +33 -0
  216. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/WildcardTransition.cpp +21 -0
  217. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/atn/WildcardTransition.h +27 -0
  218. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFA.cpp +115 -0
  219. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFA.h +96 -0
  220. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFASerializer.cpp +60 -0
  221. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFASerializer.h +32 -0
  222. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFAState.cpp +59 -0
  223. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/DFAState.h +153 -0
  224. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/LexerDFASerializer.cpp +17 -0
  225. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/dfa/LexerDFASerializer.h +22 -0
  226. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/InterpreterDataReader.cpp +124 -0
  227. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/InterpreterDataReader.h +33 -0
  228. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/Interval.cpp +61 -0
  229. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/Interval.h +84 -0
  230. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/IntervalSet.cpp +508 -0
  231. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/IntervalSet.h +190 -0
  232. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/MurmurHash.cpp +120 -0
  233. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/MurmurHash.h +102 -0
  234. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/Predicate.cpp +4 -0
  235. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/misc/Predicate.h +21 -0
  236. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Any.cpp +8 -0
  237. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Any.h +16 -0
  238. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Arrays.cpp +43 -0
  239. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Arrays.h +149 -0
  240. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/BitSet.h +76 -0
  241. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/CPPUtils.cpp +207 -0
  242. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/CPPUtils.h +65 -0
  243. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Casts.h +34 -0
  244. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Declarations.h +161 -0
  245. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/StringUtils.cpp +38 -0
  246. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/StringUtils.h +16 -0
  247. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Unicode.h +28 -0
  248. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Utf8.cpp +242 -0
  249. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/support/Utf8.h +54 -0
  250. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/AbstractParseTreeVisitor.h +129 -0
  251. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ErrorNode.h +24 -0
  252. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ErrorNodeImpl.cpp +54 -0
  253. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ErrorNodeImpl.h +43 -0
  254. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/IterativeParseTreeWalker.cpp +66 -0
  255. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/IterativeParseTreeWalker.h +53 -0
  256. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTree.cpp +12 -0
  257. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTree.h +111 -0
  258. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeListener.cpp +9 -0
  259. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeListener.h +39 -0
  260. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeProperty.h +50 -0
  261. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeType.h +22 -0
  262. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeVisitor.cpp +9 -0
  263. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeVisitor.h +57 -0
  264. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeWalker.cpp +48 -0
  265. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/ParseTreeWalker.h +55 -0
  266. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/TerminalNode.h +40 -0
  267. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp +54 -0
  268. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h +32 -0
  269. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/Trees.cpp +241 -0
  270. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/Trees.h +78 -0
  271. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/Chunk.cpp +9 -0
  272. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/Chunk.h +44 -0
  273. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreeMatch.cpp +69 -0
  274. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreeMatch.h +132 -0
  275. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreePattern.cpp +64 -0
  276. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreePattern.h +105 -0
  277. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp +370 -0
  278. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.h +185 -0
  279. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.cpp +77 -0
  280. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.h +117 -0
  281. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TagChunk.cpp +39 -0
  282. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TagChunk.h +86 -0
  283. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TextChunk.cpp +28 -0
  284. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TextChunk.h +51 -0
  285. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TokenTagToken.cpp +36 -0
  286. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/pattern/TokenTagToken.h +80 -0
  287. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp +154 -0
  288. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPath.h +86 -0
  289. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathElement.cpp +31 -0
  290. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathElement.h +40 -0
  291. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp +182 -0
  292. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h +47 -0
  293. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.cpp +13 -0
  294. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.h +22 -0
  295. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp +20 -0
  296. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathRuleAnywhereElement.h +27 -0
  297. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.cpp +30 -0
  298. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.h +26 -0
  299. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp +20 -0
  300. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathTokenAnywhereElement.h +25 -0
  301. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.cpp +33 -0
  302. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.h +26 -0
  303. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp +23 -0
  304. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h +23 -0
  305. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathWildcardElement.cpp +24 -0
  306. data/ext/did_parser/antlr4-upstream/runtime/Cpp/runtime/src/tree/xpath/XPathWildcardElement.h +23 -0
  307. data/ext/did_parser/antlrgen/DIDLexer.cpp +225 -0
  308. data/ext/did_parser/antlrgen/DIDLexer.h +52 -0
  309. data/ext/did_parser/antlrgen/DIDLexer.interp +93 -0
  310. data/ext/did_parser/antlrgen/DIDLexer.tokens +42 -0
  311. data/ext/did_parser/antlrgen/DIDParser.cpp +2042 -0
  312. data/ext/did_parser/antlrgen/DIDParser.h +537 -0
  313. data/ext/did_parser/antlrgen/DIDParser.interp +74 -0
  314. data/ext/did_parser/antlrgen/DIDParser.tokens +42 -0
  315. data/ext/did_parser/antlrgen/DIDParserBaseListener.cpp +7 -0
  316. data/ext/did_parser/antlrgen/DIDParserBaseListener.h +104 -0
  317. data/ext/did_parser/antlrgen/DIDParserBaseVisitor.cpp +7 -0
  318. data/ext/did_parser/antlrgen/DIDParserBaseVisitor.h +124 -0
  319. data/ext/did_parser/antlrgen/DIDParserListener.cpp +7 -0
  320. data/ext/did_parser/antlrgen/DIDParserListener.h +97 -0
  321. data/ext/did_parser/antlrgen/DIDParserVisitor.cpp +7 -0
  322. data/ext/did_parser/antlrgen/DIDParserVisitor.h +76 -0
  323. data/ext/did_parser/did_parser.cpp +2996 -0
  324. data/ext/did_parser/extconf.rb +38 -0
  325. data/lib/did_parser/version.rb +3 -0
  326. data/lib/did_parser.rb +2 -0
  327. metadata +382 -0
@@ -0,0 +1,911 @@
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
+
6
+ #pragma once
7
+
8
+ #include "PredictionMode.h"
9
+ #include "dfa/DFAState.h"
10
+ #include "atn/ATNSimulator.h"
11
+ #include "atn/PredictionContext.h"
12
+ #include "atn/PredictionContextMergeCache.h"
13
+ #include "atn/ParserATNSimulatorOptions.h"
14
+ #include "SemanticContext.h"
15
+ #include "atn/ATNConfig.h"
16
+
17
+ namespace antlr4 {
18
+ namespace atn {
19
+
20
+ /**
21
+ * The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
22
+ *
23
+ * <p>
24
+ * The basic complexity of the adaptive strategy makes it harder to understand.
25
+ * We begin with ATN simulation to build paths in a DFA. Subsequent prediction
26
+ * requests go through the DFA first. If they reach a state without an edge for
27
+ * the current symbol, the algorithm fails over to the ATN simulation to
28
+ * complete the DFA path for the current input (until it finds a conflict state
29
+ * or uniquely predicting state).</p>
30
+ *
31
+ * <p>
32
+ * All of that is done without using the outer context because we want to create
33
+ * a DFA that is not dependent upon the rule invocation stack when we do a
34
+ * prediction. One DFA works in all contexts. We avoid using context not
35
+ * necessarily because it's slower, although it can be, but because of the DFA
36
+ * caching problem. The closure routine only considers the rule invocation stack
37
+ * created during prediction beginning in the decision rule. For example, if
38
+ * prediction occurs without invoking another rule's ATN, there are no context
39
+ * stacks in the configurations. When lack of context leads to a conflict, we
40
+ * don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
41
+ * strategy (versus full LL(*)).</p>
42
+ *
43
+ * <p>
44
+ * When SLL yields a configuration set with conflict, we rewind the input and
45
+ * retry the ATN simulation, this time using full outer context without adding
46
+ * to the DFA. Configuration context stacks will be the full invocation stacks
47
+ * from the start rule. If we get a conflict using full context, then we can
48
+ * definitively say we have a true ambiguity for that input sequence. If we
49
+ * don't get a conflict, it implies that the decision is sensitive to the outer
50
+ * context. (It is not context-sensitive in the sense of context-sensitive
51
+ * grammars.)</p>
52
+ *
53
+ * <p>
54
+ * The next time we reach this DFA state with an SLL conflict, through DFA
55
+ * simulation, we will again retry the ATN simulation using full context mode.
56
+ * This is slow because we can't save the results and have to "interpret" the
57
+ * ATN each time we get that input.</p>
58
+ *
59
+ * <p>
60
+ * <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
61
+ *
62
+ * <p>
63
+ * We could cache results from full context to predicted alternative easily and
64
+ * that saves a lot of time but doesn't work in presence of predicates. The set
65
+ * of visible predicates from the ATN start state changes depending on the
66
+ * context, because closure can fall off the end of a rule. I tried to cache
67
+ * tuples (stack context, semantic context, predicted alt) but it was slower
68
+ * than interpreting and much more complicated. Also required a huge amount of
69
+ * memory. The goal is not to create the world's fastest parser anyway. I'd like
70
+ * to keep this algorithm simple. By launching multiple threads, we can improve
71
+ * the speed of parsing across a large number of files.</p>
72
+ *
73
+ * <p>
74
+ * There is no strict ordering between the amount of input used by SLL vs LL,
75
+ * which makes it really hard to build a cache for full context. Let's say that
76
+ * we have input A B C that leads to an SLL conflict with full context X. That
77
+ * implies that using X we might only use A B but we could also use A B C D to
78
+ * resolve conflict. Input A B C D could predict alternative 1 in one position
79
+ * in the input and A B C E could predict alternative 2 in another position in
80
+ * input. The conflicting SLL configurations could still be non-unique in the
81
+ * full context prediction, which would lead us to requiring more input than the
82
+ * original A B C. To make a prediction cache work, we have to track the exact
83
+ * input used during the previous prediction. That amounts to a cache that maps
84
+ * X to a specific DFA for that context.</p>
85
+ *
86
+ * <p>
87
+ * Something should be done for left-recursive expression predictions. They are
88
+ * likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
89
+ * with full LL thing Sam does.</p>
90
+ *
91
+ * <p>
92
+ * <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
93
+ *
94
+ * <p>
95
+ * We avoid doing full context retry when the outer context is empty, we did not
96
+ * dip into the outer context by falling off the end of the decision state rule,
97
+ * or when we force SLL mode.</p>
98
+ *
99
+ * <p>
100
+ * As an example of the not dip into outer context case, consider as super
101
+ * constructor calls versus function calls. One grammar might look like
102
+ * this:</p>
103
+ *
104
+ * <pre>
105
+ * ctorBody
106
+ * : '{' superCall? stat* '}'
107
+ * ;
108
+ * </pre>
109
+ *
110
+ * <p>
111
+ * Or, you might see something like</p>
112
+ *
113
+ * <pre>
114
+ * stat
115
+ * : superCall ';'
116
+ * | expression ';'
117
+ * | ...
118
+ * ;
119
+ * </pre>
120
+ *
121
+ * <p>
122
+ * In both cases I believe that no closure operations will dip into the outer
123
+ * context. In the first case ctorBody in the worst case will stop at the '}'.
124
+ * In the 2nd case it should stop at the ';'. Both cases should stay within the
125
+ * entry rule and not dip into the outer context.</p>
126
+ *
127
+ * <p>
128
+ * <strong>PREDICATES</strong></p>
129
+ *
130
+ * <p>
131
+ * Predicates are always evaluated if present in either SLL or LL both. SLL and
132
+ * LL simulation deals with predicates differently. SLL collects predicates as
133
+ * it performs closure operations like ANTLR v3 did. It delays predicate
134
+ * evaluation until it reaches and accept state. This allows us to cache the SLL
135
+ * ATN simulation whereas, if we had evaluated predicates on-the-fly during
136
+ * closure, the DFA state configuration sets would be different and we couldn't
137
+ * build up a suitable DFA.</p>
138
+ *
139
+ * <p>
140
+ * When building a DFA accept state during ATN simulation, we evaluate any
141
+ * predicates and return the sole semantically valid alternative. If there is
142
+ * more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
143
+ * we throw an exception. Alternatives without predicates act like they have
144
+ * true predicates. The simple way to think about it is to strip away all
145
+ * alternatives with false predicates and choose the minimum alternative that
146
+ * remains.</p>
147
+ *
148
+ * <p>
149
+ * When we start in the DFA and reach an accept state that's predicated, we test
150
+ * those and return the minimum semantically viable alternative. If no
151
+ * alternatives are viable, we throw an exception.</p>
152
+ *
153
+ * <p>
154
+ * During full LL ATN simulation, closure always evaluates predicates and
155
+ * on-the-fly. This is crucial to reducing the configuration set size during
156
+ * closure. It hits a landmine when parsing with the Java grammar, for example,
157
+ * without this on-the-fly evaluation.</p>
158
+ *
159
+ * <p>
160
+ * <strong>SHARING DFA</strong></p>
161
+ *
162
+ * <p>
163
+ * All instances of the same parser share the same decision DFAs through a
164
+ * static field. Each instance gets its own ATN simulator but they share the
165
+ * same {@link #decisionToDFA} field. They also share a
166
+ * {@link PredictionContextCache} object that makes sure that all
167
+ * {@link PredictionContext} objects are shared among the DFA states. This makes
168
+ * a big size difference.</p>
169
+ *
170
+ * <p>
171
+ * <strong>THREAD SAFETY</strong></p>
172
+ *
173
+ * <p>
174
+ * The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when
175
+ * it adds a new DFA object to that array. {@link #addDFAEdge}
176
+ * locks on the DFA for the current decision when setting the
177
+ * {@link DFAState#edges} field. {@link #addDFAState} locks on
178
+ * the DFA for the current decision when looking up a DFA state to see if it
179
+ * already exists. We must make sure that all requests to add DFA states that
180
+ * are equivalent result in the same shared DFA object. This is because lots of
181
+ * threads will be trying to update the DFA at once. The
182
+ * {@link #addDFAState} method also locks inside the DFA lock
183
+ * but this time on the shared context cache when it rebuilds the
184
+ * configurations' {@link PredictionContext} objects using cached
185
+ * subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
186
+ * safe as long as we can guarantee that all threads referencing
187
+ * {@code s.edge[t]} get the same physical target {@link DFAState}, or
188
+ * {@code null}. Once into the DFA, the DFA simulation does not reference the
189
+ * {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
190
+ * targets. The DFA simulator will either find {@link DFAState#edges} to be
191
+ * {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or
192
+ * {@code dfa.edges[t]} to be non-null. The
193
+ * {@link #addDFAEdge} method could be racing to set the field
194
+ * but in either case the DFA simulator works; if {@code null}, and requests ATN
195
+ * simulation. It could also race trying to get {@code dfa.edges[t]}, but either
196
+ * way it will work because it's not doing a test and set operation.</p>
197
+ *
198
+ * <p>
199
+ * <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
200
+ * Parsing)</strong></p>
201
+ *
202
+ * <p>
203
+ * Sam pointed out that if SLL does not give a syntax error, then there is no
204
+ * point in doing full LL, which is slower. We only have to try LL if we get a
205
+ * syntax error. For maximum speed, Sam starts the parser set to pure SLL
206
+ * mode with the {@link BailErrorStrategy}:</p>
207
+ *
208
+ * <pre>
209
+ * parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
210
+ * parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
211
+ * </pre>
212
+ *
213
+ * <p>
214
+ * If it does not get a syntax error, then we're done. If it does get a syntax
215
+ * error, we need to retry with the combined SLL/LL strategy.</p>
216
+ *
217
+ * <p>
218
+ * The reason this works is as follows. If there are no SLL conflicts, then the
219
+ * grammar is SLL (at least for that input set). If there is an SLL conflict,
220
+ * the full LL analysis must yield a set of viable alternatives which is a
221
+ * subset of the alternatives reported by SLL. If the LL set is a singleton,
222
+ * then the grammar is LL but not SLL. If the LL set is the same size as the SLL
223
+ * set, the decision is SLL. If the LL set has size &gt; 1, then that decision
224
+ * is truly ambiguous on the current input. If the LL set is smaller, then the
225
+ * SLL conflict resolution might choose an alternative that the full LL would
226
+ * rule out as a possibility based upon better context information. If that's
227
+ * the case, then the SLL parse will definitely get an error because the full LL
228
+ * analysis says it's not viable. If SLL conflict resolution chooses an
229
+ * alternative within the LL set, them both SLL and LL would choose the same
230
+ * alternative because they both choose the minimum of multiple conflicting
231
+ * alternatives.</p>
232
+ *
233
+ * <p>
234
+ * Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
235
+ * a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
236
+ * parsing will get an error because SLL will pursue alternative 1. If
237
+ * <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
238
+ * choose the same alternative because alternative one is the minimum of either
239
+ * set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
240
+ * error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
241
+ *
242
+ * <p>
243
+ * Of course, if the input is invalid, then we will get an error for sure in
244
+ * both SLL and LL parsing. Erroneous input will therefore require 2 passes over
245
+ * the input.</p>
246
+ */
247
+ class ANTLR4CPP_PUBLIC ParserATNSimulator : public ATNSimulator {
248
+ public:
249
+ /// Testing only!
250
+ ParserATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
251
+ PredictionContextCache &sharedContextCache);
252
+
253
+ ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
254
+ PredictionContextCache &sharedContextCache);
255
+
256
+ ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
257
+ PredictionContextCache &sharedContextCache,
258
+ const ParserATNSimulatorOptions &options);
259
+
260
+ virtual void reset() override;
261
+ virtual void clearDFA() override;
262
+ virtual size_t adaptivePredict(TokenStream *input, size_t decision, ParserRuleContext *outerContext);
263
+
264
+ static const bool TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT;
265
+
266
+ std::vector<dfa::DFA> &decisionToDFA;
267
+
268
+ /** Implements first-edge (loop entry) elimination as an optimization
269
+ * during closure operations. See antlr/antlr4#1398.
270
+ *
271
+ * The optimization is to avoid adding the loop entry config when
272
+ * the exit path can only lead back to the same
273
+ * StarLoopEntryState after popping context at the rule end state
274
+ * (traversing only epsilon edges, so we're still in closure, in
275
+ * this same rule).
276
+ *
277
+ * We need to detect any state that can reach loop entry on
278
+ * epsilon w/o exiting rule. We don't have to look at FOLLOW
279
+ * links, just ensure that all stack tops for config refer to key
280
+ * states in LR rule.
281
+ *
282
+ * To verify we are in the right situation we must first check
283
+ * closure is at a StarLoopEntryState generated during LR removal.
284
+ * Then we check that each stack top of context is a return state
285
+ * from one of these cases:
286
+ *
287
+ * 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
288
+ * 2. expr op expr. The return state is the block end of internal block of (...)*
289
+ * 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
290
+ * That state points at block end of internal block of (...)*.
291
+ * 4. expr '?' expr ':' expr. The return state points at block end,
292
+ * which points at loop entry state.
293
+ *
294
+ * If any is true for each stack top, then closure does not add a
295
+ * config to the current config set for edge[0], the loop entry branch.
296
+ *
297
+ * Conditions fail if any context for the current config is:
298
+ *
299
+ * a. empty (we'd fall out of expr to do a global FOLLOW which could
300
+ * even be to some weird spot in expr) or,
301
+ * b. lies outside of expr or,
302
+ * c. lies within expr but at a state not the BlockEndState
303
+ * generated during LR removal
304
+ *
305
+ * Do we need to evaluate predicates ever in closure for this case?
306
+ *
307
+ * No. Predicates, including precedence predicates, are only
308
+ * evaluated when computing a DFA start state. I.e., only before
309
+ * the lookahead (but not parser) consumes a token.
310
+ *
311
+ * There are no epsilon edges allowed in LR rule alt blocks or in
312
+ * the "primary" part (ID here). If closure is in
313
+ * StarLoopEntryState any lookahead operation will have consumed a
314
+ * token as there are no epsilon-paths that lead to
315
+ * StarLoopEntryState. We do not have to evaluate predicates
316
+ * therefore if we are in the generated StarLoopEntryState of a LR
317
+ * rule. Note that when making a prediction starting at that
318
+ * decision point, decision d=2, compute-start-state performs
319
+ * closure starting at edges[0], edges[1] emanating from
320
+ * StarLoopEntryState. That means it is not performing closure on
321
+ * StarLoopEntryState during compute-start-state.
322
+ *
323
+ * How do we know this always gives same prediction answer?
324
+ *
325
+ * Without predicates, loop entry and exit paths are ambiguous
326
+ * upon remaining input +b (in, say, a+b). Either paths lead to
327
+ * valid parses. Closure can lead to consuming + immediately or by
328
+ * falling out of this call to expr back into expr and loop back
329
+ * again to StarLoopEntryState to match +b. In this special case,
330
+ * we choose the more efficient path, which is to take the bypass
331
+ * path.
332
+ *
333
+ * The lookahead language has not changed because closure chooses
334
+ * one path over the other. Both paths lead to consuming the same
335
+ * remaining input during a lookahead operation. If the next token
336
+ * is an operator, lookahead will enter the choice block with
337
+ * operators. If it is not, lookahead will exit expr. Same as if
338
+ * closure had chosen to enter the choice block immediately.
339
+ *
340
+ * Closure is examining one config (some loopentrystate, some alt,
341
+ * context) which means it is considering exactly one alt. Closure
342
+ * always copies the same alt to any derived configs.
343
+ *
344
+ * How do we know this optimization doesn't mess up precedence in
345
+ * our parse trees?
346
+ *
347
+ * Looking through expr from left edge of stat only has to confirm
348
+ * that an input, say, a+b+c; begins with any valid interpretation
349
+ * of an expression. The precedence actually doesn't matter when
350
+ * making a decision in stat seeing through expr. It is only when
351
+ * parsing rule expr that we must use the precedence to get the
352
+ * right interpretation and, hence, parse tree.
353
+ */
354
+ bool canDropLoopEntryEdgeInLeftRecursiveRule(ATNConfig *config) const;
355
+ virtual std::string getRuleName(size_t index);
356
+
357
+ virtual Ref<ATNConfig> precedenceTransition(Ref<ATNConfig> const& config, const PrecedencePredicateTransition *pt,
358
+ bool collectPredicates, bool inContext, bool fullCtx);
359
+
360
+ void setPredictionMode(PredictionMode newMode);
361
+ PredictionMode getPredictionMode();
362
+
363
+ Parser* getParser();
364
+
365
+ virtual std::string getTokenName(size_t t);
366
+
367
+ virtual std::string getLookaheadName(TokenStream *input);
368
+
369
+ /// <summary>
370
+ /// Used for debugging in adaptivePredict around execATN but I cut
371
+ /// it out for clarity now that alg. works well. We can leave this
372
+ /// "dead" code for a bit.
373
+ /// </summary>
374
+ virtual void dumpDeadEndConfigs(NoViableAltException &nvae);
375
+
376
+ protected:
377
+ Parser *const parser;
378
+
379
+ /// <summary>
380
+ /// Each prediction operation uses a cache for merge of prediction contexts.
381
+ /// Don't keep around as it wastes huge amounts of memory. The merge cache
382
+ /// isn't synchronized but we're ok since two threads shouldn't reuse same
383
+ /// parser/atnsim object because it can only handle one input at a time.
384
+ /// This maps graphs a and b to merged result c. (a,b)->c. We can avoid
385
+ /// the merge if we ever see a and b again. Note that (b,a)->c should
386
+ /// also be examined during cache lookup.
387
+ /// </summary>
388
+ PredictionContextMergeCache mergeCache;
389
+ size_t _mergeCacheCounter = 0;
390
+
391
+ // LAME globals to avoid parameters!!!!! I need these down deep in predTransition
392
+ TokenStream *_input;
393
+ size_t _startIndex;
394
+ ParserRuleContext *_outerContext;
395
+ dfa::DFA *_dfa; // Reference into the decisionToDFA vector.
396
+
397
+ /// <summary>
398
+ /// Performs ATN simulation to compute a predicted alternative based
399
+ /// upon the remaining input, but also updates the DFA cache to avoid
400
+ /// having to traverse the ATN again for the same input sequence.
401
+ ///
402
+ /// There are some key conditions we're looking for after computing a new
403
+ /// set of ATN configs (proposed DFA state):
404
+ /// if the set is empty, there is no viable alternative for current symbol
405
+ /// does the state uniquely predict an alternative?
406
+ /// does the state have a conflict that would prevent us from
407
+ /// putting it on the work list?
408
+ ///
409
+ /// We also have some key operations to do:
410
+ /// add an edge from previous DFA state to potentially new DFA state, D,
411
+ /// upon current symbol but only if adding to work list, which means in all
412
+ /// cases except no viable alternative (and possibly non-greedy decisions?)
413
+ /// collecting predicates and adding semantic context to DFA accept states
414
+ /// adding rule context to context-sensitive DFA accept states
415
+ /// consuming an input symbol
416
+ /// reporting a conflict
417
+ /// reporting an ambiguity
418
+ /// reporting a context sensitivity
419
+ /// reporting insufficient predicates
420
+ ///
421
+ /// cover these cases:
422
+ /// dead end
423
+ /// single alt
424
+ /// single alt + preds
425
+ /// conflict
426
+ /// conflict + preds
427
+ /// </summary>
428
+ virtual size_t execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex,
429
+ ParserRuleContext *outerContext);
430
+
431
+ /// <summary>
432
+ /// Get an existing target state for an edge in the DFA. If the target state
433
+ /// for the edge has not yet been computed or is otherwise not available,
434
+ /// this method returns {@code null}.
435
+ /// </summary>
436
+ /// <param name="previousD"> The current DFA state </param>
437
+ /// <param name="t"> The next input symbol </param>
438
+ /// <returns> The existing target DFA state for the given input symbol
439
+ /// {@code t}, or {@code null} if the target state for this edge is not
440
+ /// already cached </returns>
441
+ virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, size_t t);
442
+
443
+ /// <summary>
444
+ /// Compute a target state for an edge in the DFA, and attempt to add the
445
+ /// computed state and corresponding edge to the DFA.
446
+ /// </summary>
447
+ /// <param name="dfa"> The DFA </param>
448
+ /// <param name="previousD"> The current DFA state </param>
449
+ /// <param name="t"> The next input symbol
450
+ /// </param>
451
+ /// <returns> The computed target DFA state for the given input symbol
452
+ /// {@code t}. If {@code t} does not lead to a valid DFA state, this method
453
+ /// returns <seealso cref="#ERROR"/>. </returns>
454
+ virtual dfa::DFAState *computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, size_t t);
455
+
456
+ virtual void predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState);
457
+
458
+ // comes back with reach.uniqueAlt set to a valid alt
459
+ virtual size_t execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, ATNConfigSet *s0,
460
+ TokenStream *input, size_t startIndex, ParserRuleContext *outerContext); // how far we got before failing over
461
+
462
+ virtual std::unique_ptr<ATNConfigSet> computeReachSet(ATNConfigSet *closure, size_t t, bool fullCtx);
463
+
464
+ /// <summary>
465
+ /// Return a configuration set containing only the configurations from
466
+ /// {@code configs} which are in a <seealso cref="RuleStopState"/>. If all
467
+ /// configurations in {@code configs} are already in a rule stop state, this
468
+ /// method simply returns {@code configs}.
469
+ /// <p/>
470
+ /// When {@code lookToEndOfRule} is true, this method uses
471
+ /// <seealso cref="ATN#nextTokens"/> for each configuration in {@code configs} which is
472
+ /// not already in a rule stop state to see if a rule stop state is reachable
473
+ /// from the configuration via epsilon-only transitions.
474
+ /// </summary>
475
+ /// <param name="configs"> the configuration set to update </param>
476
+ /// <param name="lookToEndOfRule"> when true, this method checks for rule stop states
477
+ /// reachable by epsilon-only transitions from each configuration in
478
+ /// {@code configs}.
479
+ /// </param>
480
+ /// <returns> {@code configs} if all configurations in {@code configs} are in a
481
+ /// rule stop state, otherwise return a new configuration set containing only
482
+ /// the configurations from {@code configs} which are in a rule stop state </returns>
483
+ virtual ATNConfigSet* removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule);
484
+
485
+ virtual std::unique_ptr<ATNConfigSet> computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx);
486
+
487
+ /* parrt internal source braindump that doesn't mess up
488
+ * external API spec.
489
+
490
+ applyPrecedenceFilter is an optimization to avoid highly
491
+ nonlinear prediction of expressions and other left recursive
492
+ rules. The precedence predicates such as {3>=prec}? Are highly
493
+ context-sensitive in that they can only be properly evaluated
494
+ in the context of the proper prec argument. Without pruning,
495
+ these predicates are normal predicates evaluated when we reach
496
+ conflict state (or unique prediction). As we cannot evaluate
497
+ these predicates out of context, the resulting conflict leads
498
+ to full LL evaluation and nonlinear prediction which shows up
499
+ very clearly with fairly large expressions.
500
+
501
+ Example grammar:
502
+
503
+ e : e '*' e
504
+ | e '+' e
505
+ | INT
506
+ ;
507
+
508
+ We convert that to the following:
509
+
510
+ e[int prec]
511
+ : INT
512
+ ( {3>=prec}? '*' e[4]
513
+ | {2>=prec}? '+' e[3]
514
+ )*
515
+ ;
516
+
517
+ The (..)* loop has a decision for the inner block as well as
518
+ an enter or exit decision, which is what concerns us here. At
519
+ the 1st + of input 1+2+3, the loop entry sees both predicates
520
+ and the loop exit also sees both predicates by falling off the
521
+ edge of e. This is because we have no stack information with
522
+ SLL and find the follow of e, which will hit the return states
523
+ inside the loop after e[4] and e[3], which brings it back to
524
+ the enter or exit decision. In this case, we know that we
525
+ cannot evaluate those predicates because we have fallen off
526
+ the edge of the stack and will in general not know which prec
527
+ parameter is the right one to use in the predicate.
528
+
529
+ Because we have special information, that these are precedence
530
+ predicates, we can resolve them without failing over to full
531
+ LL despite their context sensitive nature. We make an
532
+ assumption that prec[-1] <= prec[0], meaning that the current
533
+ precedence level is greater than or equal to the precedence
534
+ level of recursive invocations above us in the stack. For
535
+ example, if predicate {3>=prec}? is true of the current prec,
536
+ then one option is to enter the loop to match it now. The
537
+ other option is to exit the loop and the left recursive rule
538
+ to match the current operator in rule invocation further up
539
+ the stack. But, we know that all of those prec are lower or
540
+ the same value and so we can decide to enter the loop instead
541
+ of matching it later. That means we can strip out the other
542
+ configuration for the exit branch.
543
+
544
+ So imagine we have (14,1,$,{2>=prec}?) and then
545
+ (14,2,$-dipsIntoOuterContext,{2>=prec}?). The optimization
546
+ allows us to collapse these two configurations. We know that
547
+ if {2>=prec}? is true for the current prec parameter, it will
548
+ also be true for any prec from an invoking e call, indicated
549
+ by dipsIntoOuterContext. As the predicates are both true, we
550
+ have the option to evaluate them early in the decision start
551
+ state. We do this by stripping both predicates and choosing to
552
+ enter the loop as it is consistent with the notion of operator
553
+ precedence. It's also how the full LL conflict resolution
554
+ would work.
555
+
556
+ The solution requires a different DFA start state for each
557
+ precedence level.
558
+
559
+ The basic filter mechanism is to remove configurations of the
560
+ form (p, 2, pi) if (p, 1, pi) exists for the same p and pi. In
561
+ other words, for the same ATN state and predicate context,
562
+ remove any configuration associated with an exit branch if
563
+ there is a configuration associated with the enter branch.
564
+
565
+ It's also the case that the filter evaluates precedence
566
+ predicates and resolves conflicts according to precedence
567
+ levels. For example, for input 1+2+3 at the first +, we see
568
+ prediction filtering
569
+
570
+ [(11,1,[$],{3>=prec}?), (14,1,[$],{2>=prec}?), (5,2,[$],up=1),
571
+ (11,2,[$],up=1), (14,2,[$],up=1)],hasSemanticContext=true,dipsIntoOuterContext
572
+
573
+ to
574
+
575
+ [(11,1,[$]), (14,1,[$]), (5,2,[$],up=1)],dipsIntoOuterContext
576
+
577
+ This filters because {3>=prec}? evals to true and collapses
578
+ (11,1,[$],{3>=prec}?) and (11,2,[$],up=1) since early conflict
579
+ resolution based upon rules of operator precedence fits with
580
+ our usual match first alt upon conflict.
581
+
582
+ We noticed a problem where a recursive call resets precedence
583
+ to 0. Sam's fix: each config has flag indicating if it has
584
+ returned from an expr[0] call. then just don't filter any
585
+ config with that flag set. flag is carried along in
586
+ closure(). so to avoid adding field, set bit just under sign
587
+ bit of dipsIntoOuterContext (SUPPRESS_PRECEDENCE_FILTER).
588
+ With the change you filter "unless (p, 2, pi) was reached
589
+ after leaving the rule stop state of the LR rule containing
590
+ state p, corresponding to a rule invocation with precedence
591
+ level 0"
592
+ */
593
+
594
+ /**
595
+ * This method transforms the start state computed by
596
+ * {@link #computeStartState} to the special start state used by a
597
+ * precedence DFA for a particular precedence value. The transformation
598
+ * process applies the following changes to the start state's configuration
599
+ * set.
600
+ *
601
+ * <ol>
602
+ * <li>Evaluate the precedence predicates for each configuration using
603
+ * {@link SemanticContext#evalPrecedence}.</li>
604
+ * <li>When {@link ATNConfig#isPrecedenceFilterSuppressed} is {@code false},
605
+ * remove all configurations which predict an alternative greater than 1,
606
+ * for which another configuration that predicts alternative 1 is in the
607
+ * same ATN state with the same prediction context. This transformation is
608
+ * valid for the following reasons:
609
+ * <ul>
610
+ * <li>The closure block cannot contain any epsilon transitions which bypass
611
+ * the body of the closure, so all states reachable via alternative 1 are
612
+ * part of the precedence alternatives of the transformed left-recursive
613
+ * rule.</li>
614
+ * <li>The "primary" portion of a left recursive rule cannot contain an
615
+ * epsilon transition, so the only way an alternative other than 1 can exist
616
+ * in a state that is also reachable via alternative 1 is by nesting calls
617
+ * to the left-recursive rule, with the outer calls not being at the
618
+ * preferred precedence level. The
619
+ * {@link ATNConfig#isPrecedenceFilterSuppressed} property marks ATN
620
+ * configurations which do not meet this condition, and therefore are not
621
+ * eligible for elimination during the filtering process.</li>
622
+ * </ul>
623
+ * </li>
624
+ * </ol>
625
+ *
626
+ * <p>
627
+ * The prediction context must be considered by this filter to address
628
+ * situations like the following.
629
+ * </p>
630
+ * <code>
631
+ * <pre>
632
+ * grammar TA;
633
+ * prog: statement* EOF;
634
+ * statement: letterA | statement letterA 'b' ;
635
+ * letterA: 'a';
636
+ * </pre>
637
+ * </code>
638
+ * <p>
639
+ * If the above grammar, the ATN state immediately before the token
640
+ * reference {@code 'a'} in {@code letterA} is reachable from the left edge
641
+ * of both the primary and closure blocks of the left-recursive rule
642
+ * {@code statement}. The prediction context associated with each of these
643
+ * configurations distinguishes between them, and prevents the alternative
644
+ * which stepped out to {@code prog} (and then back in to {@code statement}
645
+ * from being eliminated by the filter.
646
+ * </p>
647
+ *
648
+ * @param configs The configuration set computed by
649
+ * {@link #computeStartState} as the start state for the DFA.
650
+ * @return The transformed configuration set representing the start state
651
+ * for a precedence DFA at a particular precedence level (determined by
652
+ * calling {@link Parser#getPrecedence}).
653
+ */
654
+ std::unique_ptr<ATNConfigSet> applyPrecedenceFilter(ATNConfigSet *configs);
655
+
656
+ virtual ATNState *getReachableTarget(const Transition *trans, size_t ttype);
657
+
658
+ virtual std::vector<Ref<const SemanticContext>> getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts,
659
+ ATNConfigSet *configs, size_t nalts);
660
+
661
+ std::vector<dfa::DFAState::PredPrediction> getPredicatePredictions(const antlrcpp::BitSet &ambigAlts,
662
+ const std::vector<Ref<const SemanticContext>> &altToPred);
663
+
664
+ /**
665
+ * This method is used to improve the localization of error messages by
666
+ * choosing an alternative rather than throwing a
667
+ * {@link NoViableAltException} in particular prediction scenarios where the
668
+ * {@link #ERROR} state was reached during ATN simulation.
669
+ *
670
+ * <p>
671
+ * The default implementation of this method uses the following
672
+ * algorithm to identify an ATN configuration which successfully parsed the
673
+ * decision entry rule. Choosing such an alternative ensures that the
674
+ * {@link ParserRuleContext} returned by the calling rule will be complete
675
+ * and valid, and the syntax error will be reported later at a more
676
+ * localized location.</p>
677
+ *
678
+ * <ul>
679
+ * <li>If a syntactically valid path or paths reach the end of the decision rule and
680
+ * they are semantically valid if predicated, return the min associated alt.</li>
681
+ * <li>Else, if a semantically invalid but syntactically valid path exist
682
+ * or paths exist, return the minimum associated alt.
683
+ * </li>
684
+ * <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
685
+ * </ul>
686
+ *
687
+ * <p>
688
+ * In some scenarios, the algorithm described above could predict an
689
+ * alternative which will result in a {@link FailedPredicateException} in
690
+ * the parser. Specifically, this could occur if the <em>only</em> configuration
691
+ * capable of successfully parsing to the end of the decision rule is
692
+ * blocked by a semantic predicate. By choosing this alternative within
693
+ * {@link #adaptivePredict} instead of throwing a
694
+ * {@link NoViableAltException}, the resulting
695
+ * {@link FailedPredicateException} in the parser will identify the specific
696
+ * predicate which is preventing the parser from successfully parsing the
697
+ * decision rule, which helps developers identify and correct logic errors
698
+ * in semantic predicates.
699
+ * </p>
700
+ *
701
+ * @param configs The ATN configurations which were valid immediately before
702
+ * the {@link #ERROR} state was reached
703
+ * @param outerContext The is the \gamma_0 initial parser context from the paper
704
+ * or the parser stack at the instant before prediction commences.
705
+ *
706
+ * @return The value to return from {@link #adaptivePredict}, or
707
+ * {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
708
+ * identified and {@link #adaptivePredict} should report an error instead.
709
+ */
710
+ size_t getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs,
711
+ ParserRuleContext *outerContext);
712
+
713
+ virtual size_t getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs);
714
+
715
+ /** Walk the list of configurations and split them according to
716
+ * those that have preds evaluating to true/false. If no pred, assume
717
+ * true pred and include in succeeded set. Returns Pair of sets.
718
+ *
719
+ * Create a new set so as not to alter the incoming parameter.
720
+ *
721
+ * Assumption: the input stream has been restored to the starting point
722
+ * prediction, which is where predicates need to evaluate.
723
+ */
724
+ std::pair<ATNConfigSet *, ATNConfigSet *> splitAccordingToSemanticValidity(ATNConfigSet *configs,
725
+ ParserRuleContext *outerContext);
726
+
727
+ /// <summary>
728
+ /// Look through a list of predicate/alt pairs, returning alts for the
729
+ /// pairs that win. A {@code NONE} predicate indicates an alt containing an
730
+ /// unpredicated config which behaves as "always true." If !complete
731
+ /// then we stop at the first predicate that evaluates to true. This
732
+ /// includes pairs with null predicates.
733
+ /// </summary>
734
+ antlrcpp::BitSet evalSemanticContext(const std::vector<dfa::DFAState::PredPrediction> &predPredictions,
735
+ ParserRuleContext *outerContext, bool complete);
736
+
737
+ /**
738
+ * Evaluate a semantic context within a specific parser context.
739
+ *
740
+ * <p>
741
+ * This method might not be called for every semantic context evaluated
742
+ * during the prediction process. In particular, we currently do not
743
+ * evaluate the following but it may change in the future:</p>
744
+ *
745
+ * <ul>
746
+ * <li>Precedence predicates (represented by
747
+ * {@link SemanticContext.PrecedencePredicate}) are not currently evaluated
748
+ * through this method.</li>
749
+ * <li>Operator predicates (represented by {@link SemanticContext.AND} and
750
+ * {@link SemanticContext.OR}) are evaluated as a single semantic
751
+ * context, rather than evaluating the operands individually.
752
+ * Implementations which require evaluation results from individual
753
+ * predicates should override this method to explicitly handle evaluation of
754
+ * the operands within operator predicates.</li>
755
+ * </ul>
756
+ *
757
+ * @param pred The semantic context to evaluate
758
+ * @param parserCallStack The parser context in which to evaluate the
759
+ * semantic context
760
+ * @param alt The alternative which is guarded by {@code pred}
761
+ * @param fullCtx {@code true} if the evaluation is occurring during LL
762
+ * prediction; otherwise, {@code false} if the evaluation is occurring
763
+ * during SLL prediction
764
+ *
765
+ * @since 4.3
766
+ */
767
+ virtual bool evalSemanticContext(Ref<const SemanticContext> const& pred, ParserRuleContext *parserCallStack,
768
+ size_t alt, bool fullCtx);
769
+
770
+ /* TODO: If we are doing predicates, there is no point in pursuing
771
+ closure operations if we reach a DFA state that uniquely predicts
772
+ alternative. We will not be caching that DFA state and it is a
773
+ waste to pursue the closure. Might have to advance when we do
774
+ ambig detection thought :(
775
+ */
776
+ virtual void closure(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
777
+ bool collectPredicates, bool fullCtx, bool treatEofAsEpsilon);
778
+
779
+ virtual void closureCheckingStopState(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
780
+ bool collectPredicates, bool fullCtx, int depth, bool treatEofAsEpsilon);
781
+
782
+ /// Do the actual work of walking epsilon edges.
783
+ virtual void closure_(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
784
+ bool collectPredicates, bool fullCtx, int depth, bool treatEofAsEpsilon);
785
+
786
+ virtual Ref<ATNConfig> getEpsilonTarget(Ref<ATNConfig> const& config, const Transition *t, bool collectPredicates,
787
+ bool inContext, bool fullCtx, bool treatEofAsEpsilon);
788
+ virtual Ref<ATNConfig> actionTransition(Ref<ATNConfig> const& config, const ActionTransition *t);
789
+
790
+ virtual Ref<ATNConfig> predTransition(Ref<ATNConfig> const& config, const PredicateTransition *pt, bool collectPredicates,
791
+ bool inContext, bool fullCtx);
792
+
793
+ virtual Ref<ATNConfig> ruleTransition(Ref<ATNConfig> const& config, const RuleTransition *t);
794
+
795
+ /**
796
+ * Gets a {@link BitSet} containing the alternatives in {@code configs}
797
+ * which are part of one or more conflicting alternative subsets.
798
+ *
799
+ * @param configs The {@link ATNConfigSet} to analyze.
800
+ * @return The alternatives in {@code configs} which are part of one or more
801
+ * conflicting alternative subsets. If {@code configs} does not contain any
802
+ * conflicting subsets, this method returns an empty {@link BitSet}.
803
+ */
804
+ virtual antlrcpp::BitSet getConflictingAlts(ATNConfigSet *configs);
805
+
806
+ /// <summary>
807
+ /// Sam pointed out a problem with the previous definition, v3, of
808
+ /// ambiguous states. If we have another state associated with conflicting
809
+ /// alternatives, we should keep going. For example, the following grammar
810
+ ///
811
+ /// s : (ID | ID ID?) ';' ;
812
+ ///
813
+ /// When the ATN simulation reaches the state before ';', it has a DFA
814
+ /// state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
815
+ /// 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
816
+ /// because alternative to has another way to continue, via [6|2|[]].
817
+ /// The key is that we have a single state that has config's only associated
818
+ /// with a single alternative, 2, and crucially the state transitions
819
+ /// among the configurations are all non-epsilon transitions. That means
820
+ /// we don't consider any conflicts that include alternative 2. So, we
821
+ /// ignore the conflict between alts 1 and 2. We ignore a set of
822
+ /// conflicting alts when there is an intersection with an alternative
823
+ /// associated with a single alt state in the state->config-list map.
824
+ ///
825
+ /// It's also the case that we might have two conflicting configurations but
826
+ /// also a 3rd nonconflicting configuration for a different alternative:
827
+ /// [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
828
+ ///
829
+ /// a : A | A | A B ;
830
+ ///
831
+ /// After matching input A, we reach the stop state for rule A, state 1.
832
+ /// State 8 is the state right before B. Clearly alternatives 1 and 2
833
+ /// conflict and no amount of further lookahead will separate the two.
834
+ /// However, alternative 3 will be able to continue and so we do not
835
+ /// stop working on this state. In the previous example, we're concerned
836
+ /// with states associated with the conflicting alternatives. Here alt
837
+ /// 3 is not associated with the conflicting configs, but since we can continue
838
+ /// looking for input reasonably, I don't declare the state done. We
839
+ /// ignore a set of conflicting alts when we have an alternative
840
+ /// that we still need to pursue.
841
+ /// </summary>
842
+
843
+ virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet *configs);
844
+
845
+ virtual NoViableAltException noViableAlt(TokenStream *input, ParserRuleContext *outerContext,
846
+ ATNConfigSet *configs, size_t startIndex, bool deleteConfigs);
847
+
848
+ static size_t getUniqueAlt(ATNConfigSet *configs);
849
+
850
+ /// <summary>
851
+ /// Add an edge to the DFA, if possible. This method calls
852
+ /// <seealso cref="#addDFAState"/> to ensure the {@code to} state is present in the
853
+ /// DFA. If {@code from} is {@code null}, or if {@code t} is outside the
854
+ /// range of edges that can be represented in the DFA tables, this method
855
+ /// returns without adding the edge to the DFA.
856
+ /// <p/>
857
+ /// If {@code to} is {@code null}, this method returns {@code null}.
858
+ /// Otherwise, this method returns the <seealso cref="DFAState"/> returned by calling
859
+ /// <seealso cref="#addDFAState"/> for the {@code to} state.
860
+ /// </summary>
861
+ /// <param name="dfa"> The DFA </param>
862
+ /// <param name="from"> The source state for the edge </param>
863
+ /// <param name="t"> The input symbol </param>
864
+ /// <param name="to"> The target state for the edge
865
+ /// </param>
866
+ /// <returns> If {@code to} is {@code null}, this method returns {@code null};
867
+ /// otherwise this method returns the result of calling <seealso cref="#addDFAState"/>
868
+ /// on {@code to} </returns>
869
+ virtual dfa::DFAState *addDFAEdge(dfa::DFA &dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to);
870
+
871
+ /// <summary>
872
+ /// Add state {@code D} to the DFA if it is not already present, and return
873
+ /// the actual instance stored in the DFA. If a state equivalent to {@code D}
874
+ /// is already in the DFA, the existing state is returned. Otherwise this
875
+ /// method returns {@code D} after adding it to the DFA.
876
+ /// <p/>
877
+ /// If {@code D} is <seealso cref="#ERROR"/>, this method returns <seealso cref="#ERROR"/> and
878
+ /// does not change the DFA.
879
+ /// </summary>
880
+ /// <param name="dfa"> The dfa </param>
881
+ /// <param name="D"> The DFA state to add </param>
882
+ /// <returns> The state stored in the DFA. This will be either the existing
883
+ /// state if {@code D} is already in the DFA, or {@code D} itself if the
884
+ /// state was not already present. </returns>
885
+ virtual dfa::DFAState *addDFAState(dfa::DFA &dfa, dfa::DFAState *D);
886
+
887
+ virtual void reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts,
888
+ ATNConfigSet *configs, size_t startIndex, size_t stopIndex);
889
+
890
+ virtual void reportContextSensitivity(dfa::DFA &dfa, size_t prediction, ATNConfigSet *configs,
891
+ size_t startIndex, size_t stopIndex);
892
+
893
+ /// If context sensitive parsing, we know it's ambiguity not conflict.
894
+ virtual void reportAmbiguity(dfa::DFA &dfa,
895
+ dfa::DFAState *D, // the DFA state from execATN() that had SLL conflicts
896
+ size_t startIndex, size_t stopIndex,
897
+ bool exact,
898
+ const antlrcpp::BitSet &ambigAlts,
899
+ ATNConfigSet *configs); // configs that LL not SLL considered conflicting
900
+
901
+ private:
902
+ // SLL, LL, or LL + exact ambig detection?
903
+ PredictionMode _mode;
904
+
905
+ static bool getLrLoopSetting();
906
+ void InitializeInstanceFields();
907
+ };
908
+
909
+ } // namespace atn
910
+ } // namespace antlr4
911
+