Skip to main content
Version: 2.x

Parser Reference

A Parser[Err, In, Result] reads input of type In and produces a Result, or fails with an Err. Parsers are one half of a Syntax, available as Syntax#asParser. You usually build a Syntax and use its parser through parseString, but a Parser can be used on its own when you never need the printer.

The type has three parameters. Err is the error type, which is String for most constructors. In is the input element type, which is Char for text. Result is the value the parser produces. Parser is covariant in Err and Result and contravariant in In, so a parser that needs fewer input types can be used where more are available.

For combinators that take a Syntax or Printer as an argument, see the Syntax reference. Every method on Parser below has the same behavior as the Syntax method of the same name, applied to the parser half only.

Running a Parserโ€‹

MethodResultDescription
parseString(input)Either[StringParserError[Err], Result]Parses a String. Requires In to be Char
parseString(input, implementation)Either[StringParserError[Err], Result]As above, with a ParserImplementation
parseChars(input)Either[StringParserError[Err], Result]Parses a Chunk[Char]
parseChars(input, implementation)Either[StringParserError[Err], Result]As above, with a ParserImplementation
parseChunk(input)Either[ParserError[Err], Result]Parses any Chunk[In]. Returns the raw error, without the input

StringParserError holds the input and the ParserError, and pretty formats it. See Error Handling for the cases of ParserError.

Transforming Resultsโ€‹

MethodDescription
map(f)Transforms the result
as(value)Replaces the result with a constant
unitDiscards the result
flatMap(f)Runs the parser returned by f on the rest of the input. Use it when the next step depends on this result
transformEither(to)Transforms the result. to returns an Either, and a Left fails the parser
transformOption(to)Transforms the result. to returns an Option, and None fails the parser
filter(condition, failure)Fails with failure when the result does not satisfy condition
stringConverts a Chunk[Char] result into a String
to[CaseClass]Maps a tuple result to a case class. Needs a TupleConversion for the case class
stripRemoves all name information to make parsing faster. Failure messages become less detailed
flattenJoins a Chunk[String] result into one String. Available only when the result is a Chunk[String]
mapError(f)Transforms the error type

Sequencing and Alternativesโ€‹

MethodResultDescription
~ / zipParser[..., (A, B)]Runs this parser, then the other one, and pairs the results
<~ / zipLeftParser[..., A]Runs both and keeps the left result
~> / zipRightParser[..., B]Runs both and keeps the right result
| / <> / orElseParser[..., A]Tries this parser, then the other one, if this one fails
<+> / orElseEitherParser[..., Either[A, B]]As orElse, but keeps which branch matched
?? / named(name)Parser[..., A]Adds a name to errors raised inside this parser

Repetition and Optional Resultsโ€‹

MethodResultDescription
repeat / +Chunk[A]One or more times
repeat0 / *Chunk[A]Zero or more times
atLeast(n)Chunk[A]At least n times
exactly(n)Chunk[A]Exactly n times
repeatWithSep(sep)Chunk[A]One or more times with sep between them
repeatWithSep0(sep)Chunk[A]Zero or more times with sep between them
repeatUntil(stop)Chunk[A]Repeats until stop succeeds
optional / ?Option[A]Zero or one time
between(left, right)Parser[..., A]Runs left and right around this parser and keeps the result
surroundedBy(other)Parser[..., A]Runs other before and after this parser
not(failure)Parser[..., Unit]Succeeds when this parser fails, and fails with failure when this parser succeeds

Backtrackingโ€‹

MethodDescription
backtrackRewinds the input to where this parser started if it fails
autoBacktrackingTurns automatic backtracking on for this parser
manualBacktrackingTurns automatic backtracking off for this parser
setAutoBacktracking(enabled)Sets the backtracking mode for this parser

The Syntax reference explains when to use each of them.

The Companion Objectโ€‹

These constructors are available on Parser directly. Most have a Syntax counterpart with the same name.

ConstructorDescription
succeed(value)Succeeds with value without consuming input
fail(failure)Fails with failure
char(c) / char(c, failure)Matches one character
notChar(c) / notChar(c, failure)Matches any character except c
charIn(chars*) / charNotIn(chars*)Matches a character in or outside a set
anyCharMatches any one character
any[T]Matches any one element of type T
string(str, value)Matches str and produces value
anyStringMatches the rest of the input as a String
ignoreRestConsumes the rest of the input without producing a value
digit, letter, alphaNumeric, whitespaceMatch one character of that class
regex(regex, failure)Matches a Regex and produces the matched Chunk[Char]
regexChar(regex, failure)Matches a Regex and produces its last character
regexDiscard(regex, failure)Matches a Regex and discards it
unitSucceeds with ()
indexProduces the current position in the input
endSucceeds only at the end of the input

The unsafe variants, such as unsafeRegex and unsafeRegexChar, have no failure value, so a failure is reported as an UnknownFailure. Use them only with regexes that cannot fail.

Error Casesโ€‹

Parser.ParserError has five cases: Failure, UnknownFailure, UnexpectedEndOfInput, NotConsumedAll and AllBranchesFailed. Each case, what it means and how pretty shows it, is described in Error Handling.

ZIO Chat

๐ŸŒŸ Greetings from ZIO Chat! ๐ŸŒŸ Have questions about ZIO? Look no further! I'm here to be your ZIO guru. Whether you're a seasoned pro or just starting out, I've got answers tailored just for you. Fire away with your ZIO queries, and I'll hit you back with the best insights I've got. Let's dive into the world of ZIO together!