count n p parses n occurrences of p. If n is smaller or
equal to zero, the parser equals to return []. Returns a list of
n values returned by p.
Moduletrifecta-2.1.4Haskell2010
Text.Trifecta
For a short introduction, see the Text.Trifecta.Tutorial module.
- 4 types
- 3 classes
- 80 values
- Packagetrifecta-2.1.4
- Exports89
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceTrifecta.hs
module Text.Trifecta.Rendering
module Text.Trifecta.Highlight
module Text.Trifecta.Parser
module Text.Trifecta.Combinators
module Text.Trifecta.Result
module Text.Trifecta.Rope
Zero or more.
Examples
many (putStr "la")lalalalalalalalala... * goes on forever *
many NothingJust []
take 5 <$> many (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case many parser will attempt to
parse parser zero or more times until it fails.
One or more.
Examples
some (putStr "la")lalalalalalalalala... * goes on forever *
some Nothingnothing
take 5 <$> some (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case some parser will attempt to
parse parser one or more times until it fails.
One or none.
It is useful for modelling any computation that is allowed to fail.
Examples
Using the Alternative instance of Control.Monad.Except, the following functions:
import Control.Monad.ExceptcanFail = throwError "it failed" :: Except String Intfinal = return 42 :: Except String Int
Can be combined by allowing the first function to fail:
runExcept $ canFail *> finalLeft "it failed"
runExcept $ optional canFail *> finalRight 42
sepBy1 p sep parses one or more occurrences of p, separated
by sep. Returns a list of values returned by p.
option x p tries to apply parser p. If p fails without
consuming input, it returns the value x, otherwise the value
returned by p.
priority = option 0 (digitToInt <$> digit)Additional functionality needed to describe parsers independent of input type.
Methods
try :: m a -> m aTake a parser that may consume input, and on failure, go back to where we started and fail as if we didn't consume input.
(<?>) :: m a -> String -> m ainfixr 0Give a parser a name
skipMany :: m a -> m ()A version of many that discards its input. Specialized because it can often be implemented more cheaply.
skipSome :: m a -> m ()skipSome papplies the parserpone or more times, skipping its result. (aka skipMany1 in parsec)unexpected :: String -> m aUsed to emit an error on an unexpected token
eof :: m ()This parser only succeeds at the end of the input. This is not a primitive parser but it is defined using notFollowedBy.
eof = notFollowedBy anyChar <?> "end of input"notFollowedBy :: Show a => m a -> m ()notFollowedBy ponly succeeds when parserpfails. This parser does not consume any input. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for examplelet), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier (for examplelets). We can program this behaviour as follows:keywordLet = try $ string "let" <* notFollowedBy alphaNum
Instances16Parsing, …
Parsing GetDefined in parsers-0.12.12 · Text.Parser.CombinatorsParsing ReadPDefined in parsers-0.12.12 · Text.Parser.CombinatorsParsing ParserDefined in trifecta-2.1.4 · Text.Trifecta.ParserChunk t => Parsing (Parser t)Defined in parsers-0.12.12 · Text.Parser.CombinatorsParsing m => Parsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.Token(Parsing m, Monad m) => Parsing (IdentityT m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (ReaderT e m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Stream s m t, Show t) => Parsing (ParsecT s u m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Combinators
between open close p parses open, followed by p and close.
Returns the value returned by p.
braces = between (symbol "{") (symbol "}")choice ps tries to apply the parsers in the list ps in order,
until one of them succeeds. Returns the value of the succeeding
parser.
sepBy p sep parses zero or more occurrences of p, separated
by sep. Returns a list of values returned by p.
commaSep p = p `sepBy` (symbol ",")chainl p op x parses zero or more occurrences of p,
separated by op. Returns a value obtained by a left associative
application of all functions returned by op to the values returned
by p. If there are zero occurrences of p, the value x is
returned.
chainl1 p op x parses one or more occurrences of p,
separated by op Returns a value obtained by a left associative
application of all functions returned by op to the values returned
by p. . This parser can for example be used to eliminate left
recursion which typically occurs in expression grammars.
expr = term `chainl1` addop
term = factor `chainl1` mulop
factor = parens expr <|> integer
mulop = (*) <$ symbol "*"
<|> div <$ symbol "/"
addop = (+) <$ symbol "+"
<|> (-) <$ symbol "-"chainr p op x parses zero or more occurrences of p,
separated by op Returns a value obtained by a right associative
application of all functions returned by op to the values returned
by p. If there are no occurrences of p, the value x is
returned.
chainr1 p op x parses one or more occurrences of p,
separated by op Returns a value obtained by a right associative
application of all functions returned by op to the values returned
by p.
endBy p sep parses zero or more occurrences of p, separated
and ended by sep. Returns a list of values returned by p.
cStatements = cStatement `endBy` semiendBy1 p sep parses one or more occurrences of p, separated
and ended by sep. Returns a list of values returned by p.
endByNonEmpty p sep parses one or more occurrences of p, separated
and ended by sep. Returns a non-empty list of values returned by p.
manyTill p end applies parser p zero or more times until
parser end succeeds. Returns the list of values returned by p.
This parser can be used to scan comments:
simpleComment = do{ string "<!--"
; manyTill anyChar (try (string "-->"))
}Note the overlapping parsers anyChar and string "-->", and
therefore the use of the try combinator.
sepByNonEmpty p sep parses one or more occurrences of p, separated
by sep. Returns a non-empty list of values returned by p.
sepEndBy p sep parses zero or more occurrences of p,
separated and optionally ended by sep, ie. haskell style
statements. Returns a list of values returned by p.
haskellStatements = haskellStatement `sepEndBy` semisepEndBy1 p sep parses one or more occurrences of p,
separated and optionally ended by sep. Returns a list of values
returned by p.
sepEndByNonEmpty p sep parses one or more occurrences of p,
separated and optionally ended by sep. Returns a non-empty list of values
returned by p.
skipOptional p tries to apply parser p. It will parse p or nothing.
It only fails if p fails after consuming input. It discards the result
of p. (Plays the role of parsec's optional, which conflicts with Applicative's optional)
p `surroundedBy` f is p surrounded by f. Shortcut for between f f p.
As in between, returns the value returned by p.
Additional functionality needed to parse character streams.
Methods
satisfy :: (Char -> Bool) -> m CharParse a single character of the input, with UTF-8 decoding
char :: Char -> m CharnotChar :: Char -> m CharnotChar cparses any single character other thanc. Returns the parsed character.anyChar :: m CharThis parser succeeds for any character. Returns the parsed character.
string :: String -> m Stringstring sparses a sequence of characters given bys. Returns the parsed string (i.e.s).divOrMod = string "div" <|> string "mod"text :: Text -> m Texttext tparses a sequence of characters determined by the texttReturns the parsed text fragment (i.e.t).Using
OverloadedStrings:divOrMod = text "div" <|> text "mod"
Instances15CharParsing, …
CharParsing ReadPDefined in parsers-0.12.12 · Text.Parser.CharCharParsing ParserDefined in trifecta-2.1.4 · Text.Trifecta.ParserChunk t => CharParsing (Parser t)Defined in parsers-0.12.12 · Text.Parser.CharCharParsing m => CharParsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenCharParsing m => CharParsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenCharParsing m => CharParsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.Token(CharParsing m, MonadPlus m) => CharParsing (IdentityT m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m) => CharParsing (ReaderT e m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m) => CharParsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m) => CharParsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m, Monoid w) => CharParsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m, Monoid w) => CharParsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.CharStream s m Char => CharParsing (ParsecT s u m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m, Monoid w) => CharParsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Char(CharParsing m, MonadPlus m, Monoid w) => CharParsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Char
As the dual of oneOf, noneOf cs succeeds if the current
character is not in the supplied list of characters cs. Returns the
parsed character.
consonant = noneOf "aeiou"Parses a white space character (any character which satisfies isSpace) Returns the parsed character.
Skips zero or more white space characters. See also skipMany.
Parses a lower case character. Returns the parsed character.
Parses a digit. Returns the parsed character.
Parses a hexadecimal digit (a digit or a letter between 'a' and 'f' or 'A' and 'F'). Returns the parsed character.
Parses an octal digit (a character between '0' and '7'). Returns the parsed character.
oneOf cs succeeds if the current character is in the supplied
list of characters cs. Returns the parsed character. See also
satisfy.
vowel = oneOf "aeiou"Parses an upper case letter. Returns the parsed character.
Parses a letter or digit. Returns the parsed character.
Parses a letter (an upper case or lower case character). Returns the parsed character.
Parses a newline character ('\n'). Returns a newline character.
As the dual of oneOf, noneOf cs succeeds if the current
character is not in the supplied list of characters cs. Returns the
parsed character.
consonant = noneOf "aeiou"oneOfSet cs succeeds if the current character is in the supplied
set of characters cs. Returns the parsed character. See also
satisfy.
vowel = oneOf "aeiou"Parses a tab character ('\t'). Returns a tab character.
This token parser parses a floating point value. Returns the value of the number. The number is parsed according to the grammar rules defined in the Haskell report.
Additional functionality that is needed to tokenize input while ignoring whitespace.
Methods
someSpace :: m ()Usually, someSpace consists of one or more occurrences of a space. Some parsers may choose to recognize line comments or block (multi line) comments as white space as well.
nesting :: m a -> m aCalled when we enter a nested pair of symbols. Overloadable to enable disabling layout
semi :: m CharThe token parser |semi| parses the character ';' and skips any trailing white space. Returns the character ';'. Overloadable to permit automatic semicolon insertion or Haskell-style layout.
highlight :: Highlight -> m a -> m aTag a region of parsed text with a bit of semantic information. Most parsers won't use this, but it is indispensible for highlighters.
token :: m a -> m atoken pfirst applies parserpand then the whiteSpace parser, returning the value ofp. Every lexical token (token) is defined usingtoken, this way every parse starts at a point without white space. Parsers that usetokenare called token parsers in this document.The only point where the whiteSpace parser should be called explicitly is the start of the main parser in order to skip any leading white space.
Alternatively, one might define token as first parsing whiteSpace and then parser
p. By parsing whiteSpace first, the parser is able to return before parsing additional whiteSpace, improving laziness.mainParser = sum <$ whiteSpace <*> many (token digit) <* eof
Instances15TokenParsing, …
TokenParsing ReadPDefined in parsers-0.12.12 · Text.Parser.TokenTokenParsing ParserDefined in trifecta-2.1.4 · Text.Trifecta.ParserChunk t => TokenParsing (Parser t)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m) => TokenParsing (IdentityT m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m) => TokenParsing (ReaderT e m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m) => TokenParsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m) => TokenParsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.TokenStream s m Char => TokenParsing (ParsecT s u m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Token(TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Token
Token parser angles p parses p enclosed in angle brackets ('<'
and '>'), returning the value of p.
Token parser braces p parses p enclosed in braces ('{' and
'}'), returning the value of p.
Token parser brackets p parses p enclosed in brackets ('['
and ']'), returning the value of p.
Token parser colon parses the character ':' and skips any
trailing white space. Returns the string ":".
Token parser comma parses the character ',' and skips any
trailing white space. Returns the string ",".
Token parser dot parses the character '.' and skips any
trailing white space. Returns the string ".".
Token parser parens p parses p enclosed in parenthesis,
returning the value of p.
Parse a non-reserved identifier or symbol
This token parser parses a floating point value. Returns the value of the number. The number is parsed according to the grammar rules defined in the Haskell report.
This token parser parses an integer (a whole number). This parser is like natural except that it can be prefixed with sign (i.e. '-' or '+'). Returns the value of the number. The number can be specified in decimal, hexadecimal or octal. The number is parsed according to the grammar rules in the Haskell report.
This is a parser transformer you can use to disable syntax highlighting over a range of text you are parsing.
Constructors
Instances12MonadTrans, MonadReader, MonadState, MonadWriter, Monad, Functor, …
MonadTrans UnhighlightedDefined in parsers-0.12.12 · Text.Parser.TokenMonadReader e m => MonadReader e (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadState s m => MonadState s (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadWriter e m => MonadWriter e (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenMonad m => Monad (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenFunctor m => Functor (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenApplicative m => Applicative (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenAlternative m => Alternative (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadPlus m => MonadPlus (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenCharParsing m => CharParsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.Token
This is a parser transformer you can use to disable the automatic trailing newline (but not whitespace-in-general) consumption of a Token parser.
Constructors
UnlinedrunUnlined :: m a
Instances12MonadTrans, MonadReader, MonadState, MonadWriter, Monad, Functor, …
MonadTrans UnlinedDefined in parsers-0.12.12 · Text.Parser.TokenMonadReader e m => MonadReader e (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadState s m => MonadState s (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadWriter e m => MonadWriter e (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenMonad m => Monad (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenFunctor m => Functor (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenApplicative m => Applicative (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenAlternative m => Alternative (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadPlus m => MonadPlus (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenCharParsing m => CharParsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.Token
This is a parser transformer you can use to disable the automatic trailing space consumption of a Token parser.
Constructors
UnspacedrunUnspaced :: m a
Instances12MonadTrans, MonadReader, MonadState, MonadWriter, Monad, Functor, …
MonadTrans UnspacedDefined in parsers-0.12.12 · Text.Parser.TokenMonadReader e m => MonadReader e (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadState s m => MonadState s (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadWriter e m => MonadWriter e (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenMonad m => Monad (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenFunctor m => Functor (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenApplicative m => Applicative (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenAlternative m => Alternative (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenMonadPlus m => MonadPlus (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenCharParsing m => CharParsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.TokenTokenParsing m => TokenParsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.Token
This token parser parses a single literal character. Returns the literal character value. This parsers deals correctly with escape sequences. The literal character is parsed according to the grammar rules defined in the Haskell report (which matches most programming languages quite closely).
This parser parses a character literal without the surrounding quotation marks.
This parser does NOT swallow trailing whitespace
Token parser commaSep p parses zero or more occurrences of
p separated by comma. Returns a list of values returned
by p.
Token parser commaSep1 p parses one or more occurrences of
p separated by comma. Returns a list of values returned
by p.
Parses a non-negative whole number in the decimal system. Returns the value of the number.
This parser does NOT swallow trailing whitespace
Parses a non-negative whole number in the hexadecimal system. The number should be prefixed with "x" or "X". Returns the value of the number.
This parser does NOT swallow trailing whitespace
This parser parses an integer (a whole number). This parser is like natural except that it can be prefixed with sign (i.e. '-' or '+'). Returns the value of the number. The number can be specified in decimal, hexadecimal or octal. The number is parsed according to the grammar rules in the Haskell report.
This parser does NOT swallow trailing whitespace.
Also, unlike the integer parser, this parser does not admit spaces between the sign and the number.
This token parser is like naturalOrDouble, but handles
leading - or +.
This token parser is like naturalOrScientific, but handles
leading - or +.
Lift an identifier style into a monad transformer
Using over from the lens package:
liftIdentifierStyle = over styleChars liftThis token parser parses a natural number (a non-negative whole number). Returns the value of the number. The number can be specified in decimal, hexadecimal or octal. The number is parsed according to the grammar rules in the Haskell report.
This token parser parses either natural or a float.
Returns the value of the number. This parsers deals with
any overlap in the grammar rules for naturals and floats. The number
is parsed according to the grammar rules defined in the Haskell report.
This token parser parses either natural or a scientific. Returns the value of the number. This parsers deals with any overlap in the grammar rules for naturals and floats. The number is parsed according to the grammar rules defined in the Haskell report.
Parses a non-negative whole number in the octal system. The number should be prefixed with "o" or "O". Returns the value of the number.
This parser does NOT swallow trailing whitespace
parse a reserved operator or identifier using a given style
parse a reserved operator or identifier using a given style given Text.
Token parser semiSep p parses zero or more occurrences of p
separated by semi. Returns a list of values returned by p.
Token parser semiSep1 p parses one or more occurrences of p
separated by semi. Returns a list of values returned by p.
This token parser parses a literal string. Returns the literal string value. This parsers deals correctly with escape sequences and gaps. The literal string is parsed according to the grammar rules defined in the Haskell report (which matches most programming languages quite closely).
This token parser behaves as stringLiteral, but for single-quoted strings.
This is a traversal of both actions in contained in an IdentifierStyle.
styleChars :: Traversal (IdentifierStyle m) (IdentifierStyle n) (m Char) (n Char)This is a lens that can be used to modify the highlight used for this identifier set.
styleHighlight :: Lens' (IdentifierStyle m) HighlightThis is a traversal that can be used to modify the highlights used for both non-reserved and reserved identifiers in this identifier set.
styleHighlights :: Traversal' (IdentifierStyle m) HighlightThis lens can be used to update the action used to recognize subsequent letters in an identifier.
styleLetter :: Lens' (IdentifierStyle m) (m Char)This lens can be used to update the name for this style of identifier.
styleName :: Lens' (IdentifierStyle m) StringThis is a lens that can be used to modify the reserved identifier set.
styleReserved :: Lens' (IdentifierStyle m) (HashSet String)This is a lens that can be used to modify the highlight used for reserved identifiers in this identifier set.
styleReservedHighlight :: Lens' (IdentifierStyle m) HighlightThis lens can be used to update the action used to recognize the first letter in an identifier.
styleStart :: Lens' (IdentifierStyle m) (m Char)Token parser symbol s parses string s and skips
trailing white space.
Token parser symbolic s parses char s and skips
trailing white space.
Token parser textSymbol t parses text s and skips
trailing white space.
Skip zero or more bytes worth of white space. More complex parsers are free to consider comments as white space.
Used to describe an input style for constructors, values, operators, etc.