Skip to content

Commit 4187411

Browse files
committed
Add Text.Parsing.Parser.Language and Token modules.
This commit adds the `Text.Parsing.Parser.Language` and `Text.Parsing.Parser.Token` modules. This is a straight port of modules of the same name in Haskell's `parsec` library. This commit also adds the `(<??>)` combinator, which acts like a flipped `(<?>)`. It is convenient to use with monadic parsers: ```purescript foo :: Parser String Unit foo = "some message" <??> do string "foo" bar pure unit ```
1 parent 60f4abb commit 4187411

File tree

5 files changed

+1219
-11
lines changed

5 files changed

+1219
-11
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"purescript-lists": "^0.7.0",
3030
"purescript-maybe": "^0.3.0",
3131
"purescript-strings": "~0.7.0",
32-
"purescript-transformers": "^0.8.1"
32+
"purescript-transformers": "^0.8.1",
33+
"purescript-unicode": "git://github.com/purescript-contrib/purescript-unicode#unicode",
34+
"purescript-integers": "^0.2.0"
3335
},
3436
"devDependencies": {
3537
"purescript-console": "^0.1.0",

src/Text/Parsing/Parser/Combinators.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ import Text.Parsing.Parser
3838
(<?>) :: forall m s a. (Monad m) => ParserT s m a -> String -> ParserT s m a
3939
(<?>) p msg = p <|> fail ("Expected " ++ msg)
4040

41+
-- | Flipped `(<?>)`.
42+
(<??>) :: forall m s a. (Monad m) => String -> ParserT s m a -> ParserT s m a
43+
(<??>) = flip (<?>)
44+
4145
-- | Wrap a parser with opening and closing markers.
4246
-- |
4347
-- | For example:
@@ -196,4 +200,3 @@ many1Till p end = do
196200
x <- p
197201
xs <- manyTill p end
198202
return (x:xs)
199-

src/Text/Parsing/Parser/Language.purs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
module Text.Parsing.Parser.Language
3+
-- ( haskellDef, haskell
4+
-- , mondrianDef, mondrian
5+
-- , emptyDef
6+
-- , haskellStyle
7+
-- , javaStyle
8+
-- )
9+
where
10+
11+
import Prelude
12+
13+
import Control.Alt
14+
15+
import Text.Parsing.Parser
16+
import Text.Parsing.Parser.String
17+
import Text.Parsing.Parser.Token
18+
19+
-----------------------------------------------------------
20+
-- Styles: haskellStyle, javaStyle
21+
-----------------------------------------------------------
22+
23+
-- | This is a minimal token definition for Haskell style languages. It
24+
-- defines the style of comments, valid identifiers and case
25+
-- sensitivity. It does not define any reserved words or operators.
26+
27+
haskellStyle :: LanguageDef
28+
haskellStyle = LanguageDef (unGenLanguageDef emptyDef)
29+
{ commentStart = "{-"
30+
, commentEnd = "-}"
31+
, commentLine = "--"
32+
, nestedComments = true
33+
, identStart = letter
34+
, identLetter = alphaNum <|> oneOf ['_', '\'']
35+
, opStart = op'
36+
, opLetter = op'
37+
, reservedOpNames = []
38+
, reservedNames = []
39+
, caseSensitive = true
40+
}
41+
where
42+
op' :: forall m . (Monad m) => ParserT String m Char
43+
op' = oneOf [':', '!', '#', '$', '%', '&', '*', '+', '.', '/', '<', '=', '>', '?', '@', '\\', '^', '|', '-', '~']
44+
45+
-- | This is a minimal token definition for Java style languages. It
46+
-- defines the style of comments, valid identifiers and case
47+
-- sensitivity. It does not define any reserved words or operators.
48+
49+
javaStyle :: LanguageDef
50+
javaStyle = LanguageDef (unGenLanguageDef emptyDef)
51+
{ commentStart = "/*"
52+
, commentEnd = "*/"
53+
, commentLine = "//"
54+
, nestedComments = true
55+
, identStart = letter
56+
, identLetter = alphaNum <|> oneOf ['_', '\'']
57+
, reservedNames = []
58+
, reservedOpNames = []
59+
, caseSensitive = false
60+
}
61+
62+
-----------------------------------------------------------
63+
-- minimal language definition
64+
--------------------------------------------------------
65+
66+
-- | This is the most minimal token definition. It is recommended to use
67+
-- this definition as the basis for other definitions. `emptyDef` has
68+
-- no reserved names or operators, is case sensitive and doesn't accept
69+
-- comments, identifiers or operators.
70+
71+
emptyDef :: LanguageDef
72+
emptyDef = LanguageDef
73+
{ commentStart: ""
74+
, commentEnd: ""
75+
, commentLine: ""
76+
, nestedComments: true
77+
, identStart: letter <|> char '_'
78+
, identLetter: alphaNum <|> oneOf ['_', '\'']
79+
, opStart: op'
80+
, opLetter: op'
81+
, reservedOpNames: []
82+
, reservedNames: []
83+
, caseSensitive: true
84+
}
85+
where
86+
op' :: forall m . (Monad m) => ParserT String m Char
87+
op' = oneOf [':', '!', '#', '$', '%', '&', '*', '+', '.', '/', '<', '=', '>', '?', '@', '\\', '^', '|', '-', '~']
88+
89+
-- -----------------------------------------------------------
90+
-- -- Haskell
91+
-- -----------------------------------------------------------
92+
93+
-- -- | A lexer for the haskell language.
94+
95+
haskell :: TokenParser
96+
haskell = makeTokenParser haskellDef
97+
98+
-- -- | The language definition for the Haskell language.
99+
100+
haskellDef :: LanguageDef
101+
haskellDef =
102+
case haskell98Def of
103+
(LanguageDef def) -> LanguageDef def
104+
{ identLetter = def.identLetter <|> char '#'
105+
, reservedNames = def.reservedNames <>
106+
["foreign","import","export","primitive"
107+
,"_ccall_","_casm_"
108+
,"forall"
109+
]
110+
}
111+
112+
-- -- | The language definition for the language Haskell98.
113+
114+
haskell98Def :: LanguageDef
115+
haskell98Def = LanguageDef (unGenLanguageDef haskellStyle)
116+
{ reservedOpNames = ["::","..","=","\\","|","<-","->","@","~","=>"]
117+
, reservedNames = [ "let","in","case","of","if","then","else"
118+
, "data","type"
119+
, "class","default","deriving","do","import"
120+
, "infix","infixl","infixr","instance","module"
121+
, "newtype","where"
122+
, "primitive"
123+
-- "as","qualified","hiding"
124+
]
125+
}

0 commit comments

Comments
 (0)