Java 类org.antlr.runtime.UnwantedTokenException 实例源码

项目:goworks    文件:ANTLRErrorProvidingParser.java   
@Override
public String toString()
{
    if (trappedException instanceof MissingTokenException)
    {
        return "<missing type: " +
               ( (MissingTokenException)trappedException ).getMissingType() +
               ">";
    } else if (trappedException instanceof UnwantedTokenException) {
        return "<extraneous: " +
               ( (UnwantedTokenException)trappedException ).getUnexpectedToken() +
               ", resync=" + getText() + ">";
    } else if (trappedException instanceof MismatchedTokenException) {
        return "<mismatched token: " + trappedException.token + ", resync=" + getText() + ">";
    } else if (trappedException instanceof NoViableAltException) {
        return "<unexpected: " + trappedException.token +
               ", resync=" + getText() + ">";
    }
    return "<error: " + getText() + ">";
}
项目:asup    文件:LexerHelper.java   
/**
    * Method called in AntLR grammars for AntLR lexer to CLScript exceptions conversion
 * 
    * @param e
    * @return
    */
public RuntimeException createException(RecognitionException e) {
       String message = "";

       if (e instanceof NoViableAltException) {
           message = "Syntax error. ";
       } else if (e instanceof MissingTokenException) {
           message = "Missing token ";
       } else if (e instanceof UnwantedTokenException) {
           UnwantedTokenException ex = (UnwantedTokenException) e;
           ex.getUnexpectedToken().getText();
           message = "Unkow token '" + ex.getUnexpectedToken().getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine();        
       } else {
           message = "Syntax error near ";
       }

       return new CLScriptException(message,e);
   }
项目:asup    文件:ParserHelper.java   
/**
 * Method called in AntLR grammars for AntLR parser to CLScript exceptions conversion
 * 
 * @param e
 * @return
 */
public RuntimeException createException(RecognitionException e) {
       String message = "";
       boolean addTokenAndLine = true;
       if (e instanceof NoViableAltException) {
           message = "Syntax error. ";
       } else if (e instanceof MissingTokenException) {
           message = "Missing token ";
       } else if (e instanceof UnwantedTokenException) {
           UnwantedTokenException ex = (UnwantedTokenException) e;
           ex.getUnexpectedToken().getText();
           message = "Unkow token '" + ex.getUnexpectedToken().getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine();
           addTokenAndLine = false;
       } else if(e instanceof ParserHelperException){
        message = e.toString();         
       }
       else {
           message = "Syntax error near ";
       }
       if (addTokenAndLine) {
           message = message + "'" + e.token.getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine();
       }
       return new CLScriptException(message,e);
   }
项目:antlrworks2    文件:ANTLRErrorProvidingParser.java   
@Override
public String toString()
{
    if (trappedException instanceof MissingTokenException)
    {
        return "<missing type: " +
               ( (MissingTokenException)trappedException ).getMissingType() +
               ">";
    } else if (trappedException instanceof UnwantedTokenException) {
        return "<extraneous: " +
               ( (UnwantedTokenException)trappedException ).getUnexpectedToken() +
               ", resync=" + getText() + ">";
    } else if (trappedException instanceof MismatchedTokenException) {
        return "<mismatched token: " + trappedException.token + ", resync=" + getText() + ">";
    } else if (trappedException instanceof NoViableAltException) {
        return "<unexpected: " + trappedException.token +
               ", resync=" + getText() + ">";
    }
    return "<error: " + getText() + ">";
}
项目:legstar-cob2xsd    文件:RecognizerErrorHandler.java   
/**
 * Format an error message as expected by ANTLR. It is basically the
 * same error message that ANTL BaseRecognizer generates with some
 * additional data.
 * Also used to log debugging information.
 * @param log the logger to use at debug time
 * @param recognizer the lexer or parser who generated the error
 * @param e the exception that occured
 * @param superMessage the error message that the super class generated
 * @param tokenNames list of token names
 * @return a formatted error message
 */
