最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】antlr异常处理:在ParseTree去getTree之后,判断某个树节点是否出现异常

ANTLR crifan 3232浏览 0评论

【背景】

继续折腾antlr的异常处理。

此处,是已知:

antlr的语法中,已经默认就支持了异常的恢复,即内部实现中,遇到一些异常,比如MismatchedSetException,已经会去:

捕获异常

恢复异常

记录异常信息

比如:

		catch (RecognitionException re) {
			reportError(re);
			recover(input,re);
			retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re);
		}

所以,也就无需手动再去写类似的异常恢复的代码了。

但是,希望在后续的解析树的时候,能够判断某个节点,是否已经出现异常了。

从而采取相应措施,比如直接扔掉不处理,等等。

所以,此处需要:

针对某个树节点,判断是否已经是异常了。

 

【折腾过程】

1.期间,需要搞懂:

【已解决】Java中获得变量的类型

2.然后,是通过如下代码:

EddlVariableErrorHandleLexer lexer = new EddlVariableErrorHandleLexer(cs);
CommonTokenStream tokens = new CommonTokenStream();
tokens.setTokenSource(lexer);

ParseTreeBuilder builder = new ParseTreeBuilder("startParse");

EddlVariableErrorHandleParser parser = new EddlVariableErrorHandleParser(tokens, builder);
parser.startParse();
ParseTree parseTree = builder.getTree();
Object payloadRoot = parseTree.payload;
ParseTree parseTreeVaraible = (ParseTree) parseTree.getChild(0).getChild(0);
ParseTree parseTreeVaraibleVaraibleBody = (ParseTree) parseTreeVaraible.getChild(3);
ParseTree parseTreeVaraibleBodyClass = (ParseTree) parseTreeVaraibleVaraibleBody.getChild(1).getChild(0);
ParseTree parseTreeVaraibleBodyClassValue = (ParseTree) parseTreeVaraibleBodyClass.getChild(1);
ParseTree parseTreeVaraibleBodyClassValueSingle = (ParseTree) parseTreeVaraibleBodyClassValue.getChild(0);
ParseTree parseTreeVaraibleBodyClassValueSingleCommon = (ParseTree) parseTreeVaraibleBodyClassValueSingle.getChild(0);

ParseTree parseTreeVaraibleBodyClassValueSingleCommonClass = (ParseTree) parseTreeVaraibleBodyClassValueSingleCommon.getChild(0);

String commonClassTreeStr =parseTreeVaraibleBodyClassValueSingleCommonClass.toStringTree(); 
System.out.println("commonClassTreeStr=" + commonClassTreeStr);
int commonClassType =parseTreeVaraibleBodyClassValueSingleCommonClass.getType(); 
System.out.println("commonClassType=" + commonClassType);

Class commonClassValue = parseTreeVaraibleBodyClassValueSingleCommonClass.getClass();
System.out.println("commonClassValue=" + commonClassValue);
String commonClassName = commonClassValue.getName();
System.out.println("commonClassName=" + commonClassName);

Object objPayload = parseTreeVaraibleBodyClassValueSingleCommonClass.payload;
String payloadClassName =objPayload.getClass().getName(); 
System.out.println("payloadClassName=" + payloadClassName);

boolean isException = false;
isException = objPayload instanceof RecognitionException;
System.out.println("isException=" + isException);

可以判断出,我所关心的,某个叶子节点parseTreeVaraibleBodyClassValueSingleCommonClass是否是RecognitionException。

即,判断某个数节点,是否是antlr的异常。

注:

RecognitionException是antlr的异常的root class。

 

输出为:

D:/DevRoot/eclipse/common_root/EddlVariableErrorHandle/src/demoEddlVariableError.ddl line 5:12 mismatched input 'INPUT1' expecting set null
commonClassTreeStr=MismatchedSetException(12!=null)
commonClassType=0
commonClassValue=class org.antlr.runtime.tree.ParseTree
commonClassName=org.antlr.runtime.tree.ParseTree
payloadClassName=org.antlr.runtime.MismatchedSetException
isException=true

如图:

check common class value is antlr exception result is true

 

【总结】

此处,最终对于antlr的某个树节点:

parseTreeVaraibleBodyClassValueSingleCommonClass

判断是否有异常,是通过:

ParseTree parseTreeVaraibleBodyClassValueSingleCommonClass = (ParseTree) parseTreeVaraibleBodyClassValueSingleCommon.getChild(0);

Object objPayload = parseTreeVaraibleBodyClassValueSingleCommonClass.payload;
//String payloadClassName =objPayload.getClass().getName(); 
//System.out.println("payloadClassName=" + payloadClassName);

boolean isException = false;
isException = objPayload instanceof RecognitionException;
System.out.println("isException=" + isException);

即可。

转载请注明:在路上 » 【记录】antlr异常处理:在ParseTree去getTree之后,判断某个树节点是否出现异常

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.186 seconds, using 22.14MB memory