Skip to content

Commit cd65474

Browse files
committed
Merge pull request #27 from cdepillabout/token
Add Text.Parsing.Parser.Language and Token modules.
2 parents 60f4abb + cc562ab commit cd65474

File tree

6 files changed

+1297
-12
lines changed

6 files changed

+1297
-12
lines changed

LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,31 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1818
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1919
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2020
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
-------------------------------------------------------------------------------
23+
24+
The `Text.Parsing.Parser.Token` and `Text.Parsing.Parser.Language` modules have
25+
large amounts of code adopted from the Haskell library parsec. parsec's
26+
license is reproduced below:
27+
28+
Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.
29+
30+
Redistribution and use in source and binary forms, with or without
31+
modification, are permitted provided that the following conditions are met:
32+
33+
* Redistributions of source code must retain the above copyright notice,
34+
this list of conditions and the following disclaimer.
35+
* Redistributions in binary form must reproduce the above copyright
36+
notice, this list of conditions and the following disclaimer in the
37+
documentation and/or other materials provided with the distribution.
38+
39+
This software is provided by the copyright holders "as is" and any express or
40+
implied warranties, including, but not limited to, the implied warranties of
41+
merchantability and fitness for a particular purpose are disclaimed. In no
42+
event shall the copyright holders be liable for any direct, indirect,
43+
incidental, special, exemplary, or consequential damages (including, but not
44+
limited to, procurement of substitute goods or services; loss of use, data,
45+
or profits; or business interruption) however caused and on any theory of
46+
liability, whether in contract, strict liability, or tort (including
47+
negligence or otherwise) arising in any way out of the use of this software,
48+
even if advised of the possibility of such damage.

bower.json

+3-1
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": "^0.0.1",
34+
"purescript-integers": "^0.2.0"
3335
},
3436
"devDependencies": {
3537
"purescript-console": "^0.1.0",

src/Text/Parsing/Parser/Combinators.purs

+4-1
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

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

0 commit comments

Comments
 (0)