public static String getErrorMessage(
        final Log log,
        final BaseRecognizer recognizer,
        final RecognitionException e,
        final String superMessage,
        final String[] tokenNames) {
    if (log.isDebugEnabled()) {
        List < ? > stack = BaseRecognizer.getRuleInvocationStack(
                e, recognizer.getClass().getSuperclass().getName());
        String debugMsg = recognizer.getErrorHeader(e)
            + " " + e.getClass().getSimpleName()
            + ": " + superMessage
            + ":";
        if (e instanceof NoViableAltException) {
            NoViableAltException nvae = (NoViableAltException) e;
            debugMsg += " (decision=" + nvae.decisionNumber
            + " state=" + nvae.stateNumber + ")"
            + " decision=<<" + nvae.grammarDecisionDescription + ">>";
        } else if (e instanceof UnwantedTokenException) {
            UnwantedTokenException ute = (UnwantedTokenException) e;
            debugMsg += " (unexpected token=" + toString(ute.getUnexpectedToken(), tokenNames) + ")";

        } else if (e instanceof EarlyExitException) {
            EarlyExitException eea = (EarlyExitException) e;
            debugMsg += " (decision=" + eea.decisionNumber + ")";
        }
        debugMsg += " ruleStack=" + stack.toString();
        log.debug(debugMsg);
    }

    return makeUserMsg(e, superMessage);
}
项目:legstar-cob2xsd    文件:RecognizerErrorHandler.java   
/**
 * Simplify error message text for end users.
 * @param e exception that occurred
 * @param msg as formatted by ANTLR
 * @return a more readable error message
 */
public static String makeUserMsg(final RecognitionException e, final String msg) {
    if (e instanceof NoViableAltException) {
        return msg.replace("no viable alternative at", "unrecognized");
    } else if (e instanceof UnwantedTokenException) {
        return msg.replace("extraneous input", "unexpected token");
    } else if (e instanceof MismatchedTokenException) {
        if (msg.contains("mismatched input '<EOF>'")) {
            return msg.replace("mismatched input '<EOF>' expecting", "reached end of file looking for");
        } else {
            return msg.replace("mismatched input", "unexpected token");
        }
    } else if (e instanceof EarlyExitException) {
        return msg.replace("required (...)+ loop did not match anything", "required tokens not found");
    } else if (e instanceof FailedPredicateException) {
        if (msg.contains("picture_string failed predicate: {Unbalanced parentheses}")) {
            return "Unbalanced parentheses in picture string";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Contains invalid picture symbols}")) {
            return "Picture string contains invalid symbols";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Syntax error in last picture clause}")) {
            return "Syntax error in last picture clause";
        }
        if (msg.contains("DATA_NAME failed predicate: {Syntax error in last clause}")) {
            return "Syntax error in last COBOL clause";
        }
    }
    return msg;
}
项目:legstar-core2    文件:RecognizerErrorHandler.java   
/**
 * Format an error message as expected by ANTLR. It is basically the
 * same error message that ANTL BaseRecognizer generates with some
 * additional data.
 * Also used to log debugging information.
 * @param log the logger to use at debug time
 * @param recognizer the lexer or parser who generated the error
 * @param e the exception that occured
 * @param superMessage the error message that the super class generated
 * @param tokenNames list of token names
 * @return a formatted error message
 */
public static String getErrorMessage(
        final Logger log,
        final BaseRecognizer recognizer,
        final RecognitionException e,
        final String superMessage,
        final String[] tokenNames) {
    if (log.isDebugEnabled()) {
        List < ? > stack = BaseRecognizer.getRuleInvocationStack(
                e, recognizer.getClass().getSuperclass().getName());
        String debugMsg = recognizer.getErrorHeader(e)
            + " " + e.getClass().getSimpleName()
            + ": " + superMessage
            + ":";
        if (e instanceof NoViableAltException) {
            NoViableAltException nvae = (NoViableAltException) e;
            debugMsg += " (decision=" + nvae.decisionNumber
            + " state=" + nvae.stateNumber + ")"
            + " decision=<<" + nvae.grammarDecisionDescription + ">>";
        } else if (e instanceof UnwantedTokenException) {
            UnwantedTokenException ute = (UnwantedTokenException) e;
            debugMsg += " (unexpected token=" + toString(ute.getUnexpectedToken(), tokenNames) + ")";

        } else if (e instanceof EarlyExitException) {
            EarlyExitException eea = (EarlyExitException) e;
            debugMsg += " (decision=" + eea.decisionNumber + ")";
        }
        debugMsg += " ruleStack=" + stack.toString();
        log.debug(debugMsg);
    }

    return makeUserMsg(e, superMessage);
}
项目:legstar-core2    文件:RecognizerErrorHandler.java   
/**
 * Simplify error message text for end users.
 * @param e exception that occurred
 * @param msg as formatted by ANTLR
 * @return a more readable error message
 */
