示例#1
0
        /// <summary>
        /// Rely on the error handler for this parser but, if no tokens are consumed
        /// to recover, add an error node.
        /// </summary>
        /// <remarks>
        /// Rely on the error handler for this parser but, if no tokens are consumed
        /// to recover, add an error node. Otherwise, nothing is seen in the parse
        /// tree.
        /// </remarks>
        protected internal virtual void Recover(RecognitionException e)
        {
            int i = _input.Index;

            ErrorHandler.Recover(this, e);
            if (_input.Index == i)
            {
                // no input consumed, better add an error node
                if (e is InputMismatchException)
                {
                    InputMismatchException ime = (InputMismatchException)e;
                    IToken tok = e.OffendingToken;
                    int    expectedTokenType = ime.GetExpectedTokens().MinElement;
                    // get any element
                    IToken errToken = TokenFactory.Create(Tuple.Create(tok.TokenSource, tok.TokenSource.InputStream), expectedTokenType, tok.Text, TokenConstants.DefaultChannel, -1, -1, tok.Line, tok.Column);
                    // invalid start/stop
                    _ctx.AddErrorNode(errToken);
                }
                else
                {
                    // NoViableAlt
                    IToken tok      = e.OffendingToken;
                    IToken errToken = TokenFactory.Create(Tuple.Create(tok.TokenSource, tok.TokenSource.InputStream), TokenConstants.InvalidType, tok.Text, TokenConstants.DefaultChannel, -1, -1, tok.Line, tok.Column);
                    // invalid start/stop
                    _ctx.AddErrorNode(errToken);
                }
            }
        }
        /// <summary>
        /// This is called by
        /// <see cref="ReportError(Parser, RecognitionException)">ReportError(Parser, RecognitionException)
        ///     </see>
        /// when the exception is an
        /// <see cref="InputMismatchException">InputMismatchException</see>
        /// .
        /// </summary>
        /// <seealso cref="ReportError(Parser, RecognitionException)">ReportError(Parser, RecognitionException)
        ///     </seealso>
        /// <param name="recognizer">the parser instance</param>
        /// <param name="e">the recognition exception</param>
        protected internal virtual void ReportInputMismatch(Parser recognizer, InputMismatchException
                                                            e)
        {
            string msg = "mismatched input " + GetTokenErrorDisplay(e.OffendingToken) + " expecting "
                         + e.GetExpectedTokens().ToString(recognizer.TokenNames);

            NotifyErrorListeners(recognizer, msg, e);
        }
示例#3
0
 /// <summary>
 /// This is called by
 /// <see cref="ReportError(Parser, RecognitionException)"/>
 /// when the exception is an
 /// <see cref="InputMismatchException"/>
 /// .
 /// </summary>
 /// <seealso cref="ReportError(Parser, RecognitionException)"/>
 /// <param name="recognizer">the parser instance</param>
 /// <param name="e">the recognition exception</param>
 protected internal virtual void ReportInputMismatch(Parser recognizer, InputMismatchException e)
 {
     string msg = "mismatched input " + GetTokenErrorDisplay(e.OffendingToken) + " expecting " + e.GetExpectedTokens().ToString(recognizer.Vocabulary);
     NotifyErrorListeners(recognizer, msg, e);
 }