<commit_before>{-# LANGUAGE FlexibleContexts #-}
module Main where

import System.Environment (getArgs)
import FrontEnd.RuleParser
import BackEnd.CPP
import Control.Applicative ((<$>))
import Core.Partition (PartitionL)

main :: IO ()
main = do rulesFile <- head <$> getArgs
          rulesStr <- readFile rulesFile
          let parsed = parseRules rulesStr
          case parsed of
            Left errMsg -> putStrLn $ "Parsing of rules was not successful: "
                                      ++ show errMsg
            Right rules -> do
              let rules' = map pRule (rules :: [ParsedRule PartitionL])
              putStrLn ""
              putStrLn $ generateCode 800 rules'

{-
This can be useful for debugging:

infixl 9 !!!

(!!!) :: (U.IArray UArray b, Ix a) => UArray a b -> a -> b<commit_msg>Processing of command line arguments.<commit_after>{-# LANGUAGE FlexibleContexts, 
             DeriveDataTypeable #-}
module Main where

import FrontEnd.RuleParser
import BackEnd.CPP
import Core.Partition (PartitionL)
import System.Console.CmdArgs

data CrepArgs = CArgs {
                        maxWordLen :: Integer
                      , rulesFile :: String
                      , outputFile :: String
                      }
              deriving (Show, Data, Typeable)

crepArgs :: Mode CrepArgs
crepArgs
  = mode $ CArgs {
                   maxWordLen = 1024
                                &= text "Maximal length of matching words"
                                & explicit
                                & flag "k"
                                & typ "NUMBER"
                 , rulesFile = def
                               &= typ "rules-file"
                               & argPos 0
                               & text "a"
                 , outputFile = def
                                &= typ "output-file"
                                & argPos 1
                                & text "b"
                 } &= text ("Generates C++ program for processing text " ++
                            "according to the given rules.")

wordLenBnds :: (Integer, Integer)
wordLenBnds = (1, 512 * 1024)

main :: IO ()
main = do a <- cmdArgs "crep 0.1, (C) 2009-2010 <NAME>" [crepArgs]
          if maxWordLen a < fst wordLenBnds || maxWordLen a > snd wordLenBnds
            then putStrLn $"Maximal length of matching words must be between "
                           ++ show (fst wordLenBnds) ++ " and "
                           ++ show (snd wordLenBnds)
            else do
              rulesStr <- readFile (rulesFile a)
              let parsed = parseRules rulesStr
              case parsed of
                Left errMsg
                  -> putStrLn $"Parsing of rules was not successful: "
                               ++ show errMsg
                Right rs
                  -> do let rules = map pRule (rs :: [ParsedRule PartitionL])
                        writeFile (outputFile a)
                          $ generateCode (fromInteger $ maxWordLen a) rules

{-
This can be useful for debugging:

infixl 9 !!!

(!!!) :: (U.IArray UArray b, Ix a) => UArray a b -> a -> b
{'fraction_non_alphanumeric': 0.0854126679462572, 'fraction_numerical': 0.009277031349968011, 'mean_word_length': 1.3162962962962963, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2569050', 'n_tokens_mistral': 848, 'n_tokens_neox': 794, 'n_words': 263}