Skip to content

Commit d019952

Browse files
committed
Improve Syntax Errors
1 parent 2a8f9b7 commit d019952

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/ExternalRefinementTypeChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public <R> void visitCtMethod(CtMethod<R> method) {
9494
}
9595

9696
protected void getGhostFunction(String value, CtElement element) throws LJError {
97-
GhostDTO f = RefinementsParser.getGhostDeclaration(value);
97+
GhostDTO f = getGhostDeclaration(value, element);
9898
if (element.getParent() instanceof CtInterface<?>) {
9999
GhostFunction gh = new GhostFunction(f, factory, prefix);
100100
context.addGhostFunction(gh);

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/TypeChecker.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,20 @@ private void createStateSet(CtNewArray<String> e, int set, CtElement element) th
163163
}
164164
}
165165

166+
protected GhostDTO getGhostDeclaration(String value, CtElement element) throws LJError {
167+
try {
168+
return RefinementsParser.getGhostDeclaration(value);
169+
} catch (LJError e) {
170+
// add location info to error
171+
SourcePosition annPosition = Utils.getAnnotationPosition(element, value);
172+
e.setPosition(annPosition);
173+
throw e;
174+
}
175+
}
176+
166177
private void createStateGhost(String string, CtAnnotation<? extends Annotation> ann, CtElement element)
167178
throws LJError {
168-
GhostDTO gd = RefinementsParser.getGhostDeclaration(string);
179+
GhostDTO gd = getGhostDeclaration(string, ann);
169180
if (!gd.paramTypes().isEmpty()) {
170181
throw new CustomError(
171182
"Ghost States have the class as parameter " + "by default, no other parameters are allowed",
@@ -221,7 +232,7 @@ protected Optional<GhostFunction> createStateGhost(int order, CtElement element)
221232
}
222233

223234
protected void getGhostFunction(String value, CtElement element) throws LJError {
224-
GhostDTO f = RefinementsParser.getGhostDeclaration(value);
235+
GhostDTO f = getGhostDeclaration(value, element);
225236
if (element.getParent()instanceof CtClass<?> klass) {
226237
GhostFunction gh = new GhostFunction(f, factory, klass.getQualifiedName());
227238
context.addGhostFunction(gh);

liquidjava-verifier/src/main/java/liquidjava/rj_language/parsing/RefinementsParser.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ public static GhostDTO getGhostDeclaration(String s) throws LJError {
3535
ParseTree rc = compile(s);
3636
GhostDTO g = GhostVisitor.getGhostDecl(rc);
3737
if (g == null)
38-
throw new SyntaxError("Ghost declarations should be in format <type> <name> (<parameters>)", s);
38+
throw new SyntaxError("Invalid ghost declaration, e.g. \"int size\"", s);
3939
return g;
4040
}
4141

4242
public static AliasDTO getAliasDeclaration(String s) throws LJError {
43-
Optional<String> os = getErrors(s);
44-
if (os.isPresent())
45-
throw new SyntaxError(os.get(), s);
46-
CodePointCharStream input;
47-
input = CharStreams.fromString(s);
43+
CodePointCharStream input = CharStreams.fromString(s);
4844
RJErrorListener err = new RJErrorListener();
4945
RJLexer lexer = new RJLexer(input);
5046
lexer.removeErrorListeners();
@@ -60,14 +56,14 @@ public static AliasDTO getAliasDeclaration(String s) throws LJError {
6056
AliasVisitor av = new AliasVisitor(input);
6157
AliasDTO alias = av.getAlias(rc);
6258
if (alias == null)
63-
throw new SyntaxError("Alias definitions should be in format <name>(<parameters>) { <definition> }", s);
59+
throw new SyntaxError("Invalid alias definition, e.g. \"Positive(int v) { v >= 0 }\"", s);
6460
return alias;
6561
}
6662

6763
private static ParseTree compile(String toParse) throws LJError {
6864
Optional<String> s = getErrors(toParse);
6965
if (s.isPresent())
70-
throw new SyntaxError(s.get(), toParse);
66+
throw new SyntaxError("Invalid refinement expression, e.g. \"v > 0\"", toParse);
7167

7268
CodePointCharStream input = CharStreams.fromString(toParse);
7369
RJErrorListener err = new RJErrorListener();

0 commit comments

Comments
 (0)