antlr3cyclicdfa.inl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. namespace antlr3 {
  2. template<class ImplTraits, class CtxType>
  3. CyclicDFA<ImplTraits, CtxType>::CyclicDFA( ANTLR_INT32 decisionNumber
  4. , const ANTLR_UCHAR* description
  5. , const ANTLR_INT32* const eot
  6. , const ANTLR_INT32* const eof
  7. , const ANTLR_INT32* const min
  8. , const ANTLR_INT32* const max
  9. , const ANTLR_INT32* const accept
  10. , const ANTLR_INT32* const special
  11. , const ANTLR_INT32* const *const transition )
  12. :m_decisionNumber(decisionNumber)
  13. , m_eot(eot)
  14. , m_eof(eof)
  15. , m_min(min)
  16. , m_max(max)
  17. , m_accept(accept)
  18. , m_special(special)
  19. , m_transition(transition)
  20. {
  21. m_description = description;
  22. }
  23. template<class ImplTraits, class CtxType>
  24. CyclicDFA<ImplTraits, CtxType>::CyclicDFA( const CyclicDFA& dfa )
  25. {
  26. m_decisionNumber = dfa.m_decisionNumber;
  27. m_description = dfa.m_description;
  28. m_eot = dfa.m_eot;
  29. m_eof = dfa.m_eof;
  30. m_min = dfa.m_min;
  31. m_max = dfa.m_max;
  32. m_accept = dfa.m_accept;
  33. m_special = dfa.m_special;
  34. m_transition = dfa.m_transition;
  35. }
  36. template<class ImplTraits, class CtxType>
  37. CyclicDFA<ImplTraits, CtxType>& CyclicDFA<ImplTraits, CtxType>::operator=( const CyclicDFA& dfa)
  38. {
  39. m_decisionNumber = dfa.m_decisionNumber;
  40. m_description = dfa.m_description;
  41. m_eot = dfa.m_eot;
  42. m_eof = dfa.m_eof;
  43. m_min = dfa.m_min;
  44. m_max = dfa.m_max;
  45. m_accept = dfa.m_accept;
  46. m_special = dfa.m_special;
  47. m_transition = dfa.m_transition;
  48. return *this;
  49. }
  50. template<class ImplTraits, class CtxType>
  51. ANTLR_INT32 CyclicDFA<ImplTraits, CtxType>::specialStateTransition(CtxType * ,
  52. RecognizerType* ,
  53. IntStreamType* , ANTLR_INT32 )
  54. {
  55. return -1;
  56. }
  57. template<class ImplTraits, class CtxType>
  58. ANTLR_INT32 CyclicDFA<ImplTraits, CtxType>::specialTransition(CtxType * /*ctx*/,
  59. RecognizerType* /*recognizer*/,
  60. IntStreamType* /*is*/, ANTLR_INT32 /*s*/)
  61. {
  62. return 0;
  63. }
  64. template<class ImplTraits, class CtxType>
  65. template<typename SuperType>
  66. ANTLR_INT32 CyclicDFA<ImplTraits, CtxType>::predict(CtxType * ctx,
  67. RecognizerType* recognizer,
  68. IntStreamType* is, SuperType& super)
  69. {
  70. ANTLR_MARKER mark;
  71. ANTLR_INT32 s;
  72. ANTLR_INT32 specialState;
  73. ANTLR_INT32 c;
  74. mark = is->mark(); /* Store where we are right now */
  75. s = 0; /* Always start with state 0 */
  76. for (;;)
  77. {
  78. /* Pick out any special state entry for this state
  79. */
  80. specialState = m_special[s];
  81. /* Transition the special state and consume an input token
  82. */
  83. if (specialState >= 0)
  84. {
  85. s = super.specialStateTransition(ctx, recognizer, is, specialState);
  86. // Error?
  87. //
  88. if (s<0)
  89. {
  90. // If the predicate/rule raised an exception then we leave it
  91. // in tact, else we have an NVA.
  92. //
  93. if (recognizer->get_state()->get_error() != true)
  94. {
  95. this->noViableAlt(recognizer, s);
  96. }
  97. is->rewind(mark);
  98. return 0;
  99. }
  100. is->consume();
  101. continue;
  102. }
  103. /* Accept state?
  104. */
  105. if (m_accept[s] >= 1)
  106. {
  107. is->rewind(mark);
  108. return m_accept[s];
  109. }
  110. /* Look for a normal transition state based upon the input token element
  111. */
  112. c = is->LA(1);
  113. /* Check against min and max for this state
  114. */
  115. if (c>= m_min[s] && c <= m_max[s])
  116. {
  117. ANTLR_INT32 snext;
  118. /* What is the next state?
  119. */
  120. snext = m_transition[s][c - m_min[s]];
  121. if (snext < 0)
  122. {
  123. /* Was in range but not a normal transition
  124. * must check EOT, which is like the else clause.
  125. * eot[s]>=0 indicates that an EOT edge goes to another
  126. * state.
  127. */
  128. if ( m_eot[s] >= 0)
  129. {
  130. s = m_eot[s];
  131. is->consume();
  132. continue;
  133. }
  134. this->noViableAlt(recognizer, s);
  135. is->rewind(mark);
  136. return 0;
  137. }
  138. /* New current state - move to it
  139. */
  140. s = snext;
  141. is->consume();
  142. continue;
  143. }
  144. /* EOT Transition?
  145. */
  146. if ( m_eot[s] >= 0)
  147. {
  148. s = m_eot[s];
  149. is->consume();
  150. continue;
  151. }
  152. /* EOF transition to accept state?
  153. */
  154. if ( c == ImplTraits::CommonTokenType::TOKEN_EOF && m_eof[s] >= 0)
  155. {
  156. is->rewind(mark);
  157. return m_accept[m_eof[s]];
  158. }
  159. /* No alt, so bomb
  160. */
  161. this->noViableAlt(recognizer, s);
  162. is->rewind(mark);
  163. return 0;
  164. }
  165. }
  166. template<class ImplTraits, class CtxType>
  167. void CyclicDFA<ImplTraits, CtxType>::noViableAlt(RecognizerType* rec, ANTLR_UINT32 s)
  168. {
  169. // In backtracking mode, we just set the failed flag so that the
  170. // alt can just exit right now. If we are parsing though, then
  171. // we want the exception to be raised.
  172. //
  173. if (rec->get_state()->get_backtracking() > 0)
  174. {
  175. rec->get_state()->set_failed(true);
  176. }
  177. else
  178. {
  179. ANTLR_Exception<ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>* ex
  180. = new ANTLR_Exception<ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>( rec, (const char*)m_description );
  181. ex->set_decisionNum( m_decisionNumber );
  182. ex->set_state(s);
  183. }
  184. }
  185. }