Copyright | (c) Alec Theriault 2017-2018 |
---|---|
License | BSD-style |
Maintainer | alec.theriault@gmail.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
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
.
- data P a
- execParser :: P a -> InputStream -> Position -> Either ParseFail a
- execParser' :: P a -> InputStream -> Position -> (Token -> Token) -> Either ParseFail a
- initPos :: Position
- data PState = PState {
- curPos :: !Position
- curInput :: !InputStream
- prevPos :: Position
- pushedTokens :: [Spanned Token]
- swapFunction :: Token -> Token
- getPState :: P PState
- setPState :: PState -> P ()
- getPosition :: P Position
- setPosition :: Position -> P ()
- getInput :: P InputStream
- setInput :: InputStream -> P ()
- popToken :: P (Maybe (Spanned Token))
- pushToken :: Spanned Token -> P ()
- swapToken :: Token -> P Token
- data ParseFail = ParseFail Position String
- parseError :: Show b => b -> P a
Parsing monad
Parsing and lexing monad. A value of type
represents a parser that can be run (using
P
aexecParser
) to possibly produce a value of type 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.
State that the lexer and parser share
PState | |
|
Monadic operations
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.
pushToken :: Spanned Token -> P () Source #
Manually push a
. This turns out to be useful when parsing tokens that need
to be broken up. For example, when seeing a Spanned
Token
GreaterEqual
token but only
expecting a Greater
token, one can consume the
GreaterEqual
token and push back an Equal
token.
Error reporting
Exceptions that occur during parsing
parseError :: Show b => b -> P a Source #
Signal a syntax error.