packages feed

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

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

-- | Canonical printer for Hydra's translingual regular-expression syntax (docs/specification/regex.md): hydra.core.regex AST -> canonical text. Escapes minimally (a character is escaped only where it is special in its context), so that print.regex . parse.regex is idempotent on well-formed input. Per-dialect renderers (hydra.core.print.<dialect>.regex) build on this. See issue #567.

module Hydra.Core.Print.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.
alternation :: [[Regex.Quantified]] -> String
alternation alt = Strings.join "|" (Lists.map regexSequence alt)

-- | Render a single atom (literal, ., anchor, group, or class) to canonical text.
atom :: Regex.Atom -> String
atom a =
    case a of
      Regex.AtomLiteral v0 -> escapeLiteral v0
      Regex.AtomAny -> "."
      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) to canonical text.
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, backslash-escaping it iff it is one of the class metacharacters \ ] ^ -. Escaping is uniform, with no positional exceptions (a literal '-' always prints as \-).
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 it iff it is one of the top-level metacharacters . ^ $ * + ? ( ) [ ] { } | \. A '-' is not special at top level and prints bare.
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 to canonical Hydra regex text. Escapes minimally, so that printRegex . parseRegex is the identity on well-formed patterns.
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)