<commit_before>module Network.IPTables.IpassmtParser
( parseIpAssmt
, IpAssmt
, IsabelleIpAssmt
, ipAssmtToIsabelle) where

import           Text.Parsec hiding (token)
import           Data.Functor ((<$>), ($>))
import           Control.Applicative ((<*), (*>))
import           Network.IPTables.Ipassmt
import qualified Network.IPTables.Generated as Isabelle
import           Network.IPTables.ParserHelper
import           Network.IPTables.IsabelleToString(Word32)

ipAssmtToIsabelle:: IpAssmt -> IsabelleIpAssmt
ipAssmtToIsabelle (IpAssmt assmt) = Isabelle.to_ipassmt assmt

parseIpAssmt :: SourceName -> String -> Either ParseError IpAssmt
parseIpAssmt = runParser ifconfig ()

ifconfig :: Parsec String s IpAssmt
ifconfig = IpAssmt <$> many (skipWS *> ipAssmt <* skipWS)

ipAssmt :: Parsec String s (Isabelle.Iface, Isabelle.Negation_type [Isabelle.Ipt_iprange Word32])
ipAssmt = do
    ifce <- iface
    skipWS *> char '=' <* skipWS
    rng <- choice [try neg, try pos]
    return (ifce, rng)
        where pos = do rng <- ipRange
                       return (Isabelle.Pos rng)
              neg = do skipWS *> string "all_but_those_ips" <* skipWS
                       rng <- ipRange
                       return (Isabelle.Neg rng)

ipRange :: Parsec String s [Isabelle.Ipt_iprange Word32]
ipRange = enclosedList '[' ips ']'
    where ips = choice [try ipv4cidr, try ipv4range, try ipv4addr]

enclosedList :: Char -> Parsec String s a -> Char -> Parsec String s [a]
enclosedList left parser right = between (char left <* skipWS) (char right) $ (parser <* skipWS) `sepBy` (char ',' <* skipWS)

skipWS :: Parsec String s ()
skipWS = (many $ oneOf " \t\n") $> ()
<commit_msg>FlexibleInstances<commit_after>module Network.IPTables.IpassmtParser
( parseIpAssmt
, IpAssmt
, IsabelleIpAssmt
, ipAssmtToIsabelle) where

import           Text.Parsec hiding (token)
import           Data.Functor ((<$>), ($>))
import           Control.Applicative ((<*), (*>))
import           Network.IPTables.Ipassmt
import qualified Network.IPTables.Generated as Isabelle
import           Network.IPTables.ParserHelper
import           Network.IPTables.IsabelleToString(Word32)

ipAssmtToIsabelle:: IpAssmt Word32 -> IsabelleIpAssmt
ipAssmtToIsabelle (IpAssmt assmt) = Isabelle.to_ipassmt assmt

-- returns IpAssmt instead of IsabelleIpAssmt because we can only show the former
parseIpAssmt :: SourceName -> String -> Either ParseError (IpAssmt Word32)
parseIpAssmt = runParser ifconfig ()

ifconfig :: Parsec String s (IpAssmt Word32)
ifconfig = IpAssmt <$> many (skipWS *> ipAssmt <* skipWS)

ipAssmt :: Parsec String s (Isabelle.Iface, Isabelle.Negation_type [Isabelle.Ipt_iprange Word32])
ipAssmt = do
    ifce <- iface
    skipWS *> char '=' <* skipWS
    rng <- choice [try neg, try pos]
    return (ifce, rng)
        where pos = do rng <- ipRange
                       return (Isabelle.Pos rng)
              neg = do skipWS *> string "all_but_those_ips" <* skipWS
                       rng <- ipRange
                       return (Isabelle.Neg rng)

ipRange :: Parsec String s [Isabelle.Ipt_iprange Word32]
ipRange = enclosedList '[' ips ']'
    where ips = choice [try ipv4cidr, try ipv4range, try ipv4addr]

enclosedList :: Char -> Parsec String s a -> Char -> Parsec String s [a]
enclosedList left parser right = between (char left <* skipWS) (char right) $ (parser <* skipWS) `sepBy` (char ',' <* skipWS)

skipWS :: Parsec String s ()
skipWS = (many $ oneOf " \t\n") $> ()

{'fraction_non_alphanumeric': 0.1093525179856115, 'fraction_numerical': 0.006906474820143885, 'mean_word_length': 3.094228504122497, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '1556520', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1148, 'n_words': 313}