@Override
public void recover(IntStream input, RecognitionException re) {
super.recover(input, re);
if ( state.lastErrorIndex==input.index() ) {
// uh oh, another error at same token index; must be a case
// where LT(1) is in the recovery token set so nothing is
// consumed; consume a single token so at least to prevent
// an infinite loop; this is a failsafe.
input.consume();
}
// still no improvement
if ( state.lastErrorIndex==input.index() ) {
state.failed = true;
}
}
public final IParseResult extParse(String entryRuleName) throws RecognitionException {
IParseResult result = super.parse(entryRuleName);
if (this.state.failed)
{
// we have to catch a parsing failure and build a dummy semantic tree with error
XtextTokenStream input = (XtextTokenStream) getInput();
int lookAhead = input.getCurrentLookAhead();
String completeContent = input.toString();
if (completeContent == null)
completeContent = "";
appendAllTokens();
ICompositeNode rootNode = getNodeModelBuilder().newRootNode(completeContent);
// workaround: we know the top level rule of our grammar
ICompositeNode currentNode = getNodeModelBuilder().newCompositeNode(getGrammarAccess().getUnitDefinitionRule(), lookAhead, rootNode);
getNodeModelBuilder().setSyntaxError(currentNode, new SyntaxErrorMessage("parsing error: compilation unit not correctly closed", null, null));
ICompositeNode compressedRoot = getNodeModelBuilder().compressAndReturnParent(currentNode);
result = new ParseResult(currentNode.getSemanticElement(), compressedRoot, true /*hadErrors*/);
}
return result;
}
]]>