public static String makeUserMsg(final RecognitionException e, final String msg) {
    if (e instanceof NoViableAltException) {
        return msg.replace("no viable alternative at", "unrecognized");
    } else if (e instanceof UnwantedTokenException) {
        return msg.replace("extraneous input", "unexpected token");
    } else if (e instanceof MismatchedTokenException) {
        if (msg.contains("mismatched input '<EOF>'")) {
            return msg.replace("mismatched input '<EOF>' expecting", "reached end of file looking for");
        } else {
            return msg.replace("mismatched input", "unexpected token");
        }
    } else if (e instanceof EarlyExitException) {
        return msg.replace("required (...)+ loop did not match anything", "required tokens not found");
    } else if (e instanceof FailedPredicateException) {
        if (msg.contains("picture_string failed predicate: {Unbalanced parentheses}")) {
            return "Unbalanced parentheses in picture string";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Contains invalid picture symbols}")) {
            return "Picture string contains invalid symbols";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Syntax error in last picture clause}")) {
            return "Syntax error in last picture clause";
        }
        if (msg.contains("DATA_NAME failed predicate: {Syntax error in last clause}")) {
            return "Syntax error in last COBOL clause";
        }
    }
    return msg;
}
项目:traci    文件:TraciParserTest.java   
@Test
public void testVector() throws RecognitionException
{
    runParser("[.5, 2.23, .17];");
    assertNoError();
    assertEquals(TraciParser.BLOCK, parseTree.getType());
    assertEquals(1, parseTree.getChildCount());
    Tree node = parseTree.getChild(0);
    assertEquals(TraciParser.VECTOR, node.getType());
    assertEquals(2, node.getChildCount());
    assertEquals(TraciParser.LBRACKET, node.getChild(0).getType());
    node = node.getChild(1);
    assertEquals(TraciParser.ARGS, node.getType());
    assertEquals(3, node.getChildCount());

    runParser("[.5, 2.23, .17;");
    assertError(MissingTokenException.class);

    runParser("[.5, 2.23 .17];");
    assertError(MissingTokenException.class);

    runParser("[.5, 2.23];");
    assertError(MismatchedTokenException.class);

    runParser("[.5, 2.23, .17, 5];");
    assertError(MismatchedTokenException.class);
    assertError(UnwantedTokenException.class);
}
项目:traci    文件:TraciParserTest.java   
@Test
public void testColor() throws RecognitionException
{
    runParser("color [.5, 2.23, .17];");
    assertNoError();
    assertEquals(TraciParser.BLOCK, parseTree.getType());
    assertEquals(1, parseTree.getChildCount());
    Tree node = parseTree.getChild(0);
    assertEquals(TraciParser.COLOR, node.getType());
    assertEquals(1, node.getChildCount());
    node = node.getChild(0);
    assertEquals(TraciParser.ARGS, node.getType());
    assertEquals(3, node.getChildCount());

    runParser("color [.5, 2.23, .17, .5];");
    assertNoError();
    assertEquals(TraciParser.BLOCK, parseTree.getType());
    assertEquals(1, parseTree.getChildCount());
    node = parseTree.getChild(0);
    assertEquals(TraciParser.COLOR, node.getType());
    assertEquals(1, node.getChildCount());
    node = node.getChild(0);
    assertEquals(TraciParser.ARGS, node.getType());
    assertEquals(4, node.getChildCount());

    runParser("color [.5, 2.23, .17;");
    assertError(MissingTokenException.class);

    runParser("color [.5, 2.23 .17];");
    assertError(MissingTokenException.class);

    runParser("color color [.5, 2.23, .17];");
    assertError(UnwantedTokenException.class);

    runParser("color [.5, 2.23];");
    assertError(MismatchedTokenException.class);

    runParser("color [.5, 2.23, .17, 5, 7];");
    assertError(MismatchedTokenException.class);
    assertError(UnwantedTokenException.class);
}