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 1 commit
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
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": "git://github.com/purescript-contrib/purescript-unicode#unicode",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should publish this to bower. Never actually commit git dependencies, especially when it's not a SHA.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelficarra Thanks for taking a look at this PR.

I am planning to publish purescript-unicode to bower, but I am waiting for this PR to get reviewed.

I don't think I explained it well enough in the PR comments, but this PR shouldn't be merged until purescript-unicode is released. (Or if we decide not to use the purescript-unicode dependency, then it doesn't matter.)

"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)

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

module Text.Parsing.Parser.Language
-- ( haskellDef, haskell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have explicit exports.

-- , mondrianDef, mondrian
-- , 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