Parser combinators

Parsers are everywhere. A Scala program text needs to be parsed first for it to run. We parse a JSON to get at the data structure. We then try to make sense of the text and pull out something useful from it.

Parsing may succeed or fail. For example, the text may not have what we are looking for. It may have something else equally interesting. At times, we'll want to retry different parsers on a piece of text, hoping that one of them might succeed.

A parser looking for a number in the string how do you do? will fail as there is no number. However, a parser looking for greetings will succeed.

The idea behind Scala's Parser combinators is to compose more complex parsers from simpler ones. We compose parsers that can be tried one after ...

Get Scala Functional Programming Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.