haskell-src-exts-1.18.2: Manipulating Haskell source: abstract syntax, lexer, parser, and pretty-printer

Portabilityportable
Stabilitystable
MaintainerNiklas Broberg, d00nibro@chalmers.se
Safe HaskellNone

Language.Haskell.Exts.Parser

Contents

Description

Annotated parser for Haskell with extensions.

Synopsis

General parsing

class Parseable ast where

Class providing function for parsing at many different types.

Note that for convenience of implementation, the default methods have definitions equivalent to undefined. The minimal definition is all of the visible methods.

Methods

parse :: String -> ParseResult ast

Parse a string with default mode.

parseWithMode :: ParseMode -> String -> ParseResult ast

Parse a string with an explicit ParseMode.

parseWithComments :: ParseMode -> String -> ParseResult (ast, [Comment])

Parse a string with an explicit ParseMode, returning all comments along with the AST.

data ParseMode

Static parameters governing a parse. Note that the various parse functions in Language.Haskell.Exts.Parser never look at LANGUAGE pragmas, regardless of what the ignoreLanguagePragmas flag is set to. Only the various parseFile functions in Language.Haskell.Exts will act on it, when set to False.

Constructors

ParseMode 

Fields

parseFilename :: String

original name of the file being parsed

baseLanguage :: Language

base language (e.g. Haskell98, Haskell2010)

extensions :: [Extension]

list of extensions enabled for parsing

ignoreLanguagePragmas :: Bool

if True, the parser won't care about further extensions in LANGUAGE pragmas in source files

ignoreLinePragmas :: Bool

if True, the parser won't read line position information from LINE pragmas in source files

fixities :: Maybe [Fixity]

list of fixities to be aware of

ignoreFunctionArity :: Bool

Checks whether functions have a consistent arity

defaultParseMode :: ParseMode

Default parameters for a parse. The default is an unknown filename, no extensions (i.e. Haskell 98), don't ignore LANGUAGE pragmas, do ignore LINE pragmas, and be aware of fixities from the Prelude.

data ParseResult a

The result of a parse.

Constructors

ParseOk a

The parse succeeded, yielding a value.

ParseFailed SrcLoc String

The parse failed at the specified source location, with an error message.

Instances

Monad ParseResult 
Functor ParseResult 
Applicative ParseResult 
Show a => Show (ParseResult a) 
Monoid m => Monoid (ParseResult m) 

fromParseResult :: ParseResult a -> a

Retrieve the result of a successful parse, throwing an error if the parse is actually not successful.

Parsing of specific AST elements

Modules

parseModule :: String -> ParseResult (Module SrcSpanInfo)

Parse of a string, which should contain a complete Haskell module, using defaultParseMode.

parseModuleWithMode :: ParseMode -> String -> ParseResult (Module SrcSpanInfo)

Parse of a string containing a complete Haskell module, using an explicit ParseMode.

parseModuleWithComments :: ParseMode -> String -> ParseResult (Module SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Expressions

parseExp :: String -> ParseResult (Exp SrcSpanInfo)

Parse of a string containing a Haskell expression, using defaultParseMode.

parseExpWithMode :: ParseMode -> String -> ParseResult (Exp SrcSpanInfo)

Parse of a string containing a Haskell expression, using an explicit ParseMode.

parseExpWithComments :: ParseMode -> String -> ParseResult (Exp SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Statements

parseStmt :: String -> ParseResult (Stmt SrcSpanInfo)

Parse of a string containing a Haskell statement, using defaultParseMode.

parseStmtWithMode :: ParseMode -> String -> ParseResult (Stmt SrcSpanInfo)

Parse of a string containing a Haskell type, using an explicit ParseMode.

parseStmtWithComments :: ParseMode -> String -> ParseResult (Stmt SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Patterns

parsePat :: String -> ParseResult (Pat SrcSpanInfo)

Parse of a string containing a Haskell pattern, using defaultParseMode.

parsePatWithMode :: ParseMode -> String -> ParseResult (Pat SrcSpanInfo)

Parse of a string containing a Haskell pattern, using an explicit ParseMode.

parsePatWithComments :: ParseMode -> String -> ParseResult (Pat SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Declarations

parseDecl :: String -> ParseResult (Decl SrcSpanInfo)

Parse of a string containing a Haskell top-level declaration, using defaultParseMode.

parseDeclWithMode :: ParseMode -> String -> ParseResult (Decl SrcSpanInfo)

Parse of a string containing a Haskell top-level declaration, using an explicit ParseMode.

parseDeclWithComments :: ParseMode -> String -> ParseResult (Decl SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Types

parseType :: String -> ParseResult (Type SrcSpanInfo)

Parse of a string containing a Haskell type, using defaultParseMode.

parseTypeWithMode :: ParseMode -> String -> ParseResult (Type SrcSpanInfo)

Parse of a string containing a Haskell type, using an explicit ParseMode.

parseTypeWithComments :: ParseMode -> String -> ParseResult (Type SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Imports

parseImportDecl :: String -> ParseResult (ImportDecl SrcSpanInfo)

Parse of a string containing a Haskell statement, using defaultParseMode.

parseImportDeclWithMode :: ParseMode -> String -> ParseResult (ImportDecl SrcSpanInfo)

Parse of a string containing a Haskell type, using an explicit ParseMode.

parseImportDeclWithComments :: ParseMode -> String -> ParseResult (ImportDecl SrcSpanInfo, [Comment])

Parse of a string containing a complete Haskell module, using an explicit ParseMode, retaining comments.

Non-greedy parsers

newtype NonGreedy a

Instances of Parseable for NonGreedy a will only consume the input until a is fully parsed. This means that parse errors that come later in the input will be ignored. It's also more efficient, as it's fully lazy in the remainder of the input:

>>> parse (unlines ("module A where" : "main =" : repeat "blah")) :: ParseResult PragmasAndModuleHead
ParseOk (NonGreedy {unNonGreedy = PragmasAndModuleHead [] (ModuleName "A",Nothing,Nothing)})

(this example uses the simplified AST)

Constructors

NonGreedy 

Fields

unNonGreedy :: a
 

data ListOf a

ListOf a stores lists of the AST type a, along with a SrcSpanInfo, in order to provide Parseable instances for lists. These instances are provided when the type is used as a list in the syntax, and the same delimiters are used in all of its usages. Some exceptions are made:

Constructors

ListOf SrcSpanInfo [a] 

Instances

Functor ListOf 
Typeable1 ListOf 
Eq a => Eq (ListOf a) 
Data a => Data (ListOf a) 
Ord a => Ord (ListOf a) 
Show a => Show (ListOf a) 
Parseable (NonGreedy (ListOf (ModulePragma SrcSpanInfo))) 

unListOf :: ListOf a -> [a]

Module head parsers

getTopPragmas :: String -> ParseResult [ModulePragma SrcSpanInfo]

Non-greedy parse of a string starting with a series of top-level option pragmas.

data PragmasAndModuleName l

Type intended to be used with Parseable, with instances that implement a non-greedy parse of the module name, including top-level pragmas. This means that a parse error that comes after the module header won't be returned. If the Maybe value is Nothing, then this means that there was no module header.

Constructors

PragmasAndModuleName l [ModulePragma l] (Maybe (ModuleName l)) 

Instances

data PragmasAndModuleHead l

Constructors

PragmasAndModuleHead l [ModulePragma l] (Maybe (ModuleHead l)) 

Instances

data ModuleHeadAndImports l

Constructors

ModuleHeadAndImports l [ModulePragma l] (Maybe (ModuleHead l)) [ImportDecl l] 

Instances