Skip to content

Commit

Permalink
Make Dfa non generic
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Apr 23, 2023
1 parent 586a923 commit 17a210e
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 91 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/dregex/CompiledRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
/**
* A fully-compiled regular expression that was generated from a string literal.
*/
public class CompiledRegex implements Regex {
public final class CompiledRegex implements Regex {

private final Universe _universe;
private final String _originalString;
private final Node _parsedTree;
private final Dfa<SimpleState> _dfa;
private final Dfa _dfa;

public CompiledRegex(@NonNull String originalString, @NonNull Node parsedTree, @NonNull Universe universe) {
this._universe = universe;
Expand All @@ -37,7 +37,7 @@ public Node parsedTree() {
}

@Override
public Dfa<SimpleState> dfa() {
public Dfa dfa() {
return _dfa;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/dregex/Regex.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dregex

import java.util.regex.Pattern
import dregex.impl.{Dfa, DfaAlgorithms, RegexParser, SimpleState}
import dregex.impl.{Dfa, DfaAlgorithms, RegexParser}
import dregex.impl.RegexParser.DotMatch
import org.slf4j.LoggerFactory

Expand All @@ -14,7 +14,7 @@ import scala.jdk.CollectionConverters._
*/
trait Regex {

private[dregex] def dfa: Dfa[SimpleState]
private[dregex] def dfa: Dfa

/**
* Return this regex's [[Universe]]. Only regexes of the same universe can be operated together.
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/dregex/SynteticRegex.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package dregex

import dregex.impl.Dfa
import dregex.impl.SimpleState

/**
* A regular expression that was generated by an operation between others (not parsing a string), so it lacks a
* literal expression or NFA.
*/
class SynteticRegex private[dregex] (
private[dregex] val dfa: Dfa[SimpleState],
private[dregex] val dfa: Dfa,
val universe: Universe
) extends Regex {

Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/dregex/impl/BiState.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.util.Objects;

public class BiState<A extends State> implements State {
public final class BiState implements State {

public final A first;
public final A second;
public final State first;
public final State second;

public BiState(A first, A second) {
public BiState(State first, State second) {
this.first = first;
this.second = second;
}
Expand All @@ -21,7 +21,7 @@ public String toString() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BiState<?> biState = (BiState<?>) o;
BiState biState = (BiState) o;
return Objects.equals(first, biState.first) && Objects.equals(second, biState.second);
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/dregex/impl/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Compiler(intervalMapping: java.util.Map[AbstractRange, java.util.List[Char
/**
* Transform a regular expression abstract syntax tree into a corresponding NFA
*/
def fromTree(ast: Node): Dfa[SimpleState] = {
def fromTree(ast: Node): Dfa = {
val initial = new SimpleState
val accepting = new SimpleState
val transitions = fromTreeImpl(ast, initial, accepting)
Expand Down Expand Up @@ -250,10 +250,8 @@ class Compiler(intervalMapping: java.util.Map[AbstractRange, java.util.List[Char
}
}

private type BinaryOp[A <: State] = (Dfa[A], Dfa[A]) => Dfa[BiState[A]]

private def processOp(
operation: BinaryOp[SimpleState],
operation: (Dfa, Dfa) => Dfa,
left: Node,
right: Node,
from: SimpleState,
Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/dregex/impl/Dfa.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.util.*;
import java.util.stream.Collectors;

public class Dfa<A extends State> {
public final class Dfa {

public final A initial;
public final Map<A, TreeMap<CharInterval, A>> defTransitions;
public final Set<A> accepting;
public final State initial;
public final Map<State, TreeMap<CharInterval, State>> defTransitions;
public final Set<? extends State> accepting;

public final boolean minimal;

public Dfa(A initial, Map<A, TreeMap<CharInterval, A>> defTransitions, Set<A> accepting, boolean minimal) {
public Dfa(State initial, Map<State, TreeMap<CharInterval, State>> defTransitions, Set<? extends State> accepting, boolean minimal) {
this.initial = initial;
this.defTransitions = defTransitions;
this.accepting = accepting;
Expand All @@ -23,16 +23,16 @@ public String toString() {
return String.format("initial: %s; transitions: %s; accepting: %s", initial, defTransitions, accepting);
}

public Set<A> allStates() {
var ret = new HashSet<A>();
public Set<State> allStates() {
Set<State> ret = new HashSet<>();
ret.add(initial);
ret.addAll(defTransitions.keySet());
ret.addAll(defTransitions.values().stream().flatMap(x -> x.values().stream()).collect(Collectors.toList()));
ret.addAll(accepting);
return ret;
}

public Set<A> allButAccepting() {
public Set<State> allButAccepting() {
var ret = new HashSet<>(allStates());
ret.removeAll(accepting);
return ret;
Expand All @@ -46,7 +46,7 @@ public int stateCount() {
return allStates().size();
}

public Map<CharInterval, A> transitionMap(A state) {
public Map<CharInterval, State> transitionMap(State state) {
var ret = defTransitions.get(state);
return ret == null ? Map.of() : ret;
}
Expand All @@ -55,6 +55,6 @@ public Map<CharInterval, A> transitionMap(A state) {
/**
* Match-nothing DFA
*/
public static Dfa<SimpleState> nothingDfa = new Dfa<>(new SimpleState(), Map.of(), Set.of(), false);
public static Dfa nothingDfa = new Dfa(new SimpleState(), Map.of(), Set.of(), false);

}
Loading

0 comments on commit 17a210e

Please sign in to comment.