Skip to content

Commit 0318082

Browse files
committed
Improve Parser
1 parent 45cf93e commit 0318082

File tree

3 files changed

+40
-62
lines changed

3 files changed

+40
-62
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private void createStateSet(CtNewArray<String> e, int set, CtElement element) th
164164

165165
protected GhostDTO getGhostDeclaration(String value, SourcePosition position) throws LJError {
166166
try {
167-
return RefinementsParser.getGhostDeclaration(value);
167+
return RefinementsParser.parseGhostDeclaration(value);
168168
} catch (LJError e) {
169169
// add location info to error
170170
e.setPosition(position);
@@ -238,7 +238,7 @@ protected void getGhostFunction(String value, CtElement element, SourcePosition
238238

239239
protected void handleAlias(String ref, CtElement element, SourcePosition position) throws LJError {
240240
try {
241-
AliasDTO a = RefinementsParser.getAliasDeclaration(ref);
241+
AliasDTO a = RefinementsParser.parseAliasDefinition(ref);
242242
String klass = null;
243243
String path = null;
244244
if (element instanceof CtClass) {

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

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Optional;
44

5-
import liquidjava.diagnostics.errors.LJError;
65
import liquidjava.diagnostics.errors.SyntaxError;
76
import liquidjava.processor.facade.AliasDTO;
87
import liquidjava.processor.facade.GhostDTO;
@@ -13,99 +12,85 @@
1312
import org.antlr.v4.runtime.CharStreams;
1413
import org.antlr.v4.runtime.CodePointCharStream;
1514
import org.antlr.v4.runtime.CommonTokenStream;
16-
import org.antlr.v4.runtime.RuleContext;
1715
import org.antlr.v4.runtime.tree.ParseTree;
1816
import rj.grammar.RJLexer;
1917
import rj.grammar.RJParser;
2018

2119
public class RefinementsParser {
2220

23-
public static Expression createAST(String toParse, String prefix) throws LJError {
24-
ParseTree pt = compile(toParse);
21+
/**
22+
* Parses a refinement expression from a string
23+
*/
24+
public static Expression createAST(String toParse, String prefix) throws SyntaxError {
25+
ParseTree pt = compile(toParse, "Invalid refinement expression, expected a logical predicate");
2526
CreateASTVisitor visitor = new CreateASTVisitor(prefix);
2627
return visitor.create(pt);
2728
}
2829

2930
/**
30-
* The triple information of the ghost declaration in the order <type, name, list<type,name>>
31-
*
32-
* @param s
31+
* Parses the ghost declaration from the given string
3332
*/
34-
public static GhostDTO getGhostDeclaration(String s) throws LJError {
35-
ParseTree rc = compile(s);
33+
public static GhostDTO parseGhostDeclaration(String toParse) throws SyntaxError {
34+
String errorMessage = "Invalid ghost declaration, expected e.g. @Ghost(\"int size\")";
35+
ParseTree rc = compile(toParse, errorMessage);
3636
GhostDTO g = GhostVisitor.getGhostDecl(rc);
3737
if (g == null)
38-
throw new SyntaxError("Invalid ghost declaration, expected e.g. @Ghost(\"int size\")", s);
38+
throw new SyntaxError(errorMessage, toParse);
3939
return g;
4040
}
4141

42-
public static AliasDTO getAliasDeclaration(String s) throws LJError {
43-
CodePointCharStream input = CharStreams.fromString(s);
44-
RJErrorListener err = new RJErrorListener();
45-
RJLexer lexer = new RJLexer(input);
46-
lexer.removeErrorListeners();
47-
lexer.addErrorListener(err);
48-
49-
CommonTokenStream tokens = new CommonTokenStream(lexer);
50-
RJParser parser = new RJParser(tokens);
51-
parser.setBuildParseTree(true);
52-
parser.removeErrorListeners();
53-
parser.addErrorListener(err);
54-
55-
RuleContext rc = parser.prog();
56-
AliasVisitor av = new AliasVisitor(input);
42+
/**
43+
* Parses the alias declaration from the given string, throwing a SyntaxError if it is not valid
44+
*/
45+
public static AliasDTO parseAliasDefinition(String toParse) throws SyntaxError {
46+
String errorMessage = "Invalid alias definition, expected e.g. @RefinementAlias(\"Positive(int v) { v >= 0 }\")";
47+
ParseTree rc = compile(toParse, errorMessage);
48+
AliasVisitor av = new AliasVisitor();
5749
AliasDTO alias = av.getAlias(rc);
5850
if (alias == null)
59-
throw new SyntaxError(
60-
"Invalid alias definition, expected e.g. @RefinementAlias(\"Positive(int v) { v >= 0 }\")", s);
51+
throw new SyntaxError(errorMessage, toParse);
6152
return alias;
6253
}
6354

64-
private static ParseTree compile(String toParse) throws LJError {
55+
/**
56+
* Compiles the given string into a parse tree
57+
*/
58+
private static ParseTree compile(String toParse, String errorMessage) throws SyntaxError {
6559
Optional<String> s = getErrors(toParse);
6660
if (s.isPresent())
67-
throw new SyntaxError("Invalid refinement expression, expected a logical predicate", toParse);
61+
throw new SyntaxError(errorMessage, toParse);
6862

69-
CodePointCharStream input = CharStreams.fromString(toParse);
7063
RJErrorListener err = new RJErrorListener();
71-
72-
RJLexer lexer = new RJLexer(input);
73-
lexer.removeErrorListeners();
74-
lexer.addErrorListener(err);
75-
76-
CommonTokenStream tokens = new CommonTokenStream(lexer);
77-
78-
RJParser parser = new RJParser(tokens);
79-
parser.setBuildParseTree(true);
80-
parser.removeErrorListeners();
81-
parser.addErrorListener(err);
64+
RJParser parser = createParser(toParse, err);
8265
return parser.prog();
8366
}
8467

8568
/**
86-
* First passage to check if there are syntax errors
87-
*
88-
* @param toParse
89-
*
90-
* @return
69+
* Checks if the given string can be parsed without syntax errors, returning the error messages if any
9170
*/
9271
private static Optional<String> getErrors(String toParse) {
93-
CodePointCharStream input = CharStreams.fromString(toParse);
9472
RJErrorListener err = new RJErrorListener();
73+
RJParser parser = createParser(toParse, err);
74+
parser.start(); // all consumed
75+
if (err.getErrors() > 0)
76+
return Optional.of(err.getMessages());
77+
return Optional.empty();
78+
}
9579

80+
/**
81+
* Creates a parser for the given input string and error listener
82+
*/
83+
private static RJParser createParser(String toParse, RJErrorListener err) {
84+
CodePointCharStream input = CharStreams.fromString(toParse);
9685
RJLexer lexer = new RJLexer(input);
9786
lexer.removeErrorListeners();
9887
lexer.addErrorListener(err);
9988

10089
CommonTokenStream tokens = new CommonTokenStream(lexer);
101-
10290
RJParser parser = new RJParser(tokens);
10391
parser.setBuildParseTree(true);
10492
parser.removeErrorListeners();
10593
parser.addErrorListener(err);
106-
parser.start(); // all consumed
107-
if (err.getErrors() > 0)
108-
return Optional.of(err.getMessages());
109-
return Optional.empty();
94+
return parser;
11095
}
11196
}

liquidjava-verifier/src/main/java/liquidjava/rj_language/visitors/AliasVisitor.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
import java.util.stream.Collectors;
66
import liquidjava.processor.facade.AliasDTO;
77
import liquidjava.utils.Pair;
8-
import org.antlr.v4.runtime.CodePointCharStream;
98
import org.antlr.v4.runtime.misc.Interval;
109
import org.antlr.v4.runtime.tree.ParseTree;
1110
import rj.grammar.RJParser.AliasContext;
1211
import rj.grammar.RJParser.ArgDeclIDContext;
1312
import rj.grammar.RJParser.PredContext;
1413

1514
public class AliasVisitor {
16-
CodePointCharStream input;
17-
18-
public AliasVisitor(CodePointCharStream input) {
19-
this.input = input;
20-
}
21-
2215
/**
2316
* Gets information about the alias
2417
*
@@ -56,7 +49,7 @@ private String getText(PredContext pred) {
5649
int a = pred.start.getStartIndex();
5750
int b = pred.stop.getStopIndex();
5851
Interval interval = new Interval(a, b);
59-
return input.getText(interval);
52+
return pred.start.getInputStream().getText(interval);
6053
}
6154

6255
private List<Pair<String, String>> getArgsDecl(ArgDeclIDContext argDeclID) {

0 commit comments

Comments
 (0)