Skip to content

Add Text.Parsing.Parser.Language and Token modules. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,31 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-------------------------------------------------------------------------------

The `Text.Parsing.Parser.Token` and `Text.Parsing.Parser.Language` modules have
large amounts of code adopted from the Haskell library parsec. parsec's
license is reproduced below:

Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

This software is provided by the copyright holders "as is" and any express or
implied warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed. In no
event shall the copyright holders be liable for any direct, indirect,
incidental, special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use, data,
or profits; or business interruption) however caused and on any theory of
liability, whether in contract, strict liability, or tort (including
negligence or otherwise) arising in any way out of the use of this software,
even if advised of the possibility of such damage.
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"purescript-lists": "^0.7.0",
"purescript-maybe": "^0.3.0",
"purescript-strings": "~0.7.0",
"purescript-transformers": "^0.8.1"
"purescript-transformers": "^0.8.1",
"purescript-unicode": "^0.0.1",
"purescript-integers": "^0.2.0"
},
"devDependencies": {
"purescript-console": "^0.1.0",
Expand Down
5 changes: 4 additions & 1 deletion src/Text/Parsing/Parser/Combinators.purs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import Text.Parsing.Parser
(<?>) :: forall m s a. (Monad m) => ParserT s m a -> String -> ParserT s m a
(<?>) p msg = p <|> fail ("Expected " ++ msg)

-- | Flipped `(<?>)`.
(<??>) :: forall m s a. (Monad m) => String -> ParserT s m a -> ParserT s m a
(<??>) = flip (<?>)

-- | Wrap a parser with opening and closing markers.
-- |
-- | For example:
Expand Down Expand Up @@ -196,4 +200,3 @@ many1Till p end = do
x <- p
xs <- manyTill p end
return (x:xs)

119 changes: 119 additions & 0 deletions src/Text/Parsing/Parser/Language.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

module Text.Parsing.Parser.Language
( haskellDef
, haskell
, emptyDef
, haskellStyle
, javaStyle
)
where

import Prelude

import Control.Alt

import Text.Parsing.Parser
import Text.Parsing.Parser.String
import Text.Parsing.Parser.Token

-----------------------------------------------------------
-- Styles: haskellStyle, javaStyle
-----------------------------------------------------------

-- | 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.
haskellStyle :: LanguageDef
haskellStyle = LanguageDef (unGenLanguageDef emptyDef)
{ commentStart = "{-"
, commentEnd = "-}"
, commentLine = "--"
, nestedComments = true
, identStart = letter
, identLetter = alphaNum <|> oneOf ['_', '\'']
, opStart = op'
, opLetter = op'
, reservedOpNames = []
, reservedNames = []
, caseSensitive = true
}
where
op' :: forall m . (Monad m) => ParserT String m Char
op' = oneOf [':', '!', '#', '$', '%', '&', '*', '+', '.', '/', '<', '=', '>', '?', '@', '\\', '^', '|', '-', '~']

-- | 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.
javaStyle :: LanguageDef
javaStyle = LanguageDef (unGenLanguageDef emptyDef)
{ commentStart = "/*"
, commentEnd = "*/"
, commentLine = "//"
, nestedComments = true
, identStart = letter
, identLetter = alphaNum <|> oneOf ['_', '\'']
, reservedNames = []
, reservedOpNames = []
, caseSensitive = false
}

-----------------------------------------------------------
-- minimal language definition
--------------------------------------------------------

-- | 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.
emptyDef :: LanguageDef
emptyDef = LanguageDef
{ commentStart: ""
, commentEnd: ""
, commentLine: ""
, nestedComments: true
, identStart: letter <|> char '_'
, identLetter: alphaNum <|> oneOf ['_', '\'']
, opStart: op'
, opLetter: op'
, reservedOpNames: []
, reservedNames: []
, caseSensitive: true
}
where
op' :: forall m . (Monad m) => ParserT String m Char
op' = oneOf [':', '!', '#', '$', '%', '&', '*', '+', '.', '/', '<', '=', '>', '?', '@', '\\', '^', '|', '-', '~']

-- -----------------------------------------------------------
-- -- Haskell
-- -----------------------------------------------------------

-- | A lexer for the haskell language.
haskell :: TokenParser
haskell = makeTokenParser haskellDef

-- | The language definition for the Haskell language.
haskellDef :: LanguageDef
haskellDef =
case haskell98Def of
(LanguageDef def) -> LanguageDef def
{ identLetter = def.identLetter <|> char '#'
, reservedNames = def.reservedNames <>
["foreign","import","export","primitive"
,"_ccall_","_casm_"
,"forall"
]
}

-- | The language definition for the language Haskell98.
haskell98Def :: LanguageDef
haskell98Def = LanguageDef (unGenLanguageDef 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"
]
}
Loading