Debian Codegraph @

Data/SCargot/Language/Basic.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Data.SCargot.Language.Basic
4 ( -- * Spec
5 -- $descr
6 basicParser
7 , basicPrinter
8 , locatedBasicParser
9 , locatedBasicPrinter
10 ) where
11
12 import Control.Applicative ((<$>))
13 import Data.Char (isAlphaNum)
14 import Text.Parsec (many1, satisfy)
15 import Data.Text (Text, pack)
16 import Data.Functor.Identity (Identity)
17 import Text.Parsec.Prim (ParsecT)
18
19 import Data.SCargot.Common (Located(..), located)
20 import Data.SCargot.Repr.Basic (SExpr)
21 import Data.SCargot ( SExprParser
22 , SExprPrinter
23 , mkParser
24 , flatPrint
25 )
26
27 isAtomChar :: Char -> Bool
28 isAtomChar c = isAlphaNum c
29 || c == '-' || c == '*' || c == '/'
30 || c == '+' || c == '<' || c == '>'
31 || c == '=' || c == '!' || c == '?'
32
33 pToken :: ParsecT Text a Identity Text
34 pToken = pack <$> many1 (satisfy isAtomChar)
35
36 -- $descr
37 -- The 'basicSpec' describes S-expressions whose atoms are simply
38 -- text strings that contain alphanumeric characters and a small
39 -- set of punctuation. It does no parsing of numbers or other data
40 -- types, and will accept tokens that typical Lisp implementations
41 -- would find nonsensical (like @77foo@).
42 --
43 -- Atoms recognized by the 'basicSpec' are any string matching the
44 -- regular expression @[A-Za-z0-9+*<>/=!?-]+@.
45
46 -- | A 'SExprParser' that understands atoms to be sequences of
47 -- alphanumeric characters as well as the punctuation
48 -- characters @[-*/+<>=!?]@, and does no processing of them.
49 --
50 -- >>> decode basicParser "(1 elephant)"
51 -- Right [SCons (SAtom "1") (SCons (SAtom "elephant") SNil)]
52 basicParser :: SExprParser Text (SExpr Text)
53 basicParser = mkParser pToken
54
55 -- | A 'basicParser' which produces 'Located' values
56 --
57 -- >>> decode locatedBasicParser $ pack "(1 elephant)"
58 -- Right [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)]
59 --
60 -- >>> decode locatedBasicParser $ pack "(let ((x 1))\n x)"
61 -- Right [SCons (SAtom (At (Span (line 1, column 2) (line 1, column 5)) "let")) (SCons (SCons (SCons (SAtom (At (Span (line 1, column 8) (line 1, column 9)) "x")) (SCons (SAtom (At (Span (line 1, column 10) (line 1, column 11)) "1")) SNil)) SNil) (SCons (SAtom (At (Span (line 2, column 3) (line 2, column 4)) "x")) SNil))]
62 locatedBasicParser :: SExprParser (Located Text) (SExpr (Located Text))
63 locatedBasicParser = mkParser $ located pToken
64
65 -- | A 'SExprPrinter' that prints textual atoms directly (without quoting
66 -- or any other processing) onto a single line.
67 --
68 -- >>> encode basicPrinter [L [A "1", A "elephant"]]
69 -- "(1 elephant)"
70 basicPrinter :: SExprPrinter Text (SExpr Text)
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