packages feed

hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Print/Pcre/Regex.hs

-- Note: this is an automatically generated file. Do not edit.

-- | Per-dialect printer rendering the hydra.core.regex AST into the PCRE / java.util.regex / ECMA family syntax (Java, Scala, Clojure, Python, Common Lisp, TypeScript). Differs from the canonical hydra.core.print.regex in exactly one place: Hydra's . (any character INCLUDING newline) renders as [\s\S] rather than a bare . (whose native meaning excludes newline in these engines). All other productions match the canonical form. See docs/specification/regex.md and issue #567.

module Hydra.Core.Print.Pcre.Regex where

import qualified Hydra.Core.Overlay.Haskell.Lib.Equality as Equality
import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Core.Overlay.Haskell.Lib.Literals as Literals
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Regex as Regex
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void

-- | Render an alternation, joining its branches with the | operator. Branches are ordered by descending rendered length so that PCRE/ECMA's leftmost-first matching agrees with Hydra's leftmost-longest semantics for fixed-length alternatives.
alternation :: [[Regex.Quantified]] -> String
alternation alt =

      let rendered = Lists.map regexSequence alt
          byLength = Lists.sortBy Strings.length rendered
      in (Strings.join "|" (Lists.reverse byLength))

-- | Render a single atom; the . metacharacter renders as [\s\S] to preserve newline-inclusive 'any'.
atom :: Regex.Atom -> String
atom a =
    case a of
      Regex.AtomLiteral v0 -> escapeLiteral v0
      Regex.AtomAny -> "[\\s\\S]"
      Regex.AtomAnchorStart -> "^"
      Regex.AtomAnchorEnd -> "$"
      Regex.AtomGroup v0 -> Strings.concat [
        "(",
        (alternation v0),
        ")"]
      Regex.AtomClass v0 -> characterClass v0

-- | Render a character class, including the leading ^ for a negated class.
characterClass :: Regex.CharacterClass -> String
characterClass cc =
    Strings.concat [
      "[",
      (Logic.ifElse (Regex.characterClassNegated cc) "^" ""),
      (Strings.concat (Lists.map classItem (Regex.characterClassItems cc))),
      "]"]

-- | Render one character-class member (a single character or an inclusive range).
classItem :: Regex.ClassItem -> String
classItem item =
    case item of
      Regex.ClassItemCharacter v0 -> escapeClassChar v0
      Regex.ClassItemRange v0 -> Strings.concat [
        escapeClassChar (Regex.characterRangeFrom v0),
        "-",
        (escapeClassChar (Regex.characterRangeTo v0))]

-- | Render a code point inside a character class, uniformly escaping the class metacharacters \ ] ^ -.
escapeClassChar :: Int -> String
escapeClassChar c =

      let isMeta =
              Lists.foldl (\acc -> \m -> Logic.or acc (Equality.equal c m)) False [
                92,
                93,
                94,
                45]
      in (Logic.ifElse isMeta (Strings.concat2 "\\" (printCodePoint c)) (printCodePoint c))

-- | Render a literal code point in top-level context, backslash-escaping PCRE/ECMA metacharacters.
escapeLiteral :: Int -> String
escapeLiteral c =

      let isMeta =
              Lists.foldl (\acc -> \m -> Logic.or acc (Equality.equal c m)) False [
                46,
                94,
                36,
                42,
                43,
                63,
                40,
                41,
                91,
                93,
                123,
                125,
                124,
                92]
      in (Logic.ifElse isMeta (Strings.concat2 "\\" (printCodePoint c)) (printCodePoint c))

-- | Render a single Unicode code point as a one-character string.
printCodePoint :: Int -> String
printCodePoint c = Strings.fromList [
  c]

-- | Render a hydra.core.regex AST into PCRE / java.util.regex / ECMA syntax. Identical to the canonical printer except that . (any incl. newline) becomes [\s\S].
printRegex :: [[Regex.Quantified]] -> String
printRegex r = alternation r

-- | Render an atom followed by its quantifier suffix.
quantified :: Regex.Quantified -> String
quantified qa = Strings.concat2 (atom (Regex.quantifiedAtom qa)) (quantifier (Regex.quantifiedQuantifier qa))

-- | Render a quantifier suffix; the 'one' quantifier renders as the empty string.
quantifier :: Regex.Quantifier -> String
quantifier q =
    case q of
      Regex.QuantifierOne -> ""
      Regex.QuantifierZeroOrOne -> "?"
      Regex.QuantifierZeroOrMore -> "*"
      Regex.QuantifierOneOrMore -> "+"
      Regex.QuantifierExactly v0 -> Strings.concat [
        "{",
        (Literals.printInt32 v0),
        "}"]
      Regex.QuantifierAtLeast v0 -> Strings.concat [
        "{",
        (Literals.printInt32 v0),
        ",}"]
      Regex.QuantifierRange v0 -> Strings.concat [
        "{",
        (Literals.printInt32 (Regex.rangeMin v0)),
        ",",
        (Literals.printInt32 (Regex.rangeMax v0)),
        "}"]

-- | Render a sequence of quantified atoms by concatenation.
regexSequence :: [Regex.Quantified] -> String
regexSequence s = Strings.concat (Lists.map quantified s)