<commit_before>{-# LANGUAGE NoImplicitPrelude #-}

module Cinder.DSL
    (at
    ,el
    ,co
    ,markup
    ,pretty
    ,(!)
    ,(!:)
    ,(!<)
    ,(!<<)
    ,(!>)
    ,(!>>)
    ,(!+)
    ,(!<+)
    ,Primitive(..)
    ,Markup
    ,DString
    ,atN
    ,atP
    ) where

import Prelude

type DString = String

data Primitive = Content DString
               | Element DString
               | Attribute DString DString
               | Complete
    deriving Show

type Markup = [Primitive]

markup :: Markup
markup = []

infixl 2 !
(!) :: Markup -> Primitive -> Markup
(!) = flip (:)

infixl 2 !:
(!:) :: Markup -> DString -> Markup
m !: v = m ! Content v

infixl 2 !>
(!>) :: Markup -> Primitive -> Markup
(!>) = (!)

infixl 2 !>>
(!>>) :: Markup -> DString -> Markup
m !>> t = m ! Element t

infixl 2 !<
(!<) :: Markup -> Primitive -> Markup
m !< Complete = closeAll m
m !< t = (m ! Complete) ! t

infixl 2 !<<
(!<<) :: Markup -> DString -> Markup
m !<< t = m !< Element t

infixl 2 !+
(!+) :: Markup -> Markup -> Markup
(!+) = flip (++)

infixl 2 !<+
(!<+) :: Markup -> Markup -> Markup
m !<+ t = (m ! Complete) !+ t

closeAll :: Markup -> Markup
closeAll m = if n >= 0 then replicate n Complete ++ m
                       else error "Markup has more close elements then start"
    where n = foldr nestLevel 0 m

at :: DString -> DString -> Primitive
at = Attribute

atN :: DString -> Double -> Primitive
atN t v = Attribute t (show v)

atP :: DString -> Double -> Primitive
atP t v = Attribute t (show v ++"%")

co :: DString -> Primitive
co = Content

el :: DString -> Primitive
el = Element

pretty :: Markup -> String
pretty m = concat $ zipWith cat ins rm
    where rm = reverse m
          ins = scanl (flip nestLevel) 0 rm
          cat i x = replicate (i * 4) ' ' ++ go x ++ "\n"
          go (Element   x)   = "<" ++ x ++ ">"
          go (Attribute x y) = x ++ "=\"" ++ y ++ "\""
          go (Content   x)   = "\"" ++ x ++ "\""
          go (Complete)      = "<--"

nestLevel :: Primitive -> Int -> Int
nestLevel (Element _) x = x + 1
nestLevel (Complete)  x = x - 1
nestLevel _           x = x<commit_msg>switch operator precedence and fix grammar<commit_after>{-# LANGUAGE NoImplicitPrelude #-}

module Cinder.DSL
    (at
    ,el
    ,co
    ,markup
    ,pretty
    ,(!)
    ,(!:)
    ,(!<)
    ,(!<<)
    ,(!>)
    ,(!>>)
    ,(!+)
    ,(!<+)
    ,Primitive(..)
    ,Markup
    ,DString
    ,atN
    ,atP
    ) where

import Prelude

type DString = String

data Primitive = Content DString
               | Element DString
               | Attribute DString DString
               | Complete
    deriving Show

type Markup = [Primitive]

markup :: Markup
markup = []

infixl 5 !
(!) :: Markup -> Primitive -> Markup
(!) = flip (:)

infixl 5 !:
(!:) :: Markup -> DString -> Markup
m !: v = m ! Content v

infixl 5 !>
(!>) :: Markup -> Primitive -> Markup
(!>) = (!)

infixl 5 !>>
(!>>) :: Markup -> DString -> Markup
m !>> t = m ! Element t

infixl 5 !<
(!<) :: Markup -> Primitive -> Markup
m !< Complete = closeAll m
m !< t = (m ! Complete) ! t

infixl 5 !<<
(!<<) :: Markup -> DString -> Markup
m !<< t = m !< Element t

infixl 5 !+
(!+) :: Markup -> Markup -> Markup
(!+) = flip (++)

infixl 5 !<+
(!<+) :: Markup -> Markup -> Markup
m !<+ t = (m ! Complete) !+ t

closeAll :: Markup -> Markup
closeAll m = if n >= 0 then replicate n Complete ++ m
                       else error "Markup has more close elements than open"
    where n = foldr nestLevel 0 m

at :: DString -> DString -> Primitive
at = Attribute

atN :: DString -> a -> Primitive
atN t v = Attribute t (show v)

atP :: DString -> Double -> Primitive
atP t v = Attribute t (show v ++ "%")

co :: DString -> Primitive
co = Content

el :: DString -> Primitive
el = Element

pretty :: Markup -> String
pretty m = concat $ zipWith cat ins rm
    where rm = reverse m
          ins = scanl (flip nestLevel) 0 rm
          cat i x = replicate (i * 4) ' ' ++ go x ++ "\n"
          go (Element   x)   = "<" ++ x ++ ">"
          go (Attribute x y) = x ++ "=\"" ++ y ++ "\""
          go (Content   x)   = "\"" ++ x ++ "\""
          go (Complete)      = "<--"

nestLevel :: Primitive -> Int -> Int
nestLevel (Element _) x = x + 1
nestLevel (Complete)  x = x - 1
nestLevel _           x = x
{'fraction_non_alphanumeric': 0.17883895131086142, 'fraction_numerical': 0.006554307116104869, 'mean_word_length': 2.0718907260963335, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2924366', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1516, 'n_words': 480}