-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathParserCommon.hs
57 lines (45 loc) · 2.17 KB
/
ParserCommon.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE PackageImports, ConstraintKinds, StandaloneDeriving,
GeneralizedNewtypeDeriving, FlexibleInstances #-}
module ParserCommon where
import Text.Trifecta
import Text.Trifecta.Delta
import "indentation-trifecta" Text.Trifecta.Indentation
import Text.Parser.Token as Tok
import Text.Parser.Token.Style
import qualified Text.Parser.Token.Highlight as Hi
import Control.Applicative
import Control.Monad
import Data.Char
import qualified Data.Map.Strict as M
import qualified Data.HashSet as HashSet
newtype FrankParser t m a =
FrankParser { runFrankParser :: IndentationParserT t m a }
deriving (Functor, Alternative, Applicative, Monad, Parsing
, IndentationParsing, MonadPlus)
deriving instance (DeltaParsing m) => (DeltaParsing (FrankParser Token m))
deriving instance (DeltaParsing m) => (CharParsing (FrankParser Char m))
deriving instance (DeltaParsing m) => (CharParsing (FrankParser Token m))
deriving instance (DeltaParsing m) => (TokenParsing (FrankParser Char m))
frankCommentStyle :: CommentStyle
frankCommentStyle = CommentStyle "{--" "--}" "--" True
instance DeltaParsing m => TokenParsing (FrankParser Token m) where
someSpace = FrankParser $ buildSomeSpaceParser someSpace frankCommentStyle
nesting = FrankParser . nesting . runFrankParser
semi = FrankParser $ runFrankParser semi
highlight h = FrankParser . highlight h . runFrankParser
token p = (FrankParser $ token (runFrankParser p)) <* whiteSpace
type MonadicParsing m = (TokenParsing m, IndentationParsing m, Monad m, DeltaParsing m)
frankStyle :: MonadicParsing m => IdentifierStyle m
frankStyle = IdentifierStyle {
_styleName = "Frank"
, _styleStart = satisfy (\c -> isAlpha c || c == '_')
, _styleLetter = satisfy (\c -> isAlphaNum c || c == '_' || c == '\'')
, _styleReserved = HashSet.fromList [ "data", "interface"
, "let", "in"
, "String", "Int", "Char"]
, _styleHighlight = Hi.Identifier
, _styleReservedHighlight = Hi.ReservedIdentifier }
identifier :: MonadicParsing m => m String
identifier = Tok.ident frankStyle
reserved :: MonadicParsing m => String -> m ()
reserved = Tok.reserve frankStyle