Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
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 ```
- Loading branch information
Showing
with
1,219 additions
and 11 deletions.
- +3 −1 bower.json
- +4 −1 src/Text/Parsing/Parser/Combinators.purs
- +125 −0 src/Text/Parsing/Parser/Language.purs
- +745 −0 src/Text/Parsing/Parser/Token.purs
- +342 −9 test/Main.purs
@@ -0,0 +1,125 @@ | ||
|
||
module Text.Parsing.Parser.Language | ||
-- ( haskellDef, haskell | ||
-- , 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" | ||
] | ||
} |
Oops, something went wrong.