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
- class Stream str => ParsableAsTerm str term where
- class Stream str => UnsafelyParsableAsTerm str term where
- pUnsafeNonTerm :: Ord err => ParsecT err str m term
- pUnsafeTerm :: Ord err => ParsecT err str m term
- pTree :: (Ord err, ParsableAsTerm str term, Monad m, Token str ~ Char, Tokens str ~ str) => ParsecT err str m (Tree term)
- pUnsafeTree :: (Ord err, UnsafelyParsableAsTerm str term, Monad m, Token str ~ Char, Tokens str ~ str) => ParsecT err str m (Tree term)
- type PennTreeParserT str m term = ParsecT Void str m (Tree term)
- type PennTreeParser str term = PennTreeParserT str Identity 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)
Type Classes for Node Parsers
class Stream str => ParsableAsTerm str term where Source #
A type class for node label types term
data of which can be obtained by parsing a stream of type str
that is safely carved by the tree parser
pTree
containing no spaces and parenthese.
class Stream str => UnsafelyParsableAsTerm str term where Source #
A type class for node label types term
data of which can be obtained by parsing a stream of type str
that is supposed to be embedded into the parser
pUnsafeTree
.
It is your own responsibility to avoid these parsers to crash
by letting them unintendedly consume
symbols preserved for tree node demarcation
(e.g. parentheses and spaces).
Since: 0.1.1
pUnsafeNonTerm :: Ord err => ParsecT err str m term Source #
A parser for non-terminal node labels.
pUnsafeTerm :: Ord err => ParsecT err str m term Source #
A parser for terminal node labels. This parser may not comsume empty inputs, for otherwise parsing will fall into infinte recursion.
Instances
(Stream str, Tokens str ~ term, Token str ~ Char) => UnsafelyParsableAsTerm str term Source # | |
Defined in Data.Tree.Parser.Penn.Megaparsec.Char pUnsafeNonTerm :: Ord err => ParsecT err str m term Source # pUnsafeTerm :: Ord err => ParsecT err str m term Source # |
Parsers
pTree :: (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 trees 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 preliminary parse for
node labels as raw strings of type str
.
The carved strings will contain no spaces or parentheses.
A seconsary parsing will take place on the spot
the way of parsing of which is designated by you
specifying the type 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:
(pTree :: ParsecT Void Text Identity (Tree Text))
pUnsafeTree :: (Ord err, UnsafelyParsableAsTerm str term, Monad m, Token str ~ Char, Tokens str ~ str) => ParsecT err str m (Tree term) Source #
Another parser for trees 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.
Apart from pTree
, you can customize node label parsers
in a more liberal way
by specifying the type term
as an instance of UnsafelyParsableAsTerm
.
You can let parsers consume spaces and parentheses
(perhaps with quotation markers)
unless they do not broke the parsing of the whole trees.
It is all your responsibility to make sure of it.
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:
(pUnsafeTree :: ParsecT Void Text Identity (Tree Text))
Since: 0.1.1
Parser Type Synonyms
type PennTreeParser str term = PennTreeParserT str Identity term Source #
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