gdritter repos s-cargot / dbbda78
Syntax highlighting looks nice even in ghci snippets! adding everywhere now Getty Ritter 9 years ago
1 changed file(s) with 7 addition(s) and 7 deletion(s). Collapse all Expand all
123123 We can then use this newly created atom type within an S-expression
124124 for both parsing and serialization:
125125
126 ~~~~
126 ~~~~.haskell
127127 *Data.SCargot.General T> decode mySpec "(foo 1)"
128128 Right [SCons (SAtom (Ident "foo")) (SCons (SAtom (Num 1)) SNil)]
129129 *Data.SCargot.General T> encode mySpec [SCons (SAtom (Num 0)) SNil]
161161 then we could use the `convertSpec` function to add this directly to
162162 the `SExprSpec`:
163163
164 ~~~~
164 ~~~~.haskell
165165 *Data.SCargot.General T> decode (convertSpec toExpr fromExpr (asRich spec)) "(+ 1 2)"
166166 Right [Add (Num 1) (Num 2)]
167167 *Data.SCargot.General T> decode (convertSpec toExpr fromExpr (asRich spec)) "(0 1 2)"
174174 the provided `withSemicolonComments` function will cause it to understand
175175 traditional Lisp line-oriented comments that begin with a semicolon:
176176
177 ~~~~
177 ~~~~.haskell
178178 *Data.SCargot.General> decode spec "(this ; has a comment\n inside)\n"
179179 Left "Failed reading: takeWhile1"
180180 *Data.SCargot.General> decode (withSemicolonComments spec) "(this ; has a comment\n inside)\n"
191191
192192 For example, the following adds C++-style comments to an S-expression format:
193193
194 ~~~~
194 ~~~~.haskell
195195 *Data.SCargot.General> let cppComment = string "//" >> takeWhile (/= '\n') >> return ()
196196 *Data.SCargot.General> decode (setComment cppComment spec) "(a //comment\n b)\n"
197197 Right [SCons (SAtom "a") (SCons (SAtom "b") SNil)]
207207 readers. There is a special case for the aforementioned quote, but that
208208 could easily be written by hand as
209209
210 ~~~~
210 ~~~~.haskell
211211 *Data.SCargot.General> let doQuote c = SCons (SAtom "quote") (SCons c SNil)
212212 *Data.SCargot.General> let qReader = addReader '\'' (\ p -> fmap doQuote p)
213213 *Data.SCargot.General> decode (qReader mySpec) "'foo"
220220 would like; for example, the following reader macro does not bother
221221 parsing anything else and merely returns a new token:
222222
223 ~~~~
223 ~~~~.haskell
224224 *Data.SCargot.General> let qmReader = addReader '?' (\ _ -> pure (SAtom "huh"))
225225 *Data.SCargot.General> decode (qmReader mySpec) "(?1 2)"
226226 Right [SCons (SAtom "huh") (SCons (SAtom "1") (SCons (SAtom "2") SNil))]
233233 `[` character and repeatedly calls the parser until a `]` character
234234 is reached:
235235
236 ~~~~
236 ~~~~.haskell
237237 *Data.SCargot.General> let pVec p = (char ']' *> pure SNil) <|> (SCons <$> p <*> pVec p)
238238 *Data.SCargot.General> let vec = addReader '[' pVec
239239 *Data.SCargot.General> decode (asRich (vec mySpec)) "(1 [2 3])"