• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java UnwantedTokenException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.antlr.runtime.UnwantedTokenException的典型用法代码示例。如果您正苦于以下问题:Java UnwantedTokenException类的具体用法?Java UnwantedTokenException怎么用?Java UnwantedTokenException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



UnwantedTokenException类属于org.antlr.runtime包,在下文中一共展示了UnwantedTokenException类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: toString

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
@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() + ">";
}
 
开发者ID:tunnelvisionlabs,项目名称:goworks,代码行数:21,代码来源:ANTLRErrorProvidingParser.java


示例2: createException

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
/**
    * 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);
   }
 
开发者ID:asupdev,项目名称:asup,代码行数:24,代码来源:LexerHelper.java


示例3: createException

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
/**
 * 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);
   }
 
开发者ID:asupdev,项目名称:asup,代码行数:30,代码来源:ParserHelper.java


示例4: getErrorMessage

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:legsem,项目名称:legstar-cob2xsd,代码行数:45,代码来源:RecognizerErrorHandler.java


示例5: makeUserMsg

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:legsem,项目名称:legstar-cob2xsd,代码行数:36,代码来源:RecognizerErrorHandler.java


示例6: getErrorMessage

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:legsem,项目名称:legstar-core2,代码行数:45,代码来源:RecognizerErrorHandler.java


示例7: testVector

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
@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);
}
 
开发者ID:erikpe,项目名称:traci,代码行数:29,代码来源:TraciParserTest.java


示例8: testColor

import org.antlr.runtime.UnwantedTokenException; //导入依赖的package包/类
@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);
}
 
开发者ID:erikpe,项目名称:traci,代码行数:42,代码来源:TraciParserTest.java



注:本文中的org.antlr.runtime.UnwantedTokenException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java AddItemEntityPacket类代码示例发布时间:2022-05-22
下一篇:
Java CLICommandTypes类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap