| License | Unlicense |
|---|---|
| Safe Haskell | None |
| Language | GHC2021 |
Aihc.Parser
Description
This module provides parsing functions for Haskell source code.
The main entry point is parseModule for parsing complete Haskell modules.
Additional functions are provided for parsing individual expressions,
patterns, and types.
Synopsis
- parseModule :: ParserConfig -> Text -> ParseResult Module
- data ParserConfig = ParserConfig {}
- defaultConfig :: ParserConfig
- data ParseResult a
- type ParseErrorBundle = ParseErrorBundle TokStream Void
- errorBundlePretty :: ParseErrorBundle -> String
- parseExpr :: ParserConfig -> Text -> ParseResult Expr
- parseType :: ParserConfig -> Text -> ParseResult Type
- parsePattern :: ParserConfig -> Text -> ParseResult Pattern
Parsing modules
parseModule :: ParserConfig -> Text -> ParseResult Module Source #
Parse a complete Haskell module.
>>>shorthand $ parseModule defaultConfig "module Main where\nmain = putStrLn \"Hello\""ParseOk (Module {name = "Main", decls = [DeclValue (FunctionBind "main" [Match {rhs = UnguardedRhs (EApp (EVar "putStrLn") (EString "Hello"))}])]})
Modules without a header are also supported:
>>>case parseModule defaultConfig "x = 1" of { ParseOk m -> moduleName m; ParseErr _ -> Just "error" }Nothing
Configuration
data ParserConfig Source #
Constructors
| ParserConfig | |
Fields | |
Instances
defaultConfig :: ParserConfig Source #
Default parser configuration.
parserSourceNameis set to"<input>"parserExtensionsis empty (no extensions enabled by default)
>>>parserSourceName defaultConfig"<input>"
>>>parserExtensions defaultConfig[]
Parse results
data ParseResult a Source #
Constructors
| ParseOk a | |
| ParseErr ParseErrorBundle |
Instances
| Shorthand a => Shorthand (ParseResult a) Source # | |
Defined in Aihc.Parser.Shorthand Methods shorthand :: ParseResult a -> Doc () Source # | |
| NFData a => NFData (ParseResult a) Source # | |
Defined in Aihc.Parser.Types Methods rnf :: ParseResult a -> () # | |
| Show a => Show (ParseResult a) Source # | |
Defined in Aihc.Parser.Types Methods showsPrec :: Int -> ParseResult a -> ShowS # show :: ParseResult a -> String # showList :: [ParseResult a] -> ShowS # | |
| Eq a => Eq (ParseResult a) Source # | |
Defined in Aihc.Parser.Types Methods (==) :: ParseResult a -> ParseResult a -> Bool # (/=) :: ParseResult a -> ParseResult a -> Bool # | |
type ParseErrorBundle = ParseErrorBundle TokStream Void Source #
Parse error from token parser. Use errorBundlePretty from Parser to render.
errorBundlePretty :: ParseErrorBundle -> String Source #
Pretty-print a parse error bundle.
Parsing expressions, patterns, and types
parseExpr :: ParserConfig -> Text -> ParseResult Expr Source #
Parse a Haskell expression.
>>>shorthand $ parseExpr defaultConfig "1 + 2"ParseOk (EInfix (EInt 1) "+" (EInt 2))
>>>shorthand $ parseExpr defaultConfig "\\x -> x + 1"ParseOk (ELambdaPats [PVar "x"] (EInfix (EVar "x") "+" (EInt 1)))
Parse errors are returned as ParseErr:
>>>case parseExpr defaultConfig "1 +" of { ParseErr _ -> "error"; ParseOk _ -> "ok" }"error"
parseType :: ParserConfig -> Text -> ParseResult Type Source #
Parse a Haskell type.
>>>shorthand $ parseType defaultConfig "Int -> Bool"ParseOk (TFun (TCon "Int") (TCon "Bool"))
>>>shorthand $ parseType defaultConfig "Maybe a"ParseOk (TApp (TCon "Maybe") (TVar "a"))
parsePattern :: ParserConfig -> Text -> ParseResult Pattern Source #
Parse a Haskell pattern.
>>>shorthand $ parsePattern defaultConfig "(x, y)"ParseOk (PTuple [PVar "x", PVar "y"])
>>>shorthand $ parsePattern defaultConfig "Just x"ParseOk (PCon "Just" [PVar "x"])