Better error reporting from CompilerPipeline and better body node grammar.
This commit is contained in:
parent
034e42d7d0
commit
58e9f60530
@ -13,7 +13,7 @@ preamble
|
|||||||
;
|
;
|
||||||
|
|
||||||
body
|
body
|
||||||
: bodyText? ( component bodyText? )+ | bodyText
|
: ( component | bodyText )+
|
||||||
;
|
;
|
||||||
|
|
||||||
bodyText
|
bodyText
|
||||||
|
@ -6,10 +6,6 @@ import groowt.view.component.web.antlr.*;
|
|||||||
import groowt.view.component.web.ast.DefaultAstBuilder;
|
import groowt.view.component.web.ast.DefaultAstBuilder;
|
||||||
import groowt.view.component.web.ast.DefaultNodeFactory;
|
import groowt.view.component.web.ast.DefaultNodeFactory;
|
||||||
import groowt.view.component.web.ast.node.CompilationUnitNode;
|
import groowt.view.component.web.ast.node.CompilationUnitNode;
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
|
||||||
import org.antlr.v4.runtime.tree.Tree;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -18,43 +14,22 @@ public final class CompilerPipeline {
|
|||||||
|
|
||||||
private static WebViewComponentTemplateCompileException getException(
|
private static WebViewComponentTemplateCompileException getException(
|
||||||
WebViewComponentTemplateCompileUnit compileUnit,
|
WebViewComponentTemplateCompileUnit compileUnit,
|
||||||
TerminalNode terminalNode
|
LexerError lexerError
|
||||||
) {
|
) {
|
||||||
final Token offending = terminalNode.getSymbol();
|
final String formatted = LexerErrorKt.formatLexerError(lexerError);
|
||||||
final var exception = new WebViewComponentTemplateCompileException(
|
return new WebViewComponentTemplateCompileException(
|
||||||
compileUnit, "Invalid token '" + TokenUtil.excerptToken(offending) + "'."
|
compileUnit, formatted
|
||||||
);
|
);
|
||||||
exception.setTerminalNode(terminalNode);
|
|
||||||
return exception;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WebViewComponentTemplateCompileException getException(
|
private static WebViewComponentTemplateCompileException getException(
|
||||||
WebViewComponentTemplateCompileUnit compileUnit,
|
WebViewComponentTemplateCompileUnit compileUnit,
|
||||||
ParserRuleContext parserRuleContext
|
ParserError parserError
|
||||||
) {
|
) {
|
||||||
final var exception = new WebViewComponentTemplateCompileException(
|
final String formatted = ParserErrorKt.formatParserError(parserError);
|
||||||
compileUnit,
|
return new WebViewComponentTemplateCompileException(
|
||||||
"Parser error: " + parserRuleContext.exception.getMessage(),
|
compileUnit, formatted
|
||||||
parserRuleContext.exception
|
|
||||||
);
|
);
|
||||||
exception.setParserRuleContext(parserRuleContext);
|
|
||||||
return exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static WebViewComponentTemplateCompileException getException(
|
|
||||||
WebViewComponentTemplateCompileUnit compileUnit,
|
|
||||||
Tree tree
|
|
||||||
) {
|
|
||||||
if (tree instanceof ParserRuleContext parserRuleContext) {
|
|
||||||
return getException(compileUnit, parserRuleContext);
|
|
||||||
} else if (tree instanceof TerminalNode terminalNode) {
|
|
||||||
return getException(compileUnit, terminalNode);
|
|
||||||
} else {
|
|
||||||
return new WebViewComponentTemplateCompileException(
|
|
||||||
compileUnit,
|
|
||||||
"Error at parser/lexer node " + tree.toString()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WebViewComponentTemplateCompileException getException(
|
private static WebViewComponentTemplateCompileException getException(
|
||||||
@ -78,19 +53,34 @@ public final class CompilerPipeline {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for parser/lexer errors
|
// check for lexer/parser errors
|
||||||
final var parseErrors = AntlrUtil.findErrorNodes(parseResult.getCompilationUnitContext());
|
final var lexerErrors = parseResult.getLexerErrors();
|
||||||
if (!parseErrors.isEmpty()) {
|
final var parserErrors = parseResult.getParserErrors();
|
||||||
if (parseErrors.getErrorCount() == 1) {
|
|
||||||
final var errorNode = parseErrors.getAll().getFirst();
|
if (!lexerErrors.isEmpty()) {
|
||||||
throw getException(compileUnit, errorNode);
|
if (lexerErrors.size() == 1) {
|
||||||
|
throw getException(compileUnit, lexerErrors.getFirst());
|
||||||
} else {
|
} else {
|
||||||
final var errorExceptions = parseErrors.getAll().stream()
|
final var exceptions = lexerErrors.stream()
|
||||||
.map(errorNode -> getException(compileUnit, errorNode))
|
.map(error -> getException(compileUnit, error))
|
||||||
.toList();
|
.toList();
|
||||||
throw new MultipleWebViewComponentCompileErrorsException(
|
throw new MultipleWebViewComponentCompileErrorsException(
|
||||||
compileUnit,
|
compileUnit,
|
||||||
errorExceptions
|
exceptions
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parserErrors.isEmpty()) {
|
||||||
|
if (parserErrors.size() == 1) {
|
||||||
|
throw getException(compileUnit, parserErrors.getFirst());
|
||||||
|
} else {
|
||||||
|
final var exceptions = parserErrors.stream()
|
||||||
|
.map(error -> getException(compileUnit, error))
|
||||||
|
.toList();
|
||||||
|
throw new MultipleWebViewComponentCompileErrorsException(
|
||||||
|
compileUnit,
|
||||||
|
exceptions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user