gdritter repos s-cargot / 6af98a2
Add printers for Located parsers ckoparkar 6 years ago
2 changed file(s) with 31 addition(s) and 3 deletion(s). Collapse all Expand all
66 basicParser
77 , basicPrinter
88 , locatedBasicParser
9 , locatedBasicPrinter
910 ) where
1011
1112 import Control.Applicative ((<$>))
1516 import Data.Functor.Identity (Identity)
1617 import Text.Parsec.Prim (ParsecT)
1718
18 import Data.SCargot.Common (Located, located)
19 import Data.SCargot.Common (Located(..), located)
1920 import Data.SCargot.Repr.Basic (SExpr)
2021 import Data.SCargot ( SExprParser
2122 , SExprPrinter
6869 -- "(1 elephant)"
6970 basicPrinter :: SExprPrinter Text (SExpr Text)
7071 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
66 HaskLikeAtom(..)
77 , haskLikeParser
88 , haskLikePrinter
9 , locatedHaskLikeParser
10 , locatedHaskLikePrinter
911 -- * Individual Parsers
1012 , parseHaskellString
1113 , parseHaskellFloat
1214 , parseHaskellInt
13 , locatedHaskLikeParser
1415 ) where
1516
1617 #if !MIN_VERSION_base(4,8,0)
172173 locatedHaskLikeParser :: SExprParser (Located HaskLikeAtom) (SExpr (Located HaskLikeAtom))
173174 locatedHaskLikeParser = mkParser $ located pHaskLikeAtom
174175
175
176176 -- | This 'SExprPrinter' emits s-expressions that contain Scheme-like
177177 -- tokens as well as string literals, integer literals, and floating-point
178178 -- literals, which will be emitted as the literals produced by Haskell's
183183 -- "(1 \"elephant\")"
184184 haskLikePrinter :: SExprPrinter HaskLikeAtom (SExpr HaskLikeAtom)
185185 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