The current error recovery of the FormattingWriter doesn’t work very well. I think one approach that might work better would be that FormattingWriter always throws an exception when it encounters an unexpected token, and FormattingVisitor handles it:
shared actual void visitStatement(Statement that) {
try {
value context = fWriter.openContext();
that.visitChildren(this);
if (exists mainEndToken = that.mainEndToken) {
writeSemicolon(fWriter, mainEndToken, context);
} else {
// complex statements like loops, ifs, etc. don’t end in a semicolon
fWriter.closeContext(context);
}
} catch (UnexpectedTokenException ute) {
if (exists mainEndToken = that.mainEndToken) {
fWriter.writeRecoveryToken {
mainEndToken;
// other options as in writeToken
};
} else {
throw ute;
}
}
}
where writeRecoveryToken writes out all unexpected tokens until it finds the recovery token (here, the semicolon). This is mostly what FormattingWriter currently does, but I think the fact that it tries to do it for any token makes it unstable.
The current error recovery of the
FormattingWriter
doesn’t work very well. I think one approach that might work better would be thatFormattingWriter
always throws an exception when it encounters an unexpected token, andFormattingVisitor
handles it:where
writeRecoveryToken
writes out all unexpected tokens until it finds the recovery token (here, the semicolon). This is mostly whatFormattingWriter
currently does, but I think the fact that it tries to do it for any token makes it unstable.Needs more investigation though.
CC ceylon/ceylon-ide-eclipse#1536.