<commit_before>import Text.Parsec hiding ((<|>))
import Data.Functor.Identity (Identity)
import Text.Parsec.String -- Text.Parsec.Text
import Text.Parsec.Expr
import Control.Applicative
import NCTL

type Nctl = NCTL String String

binary :: String -> (a -> a -> a) -> Assoc -> Operator String () Identity a
binary name fun assoc = Infix (do { string name; return fun }) assoc

exprP :: OperatorTable String () Identity a -> Parser a -> Parser a
exprP table factor = buildExpressionParser table factor
                  <?> "expression"

expr :: Parser (NCTL String String)
expr = exprP table factor

exprNested :: Parser (NNCTL String String)
exprNested = exprP tableNested factorNested

table = [ [binary "&&" nctlAnd AssocLeft]
        , [binary "||" NCTLOr  AssocLeft]]
  where nctlAnd a b = NCTLNot $ NCTLOr (NCTLNot a) (NCTLNot b)

tableNested = [ [binary "&&" nctlAnd AssocLeft]
              , [binary "||" NNCTLOr  AssocLeft]]
  where nctlAnd a b = NNCTLNot $ NNCTLOr (NNCTLNot a) (NNCTLNot b)        
        
factor :: Parser (NCTL String String)
factor = spaces *> value <* spaces
  where value = choice [ NCTLTrue <$ string "true"
                       , NCTLFalse <$ string "false"
                       , NCTLNot <$> (char '~' *> factor)
                       , tempModalities
                       , (char '(' *> spaces) *> expr <* (spaces <* char ')')
                       , fmap Nmod $ (char '[' *> spaces) *> exprNested <* (spaces <* char ']') ]
        tempModalities = exParser <|> euParser

factorNested :: Parser (NNCTL String String)
factorNested = spaces *> value <* spaces
  where value = choice [ NNCTLTrue <$ string "true"
                       , NNCTLFalse <$ string "false"
                       , NNCTLNot <$> (char '~' *> factorNested)
                       , tempModalities
                       , (char '(' *> spaces) *> exprNested <* (spaces <* char ')')]
        tempModalities = exParserN <|> euParserN

-- TODO: any tricks for code reuse?
exParser :: Parser (NCTL String String)                     
exParser = do  
  try $ string "EX"
  spaces >> char '(' 
  e <- expr
  spaces >> char ')'
  return $ EX e

euParser :: Parser (NCTL String String)
euParser = do
  try $ string "EU"
  spaces >> char '('
  e1 <- expr
  spaces >> char ',' >> spaces
  e2 <- expr
  spaces >> char ')'
  return $ EU e1 e2

exParserN :: Parser (NNCTL String String)                     
exParserN = do  
  try $ string "EX"
  spaces >> char '(' 
  e <- exprNested
  spaces >> char ')'
  return $ NEX e

euParserN :: Parser (NNCTL String String)
euParserN = do
  try $ string "EU"
  spaces >> char '('
  e1 <- exprNested
  spaces >> char ',' >> spaces
  e2 <- exprNested
  spaces >> char ')'
  return $ NEU e1 e2<commit_msg>Fixing & generalizing the parser<commit_after>import Text.Parsec hiding ((<|>))
import Data.Functor.Identity (Identity)
import Text.Parsec.String -- Text.Parsec.Text
import Text.Parsec.Expr
import Control.Applicative
import NCTL

binary :: String -> (a -> a -> a) -> Assoc -> Operator String () Identity a
binary name fun assoc = Infix (do { string name; return fun }) assoc

exprP :: OperatorTable String () Identity a -> Parser a -> Parser a
exprP table factor = buildExpressionParser table factor
                  <?> "expression"

expr :: Parser NCTL
expr = exprP table factor

table = [ [binary "&&" nctlAnd AssocLeft]
        , [binary "||" NCTLOr  AssocLeft]]
  where nctlAnd a b = NCTLNot $ NCTLOr (NCTLNot a) (NCTLNot b)

factor :: Parser NCTL
factor = spaces *> value <* spaces
  where value = choice [ NCTLTrue <$ string "true"
                       , NCTLFalse <$ string "false"
                       , NCTLNot <$> (char '~' *> factor)
                       , tempModalitiesAr1
                       , tempModalitiesAr2
                       , (char '(' *> spaces) *> expr <* (spaces <* char ')')
                       , fmap NMod $ (char '[' *> spaces) *> expr <* (spaces <* char ']') ]
        tempModalitiesAr1 = foldl1 (<|>) $ map (uncurry ar1Parser) (fst tempModalitiesTable)
        tempModalitiesAr2 = foldl1 (<|>) $ map (uncurry ar2Parser) (snd tempModalitiesTable)

tempModalitiesTable = ( [("EX", EX), ("EF", ef), ("AF", af), ("EG", eg), ("AG", ag)]
                      , [("EU", EU), ("AU", AU)])

  
ar1Parser :: String -> (NCTL -> NCTL) -> Parser NCTL  
ar1Parser s constr = do
  try $ string s
  spaces >> char '('
  e <- expr
  spaces >> char ')'
  return $ constr e

ar2Parser :: String -> (NCTL -> NCTL -> NCTL) -> Parser NCTL
ar2Parser s constr = do
  try $ string s
  spaces >> char '('
  e1 <- expr
  spaces >> char ',' >> spaces
  e2 <- expr
  spaces >> char ')'
  return $ constr e1 e2

{'fraction_non_alphanumeric': 0.13705366688047893, 'fraction_numerical': 0.005131494547787043, 'mean_word_length': 2.355810616929699, 'pattern_counts': {'":': 0, '<': 42, '<?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': '6560184', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1469, 'n_words': 461}