{-# OPTIONS_GHC -threaded -rtsopts #-}
{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE BangPatterns #-}

-- |
-- Module      :  Phonetic.Languages.General.Parsing
-- Copyright   :  (c) OleksandrZhabenko 2021
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
-- The additional parsing library functions for the lineVariantsG3 executable.
-- Is taken from the Phonetic.Languages.Parsing module from the
-- @phonetic-languages-simplified-examples-array@ package to reduce dependencies in general case.
-- 

module Phonetic.Languages.General.Parsing (
  -- * Predicates
  isClosingCurlyBracket
  , isSlash
  , isOpeningCurlyBracket
  , variations
  -- * Transformations
  , breakGroupOfStrings
  , breakInSlashes
  , combineVariants
  , combineHeadsWithNexts
  , transformToVariations
) where

isClosingCurlyBracket :: String -> Bool
isClosingCurlyBracket :: String -> Bool
isClosingCurlyBracket = (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"}")

isSlash :: String -> Bool
isSlash :: String -> Bool
isSlash (Char
x:String
xs)
 | Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'/' = Bool
False
 | String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
xs = Bool
True
 | Bool
otherwise = Bool
False
isSlash String
_ = Bool
False

isOpeningCurlyBracket :: String -> Bool
isOpeningCurlyBracket :: String -> Bool
isOpeningCurlyBracket = (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"{")

breakGroupOfStrings :: [String] -> (([String],[[String]]),[String])
breakGroupOfStrings :: [String] -> (([String], [[String]]), [String])
breakGroupOfStrings ![String]
xss = (([String]
tss,[String] -> [[String]] -> [[String]]
breakInSlashes [String]
uss []), Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String]
zss)
  where (![String]
yss,![String]
zss) = (String -> Bool) -> [String] -> ([String], [String])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break String -> Bool
isClosingCurlyBracket [String]
xss
        (![String]
tss,![String]
uss) = (\([String]
t1,[String]
t2) -> ([String]
t1,Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String]
t2)) (([String], [String]) -> ([String], [String]))
-> ([String] -> ([String], [String]))
-> [String]
-> ([String], [String])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> ([String], [String])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break String -> Bool
isOpeningCurlyBracket ([String] -> ([String], [String]))
-> [String] -> ([String], [String])
forall a b. (a -> b) -> a -> b
$ [String]
yss

breakInSlashes :: [String] -> [[String]] -> [[String]]
breakInSlashes :: [String] -> [[String]] -> [[String]]
breakInSlashes ![String]
wss ![[String]]
usss
 | [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
lss = [String]
kss [String] -> [[String]] -> [[String]]
forall a. a -> [a] -> [a]
: [[String]]
usss
 | Bool
otherwise = [String] -> [[String]] -> [[String]]
breakInSlashes (Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String]
lss) ([String]
kss [String] -> [[String]] -> [[String]]
forall a. a -> [a] -> [a]
: [[String]]
usss)
  where (![String]
kss,![String]
lss) = (String -> Bool) -> [String] -> ([String], [String])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break String -> Bool
isSlash [String]
wss

combineVariants :: ([String],[[String]]) -> [[String]]
combineVariants :: ([String], [[String]]) -> [[String]]
combineVariants (![String]
xss, (![String]
yss:[[String]]
ysss)) = ([String]
xss [String] -> [String] -> [String]
forall a. Monoid a => a -> a -> a
`mappend` [String]
yss) [String] -> [[String]] -> [[String]]
forall a. a -> [a] -> [a]
: ([String], [[String]]) -> [[String]]
combineVariants ([String]
xss, [[String]]
ysss)
combineVariants ([String], [[String]])
_ = []

combineHeadsWithNexts :: [[String]] -> [String] -> [[String]]
combineHeadsWithNexts :: [[String]] -> [String] -> [[String]]
combineHeadsWithNexts ![[String]]
xsss ![String]
yss
 | [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
yss = [[String]]
xsss
 | Bool
otherwise = [[String]] -> [String] -> [[String]]
combineHeadsWithNexts [[String]
xss [String] -> [String] -> [String]
forall a. Monoid a => a -> a -> a
`mappend` [String]
zss | [String]
xss <- [[String]]
xsss, [String]
zss <- [[String]]
zsss] [String]
uss
     where (!([String], [[String]])
t,![String]
uss) = [String] -> (([String], [[String]]), [String])
breakGroupOfStrings [String]
yss
           !zsss :: [[String]]
zsss = ([String], [[String]]) -> [[String]]
combineVariants ([String], [[String]])
t

transformToVariations :: [String] -> [[String]]
transformToVariations :: [String] -> [[String]]
transformToVariations ![String]
yss
 | [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
yss = []
 | Bool
otherwise = [[String]] -> [String] -> [[String]]
combineHeadsWithNexts [[String]]
xsss [String]
tss
  where (!([String], [[String]])
y,![String]
tss) = [String] -> (([String], [[String]]), [String])
breakGroupOfStrings [String]
yss
        !xsss :: [[String]]
xsss = ([String], [[String]]) -> [[String]]
combineVariants ([String], [[String]])
y

variations :: [String] -> Bool
variations :: [String] -> Bool
variations [String]
xss 
 | (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any String -> Bool
isSlash [String]
xss = if (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any String -> Bool
isOpeningCurlyBracket [String]
xss Bool -> Bool -> Bool
&& (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any String -> Bool
isClosingCurlyBracket [String]
xss then Bool
True else Bool
False
 | Bool
otherwise = Bool
False