A parsing library provides composable parsers for recognizing and processing structured string input. Its design balances expressive grammars, speed, and precise error reporting: malformed input should identify both where parsing failed and why. section 9.1
Algebraic design starts with parser operations and their laws before choosing representations. Simple repeated-letter and gibberish examples expose the essential combinators before practical formats such as JSON or HTML. section 9.1
A small parser interface grows through reusable operations:
- Recognize a character with char and a complete string with string. section 9.1
- Run a parser against a String and return either a parse error or a result. section 9.1
- Combine alternatives with or, and repeat a parser with listOfN. section 9.1
Combining alternatives and repetition
The parser (string("ab") | string("cad")).listOfN(3) accepts "ababcad" as List("ab", "ab", "cad"), and also accepts the same alternatives in other orders. section 9.1
trait Parsers[ParseError, Parser[+_]]: #1 extension [A](p: Parser[A]) def run(input: String): Either[ParseError, A] def char(c: Char): Parser[Char] #2
Browse the compact parser-library interface: Parser, run, and char show how primitive recognition and execution fit together. section 9.1
