Skip to content

Commit

Permalink
Migrate to Java: CompiledRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Mar 18, 2023
1 parent 8d85fb7 commit 2853db1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ licenses := Seq("BSD-style" -> url("http://www.opensource.org/licenses/bsd-licen

scalaVersion := "2.13.10"

javacOptions ++= Seq("-source", "11", "-target", "11")

publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Expand Down Expand Up @@ -49,6 +51,7 @@ libraryDependencies ++=
"org.slf4j" % "slf4j-api" % "2.0.6" ::
"org.scala-lang.modules" %% "scala-parser-combinators" % "2.2.0" ::
"org.scala-lang.modules" %% "scala-collection-compat" % "2.9.0" ::
"org.projectlombok" % "lombok" % "1.18.22" ::
"org.scalatest" %% "scalatest-funsuite" % "3.2.15" % Test ::
"ch.qos.logback" % "logback-classic" % "1.2.11" % Test ::
Nil
Expand Down
48 changes: 48 additions & 0 deletions src/main/scala/dregex/CompiledRegex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dregex;

import dregex.impl.Compiler;
import dregex.impl.Dfa;
import dregex.impl.RegexTree;
import dregex.impl.SimpleState;
import lombok.NonNull;

/**
* A fully-compiled regular expression that was generated from a string literal.
*/
public class CompiledRegex implements Regex {

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

public CompiledRegex(@NonNull String originalString, @NonNull RegexTree.Node parsedTree, @NonNull Universe universe) {
this._universe = universe;
this._originalString = originalString;
this._parsedTree = parsedTree;
this._dfa = new Compiler(_universe.alphabet()).fromTree(_parsedTree);
}

@Override
public Universe universe() {
return _universe;
}

public String originalString() {
return _originalString;
}

public RegexTree.Node parsedTree() {
return _parsedTree;
}

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

@Override
public String toString() {
return String.format("⟪%s⟫ (DFA states: %s)", _originalString, _dfa.stateCount());
}
}
32 changes: 0 additions & 32 deletions src/main/scala/dregex/CompiledRegex.scala

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/scala/dregex/Regex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import scala.jdk.CollectionConverters._
*/
trait Regex {

private[this] val logger = LoggerFactory.getLogger(classOf[Regex])

private[dregex] def dfa: Dfa[SimpleState]

/**
Expand Down Expand Up @@ -57,6 +55,7 @@ trait Regex {
* the DFA of the operands.
*/
def intersect(other: Regex): Regex = {
val logger = LoggerFactory.getLogger(classOf[Regex])
val (res, time) = Util.time {
checkUniverse(other)
new SynteticRegex(DfaAlgorithms.rewriteWithSimpleStates(DfaAlgorithms.intersect(this.dfa, other.dfa)), universe)
Expand All @@ -71,6 +70,7 @@ trait Regex {
* and m are the number of states of the DFA of the operands.
*/
def diff(other: Regex): Regex = {
val logger = LoggerFactory.getLogger(classOf[Regex])
val (res, time) = Util.time {
checkUniverse(other)
new SynteticRegex(DfaAlgorithms.rewriteWithSimpleStates(DfaAlgorithms.diff(this.dfa, other.dfa)), universe)
Expand All @@ -85,6 +85,7 @@ trait Regex {
* of the operands.
*/
def union(other: Regex): Regex = {
val logger = LoggerFactory.getLogger(classOf[Regex])
val (res, time) = Util.time {
checkUniverse(other)
new SynteticRegex(DfaAlgorithms.rewriteWithSimpleStates(DfaAlgorithms.union(this.dfa, other.dfa)), universe)
Expand Down

0 comments on commit 2853db1

Please sign in to comment.