megaparsec/Text/Megaparsec/Language.hs

115 lines
3.8 KiB
Haskell
Raw Normal View History

2008-01-13 20:53:15 +03:00
-- |
-- Module : Text.Megaparsec.Language
-- Copyright : © 2015 Megaparsec contributors
-- © 2007 Paolo Martini
-- © 19992001 Daan Leijen
2015-07-28 16:32:19 +03:00
-- License : BSD3
--
-- Maintainer : Mark Karpov <markkarpov@opmbx.org>
2015-07-29 11:38:32 +03:00
-- Stability : experimental
2008-01-13 20:53:15 +03:00
-- Portability : non-portable (uses non-portable module Text.Parsec.Token)
--
-- A helper module that defines some language definitions that can be used
2015-08-12 15:41:22 +03:00
-- to instantiate a token parser (see "Text.Megaparsec.Token").
2008-01-13 20:53:15 +03:00
module Text.Megaparsec.Language
2015-07-29 11:38:32 +03:00
( LanguageDef
2008-01-13 20:53:15 +03:00
, emptyDef
, haskellStyle
, javaStyle
2015-07-29 11:38:32 +03:00
, haskellDef
, mondrianDef )
2015-07-28 16:32:19 +03:00
where
2008-01-13 20:53:15 +03:00
2015-07-30 21:36:54 +03:00
import Control.Monad.Identity
import Text.Megaparsec
import Text.Megaparsec.Token
2008-01-13 20:53:15 +03:00
2015-07-29 11:38:32 +03:00
-- | This is the most minimal token definition. It is recommended to use
-- 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.
2015-07-30 21:36:54 +03:00
emptyDef :: LanguageDef String st Identity
2015-07-29 11:38:32 +03:00
emptyDef =
LanguageDef
{ commentStart = ""
, commentEnd = ""
, commentLine = ""
, nestedComments = True
, identStart = letter <|> char '_'
, identLetter = alphaNum <|> oneOf "_'"
, opStart = opLetter emptyDef
, opLetter = oneOf ":!#$%&*+./<=>?@\\^|-~"
, reservedOpNames = []
, reservedNames = []
, caseSensitive = True }
2015-08-12 15:41:22 +03:00
-- | This is a minimal token definition for Haskell-style languages. It
2015-07-29 11:38:32 +03:00
-- defines the style of comments, valid identifiers and case sensitivity. It
-- does not define any reserved words or operators.
2015-07-30 21:36:54 +03:00
haskellStyle :: LanguageDef String u Identity
2015-07-29 11:38:32 +03:00
haskellStyle =
emptyDef
{ commentStart = "{-"
, commentEnd = "-}"
, commentLine = "--"
, nestedComments = True
, identStart = letter
, identLetter = alphaNum <|> oneOf "_'"
, opStart = opLetter haskellStyle
, opLetter = oneOf ":!#$%&*+./<=>?@\\^|-~"
, reservedOpNames = []
, reservedNames = []
, caseSensitive = True }
2008-01-13 20:53:15 +03:00
2015-08-12 15:41:22 +03:00
-- | This is a minimal token definition for Java-style languages. It
2015-07-29 11:38:32 +03:00
-- defines the style of comments, valid identifiers and case sensitivity. It
-- does not define any reserved words or operators.
2015-07-30 21:36:54 +03:00
javaStyle :: LanguageDef String u Identity
2015-07-29 11:38:32 +03:00
javaStyle =
emptyDef
{ commentStart = "/*"
, commentEnd = "*/"
, commentLine = "//"
, nestedComments = True
, identStart = letter
, identLetter = alphaNum <|> oneOf "_'"
, reservedNames = []
, reservedOpNames = []
, caseSensitive = False }
2008-01-13 20:53:15 +03:00
-- | The language definition for the Haskell language.
2015-07-30 21:36:54 +03:00
haskellDef :: LanguageDef String u Identity
2015-07-29 11:38:32 +03:00
haskellDef =
haskell98Def
{ identLetter = identLetter haskell98Def <|> char '#'
, reservedNames = reservedNames haskell98Def ++
[ "foreign", "import", "export", "primitive"
, "_ccall_", "_casm_", "forall"] }
2008-01-13 20:53:15 +03:00
-- | The language definition for the language Haskell98.
2015-07-30 21:36:54 +03:00
haskell98Def :: LanguageDef String u Identity
2015-07-29 11:38:32 +03:00
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" ] }
2008-01-13 20:53:15 +03:00
-- | The language definition for the language Mondrian.
2015-07-30 21:36:54 +03:00
mondrianDef :: LanguageDef String u Identity
2015-07-29 11:38:32 +03:00
mondrianDef =
javaStyle
{ reservedNames = [ "case", "class", "default", "extends"
, "import", "in", "let", "new", "of", "package" ]
, caseSensitive = True }