SExpr.hs revision 5dc46f6d0fdd8747d730f9e79a93978145ed43bb
97f7daaffd9b6c1031302d7e551d5279fa0d0d72pquerna{- |
97f7daaffd9b6c1031302d7e551d5279fa0d0d72pquernaModule : $Header$
97f7daaffd9b6c1031302d7e551d5279fa0d0d72pquernaDescription : S-Expressions as intermediate output format
Copyright : (c) E. Schulz, D. Dietrich, C. Maeder, DFKI 2008
License : similar to LGPL, see HetCATS/LICENSE.txt
Maintainer : Christian.Maeder@dfki.de
Stability : provisional
Portability : portable
S-Expressions for the translation from HasCASL, CASL and VSE to OMDoc
-}
module Common.SExpr
( SExpr(..)
, prettySExpr
, idToSSymbol
, transToken
, transString
) where
import Common.Doc
import Common.Id
import Common.LibName
import Common.ProofUtils
import qualified Data.Map as Map
import Data.Char
data SExpr = SSymbol String | SList [SExpr] deriving (Eq, Ord, Show)
prettySExpr :: SExpr -> Doc
prettySExpr sexpr = case sexpr of
SSymbol s -> text s
SList l -> parens . fsep $ map prettySExpr l
-- | transform an overloaded identifier
idToSSymbol :: Int -> Id -> SExpr
idToSSymbol n i = SSymbol
$ transQualId i . (if n < 2 then id else showString "_O" . shows n) $ ""
transQualId :: Id -> ShowS
transQualId = transId . unQualName
transId :: Id -> ShowS
transId (Id ts cs _) =
showSepList id (showString . transToken) ts .
if null cs then id else
showString "{" . showSepList (showString "-") transId cs
. showString "}"
transToken :: Token -> String
transToken t = if isPlace t then "_2" else transString $ tokStr t
transStringAux :: String -> String
transStringAux = concatMap (\ c -> Map.findWithDefault [c] c cMap)
transString :: String -> String
transString s = case s of
c : r | isDigit c -> "_D" ++ c : transStringAux r
_ -> transStringAux s
cMap :: Map.Map Char String
cMap = Map.map ('_' :) $ Map.insert '_' "1" charMap