liveBook · Manning

Result

As explained in Functional Programming in Scala, Second Edition

Result is a parser-result type that represents a parse outcome, including a successful value and the number of input characters consumed.

Result represents the outcome of parsing: success carries a value and the number of input characters consumed, while failure carries a parse error. section 9.6.2

A parser Result records the information needed to continue sequencing:

  • Success(get, charsConsumed) returns the parsed value together with the input length consumed. section 9.6.2
  • Failure(get) reports a ParseError when parsing does not succeed. section 9.6.2
  • The consumer uses charsConsumed to update the parser's Location state before running the next parser. section 9.6.2

Sequencing parsers

For string("abra") ** string("cadabra"), a successful first Result marks "abra" as consumed, allowing the second parser to inspect the remaining input. section 9.6.2

opaque type Parser[+A] = Location => Result[A]            #1
 
enum Result[+A]:
  case Success(get: A, charsConsumed: Int)                #2
  case Failure(get: ParseError) extends Result[Nothing]   #3

Inspect the initial Scala representation of Parser and its Success and Failure Result cases. section 9.6.2

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines success and failure cases and their state-transition role, stopping at an initial representation before later refinements.
Examples
Uses string("abra") ** string("cadabra") to show how successful parsing advances through remaining input.
What it assumes
Assumes Parser sequencing and Location state while making consumed input explicit.
What you can do after
Build it from scratch by implementing parser primitives that return successful values, consumed lengths, or failures.
How it connects
Ties Parser to Location, ParseError, state actions, Either, and parser combinators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.