Skip to content

Commit

Permalink
#2196 Introduce an exception type to be used during compilation of EQL
Browse files Browse the repository at this point in the history
  • Loading branch information
homedirectory committed Mar 26, 2024
1 parent 39988c5 commit 6773383
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import ua.com.fielden.platform.eql.antlr.exceptions.EqlCompilationException;
import ua.com.fielden.platform.eql.antlr.tokens.PropToken;
import ua.com.fielden.platform.eql.antlr.tokens.util.ListTokenSource;
import ua.com.fielden.platform.eql.antlr.tokens.util.TokensFormatter;
Expand Down Expand Up @@ -114,21 +115,21 @@ public EqlCompiler(final QueryModelToStage1Transformer transformer) {
* @param tokenSource source of tokens representing the expression to compile
* @return compilation result
*/
public EqlCompilationResult compile(final ua.com.fielden.platform.eql.antlr.tokens.util.ListTokenSource tokenSource) {
public EqlCompilationResult compile(final ListTokenSource tokenSource) {
final var tokenStream = new CommonTokenStream(tokenSource);
final var parser = new EQLParser(tokenStream);

parser.addErrorListener(new ThrowingErrorListener(tokenSource));

// parsing stage, results in a complete parse tree
final var tree = parser.start();
final StartContext tree = parser.start();

// compilation stage
final var visitor = new Visitor();
try {
return tree.accept(visitor);
return visitor.visitStart(tree);
} catch (final Exception e) {
throw new EqlStage0ProcessingException(
throw new EqlCompilationException(
"""
Failed to compile an EQL expression.
Expression: %s
Expand All @@ -153,7 +154,7 @@ public <T extends EqlCompilationResult> T compile(final ListTokenSource tokenSou
if (resultType.isInstance(result)) {
return (T) result;
}
throw new RuntimeException("Expected EQL expression of type %s but was: %s".formatted(
throw new EqlCompilationException("Expected EQL expression of type %s but was: %s".formatted(
resultType.getSimpleName(), result.getClass().getSimpleName()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ua.com.fielden.platform.eql.antlr.exceptions;

import ua.com.fielden.platform.exceptions.AbstractPlatformRuntimeException;

public final class EqlCompilationException extends AbstractPlatformRuntimeException {

@java.io.Serial
private static final long serialVersionUID = 1L;

public EqlCompilationException(String s) {
super(s);
}

public EqlCompilationException(String message, Throwable cause) {
super(message, cause);
}

}

0 comments on commit 6773383

Please sign in to comment.