Add printers for Located parsers
ckoparkar
7 years ago
6 | 6 |
basicParser
|
7 | 7 |
, basicPrinter
|
8 | 8 |
, locatedBasicParser
|
| 9 |
, locatedBasicPrinter
|
9 | 10 |
) where
|
10 | 11 |
|
11 | 12 |
import Control.Applicative ((<$>))
|
|
15 | 16 |
import Data.Functor.Identity (Identity)
|
16 | 17 |
import Text.Parsec.Prim (ParsecT)
|
17 | 18 |
|
18 | |
import Data.SCargot.Common (Located, located)
|
| 19 |
import Data.SCargot.Common (Located(..), located)
|
19 | 20 |
import Data.SCargot.Repr.Basic (SExpr)
|
20 | 21 |
import Data.SCargot ( SExprParser
|
21 | 22 |
, SExprPrinter
|
|
68 | 69 |
-- "(1 elephant)"
|
69 | 70 |
basicPrinter :: SExprPrinter Text (SExpr Text)
|
70 | 71 |
basicPrinter = flatPrint id
|
| 72 |
|
| 73 |
-- | A 'SExprPrinter' for 'Located' values. Works exactly like 'basicPrinter'
|
| 74 |
-- It ignores the location tags when printing the result.
|
| 75 |
--
|
| 76 |
-- >>> let (Right dec) = decode locatedBasicParser $ pack "(1 elephant)"
|
| 77 |
-- [SCons (SAtom (At (Span (line 1, column 2) (line 1, column 3)) "1")) (SCons (SAtom (At (Span (line 1, column 4) (line 1, column 12)) "elephant")) SNil)]
|
| 78 |
--
|
| 79 |
-- >>> encode locatedBasicPrinter dec
|
| 80 |
-- "(1 elephant)"
|
| 81 |
locatedBasicPrinter :: SExprPrinter (Located Text) (SExpr (Located Text))
|
| 82 |
locatedBasicPrinter = flatPrint unLoc
|
| 83 |
where unLoc (At _loc e) = e
|
6 | 6 |
HaskLikeAtom(..)
|
7 | 7 |
, haskLikeParser
|
8 | 8 |
, haskLikePrinter
|
| 9 |
, locatedHaskLikeParser
|
| 10 |
, locatedHaskLikePrinter
|
9 | 11 |
-- * Individual Parsers
|
10 | 12 |
, parseHaskellString
|
11 | 13 |
, parseHaskellFloat
|
12 | 14 |
, parseHaskellInt
|
13 | |
, locatedHaskLikeParser
|
14 | 15 |
) where
|
15 | 16 |
|
16 | 17 |
#if !MIN_VERSION_base(4,8,0)
|
|
172 | 173 |
locatedHaskLikeParser :: SExprParser (Located HaskLikeAtom) (SExpr (Located HaskLikeAtom))
|
173 | 174 |
locatedHaskLikeParser = mkParser $ located pHaskLikeAtom
|
174 | 175 |
|
175 | |
|
176 | 176 |
-- | This 'SExprPrinter' emits s-expressions that contain Scheme-like
|
177 | 177 |
-- tokens as well as string literals, integer literals, and floating-point
|
178 | 178 |
-- literals, which will be emitted as the literals produced by Haskell's
|
|
183 | 183 |
-- "(1 \"elephant\")"
|
184 | 184 |
haskLikePrinter :: SExprPrinter HaskLikeAtom (SExpr HaskLikeAtom)
|
185 | 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
|