module Data.Express.Utils.String
( module Data.Char
, unquote
, atomic
, outernmostPrec
, isNegativeLiteral
, isInfix, isPrefix, isInfixedPrefix
, toPrefix
, prec
, variableNamesFromTemplate
, primeCycle
)
where
import Data.Char
unquote :: String -> String
unquote :: String -> String
unquote (Char
'"':String
s) | forall a. [a] -> a
last String
s forall a. Eq a => a -> a -> Bool
== Char
'"' = forall a. [a] -> [a]
init String
s
unquote String
s = String
s
atomic :: String -> Bool
atomic :: String -> Bool
atomic String
s | forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace) String
s = Bool
True
atomic (Char
'\'':String
s) | forall a. [a] -> a
last String
s forall a. Eq a => a -> a -> Bool
== Char
'\'' = Bool
True
atomic (Char
'"':String
s) | forall a. [a] -> a
last String
s forall a. Eq a => a -> a -> Bool
== Char
'"' = Bool
True
atomic (Char
'[':String
s) | forall a. [a] -> a
last String
s forall a. Eq a => a -> a -> Bool
== Char
']' = Bool
True
atomic (Char
'(':String
s) | forall a. [a] -> a
last String
s forall a. Eq a => a -> a -> Bool
== Char
')' = Bool
True
atomic String
_ = Bool
False
outernmostPrec :: String -> Maybe Int
outernmostPrec :: String -> Maybe Int
outernmostPrec String
s =
case String -> [String]
words String
s of
[String
l,String
o,String
r] | String -> Bool
isInfix String
o -> forall a. a -> Maybe a
Just (String -> Int
prec String
o)
[String]
_ -> forall a. Maybe a
Nothing
isNegativeLiteral :: String -> Bool
isNegativeLiteral :: String -> Bool
isNegativeLiteral String
s | Bool -> Bool
not (String -> Bool
atomic String
s) = Bool
False
isNegativeLiteral String
"-" = Bool
False
isNegativeLiteral (Char
'-':String
cs) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
cs
isNegativeLiteral String
_ = Bool
False
isInfix :: String -> Bool
isInfix :: String -> Bool
isInfix (Char
c:String
_) = Char
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"()'\"[_" Bool -> Bool -> Bool
&& Bool -> Bool
not (Char -> Bool
isAlphaNum Char
c)
isInfix String
"" = forall a. HasCallStack => String -> a
error String
"isInfix: empty string"
prec :: String -> Int
prec :: String -> Int
prec String
" " = Int
10
prec String
"!!" = Int
9
prec String
"." = Int
9
prec String
"^" = Int
8
prec String
"^^" = Int
8
prec String
"**" = Int
8
prec String
"*" = Int
7
prec String
"/" = Int
7
prec String
"%" = Int
7
prec String
"+" = Int
6
prec String
"-" = Int
6
prec String
":" = Int
5
prec String
"++" = Int
5
prec String
"\\" = Int
5
prec String
">" = Int
4
prec String
"<" = Int
4
prec String
">=" = Int
4
prec String
"<=" = Int
4
prec String
"==" = Int
4
prec String
"/=" = Int
4
prec String
"`elem`" = Int
4
prec String
"&&" = Int
3
prec String
"||" = Int
2
prec String
">>=" = Int
1
prec String
">>" = Int
1
prec String
">=>" = Int
1
prec String
"<=<" = Int
1
prec String
"$" = Int
0
prec String
"`seq`" = Int
0
prec String
"==>" = Int
0
prec String
"<==>" = Int
0
prec String
_ = Int
9
isPrefix :: String -> Bool
isPrefix :: String -> Bool
isPrefix = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
isInfix
isInfixedPrefix :: String -> Bool
isInfixedPrefix :: String -> Bool
isInfixedPrefix String
s | Bool -> Bool
not (String -> Bool
atomic String
s) = Bool
False
isInfixedPrefix (Char
'`':String
cs) = forall a. [a] -> a
last String
cs forall a. Eq a => a -> a -> Bool
== Char
'`'
isInfixedPrefix String
_ = Bool
False
toPrefix :: String -> String
toPrefix :: String -> String
toPrefix (Char
'`':String
cs) = forall a. [a] -> [a]
init String
cs
toPrefix String
cs = Char
'('forall a. a -> [a] -> [a]
:String
cs forall a. [a] -> [a] -> [a]
++ String
")"
primeCycle :: [String] -> [String]
primeCycle :: [String] -> [String]
primeCycle [] = []
primeCycle [String]
ss = [String]
ss forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (forall a. [a] -> [a] -> [a]
++ String
"'") ([String] -> [String]
primeCycle [String]
ss)
variableNamesFromTemplate :: String -> [String]
variableNamesFromTemplate :: String -> [String]
variableNamesFromTemplate = [String] -> [String]
primeCycle forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
f
where
f :: String -> [String]
f String
"" = String -> [String]
f String
"x"
f String
"x" = [String
"x", String
"y", String
"z"]
f String
"xy" = [String
"xy", String
"zw"]
f String
"xyz" = [String
"xyz", String
"uvw"]
f String
cs | Char -> Bool
isDigit (forall a. [a] -> a
last String
cs) = forall a b. (a -> b) -> [a] -> [b]
map (\Int
n -> forall a. [a] -> [a]
init String
cs forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n) [Char -> Int
digitToInt (forall a. [a] -> a
last String
cs)..]
f [Char
c] | Char
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'a'..Char
'x'] = let x :: Int
x = Char -> Int
ord Char
c in forall a b. (a -> b) -> [a] -> [b]
map ((forall a. a -> [a] -> [a]
:[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Char
chr) [Int
x,Int
xforall a. Num a => a -> a -> a
+Int
1,Int
xforall a. Num a => a -> a -> a
+Int
2]
f String
cs | forall a. [a] -> a
last String
cs forall a. Eq a => a -> a -> Bool
== Char
's' = (forall a. [a] -> [a] -> [a]
++ String
"s") forall a b. (a -> b) -> [a] -> [b]
`map` String -> [String]
f (forall a. [a] -> [a]
init String
cs)
f [Char
c,Char
d] | Char -> Int
ord Char
d forall a. Num a => a -> a -> a
- Char -> Int
ord Char
c forall a. Eq a => a -> a -> Bool
== Int
1 = [[Char
c,Char
d], [Int -> Char
chr forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
c forall a. Num a => a -> a -> a
+ Int
2, Int -> Char
chr forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
d forall a. Num a => a -> a -> a
+ Int
2]]
f String
cs | String
cs forall a. Eq a => a -> a -> Bool
== String
"y" Bool -> Bool -> Bool
|| String
cs forall a. Eq a => a -> a -> Bool
== String
"z" = String
cs forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map (\Integer
n -> String
cs forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Integer
n) [Integer
1..]
f String
cs = [String
cs]