language-rust-0.1.0.0: Parsing and pretty printing of Rust code

Copyright(c) Alec Theriault 2017-2018
LicenseBSD-style
Maintaineralec.theriault@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Language.Rust.Parser.ParseMonad

Contents

Description

Both the lexer and the parser run inside of the P monad. As detailed in the section on on threaded-lexers in Happy's instruction manual, the benefits of this are that:

  • Lexical errors can be treated in the same way as parse errors
  • Information such as the current position in the file shared between the lexer and parser
  • General information can be passed back from the parser to the lexer too

In our case, this shared information is held in PState.

Synopsis

Parsing monad

data P a Source #

Parsing and lexing monad. A value of type P a represents a parser that can be run (using execParser) to possibly produce a value of type a.

Instances

Monad P Source # 

Methods

(>>=) :: P a -> (a -> P b) -> P b #

(>>) :: P a -> P b -> P b #

return :: a -> P a #

fail :: String -> P a #

Functor P Source # 

Methods

fmap :: (a -> b) -> P a -> P b #

(<$) :: a -> P b -> P a #

Applicative P Source # 

Methods

pure :: a -> P a #

(<*>) :: P (a -> b) -> P a -> P b #

(*>) :: P a -> P b -> P b #

(<*) :: P a -> P b -> P a #

execParser :: P a -> InputStream -> Position -> Either ParseFail a Source #

Execute the given parser on the supplied input stream at the given start position, returning either the position of an error and the error message, or the value parsed.

execParser' :: P a -> InputStream -> Position -> (Token -> Token) -> Either ParseFail a Source #

Generalized version of execParser that expects an extra argument that lets you hot-swap a token that was just lexed before it gets passed to the parser.

initPos :: Position Source #

Starting position in a file.

data PState Source #

State that the lexer and parser share

Constructors

PState 

Fields

Monadic operations

getPState :: P PState Source #

Extract the state stored in the parser.

setPState :: PState -> P () Source #

Update the state stored in the parser.

getPosition :: P Position Source #

Retrieve the current position of the parser.

setPosition :: Position -> P () Source #

Update the current position of the parser.

getInput :: P InputStream Source #

Retrieve the current InputStream of the parser.

setInput :: InputStream -> P () Source #

Update the current InputStream of the parser.

popToken :: P (Maybe (Spanned Token)) Source #

Manually pop a Spanned Token (if there are no tokens to pop, returns Nothing). See pushToken for more details.

pushToken :: Spanned Token -> P () Source #

Manually push a Spanned Token. This turns out to be useful when parsing tokens that need to be broken up. For example, when seeing a GreaterEqual token but only expecting a Greater token, one can consume the GreaterEqual token and push back an Equal token.

swapToken :: Token -> P Token Source #

Swap a token using the swap function.

Error reporting

parseError :: Show b => b -> P a Source #

Signal a syntax error.