penntreebank-megaparsec-0.1.0: Parser combinators for trees in the Penn Treebank format

Copyright(c) 2020 Nori Hayashi
LicenseBSD3
MaintainerNori Hayashi <net@hayashi-lin.net>
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Text.PennTreebank.Parser.Megaparsec.Char

Contents

Description

Language : Haskell2010

Synopsis

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.

parse #

Arguments

:: Parsec e s a

Parser to run

-> String

Name of source file

-> s

Input for parser

-> Either (ParseErrorBundle s e) a 

parse p file input runs parser p 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 #

parseMaybe p input runs the parser p 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.

parseTest #

Arguments

:: (ShowErrorComponent e, Show a, Stream s) 
=> Parsec e s a

Parser to run

-> s

Input for parser

-> IO () 

The expression parseTest p input applies the parser p against the input input and prints the result to stdout. Useful for testing.

runParser #

Arguments

:: Parsec e s a

Parser to run

-> String

Name of source file

-> s

Input for parser

-> Either (ParseErrorBundle s e) a 

runParser p file input runs parser p 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

runParser' #

Arguments

:: Parsec e s a

Parser to run

-> State s e

Initial state

-> (State s e, Either (ParseErrorBundle s e) a) 

The function is similar to runParser with the difference that it accepts and returns parser state. This allows to specify arbitrary textual position at the beginning of parsing, for example. This is the most general way to run a parser over the Identity monad.

Since: megaparsec-4.2.0

runParserT #

Arguments

:: 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) 

runParserT p file input runs parser p 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).

runParserT' #

Arguments

:: 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