<commit_before>module Gen.Module
  ( Module(..)
  , Export(..)
  , Body(..)
  , renderModule
  , saveModule
  ) where

import Data.String.Utils
import System.Directory
import System.FilePath
import System.IO
import Text.Printf

data Module = Module
  { moduleName :: String
  , moduleExport :: [Export]
  , moduleBody :: [Body]
  } deriving (Eq, Show)

data Export
  = Section
    { sectionHeading :: String
    , sectionExport :: [String]
    }
  | Subsection
    { sectionHeading :: String
    , sectionExport :: [String]
    } deriving (Eq, Show)

data Body 
  = Import [String]
  | Function String String String
  | Pattern String String String
  | Code String
  deriving (Eq, Show)

renderModule :: Module -> String
renderModule m =
  printf 
    ("-- This file was automatically generated.\n" ++
    "{-# LANGUAGE ScopedTypeVariables, PatternSynonyms #-}\n" ++
    "module %s%s where\n\n%s")
     (moduleName m) (renderExports $ moduleExport m) (join "\n\n" . map renderBody $ moduleBody m)

renderExports :: [Export] -> String
renderExports [] = ""
renderExports exports =
    printf " (\n%s)"
  . join "\n" . map (uncurry renderExport)
  $ zip (True : repeat False) exports
  where
    renderExport :: Bool -> Export -> String
    renderExport first (Section heading export) =
      printf "\t-- * %s\n\t%s %s"
        heading
        (if first then " " else ",")
        ((++"\n") . join "\n\t, " $ export)
    renderExport first (Subsection heading export) =
      printf "\t-- ** %s\n\t%s %s"
        heading
        (if first then " " else ",")
        ((++"\n") . join "\n\t, " $ export)

renderBody :: Body -> String
renderBody body = case body of
  Import m -> join "\n" $ map (printf "import %s") m
  Function name signature body -> printf "%s :: %s\n%s %s" name signature name body
  Pattern name signature body -> printf "pattern %s %s :: %s" name body signature
  Code code -> code

buildDir :: FilePath
buildDir = "dist/build/autogen"

saveModule :: Module -> IO ()
saveModule m = do
  -- putStrLn $ "Saving " ++ moduleName m ++ " to " ++ filePath ++ " in " ++ folderPath
  createDirectoryIfMissing True folderPath
  writeFile filePath $ renderModule m
  where
    filePath = (buildDir </>) . (++".hs") . replace "." [pathSeparator] $ moduleName m
    folderPath = (join [pathSeparator] . init $ split [pathSeparator] filePath)<commit_msg>filter empty sections to avoid spurious (,)'s in the export list<commit_after>module Gen.Module
  ( Module(..)
  , Export(..)
  , Body(..)
  , renderModule
  , saveModule
  ) where

import Data.String.Utils
import System.Directory
import System.FilePath
import System.IO
import Text.Printf

data Module = Module
  { moduleName :: String
  , moduleExport :: [Export]
  , moduleBody :: [Body]
  } deriving (Eq, Show)

data Export
  = Section
    { sectionHeading :: String
    , sectionExport :: [String]
    }
  | Subsection
    { sectionHeading :: String
    , sectionExport :: [String]
    } deriving (Eq, Show)

data Body 
  = Import [String]
  | Function String String String
  | Pattern String String String
  | Code String
  deriving (Eq, Show)

renderModule :: Module -> String
renderModule m =
  printf 
    ("-- This file was automatically generated.\n" ++
    "{-# LANGUAGE ScopedTypeVariables, PatternSynonyms #-}\n" ++
    "module %s%s where\n\n%s")
     (moduleName m) (renderExports $ moduleExport m) (join "\n\n" . map renderBody $ moduleBody m)


renderExports :: [Export] -> String
renderExports [] = ""
renderExports exports =
    printf " (\n%s)"
  . join "\n" . map (uncurry renderExport)
  . zip (True : repeat False)
  $ filter nonEmpty exports
  where
    renderExport :: Bool -> Export -> String
    renderExport first (Section heading export) =
      printf "  -- * %s\n  %s %s"
        heading
        (if first then " " else ",")
        ((++"\n") . join "\n  , " $ export)
    renderExport first (Subsection heading export) =
      printf "  -- ** %s\n  %s %s"
        heading
        (if first then " " else ",")
        ((++"\n") . join "\n  , " $ export)
    nonEmpty :: Export -> Bool
    nonEmpty (Section _ []) = False
    nonEmpty (Subsection _ []) = False
    nonEmpty _ = True

renderBody :: Body -> String
renderBody body = case body of
  Import m -> join "\n" $ map (printf "import %s") m
  Function name signature body -> printf "%s :: %s\n%s %s" name signature name body
  Pattern name signature body -> printf "pattern %s %s :: %s" name body signature
  Code code -> code

buildDir :: FilePath
buildDir = "dist/build/autogen"

saveModule :: Module -> IO ()
saveModule m = do
  -- putStrLn $ "Saving " ++ moduleName m ++ " to " ++ filePath ++ " in " ++ folderPath
  createDirectoryIfMissing True folderPath
  writeFile filePath $ renderModule m
  where
    filePath = (buildDir </>) . (++".hs") . replace "." [pathSeparator] $ moduleName m
    folderPath = (join [pathSeparator] . init $ split [pathSeparator] filePath)
{'fraction_non_alphanumeric': 0.13989426596177307, 'fraction_numerical': 0.0, 'mean_word_length': 2.873228346456693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '5848398', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1521, 'n_words': 490}