{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module BNFC.Backend.Haskell.Utils
( parserName
, hsReservedWords, avoidReservedWords, mkDefName
, typeToHaskell, typeToHaskell'
, catToType
, catToVar, catvars
, tokenTextImport, tokenTextType
, tokenTextPack, tokenTextPackParens, tokenTextUnpack
) where
import Data.Char
import BNFC.PrettyPrint
import qualified BNFC.PrettyPrint as P
import BNFC.CF (Cat(..), catToStr, identCat, baseTokenCatNames, Base, Type(FunT), IsFun(..))
import BNFC.Options (TokenText(..))
import BNFC.Utils (mkNames, NameStyle(..))
tokenTextImport :: TokenText -> [String]
tokenTextImport :: TokenText -> [String]
tokenTextImport = \case
TokenText
StringToken -> []
TokenText
ByteStringToken -> [ String
"import qualified Data.ByteString.Char8 as BS" ]
TokenText
TextToken -> [ String
"import qualified Data.Text" ]
tokenTextType :: TokenText -> String
tokenTextType :: TokenText -> String
tokenTextType = \case
TokenText
StringToken -> String
"String"
TokenText
ByteStringToken -> String
"BS.ByteString"
TokenText
TextToken -> String
"Data.Text.Text"
tokenTextPack :: TokenText -> String -> String
tokenTextPack :: TokenText -> String -> String
tokenTextPack = \case
TokenText
StringToken -> String -> String
forall a. a -> a
id
TokenText
ByteStringToken -> (String
"BS.pack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
TokenText
TextToken -> (String
"Data.Text.pack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
tokenTextPackParens :: TokenText -> String -> String
tokenTextPackParens :: TokenText -> String -> String
tokenTextPackParens = \case
TokenText
StringToken -> String -> String
forall a. a -> a
id
TokenText
ByteStringToken -> String -> String
parens (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"BS.pack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
TokenText
TextToken -> String -> String
parens (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"Data.Text.pack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
where
parens :: String -> String
parens String
s = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
tokenTextUnpack :: TokenText -> String -> String
tokenTextUnpack :: TokenText -> String -> String
tokenTextUnpack = \case
TokenText
StringToken -> String -> String
forall a. a -> a
id
TokenText
ByteStringToken -> (String
"BS.unpack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
TokenText
TextToken -> (String
"Data.Text.unpack " String -> String -> String
forall a. [a] -> [a] -> [a]
++)
parserName :: Cat -> Doc
parserName :: Cat -> Doc
parserName = (Doc
"p" Doc -> Doc -> Doc
P.<>) (Doc -> Doc) -> (Cat -> Doc) -> Cat -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text (String -> Doc) -> (Cat -> String) -> Cat -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cat -> String
identCat
hsReservedWords :: [String]
hsReservedWords :: [String]
hsReservedWords =
[ String
"as"
, String
"case"
, String
"class"
, String
"data"
, String
"default"
, String
"deriving"
, String
"do"
, String
"else"
, String
"family"
, String
"forall"
, String
"foreign"
, String
"hiding"
, String
"if"
, String
"import"
, String
"in"
, String
"infix"
, String
"infixl"
, String
"infixr"
, String
"instance"
, String
"let"
, String
"mdo"
, String
"module"
, String
"newtype"
, String
"of"
, String
"pattern"
, String
"proc"
, String
"qualified"
, String
"rec"
, String
"then"
, String
"type"
, String
"where"
]
avoidReservedWords :: String -> String
avoidReservedWords :: String -> String
avoidReservedWords String
x
| String
x String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
hsReservedWords = String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'"
| Bool
otherwise = String
x
mkDefName :: IsFun f => f -> String
mkDefName :: f -> String
mkDefName = String -> String
avoidReservedWords (String -> String) -> (f -> String) -> f -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f -> String
forall a. IsFun a => a -> String
funName
catToType :: (Doc -> Doc) -> Doc -> Cat -> Doc
catToType :: (Doc -> Doc) -> Doc -> Cat -> Doc
catToType Doc -> Doc
qualify Doc
param Cat
cat = Bool -> Doc -> Doc
parensIf Bool
isApp (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Cat -> Doc
loop Cat
cat
where
isApp :: Bool
isApp = case Cat
cat of
Cat String
_ -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Doc -> Bool
isEmpty Doc
param
Cat
_ -> Bool
False
loop :: Cat -> Doc
loop = \case
ListCat Cat
c -> Doc -> Doc
brackets (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Cat -> Doc
loop Cat
c
Cat String
c -> Doc -> Doc
qualify (String -> Doc
text String
c) Doc -> Doc -> Doc
<+> Doc
param
CoercCat String
c Integer
_ -> Doc -> Doc
qualify (String -> Doc
text String
c) Doc -> Doc -> Doc
<+> Doc
param
TokenCat String
c
| String
c String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
baseTokenCatNames
-> String -> Doc
text String
c
| Bool
otherwise -> Doc -> Doc
qualify (String -> Doc
text String
c)
baseTypeToHaskell :: Base -> String
baseTypeToHaskell :: Base -> String
baseTypeToHaskell = Base -> String
forall a. Show a => a -> String
show
typeToHaskell :: Type -> String
typeToHaskell :: Type -> String
typeToHaskell = String -> Type -> String
typeToHaskell' String
"->"
typeToHaskell' :: String -> Type -> String
typeToHaskell' :: String -> Type -> String
typeToHaskell' String
arr (FunT [Base]
ts Base
t) =
(String -> String -> String) -> String -> [String] -> String
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr String -> String -> String
f (Base -> String
baseTypeToHaskell Base
t) ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Base -> String) -> [Base] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Base -> String
baseTypeToHaskell [Base]
ts
where f :: String -> String -> String
f String
a String
b = [String] -> String
unwords [String
a, String
arr, String
b]
catToVar :: Cat -> String
catToVar :: Cat -> String
catToVar = String -> String
avoidReservedWords (String -> String) -> (Cat -> String) -> Cat -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cat -> String
var
where
var :: Cat -> String
var (ListCat Cat
cat) = Cat -> String
var Cat
cat String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"s"
var (Cat String
"Ident") = String
"x"
var (Cat String
"Integer") = String
"n"
var (Cat String
"String") = String
"str"
var (Cat String
"Char") = String
"c"
var (Cat String
"Double") = String
"d"
var Cat
xs = (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Cat -> String
catToStr Cat
xs
catvars :: [Cat] -> [Doc]
catvars :: [Cat] -> [Doc]
catvars = (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
text ([String] -> [Doc]) -> ([Cat] -> [String]) -> [Cat] -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> NameStyle -> [String] -> [String]
mkNames [String]
hsReservedWords NameStyle
LowerCase ([String] -> [String]) -> ([Cat] -> [String]) -> [Cat] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Cat -> String) -> [Cat] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Cat -> String
var
where
var :: Cat -> String
var (ListCat Cat
c) = Cat -> String
var Cat
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"s"
var Cat
xs = Cat -> String
forall a. Show a => a -> String
show Cat
xs