eclipse-archived / ceylon.formatter

A formatter for the Ceylon programming language, written in Ceylon.
Apache License 2.0
14 stars 11 forks source link

Improve error recovery #124

Open lucaswerkmeister opened 8 years ago

lucaswerkmeister commented 8 years ago

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.

Needs more investigation though.

CC ceylon/ceylon-ide-eclipse#1536.