| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Miso.Util.Lexer
Contents
Description
Overview
Miso.Util.Lexer is an internal lexer combinator library used by miso's JSON pipeline (Miso.JSON.Lexer) and URI/router tokeniser (Miso.Router). It is not designed for general application use, but is exposed for downstream code that needs to build custom lexers.
The central type is Lexer:
newtypeLexertoken = Lexer {runLexer::Stream-> EitherLexerError(token,Stream) }
Lexer is a Monad, Alternative, and MonadFail. Its
Alternative instance implements the
maximal munch rule: when both branches succeed, the one that
consumes the most input wins.
Key types
Stream— the remaining input text together with aLocation(line and column) cursor.Location—{line :: Int, column :: (Int, Int)}, used in error messages andLocatedtokens.Locatedtoken — a lexed value paired with theLocationat which it was recognised.LexerError— eitherLexerError MisoString Location(unexpected input) orUnexpectedEOF Location(ran out of input).
Primitive combinators
satisfy:: (Char -> Bool) ->LexerChar -- consume one matching charchar:: Char ->LexerChar -- consume a specific charstring::MisoString->LexerMisoString-- consume a literal prefixstring':: String ->LexerString -- same forStringpeek::Lexer(Maybe Char) -- look ahead without consumingoops::Lexertoken -- always fails
Stream and location access
getInput::LexerStreamputInput::Stream->Lexer()modifyInput:: (Stream->Stream) ->Lexer()getLocation::LexerLocationsetLocation::Location->Lexer()
Running a lexer
runLexer::Lexertoken ->Stream-> EitherLexerError(token,Stream)mkStream::MisoString->Stream-- create initial stream
See also
- Miso.Util.Parser — parser combinator library that consumes
Lexeroutput - Miso.JSON.Lexer — JSON tokeniser built on this module
- Miso.Router — URI tokeniser built on this module
- Miso.Util — higher-level
sepBy,oneOf, …
Synopsis
- newtype Lexer token = Lexer {
- runLexer :: Stream -> Either LexerError (token, Stream)
- data Stream = Stream {}
- data Located token = Located {}
- data Location = Location {}
- data LexerError
- getStartColumn :: Location -> Int
- zeroLocation :: Location
- initialLocation :: Location
- mkStream :: MisoString -> Stream
- oops :: Lexer token
- streamError :: Stream -> LexerError
- string :: MisoString -> Lexer MisoString
- string' :: String -> Lexer String
- char :: Char -> Lexer Char
- satisfy :: (Char -> Bool) -> Lexer Char
- peek :: Lexer (Maybe Char)
- getInput :: Lexer Stream
- putInput :: Stream -> Lexer ()
- getLocation :: Lexer Location
- setLocation :: Location -> Lexer ()
- modifyInput :: (Stream -> Stream) -> Lexer ()
- withLocation :: ToMisoString token => Lexer token -> Lexer (Located token)
Types
A Lexer is a state monad with optional failure the abides by the
maximal munch rule in its Alternative instance.
Constructors
| Lexer | |
Fields
| |
A Stream of text used as input to lexing
Constructors
| Stream | |
Fields
| |
Constructors
| Located | |
Type to hold the location (line and column) of a Token
Constructors
| Location | |
data LexerError Source #
Potential errors during lexing
Constructors
| LexerError MisoString Location | |
| UnexpectedEOF Location |
Instances
| Show LexerError Source # | |
Defined in Miso.Util.Lexer Methods showsPrec :: Int -> LexerError -> ShowS # show :: LexerError -> String # showList :: [LexerError] -> ShowS # | |
| Eq LexerError Source # | |
Defined in Miso.Util.Lexer | |
Combinators
zeroLocation :: Location Source #
Empty Location
initialLocation :: Location Source #
Initial Location
Arguments
| :: Stream | The stream at the point of failure; used to populate the error location |
| -> LexerError |
Smart constructor for LexerError
Arguments
| :: MisoString | Literal string prefix to consume from the input |
| -> Lexer MisoString |
Lexer combinator for matching a MisoString
Lexer combinator for matching a String
Lexer combinator for matching a Char
Arguments
| :: (Char -> Bool) | Predicate; the next character is consumed only if this returns |
| -> Lexer Char |
Predicate combinator that consumes matching input
peek :: Lexer (Maybe Char) Source #
Fetches the first character in the Stream, does not consume input
Modifies a Stream
Arguments
| :: ToMisoString token | |
| => Lexer token | Inner lexer whose result is wrapped with its source |
| -> Lexer (Located token) |