Skip to main content

Crate parsec_base

Crate parsec_base 

Source
Expand description

What Parsec’s parsers share, and nothing they don’t: a Span of input, a LineIndex that turns byte offsets into lines, columns and UTF-16 offsets, a Diagnostic with a code and a span that Diagnostic::render prints as a code frame, and the Limits and Budget that keep hostile input from costing more than you allow.

Each language keeps its own tokens, tree and error recovery; this crate is only the plumbing under them, and it has no dependencies.

use parsec_base::{Diagnostic, LineIndex, Span};

let src = "let a = 1;\nlet é = 'x\n";
let bad = Diagnostic::error("unterminated-string", Span::new(20, 22), "a string has no closing quote");
let lines = LineIndex::new(src);
assert_eq!(lines.line_col(bad.span.start), (2, 9).into());
assert_eq!(bad.span.text(src), "'x");

Structs§

Budget
Steps left to spend on a parse. Parsers spend from it on the work whose cost the input controls and stop when it runs out, so the cost of a hostile input is one you chose.
Diagnostic
Something a parser has to say about its input: a stable code to match on (like unterminated-string), where, and a sentence for people. With a 'static message and no labels, making one allocates nothing.
Exhausted
A Budget ran out.
Label
A second place a Diagnostic points at, with what it is.
Limits
The most a parse may cost. None is no limit.
LineCol
A line and a column, both counted from 1.
LineIndex
Where each line starts in a text, for turning byte offsets into lines and columns, in characters or in UTF-16 code units as editors and the language server protocol count them.
RenderOptions
How Diagnostic::render lays a code frame out.
Span
A byte range in the input, start..end.

Enums§

Severity
How bad a Diagnostic is.