{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}
module BNFC.Backend.Haskell.CFtoAbstract (cf2Abstract, definedRules) where
import Prelude hiding ((<>))
import Data.Either (isRight)
import Data.Maybe
import qualified Data.List as List
import BNFC.CF
import BNFC.Options ( SharedOptions(..), TokenText(..) )
import BNFC.PrettyPrint
import BNFC.Utils ( when )
import BNFC.Backend.Haskell.Utils
( avoidReservedWords, catToType, mkDefName
, tokenTextImport, tokenTextType, typeToHaskell
, posType, posConstr, noPosConstr
, hasPositionClass, hasPositionMethod
)
cf2Abstract
:: SharedOptions
-> String
-> CF
-> Doc
cf2Abstract :: SharedOptions -> [Char] -> CF -> Doc
cf2Abstract Options{ [Char]
lang :: SharedOptions -> [Char]
lang :: [Char]
lang, TokenText
tokenText :: SharedOptions -> TokenText
tokenText :: TokenText
tokenText, Bool
generic :: SharedOptions -> Bool
generic :: Bool
generic, Bool
functor :: SharedOptions -> Bool
functor :: Bool
functor } [Char]
name CF
cf = [Doc] -> Doc
vsep ([Doc] -> Doc) -> ([[Doc]] -> [Doc]) -> [[Doc]] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Doc]] -> Doc) -> [[Doc]] -> Doc
forall a b. (a -> b) -> a -> b
$
[ [ [Doc] -> Doc
vcat
[ Doc
"-- Haskell data types for the abstract syntax."
, Doc
"-- Generated by the BNF converter."
]
]
, [ [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([[Doc]] -> [Doc]) -> [[Doc]] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Doc]] -> Doc) -> [[Doc]] -> Doc
forall a b. (a -> b) -> a -> b
$
[ [ Doc
"{-# LANGUAGE DeriveDataTypeable #-}" | Bool
gen ]
, [ Doc
"{-# LANGUAGE DeriveGeneric #-}" | Bool
gen ]
, [ Doc
"{-# LANGUAGE DeriveTraversable #-}" | Bool
fun ]
, [ Doc
"{-# LANGUAGE FlexibleInstances #-}" | Bool
fun ]
, [ Doc
"{-# LANGUAGE GeneralizedNewtypeDeriving #-}" | Bool
hasIdentLike ]
, [ Doc
"{-# LANGUAGE LambdaCase #-}" | Bool
fun ]
, [ Doc
"{-# LANGUAGE PatternSynonyms #-}" | Bool
defPosition ]
, [ Doc
"{-# LANGUAGE OverloadedStrings #-}" | TokenText
tokenText TokenText -> TokenText -> Bool
forall a. Eq a => a -> a -> Bool
/= TokenText
StringToken ]
]
]
, [ Doc
"-- | The abstract syntax of language" Doc -> Doc -> Doc
<+> [Char] -> Doc
text [Char]
lang Doc -> Doc -> Doc
<> Doc
"." ]
, [ [Doc] -> Doc
hsep [ Doc
"module", [Char] -> Doc
text [Char]
name, Doc
"where" ] ]
, [ [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([[Doc]] -> [Doc]) -> [[Doc]] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Doc]] -> Doc) -> [[Doc]] -> Doc
forall a b. (a -> b) -> a -> b
$
[ [ [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"import Prelude (" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
List.intercalate [Char]
", " [[Char]]
typeImports [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
")"
| Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Char]]
typeImports ]
, [ Int -> Doc -> Doc -> Doc -> Doc -> [Doc] -> Doc
prettyList Int
2 Doc
"import qualified Prelude as C" Doc
"(" Doc
")" Doc
"," ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc]
qualifiedPreludeImports
| Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Doc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Doc]
qualifiedPreludeImports ]
, [ Doc
"import qualified Data.String"
| Bool
hasIdentLike ]
]
]
, [ [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([[Doc]] -> [Doc]) -> [[Doc]] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Doc]] -> Doc) -> [[Doc]] -> Doc
forall a b. (a -> b) -> a -> b
$
[ ([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Doc
text ([[Char]] -> [Doc]) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> a -> b
$ TokenText -> [[Char]]
tokenTextImport TokenText
tokenText
, [ Doc
"import qualified Data.Data as C (Data, Typeable)" | Bool
gen ]
, [ Doc
"import qualified GHC.Generics as C (Generic)" | Bool
gen ]
]
]
, (Data -> Doc) -> [Data] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> [[Char]] -> Data -> Doc
prData Bool
functor (Bool -> [[Char]]
derivingClasses Bool
functor)) [Data]
datas
, Bool -> CF -> [Doc]
definedRules Bool
functor CF
cf
, (([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
`map` CF -> [[Char]]
specialCats CF
cf) (([Char] -> Doc) -> [Doc]) -> ([Char] -> Doc) -> [Doc]
forall a b. (a -> b) -> a -> b
$ \ [Char]
c ->
let hasPos :: Bool
hasPos = CF -> [Char] -> Bool
forall f. CFG f -> [Char] -> Bool
isPositionCat CF
cf [Char]
c
in TokenText -> Bool -> [[Char]] -> [Char] -> Doc
prSpecialData TokenText
tokenText Bool
hasPos (Bool -> [[Char]]
derivingClassesTokenType Bool
hasPos) [Char]
c
, [ [Doc] -> Doc
vcat
[ Doc
"-- | Start position (line, column) of something."
, Doc
""
, Doc
"type" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posType Doc -> Doc -> Doc
<+> Doc
"=" Doc -> Doc -> Doc
<+> Doc
"C.Maybe (C.Int, C.Int)"
, Doc
""
, Doc
"pattern" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
noPosConstr Doc -> Doc -> Doc
<+> Doc
"::" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posType
, Doc
"pattern" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
noPosConstr Doc -> Doc -> Doc
<+> Doc
"=" Doc -> Doc -> Doc
<+> Doc
"C.Nothing"
, Doc
""
, Doc
"pattern" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posConstr Doc -> Doc -> Doc
<+> Doc
":: C.Int -> C.Int ->" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posType
, Doc
"pattern" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posConstr Doc -> Doc -> Doc
<+> Doc
"line col =" Doc -> Doc -> Doc
<+> Doc
"C.Just (line, col)"
]
| Bool
defPosition
]
, [ [Doc] -> Doc
vcat
[ Doc
"-- | Get the start position of something."
, Doc
""
, Doc
"class" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
hasPositionClass Doc -> Doc -> Doc
<+> Doc
"a where"
, Int -> Doc -> Doc
nest Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
forall a. IsString a => a
hasPositionMethod Doc -> Doc -> Doc
<+> Doc
":: a ->" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
posType
]
| Bool
hasPosition
]
, Bool -> [Doc] -> [Doc]
forall m. Monoid m => Bool -> m -> m
when Bool
functor ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (Data -> Doc) -> [Data] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Data -> Doc
instanceHasPositionData [Data]
datas
, ([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Doc
instanceHasPositionTokenType [[Char]]
positionCats
, [ Doc
"" ]
]
where
datas :: [Data]
datas = CF -> [Data]
cf2data CF
cf
positionCats :: [[Char]]
positionCats = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (CF -> [Char] -> Bool
forall f. CFG f -> [Char] -> Bool
isPositionCat CF
cf) ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ CF -> [[Char]]
specialCats CF
cf
hasIdentLike :: Bool
hasIdentLike = CF -> Bool
forall g. CFG g -> Bool
hasIdentLikeTokens CF
cf
hasData :: Bool
hasData = Bool -> Bool
not ([Data] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Data]
datas)
defPosition :: Bool
defPosition = CF -> Bool
forall g. CFG g -> Bool
hasPositionTokens CF
cf Bool -> Bool -> Bool
|| Bool
functor
hasPosition :: Bool
hasPosition = CF -> Bool
forall g. CFG g -> Bool
hasPositionTokens CF
cf Bool -> Bool -> Bool
|| Bool
fun
gen :: Bool
gen = Bool
generic Bool -> Bool -> Bool
&& Bool
hasData
fun :: Bool
fun = Bool
functor Bool -> Bool -> Bool
&& Bool
hasData
stdClasses :: [[Char]]
stdClasses = [ [Char]
"Eq", [Char]
"Ord", [Char]
"Show", [Char]
"Read" ]
funClasses :: [[Char]]
funClasses = [ [Char]
"Functor", [Char]
"Foldable", [Char]
"Traversable" ]
genClasses :: [[Char]]
genClasses = [ [Char]
"Data", [Char]
"Typeable", [Char]
"Generic" ]
derivingClasses :: Bool -> [[Char]]
derivingClasses Bool
functor = ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char]
"C." [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++) ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [[[Char]]] -> [[Char]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [[Char]]
stdClasses
, Bool -> [[Char]] -> [[Char]]
forall m. Monoid m => Bool -> m -> m
when Bool
functor [[Char]]
funClasses
, Bool -> [[Char]] -> [[Char]]
forall m. Monoid m => Bool -> m -> m
when Bool
generic [[Char]]
genClasses
]
derivingClassesTokenType :: Bool -> [[Char]]
derivingClassesTokenType Bool
hasPos = [[[Char]]] -> [[Char]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ Bool -> [[Char]]
derivingClasses Bool
False
, [ [Char]
"Data.String.IsString" | Bool -> Bool
not Bool
hasPos ]
]
typeImports :: [[Char]]
typeImports =
([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (\ [Char]
s -> Bool
hasData Bool -> Bool -> Bool
&& [Char]
s [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` CF -> [[Char]]
forall function. CFG function -> [[Char]]
cfgLiterals CF
cf
Bool -> Bool -> Bool
|| Bool
hasIdentLike Bool -> Bool -> Bool
&& TokenText
tokenText TokenText -> TokenText -> Bool
forall a. Eq a => a -> a -> Bool
== TokenText
StringToken Bool -> Bool -> Bool
&& [Char]
s [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"String")
[[Char]]
baseTokenCatNames
qualifiedPreludeImports :: [Doc]
qualifiedPreludeImports = [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
List.intercalate [Char]
", " [[Char]]
stdClasses | Bool
hasIdentLike Bool -> Bool -> Bool
|| Bool
hasData ]
, [ [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
List.intercalate [Char]
", " [[Char]]
funClasses | Bool
fun ]
, [ [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"Int, Maybe(..)" | Bool
defPosition ]
]
prData :: Bool -> [String] -> Data -> Doc
prData :: Bool -> [[Char]] -> Data -> Doc
prData Bool
functor [[Char]]
derivingClasses (Cat
cat,[([Char], [Cat])]
rules) = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ [Doc] -> Doc
hsep [ Doc
"type", Doc
unprimedType, Doc
"=", Doc
primedType, Doc
forall a. IsString a => a
posType ] | Bool
functor ]
, [ Doc -> Int -> Doc -> Doc
hang (Doc
"data" Doc -> Doc -> Doc
<+> Doc
dataType) Int
4 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
[([Char], [Cat])] -> Doc
constructors [([Char], [Cat])]
rules ]
, [ Int -> Doc -> Doc
nest Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [[Char]] -> Doc
deriving_ [[Char]]
derivingClasses ]
]
where
prRule :: ([Char], [Cat]) -> Doc
prRule ([Char]
fun, [Cat]
cats) = [Doc] -> Doc
hsep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [[Char] -> Doc
text [Char]
fun], [Doc
"a" | Bool
functor], (Cat -> Doc) -> [Cat] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Cat -> Doc
prArg [Cat]
cats ]
unprimedType :: Doc
unprimedType = [Char] -> Doc
text (Cat -> [Char]
forall a. Show a => a -> [Char]
show Cat
cat)
primedType :: Doc
primedType = Doc -> Doc
prime Doc
unprimedType
prime :: Doc -> Doc
prime = (Doc -> Doc -> Doc
<> Doc
"'")
dataType :: Doc
dataType | Bool
functor = Doc
primedType Doc -> Doc -> Doc
<+> Doc
"a"
|Bool
otherwise= Doc
unprimedType
prArg :: Cat -> Doc
prArg Cat
c
| Bool
functor Bool -> Bool -> Bool
&& (Bool -> Bool
not (Bool -> Bool) -> (Cat -> Bool) -> Cat -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Either [Char] [Char] -> Bool
forall a b. Either a b -> Bool
isRight (Either [Char] [Char] -> Bool)
-> (Cat -> Either [Char] [Char]) -> Cat -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cat -> Either [Char] [Char]
baseCat) Cat
c
= (Doc -> Doc) -> Doc -> Cat -> Doc
catToType Doc -> Doc
prime Doc
"a" Cat
c
| Bool
otherwise = (Doc -> Doc) -> Doc -> Cat -> Doc
catToType Doc -> Doc
forall a. a -> a
id Doc
empty Cat
c
constructors :: [([Char], [Cat])] -> Doc
constructors [] = Doc
empty
constructors (([Char], [Cat])
h:[([Char], [Cat])]
t) = [Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc
"=" Doc -> Doc -> Doc
<+> ([Char], [Cat]) -> Doc
prRule ([Char], [Cat])
h] [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ (([Char], [Cat]) -> Doc) -> [([Char], [Cat])] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ((Doc
"|" Doc -> Doc -> Doc
<+>) (Doc -> Doc) -> (([Char], [Cat]) -> Doc) -> ([Char], [Cat]) -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char], [Cat]) -> Doc
prRule) [([Char], [Cat])]
t
instanceHasPositionData :: Data -> Doc
instanceHasPositionData :: Data -> Doc
instanceHasPositionData (Cat
cat, [([Char], [Cat])]
rules) = [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([[Doc]] -> [Doc]) -> [[Doc]] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Doc]] -> Doc) -> [[Doc]] -> Doc
forall a b. (a -> b) -> a -> b
$
[ [ Doc
"instance" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
hasPositionClass Doc -> Doc -> Doc
<+> Doc
dat Doc -> Doc -> Doc
<+> Doc
"where" ]
, [ Int -> Doc -> Doc
nest Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
"hasPosition = \\case" ]
, (([Char], [Cat]) -> Doc) -> [([Char], [Cat])] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\ ([Char]
c, [Cat]
args) -> Int -> Doc -> Doc
nest Int
4 (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
hsep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [[Doc]] -> [Doc]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [[Char] -> Doc
text [Char]
c, Doc
pos], Doc
"_" Doc -> [Cat] -> [Doc]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Cat]
args, [Doc
"->", Doc
pos] ]) [([Char], [Cat])]
rules
]
where
dat :: Doc
dat = [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ Cat -> [Char]
catToStr Cat
cat
pos :: Doc
pos = Doc
"p"
prSpecialData
:: TokenText
-> Bool
-> [String]
-> TokenCat
-> Doc
prSpecialData :: TokenText -> Bool -> [[Char]] -> [Char] -> Doc
prSpecialData TokenText
tokenText Bool
position [[Char]]
classes [Char]
cat = [Doc] -> Doc
vcat
[ [Doc] -> Doc
hsep [ Doc
"newtype", [Char] -> Doc
text [Char]
cat, Doc
"=", [Char] -> Doc
text [Char]
cat, Doc
contentSpec ]
, Int -> Doc -> Doc
nest Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [[Char]] -> Doc
deriving_ [[Char]]
classes
]
where
contentSpec :: Doc
contentSpec | Bool
position = Doc -> Doc
parens ( Doc
"(C.Int, C.Int), " Doc -> Doc -> Doc
<> Doc
stringType)
| Bool
otherwise = Doc
stringType
stringType :: Doc
stringType = [Char] -> Doc
text ([Char] -> Doc) -> [Char] -> Doc
forall a b. (a -> b) -> a -> b
$ TokenText -> [Char]
tokenTextType TokenText
tokenText
deriving_ :: [String] -> Doc
deriving_ :: [[Char]] -> Doc
deriving_ [[Char]]
cls = Doc
"deriving" Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
hsep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate Doc
"," ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ ([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Doc
text [[Char]]
cls)
instanceHasPositionTokenType :: TokenCat -> Doc
instanceHasPositionTokenType :: [Char] -> Doc
instanceHasPositionTokenType [Char]
cat = [Doc] -> Doc
vcat
[ Doc
"instance" Doc -> Doc -> Doc
<+> Doc
forall a. IsString a => a
hasPositionClass Doc -> Doc -> Doc
<+> Doc
t Doc -> Doc -> Doc
<+> Doc
"where"
, Int -> Doc -> Doc
nest Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
"hasPosition " Doc -> Doc -> Doc
<> Doc -> Doc
parens (Doc
t Doc -> Doc -> Doc
<+> Doc
"(p, _)") Doc -> Doc -> Doc
<+> Doc
"= C.Just p"
]
where
t :: Doc
t = [Char] -> Doc
text [Char]
cat
definedRules :: Bool -> CF -> [Doc]
definedRules :: Bool -> CF -> [Doc]
definedRules Bool
functor CF
cf = [ RFun -> [[Char]] -> Exp -> Doc
forall {a}. IsFun a => a -> [[Char]] -> Exp -> Doc
mkDef RFun
f [[Char]]
xs Exp
e | FunDef RFun
f [[Char]]
xs Exp
e <- CF -> [Pragma]
forall function. CFG function -> [Pragma]
cfgPragmas CF
cf ]
where
mkDef :: a -> [[Char]] -> Exp -> Doc
mkDef a
f [[Char]]
xs Exp
e = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Doc
text ([[Char]] -> [Doc]) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> a -> b
$ [[[Char]]] -> [[Char]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ [[Char]] -> [Char]
unwords [ [Char]
fName, [Char]
"::", Type -> [Char]
typ (Type -> [Char]) -> Type -> [Char]
forall a b. (a -> b) -> a -> b
$ WithPosition Type -> Type
forall a. WithPosition a -> a
wpThing WithPosition Type
t ]
| WithPosition Type
t <- Maybe (WithPosition Type) -> [WithPosition Type]
forall a. Maybe a -> [a]
maybeToList (Maybe (WithPosition Type) -> [WithPosition Type])
-> Maybe (WithPosition Type) -> [WithPosition Type]
forall a b. (a -> b) -> a -> b
$ a -> CF -> Maybe (WithPosition Type)
forall a. IsFun a => a -> CF -> Maybe (WithPosition Type)
sigLookup a
f CF
cf
]
, [ [[Char]] -> [Char]
unwords ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
fName [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: [[Char]]
xs' [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [ [Char]
"=", Exp -> [Char]
forall a. Show a => a -> [Char]
show (Exp -> [Char]) -> Exp -> [Char]
forall a b. (a -> b) -> a -> b
$ Exp -> Exp
sanitize Exp
e ] ]
]
where
fName :: [Char]
fName = a -> [Char]
forall f. IsFun f => f -> [Char]
mkDefName a
f
avoidReserved :: [Char] -> [Char]
avoidReserved = [[Char]] -> [Char] -> [Char]
avoidReservedWords [[Char]
fName]
xs' :: [[Char]]
xs' = ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall {t} {a}. IsString t => (t -> a) -> [a] -> [a]
addFunctorArg [Char] -> [Char]
forall a. a -> a
id ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> [Char]
avoidReserved [[Char]]
xs
typ :: Type -> [Char]
typ (FunT [Base]
ts Base
t) | Bool
functor = [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
List.intercalate [Char]
" -> " ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"a" [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: ((Base -> [Char]) -> [Base] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Base -> [Char]
funBase ([Base] -> [[Char]]) -> [Base] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [Base]
ts [Base] -> [Base] -> [Base]
forall a. [a] -> [a] -> [a]
++ [Base
t])
typ Type
t = Type -> [Char]
typeToHaskell Type
t
sanitize :: Exp -> Exp
sanitize = \case
App [Char]
x [Exp]
es
| [Char] -> Bool
tokTyp [Char]
x -> [Char] -> [Exp] -> Exp
App [Char]
x ([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ (Exp -> Exp) -> [Exp] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Exp
sanitize [Exp]
es
| Bool
otherwise -> [Char] -> [Exp] -> Exp
App [Char]
x ([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ ([Char] -> Exp) -> [Exp] -> [Exp]
forall {t} {a}. IsString t => (t -> a) -> [a] -> [a]
addFunctorArg ([Char] -> [Exp] -> Exp
`App` []) ([Exp] -> [Exp]) -> [Exp] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Exp -> Exp) -> [Exp] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Exp
sanitize [Exp]
es
Var [Char]
x -> [Char] -> Exp
Var ([Char] -> Exp) -> [Char] -> Exp
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
avoidReserved [Char]
x
e :: Exp
e@LitInt{} -> Exp
e
e :: Exp
e@LitDouble{} -> Exp
e
e :: Exp
e@LitChar{} -> Exp
e
e :: Exp
e@LitString{} -> Exp
e
addFunctorArg :: (t -> a) -> [a] -> [a]
addFunctorArg t -> a
g
| Bool
functor = (t -> a
g t
"_a" a -> [a] -> [a]
forall a. a -> [a] -> [a]
:)
| Bool
otherwise = [a] -> [a]
forall a. a -> a
id
funBase :: Base -> String
funBase :: Base -> [Char]
funBase = \case
BaseT [Char]
x
| [Char] -> Bool
tokTyp [Char]
x -> [Char]
x
| Bool
otherwise -> [Char]
x [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"' a"
ListT Base
b -> [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [Char]
"[", Base -> [Char]
funBase Base
b, [Char]
"]" ]
tokTyp :: [Char] -> Bool
tokTyp = ([Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` CF -> [[Char]]
forall function. CFG function -> [[Char]]
literals CF
cf)