Copyright |
© 2015–present Megaparsec contributors
© 2007 Paolo Martini © 1999–2001 Daan Leijen |
---|---|
License | FreeBSD |
Maintainer | Mark Karpov <markkarpov92@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Safe |
Language | Haskell2010 |
High-level parsers to help you write your lexer. The module doesn't impose how you should write your parser, but certain approaches may be more elegant than others.
Parsing of white space is an important part of any parser. We propose a
convention where
every lexeme parser assumes no spaces before the
lexeme and consumes all spaces after the lexeme
; this is what the
lexeme
combinator does, and so it's enough to wrap every lexeme parser
with
lexeme
to achieve this. Note that you'll need to call
space
manually to consume any white space before the first lexeme (i.e. at the
beginning of the file).
This module is intended to be imported qualified:
import qualified Text.Megaparsec.Char.Lexer as L
To do lexing of byte streams, see Text.Megaparsec.Byte.Lexer .
Synopsis
- space :: MonadParsec e s m => m () -> m () -> m () -> m ()
- lexeme :: MonadParsec e s m => m () -> m a -> m a
- symbol :: MonadParsec e s m => m () -> Tokens s -> m ( Tokens s)
- symbol' :: ( MonadParsec e s m, FoldCase ( Tokens s)) => m () -> Tokens s -> m ( Tokens s)
- skipLineComment :: ( MonadParsec e s m, Token s ~ Char ) => Tokens s -> m ()
- skipBlockComment :: ( MonadParsec e s m, Token s ~ Char ) => Tokens s -> Tokens s -> m ()
- skipBlockCommentNested :: ( MonadParsec e s m, Token s ~ Char ) => Tokens s -> Tokens s -> m ()
- indentLevel :: ( TraversableStream s, MonadParsec e s m) => m Pos
- incorrectIndent :: MonadParsec e s m => Ordering -> Pos -> Pos -> m a
- indentGuard :: ( TraversableStream s, MonadParsec e s m) => m () -> Ordering -> Pos -> m Pos
- nonIndented :: ( TraversableStream s, MonadParsec e s m) => m () -> m a -> m a
-
data
IndentOpt
m a b
- = IndentNone a
- | IndentMany ( Maybe Pos ) ([b] -> m a) (m b)
- | IndentSome ( Maybe Pos ) ([b] -> m a) (m b)
- indentBlock :: ( TraversableStream s, MonadParsec e s m, Token s ~ Char ) => m () -> m ( IndentOpt m a b) -> m a
- lineFold :: ( TraversableStream s, MonadParsec e s m) => m () -> (m () -> m a) -> m a
- charLiteral :: ( MonadParsec e s m, Token s ~ Char ) => m Char
- decimal :: ( MonadParsec e s m, Token s ~ Char , Num a) => m a
- binary :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a
- octal :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a
- hexadecimal :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a
- scientific :: forall e s m. ( MonadParsec e s m, Token s ~ Char ) => m Scientific
- float :: ( MonadParsec e s m, Token s ~ Char , RealFloat a) => m a
- signed :: ( MonadParsec e s m, Token s ~ Char , Num a) => m () -> m a -> m a
White space
:: MonadParsec e s m | |
=> m () |
A parser for space characters which does not accept empty
input (e.g.
|
-> m () |
A parser for a line comment (e.g.
|
-> m () |
A parser for a block comment (e.g.
|
-> m () |
produces a parser that can parse
white space in general. It's expected that you create such a parser once
and pass it to other functions in this module as needed (when you see
space
sc lineComment blockComment
spaceConsumer
in documentation, usually it means that something like
space
is expected there).
sc
is used to parse blocks of space characters. You can use
space1
from
Text.Megaparsec.Char
for this
purpose as well as your own parser (if you don't want to automatically
consume newlines, for example). Make sure that the parser does not
succeed on the empty input though. In an earlier version of the library
spaceChar
was recommended, but now parsers based
on
takeWhile1P
are preferred because of their speed.
lineComment
is used to parse line comments. You can use
skipLineComment
if you don't need anything special.
blockComment
is used to parse block (multi-line) comments. You can use
skipBlockComment
or
skipBlockCommentNested
if you don't need anything
special.
If you don't want to allow a kind of comment, simply pass
empty
which
will fail instantly when parsing of that sort of comment is attempted and
space
will just move on or finish depending on whether there is more
white space for it to consume.
:: MonadParsec e s m | |
=> m () |
How to consume white space after lexeme |
-> m a |
How to parse actual lexeme |
-> m a |
This is a wrapper for lexemes. The typical usage is to supply the first
argument (parser that consumes white space, probably defined via
space
)
and use the resulting function to wrap parsers for every lexeme.
lexeme = L.lexeme spaceConsumer integer = lexeme L.decimal
:: MonadParsec e s m | |
=> m () |
How to consume white space after lexeme |
-> Tokens s |
Symbol to parse |
-> m ( Tokens s) |
This is a helper to parse symbols, i.e. verbatim strings. You pass the
first argument (parser that consumes white space, probably defined via
space
) and then you can use the resulting function to parse strings:
symbol = L.symbol spaceConsumer parens = between (symbol "(") (symbol ")") braces = between (symbol "{") (symbol "}") angles = between (symbol "<") (symbol ">") brackets = between (symbol "[") (symbol "]") semicolon = symbol ";" comma = symbol "," colon = symbol ":" dot = symbol "."
:: ( MonadParsec e s m, FoldCase ( Tokens s)) | |
=> m () |
How to consume white space after lexeme |
-> Tokens s |
Symbol to parse (case-insensitive) |
-> m ( Tokens s) |
A case-insensitive version of
symbol
. This may be helpful if you're
working with case-insensitive languages.
:: ( MonadParsec e s m, Token s ~ Char ) | |
=> Tokens s |
Line comment prefix |
-> m () |
Given a comment prefix this function returns a parser that skips line
comments. Note that it stops just before the newline character but
doesn't consume the newline. Newline is either supposed to be consumed by
space
parser or picked up manually.
:: ( MonadParsec e s m, Token s ~ Char ) | |
=> Tokens s |
Start of block comment |
-> Tokens s |
End of block comment |
-> m () |
skips non-nested block comment starting
with
skipBlockComment
start end
start
and ending with
end
.
skipBlockCommentNested Source #
:: ( MonadParsec e s m, Token s ~ Char ) | |
=> Tokens s |
Start of block comment |
-> Tokens s |
End of block comment |
-> m () |
skips possibly nested block
comment starting with
skipBlockCommentNested
start end
start
and ending with
end
.
Since: 5.0.0
Indentation
indentLevel :: ( TraversableStream s, MonadParsec e s m) => m Pos Source #
Return the current indentation level.
The function is a simple shortcut defined as:
indentLevel = sourceColumn <$> getPosition
Since: 4.3.0
:: MonadParsec e s m | |
=> Ordering |
Desired ordering between reference level and actual level |
-> Pos |
Reference indentation level |
-> Pos |
Actual indentation level |
-> m a |
Fail reporting incorrect indentation error. The error has attached information:
- Desired ordering between reference level and actual level
- Reference indentation level
- Actual indentation level
Since: 5.0.0
:: ( TraversableStream s, MonadParsec e s m) | |
=> m () |
How to consume indentation (white space) |
-> Ordering |
Desired ordering between reference level and actual level |
-> Pos |
Reference indentation level |
-> m Pos |
Current column (indentation level) |
first consumes all white space
(indentation) with
indentGuard
spaceConsumer ord ref
spaceConsumer
parser, then it checks the column
position. Ordering between current indentation level and the reference
indentation level
ref
should be
ord
, otherwise the parser fails. On
success the current column position is returned.
When you want to parse a block of indentation, first run this parser with
arguments like
—this will make
sure you have some indentation. Use returned value to check indentation
on every subsequent line according to syntax of your language.
indentGuard
spaceConsumer
GT
pos1
:: ( TraversableStream s, MonadParsec e s m) | |
=> m () |
How to consume indentation (white space) |
-> m a |
How to parse actual data |
-> m a |
Parse a non-indented construction. This ensures that there is no indentation before actual data. Useful, for example, as a wrapper for top-level function definitions.
Since: 4.3.0
Behaviors for parsing of indented tokens. This is used in
indentBlock
, which see.
Since: 4.3.0
IndentNone a |
Parse no indented tokens, just return the value |
IndentMany ( Maybe Pos ) ([b] -> m a) (m b) |
Parse many indented tokens (possibly zero), use given indentation
level (if
|
IndentSome ( Maybe Pos ) ([b] -> m a) (m b) |
Just like
|
:: ( TraversableStream s, MonadParsec e s m, Token s ~ Char ) | |
=> m () |
How to consume indentation (white space) |
-> m ( IndentOpt m a b) |
How to parse “reference” token |
-> m a |
Parse a “reference” token and a number of other tokens that have a
greater (but the same for all of them) level of indentation than that of
the “reference” token. The reference token can influence parsing, see
IndentOpt
for more information.
Tokens must not consume newlines after them. On the other hand, the first argument of this function must consume newlines among other white space characters.
Since: 4.3.0
:: ( TraversableStream s, MonadParsec e s m) | |
=> m () |
How to consume indentation (white space) |
-> (m () -> m a) |
Callback that uses provided space-consumer |
-> m a |
Create a parser that supports line-folding. The first argument is used to consume white space between components of line fold, thus it must consume newlines in order to work properly. The second argument is a callback that receives a custom space-consuming parser as an argument. This parser should be used after separate components of line fold that can be put on different lines.
An example should clarify the usage pattern:
sc = L.space (void spaceChar) empty empty myFold = L.lineFold sc $ \sc' -> do L.symbol sc' "foo" L.symbol sc' "bar" L.symbol sc "baz" -- for the last symbol we use normal space consumer
Since: 5.0.0
Character and string literals
charLiteral :: ( MonadParsec e s m, Token s ~ Char ) => m Char Source #
The lexeme parser parses a single literal character without quotes. The purpose of this parser is to help with parsing of conventional escape sequences. It's your responsibility to take care of character literal syntax in your language (by surrounding it with single quotes or similar).
The literal character is parsed according to the grammar rules defined in the Haskell report.
Note that you can use this parser as a building block to parse various string literals:
stringLiteral = char '"' >> manyTill L.charLiteral (char '"')
Performance note : the parser is not particularly efficient at the moment.
Numbers
binary :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a Source #
Parse an integer in binary representation. The binary number is expected to be a non-empty sequence of zeroes “0” and ones “1”.
You could of course parse some prefix before the actual number:
binary = char '0' >> char' 'b' >> L.binary
Since: 7.0.0
octal :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a Source #
Parse an integer in the octal representation. The format of the octal number is expected to be according to the Haskell report except for the fact that this parser doesn't parse “0o” or “0O” prefix. It is a responsibility of the programmer to parse correct prefix before parsing the number itself.
For example you can make it conform to the Haskell report like this:
octal = char '0' >> char' 'o' >> L.octal
Note
: before version
6.0.0
the function returned
Integer
, i.e. it
wasn't polymorphic in its return type.
hexadecimal :: forall e s m a. ( MonadParsec e s m, Token s ~ Char , Num a) => m a Source #
Parse an integer in the hexadecimal representation. The format of the hexadecimal number is expected to be according to the Haskell report except for the fact that this parser doesn't parse “0x” or “0X” prefix. It is a responsibility of the programmer to parse correct prefix before parsing the number itself.
For example you can make it conform to the Haskell report like this:
hexadecimal = char '0' >> char' 'x' >> L.hexadecimal
Note
: before version
6.0.0
the function returned
Integer
, i.e. it
wasn't polymorphic in its return type.
scientific :: forall e s m. ( MonadParsec e s m, Token s ~ Char ) => m Scientific Source #
Parse a floating point value as a
Scientific
number.
Scientific
is
great for parsing of arbitrary precision numbers coming from an untrusted
source. See documentation in
Data.Scientific
for more information.
The parser can be used to parse integers or floating point values. Use
functions like
floatingOrInteger
from
Data.Scientific
to test and extract integer or real values.
This function does not parse sign, if you need to parse signed numbers,
see
signed
.
Since: 5.0.0
float :: ( MonadParsec e s m, Token s ~ Char , RealFloat a) => m a Source #
Parse a floating point number according to the syntax for floating point literals described in the Haskell report.
This function does not parse sign, if you need to parse signed numbers,
see
signed
.
Note
: before version
6.0.0
the function returned
Double
, i.e. it
wasn't polymorphic in its return type.
Note : in versions 6.0.0 – 6.1.1 this function accepted plain integers.
:: ( MonadParsec e s m, Token s ~ Char , Num a) | |
=> m () |
How to consume white space after the sign |
-> m a |
How to parse the number itself |
-> m a |
Parser for signed numbers |
parses an optional sign character (“+” or “-”), then
if there is a sign it consumes optional white space (using the
signed
space p
space
parser), then it runs the parser
p
which should return a number. Sign
of the number is changed according to the previously parsed sign
character.
For example, to parse signed integer you can write:
lexeme = L.lexeme spaceConsumer integer = lexeme L.decimal signedInteger = L.signed spaceConsumer integer