Parse.hs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. {-# LANGUAGE ViewPatterns #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. module Data.SCargot.Parse
  4. ( -- * Parsing
  5. decode
  6. , decodeOne
  7. -- * Parsing Control
  8. , SExprParser
  9. , Reader
  10. , Comment
  11. , mkParser
  12. , setCarrier
  13. , addReader
  14. , setComment
  15. -- * Specific SExprParser Conversions
  16. , asRich
  17. , asWellFormed
  18. , withQuote
  19. ) where
  20. #if !MIN_VERSION_base(4,8,0)
  21. import Control.Applicative ((<$>), (<*), pure)
  22. #endif
  23. import Control.Monad ((>=>))
  24. import Data.Map.Strict (Map)
  25. import qualified Data.Map.Strict as M
  26. import Data.Text (Text)
  27. import Data.String (IsString)
  28. import Text.Parsec ( (<|>)
  29. , (<?>)
  30. , char
  31. , eof
  32. , lookAhead
  33. , many1
  34. , runParser
  35. , skipMany
  36. )
  37. import Text.Parsec.Char (anyChar, space)
  38. import Text.Parsec.Text (Parser)
  39. import Data.SCargot.Repr ( SExpr(..)
  40. , RichSExpr
  41. , WellFormedSExpr
  42. , toRich
  43. , toWellFormed
  44. )
  45. type ReaderMacroMap atom = Map Char (Reader atom)
  46. -- | A 'Reader' represents a reader macro: it takes a parser for
  47. -- the S-Expression type and performs as much or as little
  48. -- parsing as it would like, and then returns an S-expression.
  49. type Reader atom = (Parser (SExpr atom) -> Parser (SExpr atom))
  50. -- | A 'Comment' represents any kind of skippable comment. This
  51. -- parser __must__ be able to fail if a comment is not being
  52. -- recognized, and it __must__ not consume any input in case
  53. -- of failure.
  54. type Comment = Parser ()
  55. -- | A 'SExprParser' describes a parser for a particular value
  56. -- that has been serialized as an s-expression. The @atom@ parameter
  57. -- corresponds to a Haskell type used to represent the atoms,
  58. -- and the @carrier@ parameter corresponds to the parsed S-Expression
  59. -- structure.
  60. data SExprParser atom carrier = SExprParser
  61. { sesPAtom :: Parser atom
  62. , readerMap :: ReaderMacroMap atom
  63. , comment :: Maybe Comment
  64. , postparse :: SExpr atom -> Either String carrier
  65. }
  66. -- | Create a basic 'SExprParser' when given a parser
  67. -- for an atom type.
  68. --
  69. -- >>> import Text.Parsec (alphaNum, many1)
  70. -- >>> let parser = mkParser (many1 alphaNum)
  71. -- >>> decode parser "(ele phant)"
  72. -- Right [SCons (SAtom "ele") (SCons (SAtom "phant") SNil)]
  73. mkParser :: Parser atom -> SExprParser atom (SExpr atom)
  74. mkParser parser = SExprParser
  75. { sesPAtom = parser
  76. , readerMap = M.empty
  77. , comment = Nothing
  78. , postparse = return
  79. }
  80. -- | Modify the carrier type for a 'SExprParser'. This is
  81. -- used internally to convert between various 'SExpr' representations,
  82. -- but could also be used externally to add an extra conversion layer
  83. -- onto a 'SExprParser'.
  84. --
  85. -- >>> import Text.Parsec (alphaNum, many1)
  86. -- >>> import Data.SCargot.Repr (toRich)
  87. -- >>> let parser = setCarrier (return . toRich) (mkParser (many1 alphaNum))
  88. -- >>> decode parser "(ele phant)"
  89. -- Right [RSlist [RSAtom "ele",RSAtom "phant"]]
  90. setCarrier :: (b -> Either String c) -> SExprParser a b -> SExprParser a c
  91. setCarrier f spec = spec { postparse = postparse spec >=> f }
  92. -- | Convert the final output representation from the 'SExpr' type
  93. -- to the 'RichSExpr' type.
  94. --
  95. -- >>> import Text.Parsec (alphaNum, many1)
  96. -- >>> let parser = asRich (mkParser (many1 alphaNum))
  97. -- >>> decode parser "(ele phant)"
  98. -- Right [RSlist [RSAtom "ele",RSAtom "phant"]]
  99. asRich :: SExprParser a (SExpr b) -> SExprParser a (RichSExpr b)
  100. asRich = setCarrier (return . toRich)
  101. -- | Convert the final output representation from the 'SExpr' type
  102. -- to the 'WellFormedSExpr' type.
  103. --
  104. -- >>> import Text.Parsec (alphaNum, many1)
  105. -- >>> let parser = asWellFormed (mkParser (many1 alphaNum))
  106. -- >>> decode parser "(ele phant)"
  107. -- Right [WFSList [WFSAtom "ele",WFSAtom "phant"]]
  108. asWellFormed :: SExprParser a (SExpr b) -> SExprParser a (WellFormedSExpr b)
  109. asWellFormed = setCarrier toWellFormed
  110. -- | Add the ability to execute some particular reader macro, as
  111. -- defined by its initial character and the 'Parser' which returns
  112. -- the parsed S-Expression. The 'Reader' is passed a 'Parser' which
  113. -- can be recursively called to parse more S-Expressions, and begins
  114. -- parsing after the reader character has been removed from the
  115. -- stream.
  116. --
  117. -- >>> import Text.Parsec (alphaNum, char, many1)
  118. -- >>> let vecReader p = (char ']' *> pure SNil) <|> (SCons <$> p <*> vecReader p)
  119. -- >>> let parser = addReader '[' vecReader (mkParser (many1 alphaNum))
  120. -- >>> decode parser "(an [ele phant])"
  121. -- Right [SCons (SAtom "an") (SCons (SCons (SAtom "ele") (SCons (SAtom "phant") SNil)) SNil)]
  122. addReader :: Char -> Reader a -> SExprParser a c -> SExprParser a c
  123. addReader c reader spec = spec
  124. { readerMap = M.insert c reader (readerMap spec) }
  125. -- | Add the ability to ignore some kind of comment. This gets
  126. -- factored into whitespace parsing, and it's very important that
  127. -- the parser supplied __be able to fail__ (as otherwise it will
  128. -- cause an infinite loop), and also that it __not consume any input__
  129. -- (which may require it to be wrapped in 'try'.)
  130. --
  131. -- >>> import Text.Parsec (alphaNum, anyChar, manyTill, many1, string)
  132. -- >>> let comment = string "//" *> manyTill anyChar newline *> pure ()
  133. -- >>> let parser = setComment comment (mkParser (many1 alphaNum))
  134. -- >>> decode parser "(ele //a comment\n phant)"
  135. -- Right [SCons (SAtom "ele") (SCons (SAtom "phant") SNil)]
  136. setComment :: Comment -> SExprParser a c -> SExprParser a c
  137. setComment c spec = spec { comment = Just (c <?> "comment") }
  138. -- | Add the ability to understand a quoted S-Expression.
  139. -- Many Lisps use @'sexpr@ as sugar for @(quote sexpr)@. This
  140. -- assumes that the underlying atom type implements the "IsString"
  141. -- class, and will create the @quote@ atom using @fromString "quote"@.
  142. --
  143. -- >>> import Text.Parsec (alphaNum, many1)
  144. -- >>> let parser = withQuote (mkParser (many1 alphaNum))
  145. -- >>> decode parser "'elephant"
  146. -- Right [SCons (SAtom "quote") (SCons (SAtom "foo") SNil)]
  147. withQuote :: IsString t => SExprParser t (SExpr t) -> SExprParser t (SExpr t)
  148. withQuote = addReader '\'' (fmap go)
  149. where go s = SCons "quote" (SCons s SNil)
  150. peekChar :: Parser (Maybe Char)
  151. peekChar = Just <$> lookAhead anyChar <|> pure Nothing
  152. parseGenericSExpr ::
  153. Parser atom -> ReaderMacroMap atom -> Parser () -> Parser (SExpr atom)
  154. parseGenericSExpr atom reader skip = do
  155. let sExpr = parseGenericSExpr atom reader skip <?> "s-expr"
  156. skip
  157. c <- peekChar
  158. r <- case c of
  159. Nothing -> fail "Unexpected end of input"
  160. Just '(' -> char '(' >> skip >> parseList sExpr skip
  161. Just (flip M.lookup reader -> Just r) -> anyChar >> r sExpr
  162. _ -> SAtom `fmap` atom
  163. skip
  164. return r
  165. parseList :: Parser (SExpr atom) -> Parser () -> Parser (SExpr atom)
  166. parseList sExpr skip = do
  167. i <- peekChar
  168. case i of
  169. Nothing -> fail "Unexpected end of input"
  170. Just ')' -> char ')' >> return SNil
  171. _ -> do
  172. car <- sExpr
  173. skip
  174. c <- peekChar
  175. case c of
  176. Just '.' -> do
  177. _ <- char '.'
  178. cdr <- sExpr
  179. skip
  180. _ <- char ')'
  181. skip
  182. return (SCons car cdr)
  183. Just ')' -> do
  184. _ <- char ')'
  185. skip
  186. return (SCons car SNil)
  187. _ -> do
  188. cdr <- parseList sExpr skip
  189. return (SCons car cdr)
  190. -- | Given a CommentMap, create the corresponding parser to
  191. -- skip those comments (if they exist).
  192. buildSkip :: Maybe (Parser ()) -> Parser ()
  193. buildSkip Nothing = skipMany space
  194. buildSkip (Just c) = alternate
  195. where alternate = skipMany space >> ((c >> alternate) <|> return ())
  196. doParse :: Parser a -> Text -> Either String a
  197. doParse p t = case runParser p () "" t of
  198. Left err -> Left (show err)
  199. Right x -> Right x
  200. -- | Decode a single S-expression. If any trailing input is left after
  201. -- the S-expression (ignoring comments or whitespace) then this
  202. -- will fail: for those cases, use 'decode', which returns a list of
  203. -- all the S-expressions found at the top level.
  204. decodeOne :: SExprParser atom carrier -> Text -> Either String carrier
  205. decodeOne spec = doParse (parser <* eof) >=> (postparse spec)
  206. where parser = parseGenericSExpr
  207. (sesPAtom spec)
  208. (readerMap spec)
  209. (buildSkip (comment spec))
  210. -- | Decode several S-expressions according to a given 'SExprParser'. This
  211. -- will return a list of every S-expression that appears at the top-level
  212. -- of the document.
  213. decode :: SExprParser atom carrier -> Text -> Either String [carrier]
  214. decode spec =
  215. doParse (many1 parser <* eof) >=> mapM (postparse spec)
  216. where parser = parseGenericSExpr
  217. (sesPAtom spec)
  218. (readerMap spec)
  219. (buildSkip (comment spec))
  220. {-
  221. -- | Encode (without newlines) a single S-expression.
  222. encodeSExpr :: SExpr atom -> (atom -> Text) -> Text
  223. encodeSExpr SNil _ = "()"
  224. encodeSExpr (SAtom s) t = t s
  225. encodeSExpr (SCons x xs) t = go xs (encodeSExpr x t)
  226. where go (SAtom s) rs = "(" <> rs <> " . " <> t s <> ")"
  227. go SNil rs = "(" <> rs <> ")"
  228. go (SCons x xs) rs = go xs (rs <> " " <> encodeSExpr x t)
  229. -- | Emit an S-Expression in a machine-readable way. This does no
  230. -- pretty-printing or indentation, and produces no comments.
  231. encodeOne :: SExprParser atom carrier -> carrier -> Text
  232. encodeOne spec c = encodeSExpr (preserial spec c) (sesSAtom spec)
  233. encode :: SExprParser atom carrier -> [carrier] -> Text
  234. encode spec cs = T.concat (map (encodeOne spec) cs)
  235. -}