parsec1-1.0.0.8: Portable monadic parser combinators
Copyright(c) Daan Leijen 1999-2001
LicenseBSD-style (see the file LICENSE)
MaintainerChristian Maeder <chr.maeder@web.de>
Stabilityprovisional
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell98

Text.ParserCombinators.Parsec.Prim

Description

The primitive parser combinators.

Synopsis

Documentation

(*>) :: Applicative f => f a -> f b -> f b infixl 4 #

Sequence actions, discarding the value of the first argument.

'as *> bs' can be understood as the do expression

do as
   bs

This is a tad complicated for our ApplicativeDo extension which will give it a Monad constraint. For an Applicative constraint we write it of the form

do _ <- as
   b <- bs
   pure b

(<$) :: Functor f => a -> f b -> f a infixl 4 #

Replace all locations in the input with the same value. The default definition is fmap . const, but this may be overridden with a more efficient version.

Using ApplicativeDo: 'a <$ bs' can be understood as the do expression

do bs
   pure a

with an inferred Functor constraint.

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #

An infix synonym for fmap.

The name of this operator is an allusion to $. Note the similarities between their types:

 ($)  ::              (a -> b) ->   a ->   b
(<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas $ is function application, <$> is function application lifted over a Functor.

Examples

Expand

Convert from a Maybe Int to a Maybe String using show:

>>> show <$> Nothing
Nothing
>>> show <$> Just 3
Just "3"

Convert from an Either Int Int to an Either Int String using show:

>>> show <$> Left 17
Left 17
>>> show <$> Right 17
Right "17"

Double each element of a list:

>>> (*2) <$> [1,2,3]
[2,4,6]

Apply even to the second element of a pair:

>>> even <$> (2,2)
(2,True)

(<*) :: Applicative f => f a -> f b -> f a infixl 4 #

Sequence actions, discarding the value of the second argument.

Using ApplicativeDo: 'as <* bs' can be understood as the do expression

do a <- as
   bs
   pure a

(<*>) :: Applicative f => f (a -> b) -> f a -> f b infixl 4 #

Sequential application.

A few functors support an implementation of <*> that is more efficient than the default one.

Using ApplicativeDo: 'fs <*> as' can be understood as the do expression

do f <- fs
   a <- as
   pure (f a)

(<?>) :: GenParser tok st a -> String -> GenParser tok st a infix 0 Source #

The parser p ? msg behaves as parser p, but whenever the parser p fails without consuming any input, it replaces expect error messages with the expect error message msg.

This is normally used at the end of a set alternatives where we want to return an error message in terms of a higher level construct rather than returning all possible characters. For example, if the expr parser from the try example would fail, the error message is: '...: expecting expression'. Without the (<?>) combinator, the message would be like '...: expecting "let" or letter', which is less friendly.

(<|>) :: Alternative f => f a -> f a -> f a infixl 3 #

An associative binary operation

data GenParser tok st a Source #

Instances

Instances details
Monad (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

(>>=) :: GenParser tok st a -> (a -> GenParser tok st b) -> GenParser tok st b #

(>>) :: GenParser tok st a -> GenParser tok st b -> GenParser tok st b #

return :: a -> GenParser tok st a #

Functor (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

fmap :: (a -> b) -> GenParser tok st a -> GenParser tok st b #

(<$) :: a -> GenParser tok st b -> GenParser tok st a #

MonadFail (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

fail :: String -> GenParser tok st a #

Applicative (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

pure :: a -> GenParser tok st a #

(<*>) :: GenParser tok st (a -> b) -> GenParser tok st a -> GenParser tok st b #

liftA2 :: (a -> b -> c) -> GenParser tok st a -> GenParser tok st b -> GenParser tok st c #

(*>) :: GenParser tok st a -> GenParser tok st b -> GenParser tok st b #

(<*) :: GenParser tok st a -> GenParser tok st b -> GenParser tok st a #

Alternative (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

empty :: GenParser tok st a #

(<|>) :: GenParser tok st a -> GenParser tok st a -> GenParser tok st a #

some :: GenParser tok st a -> GenParser tok st [a] #

many :: GenParser tok st a -> GenParser tok st [a] #

MonadPlus (GenParser tok st) Source # 
Instance details

Defined in Text.ParserCombinators.Parsec.Prim

Methods

mzero :: GenParser tok st a #

mplus :: GenParser tok st a -> GenParser tok st a -> GenParser tok st a #

type Parser a = GenParser Char () a Source #

parse :: GenParser tok () a -> SourceName -> [tok] -> Either ParseError a Source #

parse p filePath input runs a parser p without user state. The filePath is only used in error messages and may be the empty string. Returns either a ParseError (Left) or a value of type a (Right).

 main = case parse numbers "" "11, 2, 43" of
          Left err -> print err
          Right xs -> print (sum xs)

 numbers = commaSep integer 

parseTest :: Show a => GenParser tok () a -> [tok] -> IO () Source #

The expression parseTest p input applies a parser p against input input and prints the result to stdout. Used for testing parsers.

runParser :: GenParser tok st a -> st -> SourceName -> [tok] -> Either ParseError a Source #

The most general way to run a parser. runParser p state filePath input runs parser p on the input list of tokens input, obtained from source filePath with the initial user state st. The filePath is only used in error messages and may be the empty string. Returns either a ParseError (Left) or a value of type a (Right).

 parseFromFile p fname = do
   input <- readFile fname
   return (runParser p () fname input) 

label :: GenParser tok st a -> String -> GenParser tok st a Source #

labels :: GenParser tok st a -> [String] -> GenParser tok st a Source #

lookAhead :: GenParser tok st a -> GenParser tok st a Source #

lookAhead p parses p without consuming any input.

pzero :: GenParser tok st a Source #

token :: (tok -> String) -> (tok -> SourcePos) -> (tok -> Maybe a) -> GenParser tok st a Source #

The parser token showTok posFromTok testTok accepts a token t with result x when the function testTok t returns Just x. The source position of the t should be returned by posFromTok t and the token can be shown using showTok t.

This combinator is expressed in terms of tokenPrim. It is used to accept user defined token streams. For example, suppose that we have a stream of basic tokens tupled with source positions. We can than define a parser that accepts single tokens as:

 mytoken x
   = token showTok posFromTok testTok
   where
     showTok (pos,t)     = show t
     posFromTok (pos,t)  = pos
     testTok (pos,t)     = if x == t then Just t else Nothing 

tokenPrim :: (tok -> String) -> (SourcePos -> tok -> [tok] -> SourcePos) -> (tok -> Maybe a) -> GenParser tok st a Source #

The parser token showTok nextPos testTok accepts a token t with result x when the function testTok t returns Just x. The token can be shown using showTok t. The position of the next token should be returned when nextPos is called with the current source position pos, the current token t and the rest of the tokens toks, nextPos pos t toks.

This is the most primitive combinator for accepting tokens. For example, the char parser could be implemented as:

 char c
   = tokenPrim showChar nextPos testChar
   where
     showChar x        = "'" ++ x ++ "'"
     testChar x        = if x == c then Just x else Nothing
     nextPos pos x xs  = updatePosChar pos x 

tokenPrimEx :: (tok -> String) -> (SourcePos -> tok -> [tok] -> SourcePos) -> Maybe (SourcePos -> tok -> [tok] -> st -> st) -> (tok -> Maybe a) -> GenParser tok st a Source #

The most primitive token recogniser. The expression tokenPrimEx show nextpos mbnextstate test, recognises tokens when test returns Just x (and returns the value x). Tokens are shown in error messages using show. The position is calculated using nextpos, and finally, mbnextstate, can hold a function that updates the user state on every token recognised (nice to count tokens :-). The function is packed into a Maybe type for performance reasons.

tokens :: Eq tok => ([tok] -> String) -> (SourcePos -> [tok] -> SourcePos) -> [tok] -> GenParser tok st [tok] Source #

try :: GenParser tok st a -> GenParser tok st a Source #

The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs.

This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the (<|>) combinator will try its second alternative even when the first parser failed while consuming input.

The try combinator can for example be used to distinguish identifiers and reserved words. Both reserved words and identifiers are a sequence of letters. Whenever we expect a certain reserved word where we can also expect an identifier we have to use the try combinator. Suppose we write:

 expr        = letExpr <|> identifier <?> "expression"

 letExpr     = string "let" *> ...
 identifier  = many1 letter

If the user writes "lexical", the parser fails with: unexpected 'x', expecting 't' in "let". Indeed, since the (<|>) combinator only tries alternatives when the first alternative hasn't consumed input, the identifier parser is never tried (because the prefix "le" of the string "let" parser is already consumed). The right behaviour can be obtained by adding the try combinator:

 expr        = letExpr <|> identifier <?> "expression"

 letExpr     = try (string "let") *>
 identifier  = many1 letter 

unexpected :: String -> GenParser tok st a Source #

The parser unexpected msg always fails with an unexpected error message msg without consuming any input.

The parsers fail, (<?>) and unexpected are the three parsers used to generate error messages. Of these, only (<?>) is commonly used. For an example of the use of unexpected, see the definition of notFollowedBy.

many :: Alternative f => f a -> f [a] #

Zero or more.

skipMany :: GenParser tok st a -> GenParser tok st () Source #

skipMany p applies the parser p zero or more times, skipping its result.

 spaces = skipMany space 

getState :: GenParser tok st st Source #

Returns the current user state.

setState :: st -> GenParser tok st () Source #

setState st set the user state to st.

updateState :: (st -> st) -> GenParser tok st () Source #

updateState f applies function f to the user state. Suppose that we want to count identifiers in a source, we could use the user state as:

 expr  = do
   x <- identifier
   updateState (+1)
   return (Id x) 

data State tok st Source #

Constructors

State 

Fields

getInput :: GenParser tok st [tok] Source #

Returns the current input

getParserState :: GenParser tok st (State tok st) Source #

Returns the full parser state as a State record.

getPosition :: GenParser tok st SourcePos Source #

Returns the current source position. See also SourcePos.

setInput :: [tok] -> GenParser tok st () Source #

setInput input continues parsing with input.

setParserState :: State tok st -> GenParser tok st (State tok st) Source #

setParserState st set the full parser state to st.

setPosition :: SourcePos -> GenParser tok st () Source #

setPosition pos sets the current source position to pos.