gdritter repos s-cargot / eec5001
Merge pull request #8 from ckoparkar/master Add printers for Located parsers G. D. Ritter authored 7 years ago GitHub committed 7 years ago
3 changed file(s) with 41 addition(s) and 4 deletion(s). Collapse all Expand all
2626 , commonLispNumberAnyBase
2727 , gnuM4NumberAnyBase
2828 -- ** Source locations
29 , Location(..), Located(..), located
29 , Location(..), Located(..), located, dLocation
3030 ) where
3131
3232 #if !MIN_VERSION_base(4,8,0)
3737 import Data.Text (Text)
3838 import qualified Data.Text as T
3939 import Text.Parsec
40 import Text.Parsec.Pos (newPos)
4041 import Text.Parsec.Text (Parser)
4142
4243 -- | Parse an identifier according to the R5RS Scheme standard. This
353354 end <- getPosition
354355 return $ At (Span begin end) result
355356
357 -- | A default location value
358 dLocation :: Location
359 dLocation = Span dPos dPos
360 where dPos = newPos "" 0 0
356361
357362 {- $intro
358363
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
1 {-# LANGUAGE FlexibleInstances #-}
12 {-# LANGUAGE OverloadedStrings #-}
23
34 module Data.SCargot.Language.HaskLike
56 HaskLikeAtom(..)
67 , haskLikeParser
78 , haskLikePrinter
9 , locatedHaskLikeParser
10 , locatedHaskLikePrinter
811 -- * Individual Parsers
912 , parseHaskellString
1013 , parseHaskellFloat
1114 , parseHaskellInt
12 , locatedHaskLikeParser
1315 ) where
1416
1517 #if !MIN_VERSION_base(4,8,0)
5658
5759 instance IsString HaskLikeAtom where
5860 fromString = HSIdent . fromString
61
62 instance IsString (Located HaskLikeAtom) where
63 fromString = (At dLocation) . HSIdent . fromString
5964
6065 -- | Parse a Haskell string literal as defined by the Haskell 2010
6166 -- language specification.
168173 locatedHaskLikeParser :: SExprParser (Located HaskLikeAtom) (SExpr (Located HaskLikeAtom))
169174 locatedHaskLikeParser = mkParser $ located pHaskLikeAtom
170175
171
172176 -- | This 'SExprPrinter' emits s-expressions that contain Scheme-like
173177 -- tokens as well as string literals, integer literals, and floating-point
174178 -- literals, which will be emitted as the literals produced by Haskell's
179183 -- "(1 \"elephant\")"
180184 haskLikePrinter :: SExprPrinter HaskLikeAtom (SExpr HaskLikeAtom)
181185 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