liveBook · Manning

Location

As explained in Functional Programming in Scala, Second Edition

A representation of a position in input containing the input text, an offset, and line and column information.

A Location identifies where a parser is operating or where it encountered an error. It retains the complete input, an offset into that input, and line and column information derived lazily from the input and offset. section 9.5.1

The offset provides the precise position used by parser operations. The derived line counts newline characters before that position, while the column is measured from the most recent newline or from the beginning of the input.

Location supports several parts of parser error reporting:

  • Associates a failure with the input position where it occurred. section 9.5.1
  • Allows alternative failures at the same position to be grouped and their messages combined. section 9.8
  • Supports readable output containing line and column information, the failing input line, and a caret marking the position. section 9.8

A labeled parser failure

When a parser is wrapped with label, its failure carries a message such as “non-negative integer” together with the Location supplied by the parser implementation. The message identifies what was expected; the Location identifies where it was expected. section 9.5.1

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines the representation and derives line and column lazily, then applies locations to grouped and formatted parse errors.
Examples
Demonstrates labeled failures, same-location error collapsing, line-and-column output, and caret-marked source context.
What it assumes
Assumes parser combinators and parser failures, while introducing Location as a concrete error-reporting design.
What you can do after
Diagnose parser failures by locating them in source input and reading grouped messages with line, column, and caret context.
How it connects
Connects parser combinators, parse errors, parser alternatives, offsets, and error reporting.

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.