Skip to content

Commit

Permalink
build: move internal code to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-vrijswijk committed Dec 12, 2023
1 parent 7a457cc commit 6920fc2
Show file tree
Hide file tree
Showing 62 changed files with 378 additions and 375 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import wrx from 'weapon-regex';
let mutants = wrx.mutate('^abc(d+|[xyz])$', 'u', {
mutators: Array.from(wrx.mutators.values()),
mutationLevels: [1, 2, 3],
flavor: ParserFlavorJS,
flavor: wrx.ParserFlavorJS,
});
```

Expand Down
7 changes: 4 additions & 3 deletions core/src/main/scala/weaponregex/WeaponRegeX.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package weaponregex

import weaponregex.extension.RegexTreeExtension.RegexTreeMutator
import weaponregex.model.mutation.*
import weaponregex.internal.extension.RegexTreeExtension.RegexTreeMutator
import weaponregex.internal.parser.Parser
import weaponregex.model.mutation.{Mutant, TokenMutator}
import weaponregex.mutator.BuiltinMutators
import weaponregex.parser.{Parser, ParserFlavor, ParserFlavorJVM}
import weaponregex.parser.{ParserFlavor, ParserFlavorJVM}

/** The API facade of Weapon regeX for Scala/JVM
*/
Expand Down
80 changes: 80 additions & 0 deletions core/src/main/scala/weaponregex/internal/TokenMutatorSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package weaponregex.internal

import weaponregex.internal.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.model.regextree.{Node, RegexTree}
import weaponregex.model.Location
import weaponregex.model.mutation.{Mutant, TokenMutator as BaseTokenMutator}

/** TokenMutator with syntax trait
*/
private[internal] trait TokenMutator extends BaseTokenMutator with TokenMutatorSyntax

private[internal] trait TokenMutatorSyntax {
self: BaseTokenMutator =>

/** Extension class for a mutated pattern string to convert it into a [[weaponregex.model.mutation.Mutant]] using the
* information of the current token mutator
* @param pattern
* The string pattern to be converted
*/
implicit protected class MutatedPatternExtension(pattern: String) {

/** Convert a mutated pattern string into a [[weaponregex.model.mutation.Mutant]] with the
* [[weaponregex.model.Location]] taken from the provided token
* @param token
* The token for reference
* @param location
* The [[weaponregex.model.Location]] where the mutation occurred. If not provided, this will be taken from the
* provided token.
* @param description
* The description of the mutant. If not provided, the default description of the mutator is used instead.
* @return
* A [[weaponregex.model.mutation.Mutant]]
*/
def toMutantOf(token: RegexTree, location: Option[Location] = None, description: Option[String] = None): Mutant = {
val loc: Location = location.getOrElse(token.location)
val desc: String = description.getOrElse(self.description(token.build, pattern, loc))
Mutant(pattern, name, loc, levels, desc)
}

/** Convert a mutated pattern string into a [[weaponregex.model.mutation.Mutant]] with the
* [[weaponregex.model.Location]] starts from the start of the provided token and ends at the start of the token's
* first child
*
* If the given token has no child, the location of the given token is considered to be the location of the mutant
* @param token
* The token for reference
* @param description
* The description of the mutant. If not provided, the default description of the mutator is used instead.
* @return
* A [[weaponregex.model.mutation.Mutant]]
*/
def toMutantBeforeChildrenOf(token: RegexTree, description: Option[String] = None): Mutant = {
val loc: Location = token match {
case node: Node if node.children.nonEmpty => Location(token.location.start, node.children.head.location.start)
case _ => token.location
}
toMutantOf(token, Some(loc), description)
}

/** Convert a mutated pattern string into a [[weaponregex.model.mutation.Mutant]] with the
* [[weaponregex.model.Location]] starts from the end of the provided token's last child and ends at the end of the
* token
*
* If the given token has no child, the location of the given token is considered to be the location of the mutant
* @param token
* The token for reference
* @param description
* The description of the mutant. If not provided, the default description of the mutator is used instead.
* @return
* A [[weaponregex.model.mutation.Mutant]]
*/
def toMutantAfterChildrenOf(token: RegexTree, description: Option[String] = None): Mutant = {
val loc: Location = token match {
case node: Node if node.children.nonEmpty => Location(node.children.last.location.end, token.location.end)
case _ => token.location
}
toMutantOf(token, Some(loc), description)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package weaponregex.constant
package weaponregex.internal.constant

case object ErrorMessage {
private[weaponregex] case object ErrorMessage {
val errorHeader: String = "[Error]"
val parserErrorHeader: String = s"$errorHeader Parser: "

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package weaponregex.extension
package weaponregex.internal.extension

import weaponregex.extension.TokenMutatorExtension.TokenMutatorsFiltering
import weaponregex.internal.extension.TokenMutatorExtension.TokenMutatorsFiltering
import weaponregex.internal.model.regextree.*
import weaponregex.model.mutation.{Mutant, TokenMutator}
import weaponregex.model.regextree.*
import weaponregex.mutator.BuiltinMutators

object RegexTreeExtension {
private[weaponregex] object RegexTreeExtension {

/** The extension that build a given [[weaponregex.model.regextree.RegexTree]] into a string
/** The extension that build a given [[weaponregex.internal.model.regextree.RegexTree]] into a string
*/
implicit class RegexTreeStringBuilder(tree: RegexTree) {

Expand Down Expand Up @@ -51,7 +51,7 @@ object RegexTreeExtension {
}
}

/** The extension that traverses and mutates a given [[weaponregex.model.regextree.RegexTree]]
/** The extension that traverses and mutates a given [[weaponregex.internal.model.regextree.RegexTree]]
*/
implicit class RegexTreeMutator(tree: RegexTree) {

Expand All @@ -67,7 +67,7 @@ object RegexTreeExtension {
def mutate(mutators: Seq[TokenMutator] = BuiltinMutators.all, mutationLevels: Seq[Int] = null): Seq[Mutant] = {
if (mutationLevels != null) return mutate(mutators.atLevels(mutationLevels))

val rootMutants: Seq[Mutant] = mutators flatMap (_(tree))
val rootMutants: Seq[Mutant] = mutators.flatMap(_.mutate(tree))
val childrenMutants: Seq[Mutant] = tree match {
case node: Node =>
node.children.flatMap { child =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package weaponregex.extension
package weaponregex.internal.extension

import fastparse.internal.Util
import weaponregex.model.*

object StringExtension {
private[weaponregex] object StringExtension {
implicit class StringIndexExtension(string: String) {

/** Convert an index of into row and column numbers in the given string.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package weaponregex.extension
package weaponregex.internal.extension

import weaponregex.model.mutation.TokenMutator

object TokenMutatorExtension {
private[weaponregex] object TokenMutatorExtension {

/** The extension that filter a given sequence of [[weaponregex.model.mutation.TokenMutator]]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down Expand Up @@ -30,7 +30,7 @@ case class NamedGroup(expr: RegexTree, name: String, override val location: Loca

/** Non-capturing group with flags
* @param flagToggle
* The [[weaponregex.model.regextree.FlagToggle]] object associated with the group
* The [[weaponregex.internal.model.regextree.FlagToggle]] object associated with the group
* @param expr
* The regex inside the group
* @param location
Expand All @@ -44,7 +44,7 @@ case class FlagNCGroup(

/** Flag toggle group node
* @param flagToggle
* The [[weaponregex.model.regextree.FlagToggle]] object associated with the group
* The [[weaponregex.internal.model.regextree.FlagToggle]] object associated with the group
* @param location
* The [[weaponregex.model.Location]] of the node in the regex string
*/
Expand All @@ -64,9 +64,9 @@ case class FlagToggleGroup(flagToggle: FlagToggle, override val location: Locati
case class FlagToggle(onFlags: Flags, hasDash: Boolean, offFlags: Flags, override val location: Location)
extends Node(Seq(onFlags, offFlags), location)

/** A sequence of flags for use in [[weaponregex.model.regextree.FlagToggle]]
/** A sequence of flags for use in [[weaponregex.internal.model.regextree.FlagToggle]]
* @param flags
* The sequence of flag [[weaponregex.model.regextree.Character]] s
* The sequence of flag [[weaponregex.internal.model.regextree.Character]] s
* @param location
* The [[weaponregex.model.Location]] of the node in the regex string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand All @@ -11,15 +11,15 @@ sealed abstract class QuantifierType(syntax: String) {
override def toString: String = syntax
}

/** Greedy [[weaponregex.model.regextree.QuantifierType]]
/** Greedy [[weaponregex.internal.model.regextree.QuantifierType]]
*/
case object GreedyQuantifier extends QuantifierType("")

/** Reluctant [[weaponregex.model.regextree.QuantifierType]]
/** Reluctant [[weaponregex.internal.model.regextree.QuantifierType]]
*/
case object ReluctantQuantifier extends QuantifierType("?")

/** Possessive [[weaponregex.model.regextree.QuantifierType]]
/** Possessive [[weaponregex.internal.model.regextree.QuantifierType]]
*/
case object PossessiveQuantifier extends QuantifierType("+")

Expand All @@ -38,7 +38,7 @@ case object PossessiveQuantifier extends QuantifierType("+")
* `true` if used to represent an exact number of repetitions (e.g. {1}), `false` otherwise (e.g. {1,2} or {1,})
* @note
* This class constructor is private, instances must be created using the companion
* [[weaponregex.model.regextree.Quantifier]] object
* [[weaponregex.internal.model.regextree.Quantifier]] object
*/
case class Quantifier protected[weaponregex] (
expr: RegexTree,
Expand All @@ -55,15 +55,16 @@ case class Quantifier protected[weaponregex] (
+ s"}$quantifierType"
)

/** Companion object for [[weaponregex.model.regextree.Quantifier]] class
/** Companion object for [[weaponregex.internal.model.regextree.Quantifier]] class
*/
object Quantifier {

/** Infinity will be represented as negatives, default to -1
*/
val Infinity: Int = -1

/** Exact quantifier (e.g. {1}) factory method Create an exact [[weaponregex.model.regextree.Quantifier]] instance
/** Exact quantifier (e.g. {1}) factory method Create an exact [[weaponregex.internal.model.regextree.Quantifier]]
* instance
* @param expr
* The regex that is being quantified
* @param exact
Expand All @@ -73,7 +74,7 @@ object Quantifier {
* @param quantifierType
* The type of the quantifier: greedy, reluctant, or possessive
* @return
* A [[weaponregex.model.regextree.Quantifier]] instance
* A [[weaponregex.internal.model.regextree.Quantifier]] instance
*/
def apply(
expr: RegexTree,
Expand All @@ -94,7 +95,7 @@ object Quantifier {
* @param quantifierType
* The type of the quantifier: greedy, reluctant, or possessive
* @return
* A [[weaponregex.model.regextree.Quantifier]] instance
* A [[weaponregex.internal.model.regextree.Quantifier]] instance
*/
def apply(
expr: RegexTree,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package weaponregex.model.regextree
package weaponregex.internal.model.regextree

import weaponregex.model.Location

Expand All @@ -19,7 +19,7 @@ sealed trait RegexTree {
def location: Location
}

/** The non-terminal node of the [[weaponregex.model.regextree.RegexTree]] that have at least one child node
/** The non-terminal node of the [[weaponregex.internal.model.regextree.RegexTree]] that have at least one child node
* @param children
* The children that fall under this node
* @param location
Expand All @@ -39,7 +39,7 @@ abstract class Node(
val sep: String = ""
) extends RegexTree

/** The leaf of the [[weaponregex.model.regextree.RegexTree]] (terminal node) that have no children node
/** The leaf of the [[weaponregex.internal.model.regextree.RegexTree]] (terminal node) that have no children node
* @param value
* The value that the leaf holds
* @param location
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package weaponregex.mutator
package weaponregex.internal.mutator

import weaponregex.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.TokenMutator
import weaponregex.internal.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.model.regextree.*
import weaponregex.model.Location
import weaponregex.model.mutation.{Mutant, TokenMutator}
import weaponregex.model.regextree.*
import weaponregex.model.mutation.Mutant

/** Mutator for beginning of line character `^` removal
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package weaponregex.mutator
package weaponregex.internal.mutator

import weaponregex.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.TokenMutator
import weaponregex.internal.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.model.regextree.*
import weaponregex.model.Location
import weaponregex.model.mutation.{Mutant, TokenMutator}
import weaponregex.model.regextree.*
import weaponregex.model.mutation.Mutant

/** Mutator for capturing group to non-capturing group modification
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package weaponregex.mutator
package weaponregex.internal.mutator

import weaponregex.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.TokenMutator
import weaponregex.internal.extension.RegexTreeExtension.RegexTreeStringBuilder
import weaponregex.internal.model.regextree.*
import weaponregex.model.Location
import weaponregex.model.mutation.{Mutant, TokenMutator}
import weaponregex.model.regextree.*
import weaponregex.model.mutation.Mutant

/** Mutator for character class negation
*
Expand Down
Loading

0 comments on commit 6920fc2

Please sign in to comment.