2008-01-13 20:53:15 +03:00
|
|
|
|
-- |
|
2015-08-01 19:24:45 +03:00
|
|
|
|
-- Module : Text.Megaparsec.Language
|
|
|
|
|
-- Copyright : © 2015 Megaparsec contributors
|
2015-07-30 19:20:37 +03:00
|
|
|
|
-- © 2007 Paolo Martini
|
|
|
|
|
-- © 1999–2001 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
|
|
|
|
|
-- to instantiate a token parser (see "Text.Parsec.Token").
|
|
|
|
|
|
2015-08-01 19:24:45 +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
|
|
|
|
|
|
2015-08-01 19:24:45 +03:00
|
|
|
|
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 }
|
2008-01-22 07:06:32 +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.
|
2008-01-22 07:06:32 +03:00
|
|
|
|
|
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
|
|
|
|
|
2008-01-22 07:06:32 +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.
|
2008-01-22 07:06:32 +03:00
|
|
|
|
|
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
|
|
|
|
|
2008-01-22 07:06:32 +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
|
|
|
|
|
2008-01-22 07:06:32 +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
|
|
|
|
|
2008-01-22 07:06:32 +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 }
|