Language.hs 5.33 KB
Newer Older
1
2
{-# LANGUAGE Safe #-}

DerekElkins's avatar
DerekElkins committed
3
4
5
6
7
-----------------------------------------------------------------------------
-- |
-- Module      :  Text.Parsec.Language
-- Copyright   :  (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
-- License     :  BSD-style (see the LICENSE file)
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
8
--
DerekElkins's avatar
DerekElkins committed
9
-- Maintainer  :  derek.a.elkins@gmail.com
DerekElkins's avatar
DerekElkins committed
10
11
12
13
14
-- Stability   :  provisional
-- Portability :  non-portable (uses non-portable module Text.Parsec.Token)
--
-- A helper module that defines some language definitions that can be used
-- to instantiate a token parser (see "Text.Parsec.Token").
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
15
--
DerekElkins's avatar
DerekElkins committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-----------------------------------------------------------------------------

module Text.Parsec.Language
    ( haskellDef, haskell
    , mondrianDef, mondrian
    , emptyDef
    , haskellStyle
    , javaStyle
    , LanguageDef
    , GenLanguageDef
    ) where

import Text.Parsec
import Text.Parsec.Token

-----------------------------------------------------------
-- Styles: haskellStyle, javaStyle
-----------------------------------------------------------
34
35
36
37
38

-- | This is a minimal token definition for Haskell style languages. It
-- defines the style of comments, valid identifiers and case
-- sensitivity. It does not define any reserved words or operators.

DerekElkins's avatar
DerekElkins committed
39
haskellStyle :: LanguageDef st
40
haskellStyle = emptyDef
DerekElkins's avatar
DerekElkins committed
41
42
43
44
45
                { commentStart   = "{-"
                , commentEnd     = "-}"
                , commentLine    = "--"
                , nestedComments = True
                , identStart     = letter
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
46
47
48
                , identLetter    = alphaNum <|> oneOf "_'"
                , opStart        = opLetter haskellStyle
                , opLetter       = oneOf ":!#$%&*+./<=>?@\\^|-~"
DerekElkins's avatar
DerekElkins committed
49
50
51
52
53
                , reservedOpNames= []
                , reservedNames  = []
                , caseSensitive  = True
                }

54
55
56
57
-- | This is a minimal token definition for Java style languages. It
-- defines the style of comments, valid identifiers and case
-- sensitivity. It does not define any reserved words or operators.

DerekElkins's avatar
DerekElkins committed
58
59
javaStyle  :: LanguageDef st
javaStyle   = emptyDef
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
60
61
62
63
64
65
66
67
                { commentStart   = "/*"
                , commentEnd     = "*/"
                , commentLine    = "//"
                , nestedComments = True
                , identStart     = letter
                , identLetter    = alphaNum <|> oneOf "_'"
                , reservedNames  = []
                , reservedOpNames= []
DerekElkins's avatar
DerekElkins committed
68
                , caseSensitive  = False
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
69
                }
DerekElkins's avatar
DerekElkins committed
70
71
72

-----------------------------------------------------------
-- minimal language definition
73
74
--------------------------------------------------------

75
-- | This is the most minimal token definition. It is recommended to use
76
77
78
79
-- this definition as the basis for other definitions. @emptyDef@ has
-- no reserved names or operators, is case sensitive and doesn't accept
-- comments, identifiers or operators.

DerekElkins's avatar
DerekElkins committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
emptyDef   :: LanguageDef st
emptyDef    = LanguageDef
               { commentStart   = ""
               , commentEnd     = ""
               , commentLine    = ""
               , nestedComments = True
               , identStart     = letter <|> char '_'
               , identLetter    = alphaNum <|> oneOf "_'"
               , opStart        = opLetter emptyDef
               , opLetter       = oneOf ":!#$%&*+./<=>?@\\^|-~"
               , reservedOpNames= []
               , reservedNames  = []
               , caseSensitive  = True
               }



-----------------------------------------------------------
-- Haskell
-----------------------------------------------------------
100

Eric Lindblad's avatar
Eric Lindblad committed
101
-- | A lexer for the Haskell language.
102

DerekElkins's avatar
DerekElkins committed
103
104
105
haskell :: TokenParser st
haskell      = makeTokenParser haskellDef

106
107
-- | The language definition for the Haskell language.

DerekElkins's avatar
DerekElkins committed
108
109
haskellDef  :: LanguageDef st
haskellDef   = haskell98Def
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
110
111
112
113
114
115
                { identLetter    = identLetter haskell98Def <|> char '#'
                , reservedNames  = reservedNames haskell98Def ++
                                   ["foreign","import","export","primitive"
                                   ,"_ccall_","_casm_"
                                   ,"forall"
                                   ]
DerekElkins's avatar
DerekElkins committed
116
117
                }

118
119
-- | The language definition for the language Haskell98.

DerekElkins's avatar
DerekElkins committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
haskell98Def :: LanguageDef st
haskell98Def = haskellStyle
                { reservedOpNames= ["::","..","=","\\","|","<-","->","@","~","=>"]
                , reservedNames  = ["let","in","case","of","if","then","else",
                                    "data","type",
                                    "class","default","deriving","do","import",
                                    "infix","infixl","infixr","instance","module",
                                    "newtype","where",
                                    "primitive"
                                    -- "as","qualified","hiding"
                                   ]
                }


-----------------------------------------------------------
-- Mondrian
-----------------------------------------------------------
137

Eric Lindblad's avatar
Eric Lindblad committed
138
-- | A lexer for the Mondrian language.
139

DerekElkins's avatar
DerekElkins committed
140
141
142
mondrian :: TokenParser st
mondrian    = makeTokenParser mondrianDef

143
144
-- | The language definition for the language Mondrian.

DerekElkins's avatar
DerekElkins committed
145
146
mondrianDef :: LanguageDef st
mondrianDef = javaStyle
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
147
148
149
                { reservedNames = [ "case", "class", "default", "extends"
                                  , "import", "in", "let", "new", "of", "package"
                                  ]
DerekElkins's avatar
DerekElkins committed
150
                , caseSensitive  = True
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
151
                }