-
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.
- Loading branch information
Showing
4 changed files
with
244 additions
and
1 deletion.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
vol8/src/main/java/ru/mifi/practice/vol8/regexp/machine/Match.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,30 @@ | ||
package ru.mifi.practice.vol8.regexp.machine; | ||
|
||
import ru.mifi.practice.vol8.regexp.tree.Tree; | ||
|
||
public interface Match { | ||
boolean match(String text); | ||
|
||
final class Machine implements Match { | ||
private final State state; | ||
|
||
public Machine(Tree tree) { | ||
MachineGenerator generator = new MachineGenerator(); | ||
tree.visit(generator); | ||
this.state = generator.getState(); | ||
} | ||
|
||
@Override | ||
public boolean match(String text) { | ||
Input input = new Input.StringInput(text); | ||
return match(state, input); | ||
} | ||
|
||
private static boolean match(State state, Input input) { | ||
if (state.accept(input)) { | ||
return state.match(input); | ||
} | ||
return false; | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
vol8/src/test/java/ru/mifi/practice/vol8/regexp/machine/MatchTest.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,43 @@ | ||
package ru.mifi.practice.vol8.regexp.machine; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import ru.mifi.practice.vol8.regexp.tree.Tree; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@DisplayName("Match") | ||
class MatchTest { | ||
|
||
protected static Stream<Arguments> patternMatching() { | ||
return Stream.of( | ||
Arguments.of(true, "a", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(true, "e", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(true, "of", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(true, "pt", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(true, "bddd", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(true, "bbbbbd", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(false, "hello", "(a|b*(c?d)+|e)|(of|pt)"), | ||
Arguments.of(false, "bbcdddo", "(a|b*(c?d)+|e)|(of|pt)") | ||
); | ||
} | ||
|
||
@Disabled | ||
@ParameterizedTest | ||
@MethodSource("patternMatching") | ||
void match(boolean isMatch, String input, String pattern) { | ||
Tree tree = new Tree.Default(pattern); | ||
Match match = new Match.Machine(tree); | ||
if (isMatch) { | ||
assertTrue(match.match(input)); | ||
} else { | ||
assertFalse(match.match(input)); | ||
} | ||
} | ||
} |