我有这种语法,
locationPath returns [CustomParser xpathParser]
:^(LOCATION_PATH relativeLocationPath {**Want to throw a exception if this condition matches**})
|^(LOCATION_PATH absoluteLocationPath {$xpathParser=$absoluteLocationPath.xpathParser;})
;
这样做的方法是什么?我试过这个
locationPath returns [CustomParser xpathParser]
:^(LOCATION_PATH relativeLocationPath {throw new Exception})
|^(LOCATION_PATH absoluteLocationPath {$xpathParser=$absoluteLocationPath.xpathParser;})
但是使用这个生成的代码会产生编译错误。因为loactionapth方法没有抛出方法签名的线索。
答案 0 :(得分:3)
只有一种方法可以执行此操作:抛出未经检查的异常:
locationPath returns [CustomParser xpathParser]
: ^(LOCATION_PATH relativeLocationPath) {throw new RuntimeException("No way!");}
| ^(LOCATION_PATH absoluteLocationPath {$xpathParser=$absoluteLocationPath.xpathParser;})
;
如果编译器仍然抱怨(我不记得了,我现在无法测试),请在其前面添加if(true)
:
locationPath returns [CustomParser xpathParser]
: ^(LOCATION_PATH relativeLocationPath) {if(true) throw new RuntimeException("No way!");}
| ^(LOCATION_PATH absoluteLocationPath {$xpathParser=$absoluteLocationPath.xpathParser;})
;