Copyright | (c) 2020 Nori Hayashi |
---|---|
License | BSD3 |
Maintainer | Nori Hayashi <net@hayashi-lin.net> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Language : Haskell2010
Synopsis
- pDoc :: (Ord err, ParsableAsTerm str term, Monad m, Token str ~ Char, Tokens str ~ str) => ParsecT err str m [Tree term]
- parse :: Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
- parseMaybe :: (Ord e, Stream s) => Parsec e s a -> s -> Maybe a
- parseTest :: (ShowErrorComponent e, Show a, Stream s) => Parsec e s a -> s -> IO ()
- runParser :: Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
- runParser' :: Parsec e s a -> State s e -> (State s e, Either (ParseErrorBundle s e) a)
- runParserT :: Monad m => ParsecT e s m a -> String -> s -> m (Either (ParseErrorBundle s e) a)
- runParserT' :: Monad m => ParsecT e s m a -> State s e -> m (State s e, Either (ParseErrorBundle s e) a)
Parser
pDoc :: (Ord err, ParsableAsTerm str term, Monad m, Token str ~ Char, Tokens str ~ str) => ParsecT err str m [Tree term] Source #
A parser (parser monad transformer)
for treebank files in the Penn Treebank format,
where err
is the type of custom errors,
str
is the type of the stream,
m
is the type of the undelying monad and
term
is the type of node labels.
This parser will do a secondary parse for node labels,
results of which are of type term
.
Failures are registered to the main tree parsing process.
The secondary node label parser is designated by
specifying term
as an instance of ParsableAsTerm
.
This parser accepts various types of text stream,
including String
, Text
and Text
.
You might need to manually annotate the type of this parser
to specify what type of stream you target at in the following way:
(pDoc :: ParsecT Void Text Identity [Tree Text])
Parser Runners
Re-imported from Text.Megaparsec.
:: Parsec e s a | Parser to run |
-> String | Name of source file |
-> s | Input for parser |
-> Either (ParseErrorBundle s e) a |
runs parser parse
p file inputp
over Identity
(see
runParserT
if you're using the ParsecT
monad transformer; parse
itself is just a synonym for runParser
). It returns either a
ParseErrorBundle
(Left
) or a value of type a
(Right
).
errorBundlePretty
can be used to turn ParseErrorBundle
into the
string representation of the error message. See Text.Megaparsec.Error
if you need to do more advanced error analysis.
main = case parse numbers "" "11,2,43" of Left bundle -> putStr (errorBundlePretty bundle) Right xs -> print (sum xs) numbers = decimal `sepBy` char ','
parseMaybe :: (Ord e, Stream s) => Parsec e s a -> s -> Maybe a #
runs the parser parseMaybe
p inputp
on input
and returns the
result inside Just
on success and Nothing
on failure. This function
also parses eof
, so if the parser doesn't consume all of its input, it
will fail.
The function is supposed to be useful for lightweight parsing, where error messages (and thus file names) are not important and entire input should be parsed. For example, it can be used when parsing of a single number according to a specification of its format is desired.
:: (ShowErrorComponent e, Show a, Stream s) | |
=> Parsec e s a | Parser to run |
-> s | Input for parser |
-> IO () |
The expression
applies the parser parseTest
p inputp
against the
input input
and prints the result to stdout. Useful for testing.
:: Parsec e s a | Parser to run |
-> String | Name of source file |
-> s | Input for parser |
-> Either (ParseErrorBundle s e) a |
runs parser runParser
p file inputp
on the input stream of
tokens input
, obtained from source file
. The file
is only used in
error messages and may be the empty string. Returns either a
ParseErrorBundle
(Left
) or a value of type a
(Right
).
parseFromFile p file = runParser p file <$> readFile file
:: Parsec e s a | Parser to run |
-> State s e | Initial state |
-> (State s e, Either (ParseErrorBundle s e) a) |
:: Monad m | |
=> ParsecT e s m a | Parser to run |
-> String | Name of source file |
-> s | Input for parser |
-> m (Either (ParseErrorBundle s e) a) |
runs parser runParserT
p file inputp
on the input list of tokens
input
, obtained from source file
. The file
is only used in error
messages and may be the empty string. Returns a computation in the
underlying monad m
that returns either a ParseErrorBundle
(Left
) or
a value of type a
(Right
).
:: Monad m | |
=> ParsecT e s m a | Parser to run |
-> State s e | Initial state |
-> m (State s e, Either (ParseErrorBundle s e) a) |
This function is similar to runParserT
, but like runParser'
it
accepts and returns parser state. This is thus the most general way to
run a parser.
Since: megaparsec-4.2.0