Debian Codegraph @

Data/SCargot/Language/HaskLike.hs

raw
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
4 module Data.SCargot.Language.HaskLike
5 ( -- $info
6 HaskLikeAtom(..)
7 , haskLikeParser
8 , haskLikePrinter
9 , locatedHaskLikeParser
10 , locatedHaskLikePrinter
11 -- * Individual Parsers
12 , parseHaskellString
13 , parseHaskellFloat
14 , parseHaskellInt
15 ) where
16
17 #if !MIN_VERSION_base(4,8,0)
18 import Control.Applicative ((<$>), (<$))
19 #endif
20 import Data.Maybe (catMaybes)
21 import Data.String (IsString(..))
22 import Data.Text (Text, pack)
23 import Text.Parsec
24 import Text.Parsec.Text (Parser)
25
26 import Prelude hiding (concatMap)
27
28 import Data.SCargot.Common
29 import Data.SCargot.Repr.Basic (SExpr)
30 import Data.SCargot (SExprParser, SExprPrinter, mkParser, flatPrint)
31
32 {- $info
33
34 This module is intended for simple, ad-hoc configuration or data
35 formats that might not need their on rich structure but might benefit
36 from a few various kinds of literals. The 'haskLikeParser' understands
37 identifiers as defined by R5RS, as well as string, integer, and
38 floating-point literals as defined by the Haskell 2010 spec. It does
39 __not__ natively understand other data types, such as booleans,
40 vectors, bitstrings.
41
42 -}
43
44
45 -- | An atom type that understands Haskell-like values as well as
46 -- Scheme-like identifiers.
47 data HaskLikeAtom
48 = HSIdent Text -- ^ An identifier, parsed according to the R5RS Scheme
49 -- standard
50 | HSString Text -- ^ A string, parsed according to the syntax for string
51 -- literals in the Haskell report
52 | HSInt Integer -- ^ An arbitrary-sized integer value, parsed according to
53 -- the syntax for integer literals in the Haskell report
54 | HSFloat Double -- ^ A double-precision floating-point value, parsed
55 -- according to the syntax for floats in the Haskell
56 -- report
57 deriving (Eq, Show)
58
59 instance IsString HaskLikeAtom where
60 fromString = HSIdent . fromString
61
62 instance IsString (Located HaskLikeAtom) where
63 fromString = (At dLocation) . HSIdent . fromString
64
65 -- | Parse a Haskell string literal as defined by the Haskell 2010
66 -- language specification.
67 parseHaskellString :: Parser Text
68 parseHaskellString = pack . catMaybes <$> between (char '"') (char '"') (many (val <|> esc))
69 where val = Just <$> satisfy (\ c -> c /= '"' && c /= '\\' && c > '\026')
70 esc = do _ <- char '\\'
71 Nothing <$ (gap <|> char '&') <|>
72 Just <$> code
73 gap = many1 space >> char '\\'
74 code = eEsc <|> eNum <|> eCtrl <|> eAscii
75 eCtrl = char '^' >> unCtrl <$> upper
76 eNum = (toEnum . fromInteger) <$>
77 (decNumber <|> (char 'o' >> octNumber)
78 <|> (char 'x' >> hexNumber))
79 eEsc = choice [ char a >> return b | (a, b) <- escMap ]
80 eAscii = choice [ try (string a >> return b)
81 | (a, b) <- asciiMap ]
82 unCtrl c = toEnum (fromEnum c - fromEnum 'A' + 1)
83
84 escMap :: [(Char, Char)]
85 escMap = zip "abfntv\\\"\'" "\a\b\f\n\r\t\v\\\"\'"
86
87 asciiMap :: [(String, Char)]
88 asciiMap = zip
89 ["BS","HT","LF","VT","FF","CR","SO","SI","EM"
90 ,"FS","GS","RS","US","SP","NUL","SOH","STX","ETX"
91 ,"EOT","ENQ","ACK","BEL","DLE","DC1","DC2","DC3"
92 ,"DC4","NAK","SYN","ETB","CAN","SUB","ESC","DEL"]
93 ("\BS\HT\LF\VT\FF\CR\SO\SI\EM\FS\GS\RS\US\SP\NUL\SOH" ++
94 "\STX\ETX\EOT\ENQ\ACK\BEL\DLE\DC1\DC2\DC3\DC4\NAK" ++
95 "\SYN\ETB\CAN\SUB\ESC\DEL")
96
97 -- | Parse a Haskell floating-point number as defined by the Haskell
98 -- 2010 language specification.
99 parseHaskellFloat :: Parser Double
100 parseHaskellFloat = do
101 n <- decNumber
102 withDot n <|> noDot n
103 where withDot n = do
104 _ <- char '.'
105 m <- decNumber
106 e <- option 1.0 expn
107 return ((fromIntegral n + asDec m 0) * e)
108 noDot n = do
109 e <- expn
110 return (fromIntegral n * e)
111 expn = do
112 _ <- oneOf "eE"
113 s <- power
114 x <- decNumber
115 return (10 ** s (fromIntegral x))
116 asDec 0 k = k
117 asDec n k =
118 asDec (n `div` 10) ((fromIntegral (n `rem` 10) + k) * 0.1)
119
120 power :: Num a => Parser (a -> a)
121 power = negate <$ char '-' <|> id <$ char '+' <|> return id
122
123 -- | Parse a Haskell integer literal as defined by the Haskell 2010
124 -- language specification.
125 parseHaskellInt :: Parser Integer
126 parseHaskellInt = do
127 s <- power
128 n <- pZeroNum <|> decNumber
129 return (fromIntegral (s n))
130
131 pZeroNum :: Parser Integer
132 pZeroNum = char '0' >>
133 ( (oneOf "xX" >> hexNumber)
134 <|> (oneOf "oO" >> octNumber)
135 <|> decNumber
136 <|> return 0
137 )
138
139 pHaskLikeAtom :: Parser HaskLikeAtom
140 pHaskLikeAtom
141 = HSFloat <$> (try parseHaskellFloat <?> "float")
142 <|> HSInt <$> (try parseHaskellInt <?> "integer")
143 <|> HSString <$> (parseHaskellString <?> "string literal")
144 <|> HSIdent <$> (parseR5RSIdent <?> "token")
145
146 sHaskLikeAtom :: HaskLikeAtom -> Text
147 sHaskLikeAtom (HSIdent t) = t
148 sHaskLikeAtom (HSString s) = pack (show s)
149 sHaskLikeAtom (HSInt i) = pack (show i)
150 sHaskLikeAtom (HSFloat f) = pack (show f)
151
152 -- | This `SExprParser` understands s-expressions that contain
153 -- Scheme-like tokens, as well as string literals, integer
154 -- literals, and floating-point literals. Each of these values
155 -- is parsed according to the lexical rules in the Haskell
156 -- report, so the same set of string escapes, numeric bases,
157 -- and floating-point options are available. This spec does
158 -- not parse comments and does not understand any reader
159 -- macros.
160 --
161 -- >>> decode haskLikeParser "(0x01 \"\\x65lephant\")"
162 -- Right [SCons (SAtom (HSInt 1)) (SCons (SAtom (HSString "elephant")) SNil)]
163 haskLikeParser :: SExprParser HaskLikeAtom (SExpr HaskLikeAtom)
164 haskLikeParser = mkParser pHaskLikeAtom
165
166 -- | A 'haskLikeParser' which produces 'Located' values
167 --
168 -- >>> decode locatedHaskLikeParser $ pack "(0x01 \"\\x65lephant\")"
169 -- Right [SCons (SAtom (At (Span (line 1, column 2) (line 1, column 6)) (HSInt 1))) (SCons (SAtom (At (Span (line 1, column 7) (line 1, column 20)) (HSString "elephant"))) SNil)]
170 --
171 -- >>> decode locatedHaskLikeParser $ pack "(1 elephant)"
172 -- Right [SCons (SAtom (At (Span (line 1, column 2) (line 1, column 3)) (HSInt 1))) (SCons (SAtom (At (Span (line 1, column 4) (line 1, column 12)) (HSIdent "elephant"))) SNil)]
173 locatedHaskLikeParser :: SExprParser (Located HaskLikeAtom) (SExpr (Located HaskLikeAtom))
174 locatedHaskLikeParser = mkParser $ located pHaskLikeAtom
175
176 -- | This 'SExprPrinter' emits s-expressions that contain Scheme-like
177 -- tokens as well as string literals, integer literals, and floating-point
178 -- literals, which will be emitted as the literals produced by Haskell's
179 -- 'show' function. This printer will produce a flat s-expression with
180 -- no indentation of any kind.
181 --
182 -- >>> encode haskLikePrinter [L [A (HSInt 1), A (HSString "elephant")]]
183 -- "(1 \"elephant\")"
184 haskLikePrinter :: SExprPrinter HaskLikeAtom (SExpr HaskLikeAtom)
185 haskLikePrinter = flatPrint sHaskLikeAtom
186
187 -- | Ignore location tags when packing values into text
188 sLocatedHasklikeAtom :: Located HaskLikeAtom -> Text
189 sLocatedHasklikeAtom (At _loc e) = sHaskLikeAtom e
190
191 -- | A 'SExprPrinter' for 'Located' values. Works exactly like 'haskLikePrinter'
192 -- It ignores the location tags when printing the result.
193 --
194 -- >>> let (Right dec) = decode locatedHaskLikeParser $ pack "(1 elephant)"
195 -- [SCons (SAtom (At (Span (line 1, column 2) (line 1, column 3)) (HSInt 1))) (SCons (SAtom (At (Span (line 1, column 4) (line 1, column 12)) (HSIdent "elephant"))) SNil)]
196 --
197 -- >>> encode locatedHaskLikePrinter dec
198 -- "(1 elephant)"
199 locatedHaskLikePrinter :: SExprPrinter (Located HaskLikeAtom) (SExpr (Located HaskLikeAtom))
200 locatedHaskLikePrinter = flatPrint sLocatedHasklikeAtom