-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed implementation/testing of Evaluator.
- Loading branch information
Showing
8 changed files
with
773 additions
and
23 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/main/java/co/icecave/dialekt/evaluator/AbstractPatternMatcher.java
This file contains 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,120 @@ | ||
package co.icecave.dialekt.evaluator; | ||
|
||
import co.icecave.dialekt.ast.EmptyExpression; | ||
import co.icecave.dialekt.ast.ExpressionInterface; | ||
import co.icecave.dialekt.ast.LogicalAnd; | ||
import co.icecave.dialekt.ast.LogicalNot; | ||
import co.icecave.dialekt.ast.LogicalOr; | ||
import co.icecave.dialekt.ast.Pattern; | ||
import co.icecave.dialekt.ast.PatternChildInterface; | ||
import co.icecave.dialekt.ast.PatternLiteral; | ||
import co.icecave.dialekt.ast.PatternWildcard; | ||
import co.icecave.dialekt.ast.Tag; | ||
import co.icecave.dialekt.ast.VisitorInterface; | ||
|
||
abstract class AbstractPatternMatcher implements TagMatcherInterface, VisitorInterface<String> | ||
{ | ||
public AbstractPatternMatcher(Pattern pattern) | ||
{ | ||
this.pattern = pattern; | ||
this.regex = this.compilePattern( | ||
pattern.accept(this) | ||
); | ||
} | ||
|
||
public boolean match(String tag) | ||
{ | ||
return this.regex.matcher(tag).matches(); | ||
} | ||
|
||
protected abstract java.util.regex.Pattern compilePattern(String pattern); | ||
|
||
/** | ||
* Visit a LogicalAnd node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(LogicalAnd node) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Visit a LogicalOr node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(LogicalOr node) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Visit a LogicalNot node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(LogicalNot node) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Visit a Tag node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(Tag node) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Visit a pattern node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(Pattern node) | ||
{ | ||
String pattern = ""; | ||
|
||
for (PatternChildInterface child : node.children()) { | ||
pattern += child.accept(this); | ||
} | ||
|
||
return '^' + pattern + '$'; | ||
} | ||
|
||
/** | ||
* Visit a PatternLiteral node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(PatternLiteral node) | ||
{ | ||
return java.util.regex.Pattern.quote(node.string()); | ||
} | ||
|
||
/** | ||
* Visit a PatternWildcard node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(PatternWildcard node) | ||
{ | ||
return ".*"; | ||
} | ||
|
||
/** | ||
* Visit a EmptyExpression node. | ||
* | ||
* @param node The node to visit. | ||
*/ | ||
public String visit(EmptyExpression node) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private Pattern pattern; | ||
private java.util.regex.Pattern regex; | ||
} |
This file contains 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 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 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 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
55 changes: 55 additions & 0 deletions
55
src/test/java/co/icecave/dialekt/evaluator/EvaluationResultTest.java
This file contains 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,55 @@ | ||
package co.icecave.dialekt.evaluator; | ||
|
||
import co.icecave.dialekt.ast.ExpressionInterface; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
import org.testng.Assert; | ||
|
||
public class EvaluationResultTest | ||
{ | ||
public EvaluationResultTest() | ||
{ | ||
this.expression = Mockito.mock(ExpressionInterface.class); | ||
|
||
this.expressionResult = new ExpressionResult( | ||
this.expression, | ||
true, | ||
Collections.EMPTY_SET, | ||
Collections.EMPTY_SET | ||
); | ||
|
||
this.result = new EvaluationResult( | ||
true, | ||
Arrays.asList(this.expressionResult) | ||
); | ||
} | ||
|
||
@Test | ||
public void testIsMatch() | ||
{ | ||
Assert.assertTrue(this.result.isMatch()); | ||
} | ||
|
||
@Test | ||
public void testResultOf() | ||
{ | ||
Assert.assertSame( | ||
this.result.resultOf(this.expression), | ||
this.expressionResult | ||
); | ||
} | ||
|
||
@Test(expectedExceptions = IndexOutOfBoundsException.class) | ||
public void testResultOfWithUnknownExpression() | ||
{ | ||
this.result.resultOf( | ||
Mockito.mock(ExpressionInterface.class) | ||
); | ||
} | ||
|
||
private ExpressionInterface expression; | ||
private ExpressionResult expressionResult; | ||
private EvaluationResult result; | ||
} |
Oops, something went wrong.