-
Notifications
You must be signed in to change notification settings - Fork 35
Added enum types and verification #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cheestree
wants to merge
9
commits into
liquid-java:main
Choose a base branch
from
cheestree:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a08dbad
Added enum type support with tests
cheestree 3835584
Added EnumRefinementMessage test
cheestree 3d7fb5d
Applied fixes and suggestions for enums
cheestree c831921
Fixed grammar
cheestree d785934
Revert "Fixed grammar"
cheestree 66e982f
Reapply "Fixed grammar"
cheestree 66bebc2
Separated translation into phases
cheestree 9e79999
Refactored variable translation
cheestree cf9e867
Added enum tests
cheestree File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
liquidjava-example/src/main/java/testSuite/CorrectEnumResetMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.StateRefinement; | ||
| import liquidjava.specification.StateSet; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @StateSet({"photoMode", "videoMode", "noMode"}) | ||
| class CorrectEnumResetMode { | ||
| enum Mode { | ||
| Photo, Video, Unknown | ||
| } | ||
|
|
||
| Mode mode; | ||
|
|
||
| @StateRefinement(to="noMode(this)") | ||
| public CorrectEnumResetMode() {} | ||
|
|
||
| @StateRefinement(from="noMode(this) && mode == Mode.Photo", to="photoMode(this)") | ||
| @StateRefinement(from="noMode(this) && mode == Mode.Video", to="videoMode(this)") | ||
| public void setMode(Mode mode) { | ||
| this.mode = mode; | ||
| } | ||
|
|
||
| @StateRefinement(from="photoMode(this)", to="noMode(this)") | ||
| @StateRefinement(from="videoMode(this)", to="noMode(this)") | ||
| public void resetMode() { | ||
| this.mode = null; | ||
| } | ||
|
|
||
| @StateRefinement(from="photoMode(this)") | ||
| public void takePhoto() {} | ||
|
|
||
| @StateRefinement(from="videoMode(this)") | ||
| public void takeVideo() {} | ||
|
|
||
| public static void main(String[] args) { | ||
| CorrectEnumResetMode st = new CorrectEnumResetMode(); | ||
| st.setMode(Mode.Photo); // noMode -> photoMode | ||
| st.takePhoto(); | ||
| st.resetMode(); // photoMode -> noMode | ||
| st.setMode(Mode.Video); // noMode -> videoMode | ||
| st.takeVideo(); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
liquidjava-example/src/main/java/testSuite/CorrectEnumUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.StateRefinement; | ||
| import liquidjava.specification.StateSet; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @StateSet({"photoMode", "videoMode", "noMode"}) | ||
| class CorrectEnumUsage { | ||
| enum Mode { | ||
| Photo, Video, Unknown | ||
| } | ||
|
|
||
| Mode mode; | ||
| @StateRefinement(to="noMode(this)") | ||
| public CorrectEnumUsage() {} | ||
|
|
||
| @StateRefinement(from="noMode(this) && mode == Mode.Photo", to="photoMode(this)") | ||
| @StateRefinement(from="noMode(this) && mode == Mode.Video", to="videoMode(this)") | ||
| public void setMode(Mode mode) { | ||
| this.mode = mode; | ||
| } | ||
|
|
||
| @StateRefinement(from="photoMode(this)") | ||
| public void takePhoto() {} | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // Correct | ||
| CorrectEnumUsage st = new CorrectEnumUsage(); | ||
| st.setMode(Mode.Photo); | ||
| st.takePhoto(); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
liquidjava-example/src/main/java/testSuite/ErrorEnumFunctionRefinement.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| class ErrorEnumFunctionRefinement { | ||
| enum Color { Red, Green, Blue } | ||
|
|
||
| Color c; | ||
|
|
||
| Color changeColor(@Refinement("newColor == Color.Red || newColor == Color.Green") Color newColor) { | ||
| c = newColor; // correct | ||
| return c; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| ErrorEnumFunctionRefinement e = new ErrorEnumFunctionRefinement(); | ||
| e.changeColor(Color.Red); // correct | ||
| e.changeColor(Color.Blue); // error | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
liquidjava-example/src/main/java/testSuite/ErrorEnumNull.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| class ErrorEnumNull { | ||
| enum Color { | ||
| Red, Green, Blue | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| @Refinement("c == Color.Red || c == Color.Green") | ||
| Color c = null; // error | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
liquidjava-example/src/main/java/testSuite/ErrorEnumUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // State Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.StateRefinement; | ||
| import liquidjava.specification.StateSet; | ||
|
|
||
|
|
||
| @SuppressWarnings("unused") | ||
| @StateSet({"photoMode", "videoMode", "noMode"}) | ||
| class ErrorEnumUsage { | ||
| enum Mode { | ||
| Photo, Video, Unknown | ||
| } | ||
|
|
||
| Mode mode; | ||
| @StateRefinement(to="noMode(this)") | ||
| public ErrorEnumUsage() {} | ||
|
|
||
| @StateRefinement(from="noMode(this) && mode == Mode.Photo", to="photoMode(this)") | ||
| @StateRefinement(from="noMode(this) && mode == Mode.Video", to="videoMode(this)") | ||
| public void setMode(Mode mode) { | ||
| this.mode = mode; | ||
| } | ||
|
|
||
| @StateRefinement(from="photoMode(this)") | ||
| public void takePhoto() {} | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // Correct | ||
| ErrorEnumUsage st = new ErrorEnumUsage(); | ||
| st.setMode(Mode.Video); | ||
| st.takePhoto(); //error | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
liquidjava-example/src/main/java/testingInProgress/EnumRefinementMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package testingInProgress; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| public class EnumRefinementMessage { | ||
| enum Mode { | ||
| Photo, Video, Unknown | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| @Refinement("_==Mode.Photo") | ||
| Mode test = Mode.Video; | ||
| System.out.println(test); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
liquidjava-verifier/src/main/java/liquidjava/rj_language/ast/Enumerate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package liquidjava.rj_language.ast; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import liquidjava.diagnostics.errors.LJError; | ||
| import liquidjava.rj_language.visitors.ExpressionVisitor; | ||
|
|
||
| public class Enumerate extends Expression { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could rename it to just |
||
|
|
||
| private final String enumTypeName; | ||
| private final String enumConstantName; | ||
|
|
||
| public Enumerate(String enumTypeName, String enumConstantName) { | ||
| this.enumTypeName = enumTypeName; | ||
| this.enumConstantName = enumConstantName; | ||
| } | ||
|
|
||
| public String getEnumTypeName() { | ||
| return enumTypeName; | ||
| } | ||
|
|
||
| public String getEnumConstantName() { | ||
| return enumConstantName; | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T accept(ExpressionVisitor<T> visitor) throws LJError { | ||
| return visitor.visitEnumerate(this); | ||
| } | ||
|
|
||
| @Override | ||
| public void getVariableNames(List<String> toAdd) { | ||
| // end leaf | ||
| } | ||
|
|
||
| @Override | ||
| public void getStateInvocations(List<String> toAdd, List<String> all) { | ||
| // end leaf | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBooleanTrue() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return enumTypeName + "." + enumConstantName; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| final int prime = 31; | ||
| int result = 1; | ||
| result = prime * result + ((enumTypeName == null) ? 0 : enumTypeName.hashCode()); | ||
| result = prime * result + ((enumConstantName == null) ? 0 : enumConstantName.hashCode()); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) | ||
| return true; | ||
| if (obj == null || getClass() != obj.getClass()) | ||
| return false; | ||
| Enumerate other = (Enumerate) obj; | ||
| return enumTypeName.equals(other.enumTypeName) && enumConstantName.equals(other.enumConstantName); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression clone() { | ||
| return new Enumerate(enumTypeName, enumConstantName); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||
| import liquidjava.diagnostics.errors.SyntaxError; | ||||||||||||||||||
| import liquidjava.rj_language.ast.AliasInvocation; | ||||||||||||||||||
| import liquidjava.rj_language.ast.BinaryExpression; | ||||||||||||||||||
| import liquidjava.rj_language.ast.Enumerate; | ||||||||||||||||||
| import liquidjava.rj_language.ast.Expression; | ||||||||||||||||||
| import liquidjava.rj_language.ast.FunctionInvocation; | ||||||||||||||||||
| import liquidjava.rj_language.ast.GroupExpression; | ||||||||||||||||||
|
|
@@ -19,13 +20,15 @@ | |||||||||||||||||
| import liquidjava.rj_language.ast.UnaryExpression; | ||||||||||||||||||
| import liquidjava.rj_language.ast.Var; | ||||||||||||||||||
| import liquidjava.utils.Utils; | ||||||||||||||||||
| import liquidjava.utils.constants.Formats; | ||||||||||||||||||
| import liquidjava.utils.constants.Keys; | ||||||||||||||||||
|
|
||||||||||||||||||
| import org.antlr.v4.runtime.tree.ParseTree; | ||||||||||||||||||
| import org.apache.commons.lang3.NotImplementedException; | ||||||||||||||||||
| import rj.grammar.RJParser.AliasCallContext; | ||||||||||||||||||
| import rj.grammar.RJParser.ArgsContext; | ||||||||||||||||||
| import rj.grammar.RJParser.DotCallContext; | ||||||||||||||||||
| import rj.grammar.RJParser.EnumContext; | ||||||||||||||||||
| import rj.grammar.RJParser.ExpBoolContext; | ||||||||||||||||||
| import rj.grammar.RJParser.ExpContext; | ||||||||||||||||||
| import rj.grammar.RJParser.ExpGroupContext; | ||||||||||||||||||
|
|
@@ -156,9 +159,10 @@ private Expression literalExpressionCreate(ParseTree rc) throws LJError { | |||||||||||||||||
| return new GroupExpression(create(((LitGroupContext) rc).literalExpression())); | ||||||||||||||||||
| else if (rc instanceof LitContext) | ||||||||||||||||||
| return create(((LitContext) rc).literal()); | ||||||||||||||||||
| else if (rc instanceof VarContext) { | ||||||||||||||||||
| else if (rc instanceof VarContext) | ||||||||||||||||||
| return new Var(((VarContext) rc).ID().getText()); | ||||||||||||||||||
|
|
||||||||||||||||||
| else if (rc instanceof EnumContext) { | ||||||||||||||||||
| return enumCreate((EnumContext) rc); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| return create(((InvocationContext) rc).functionCall()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -234,6 +238,14 @@ private List<Expression> getArgs(ArgsContext args) throws LJError { | |||||||||||||||||
| return le; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private Enumerate enumCreate(EnumContext enumContext) { | ||||||||||||||||||
| String enumText = enumContext.enumCall().getText(); | ||||||||||||||||||
| int lastDot = enumText.lastIndexOf('.'); | ||||||||||||||||||
| String enumTypeName = enumText.substring(0, lastDot); | ||||||||||||||||||
| String enumConstName = enumText.substring(lastDot + 1); | ||||||||||||||||||
| return new Enumerate(enumTypeName, enumConstName); | ||||||||||||||||||
|
Comment on lines
+242
to
+246
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are always guaranteed to have exactly two ids separated by dot, we could improve this a bit:
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private Expression literalCreate(LiteralContext literalContext) throws LJError { | ||||||||||||||||||
| if (literalContext.BOOL() != null) | ||||||||||||||||||
| return new LiteralBoolean(literalContext.BOOL().getText()); | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this test to the testSuite and rename it to something like
CorrectEnumRefinement.