irrec

irrec

  • Docs
  • API
  • GitHub

›getting started

getting started

  • overview
  • getting irrec

usage

  • examples
  • regex explorer

more information

  • warnings and limitations
  • performance
  • inspiration and credits

overview

Irrec allows you to build, match, and generate regular expressions with a functional API.

The name is a shameless rip-off of irreg, which this library was inspired by. The rec in irrec originall came from recursion schemes. The current version does not use recursion schemes and is based off of the Haskell regex-applicative library.

creating regular expressions

import ceedubs.irrec.regex._, char._
import ceedubs.irrec.parse.{regex => r}
import ceedubs.irrec.regex.Greediness._
import cats.implicits._

sealed abstract class Mood
case object Happy extends Mood
case object Tired extends Mood
case object Feisty extends Mood

case class Animals(count: Int, mood: Mood, kind: String)

// RegexC is a regular expression that matches against a sequence of input `C`haracters
val animal: RegexC[String] = r("(b|c|r|gn)at")

val mood: RegexC[Mood] = r("happy").as[Mood](Happy) | r("tired").as(Tired) |
  r("feisty").as(Feisty)

val animalsR: RegexC[Animals] =
  (digit <* horizontalWhitespaceChar.repeat(1, Some(2), Greedy),
  mood <* horizontalWhitespaceChar.repeat(1, Some(2), NonGreedy),
  animal <* r("s?")
  ).mapN(Animals.apply)
animalsR.pprint
// res0: String = "[0-9][\\t\\u0020]{1,2}(happy|tired|feisty)[\\t\\u0020]{1,2}?(b|c|r|gn)ats?"

parsing with a regular expression

val animals: ParseState[Char, Animals] = animalsR.compile
animals.parseOnlyS("1 tired bat")
// res1: Option[Animals] = Some(
//   value = Animals(count = 1, mood = Tired, kind = "bat")
// )
animals.parseOnlyS("7 feisty cats")
// res2: Option[Animals] = Some(
//   value = Animals(count = 7, mood = Feisty, kind = "cat")
// )
animals.parseOnlyS("3 expensive toasters")
// res3: Option[Animals] = None

generating data that matches a regular expression

import ceedubs.irrec.regex.gen.CharRegexGen.genRegexMatchingString
import org.scalacheck.Gen
import org.scalacheck.rng.Seed

val phraseGen: Gen[String] = genRegexMatchingString(animalsR)
Gen.listOfN(3, phraseGen).apply(Gen.Parameters.default, Seed(79813L))
// res4: Option[List[String]] = Some(
//   value = List("0\ttired\tcats", "6 feisty  gnat", "1 tired\tcat")
// )
getting irrec →
  • creating regular expressions
  • parsing with a regular expression
  • generating data that matches a regular expression
irrec
Getting StartedGuideRegular Expression Explorer
GitHubmore information
Copyright © 2021 Cody Allen