Syntax highlighting looks nice even in ghci snippets! adding everywhere now
Getty Ritter
9 years ago
123 | 123 | We can then use this newly created atom type within an S-expression |
124 | 124 | for both parsing and serialization: |
125 | 125 | |
126 |
~~~~ |
|
126 | ~~~~.haskell | |
127 | 127 | *Data.SCargot.General T> decode mySpec "(foo 1)" |
128 | 128 | Right [SCons (SAtom (Ident "foo")) (SCons (SAtom (Num 1)) SNil)] |
129 | 129 | *Data.SCargot.General T> encode mySpec [SCons (SAtom (Num 0)) SNil] |
161 | 161 | then we could use the `convertSpec` function to add this directly to |
162 | 162 | the `SExprSpec`: |
163 | 163 | |
164 |
~~~~ |
|
164 | ~~~~.haskell | |
165 | 165 | *Data.SCargot.General T> decode (convertSpec toExpr fromExpr (asRich spec)) "(+ 1 2)" |
166 | 166 | Right [Add (Num 1) (Num 2)] |
167 | 167 | *Data.SCargot.General T> decode (convertSpec toExpr fromExpr (asRich spec)) "(0 1 2)" |
174 | 174 | the provided `withSemicolonComments` function will cause it to understand |
175 | 175 | traditional Lisp line-oriented comments that begin with a semicolon: |
176 | 176 | |
177 |
~~~~ |
|
177 | ~~~~.haskell | |
178 | 178 | *Data.SCargot.General> decode spec "(this ; has a comment\n inside)\n" |
179 | 179 | Left "Failed reading: takeWhile1" |
180 | 180 | *Data.SCargot.General> decode (withSemicolonComments spec) "(this ; has a comment\n inside)\n" |
191 | 191 | |
192 | 192 | For example, the following adds C++-style comments to an S-expression format: |
193 | 193 | |
194 |
~~~~ |
|
194 | ~~~~.haskell | |
195 | 195 | *Data.SCargot.General> let cppComment = string "//" >> takeWhile (/= '\n') >> return () |
196 | 196 | *Data.SCargot.General> decode (setComment cppComment spec) "(a //comment\n b)\n" |
197 | 197 | Right [SCons (SAtom "a") (SCons (SAtom "b") SNil)] |
207 | 207 | readers. There is a special case for the aforementioned quote, but that |
208 | 208 | could easily be written by hand as |
209 | 209 | |
210 |
~~~~ |
|
210 | ~~~~.haskell | |
211 | 211 | *Data.SCargot.General> let doQuote c = SCons (SAtom "quote") (SCons c SNil) |
212 | 212 | *Data.SCargot.General> let qReader = addReader '\'' (\ p -> fmap doQuote p) |
213 | 213 | *Data.SCargot.General> decode (qReader mySpec) "'foo" |
220 | 220 | would like; for example, the following reader macro does not bother |
221 | 221 | parsing anything else and merely returns a new token: |
222 | 222 | |
223 |
~~~~ |
|
223 | ~~~~.haskell | |
224 | 224 | *Data.SCargot.General> let qmReader = addReader '?' (\ _ -> pure (SAtom "huh")) |
225 | 225 | *Data.SCargot.General> decode (qmReader mySpec) "(?1 2)" |
226 | 226 | Right [SCons (SAtom "huh") (SCons (SAtom "1") (SCons (SAtom "2") SNil))] |
233 | 233 | `[` character and repeatedly calls the parser until a `]` character |
234 | 234 | is reached: |
235 | 235 | |
236 |
~~~~ |
|
236 | ~~~~.haskell | |
237 | 237 | *Data.SCargot.General> let pVec p = (char ']' *> pure SNil) <|> (SCons <$> p <*> pVec p) |
238 | 238 | *Data.SCargot.General> let vec = addReader '[' pVec |
239 | 239 | *Data.SCargot.General> decode (asRich (vec mySpec)) "(1 [2 3])" |