-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSExpr.hs
63 lines (50 loc) · 1.63 KB
/
SExpr.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{- |
Module : $Header$
Description : S-Expressions as intermediate output format
Copyright : (c) E. Schulz, D. Dietrich, C. Maeder, DFKI 2008
License : GPLv2 or higher, see 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