nom_locate

A special input type for nom parsers to locate tokens by line, column, and offset

Library
Cargo
v5.0.0
246stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
37/100Needs Attention
Development Activity0
Maintenance0
Community68
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture80
Code Quality82
Innovation74
Learning Curve72

nom_locate is a small companion crate for the nom parser-combinator library that wraps your input in a LocatedSpan, carrying byte offset, line, and column information alongside the raw fragment. As nom consumes the input, the span tracks exactly where each token was found, so parsers can report positions for errors, diagnostics, or source mapping.

Because LocatedSpan implements the same input traits nom expects, existing nom parsers work almost unchanged: alias your input to a Span, call the provided position combinator where you need a location, and read back offset/line/column from the resulting span.

What You Get

  • A LocatedSpan<T, X> type that wraps any nom-compatible input and records offset, line, and column
  • A position combinator to capture the current location at any point in a parser
  • Optional extra state (the X type parameter) threaded through the parse for custom context
  • no_std support and SIMD-accelerated line counting via bytecount feature flags

Common Use Cases

  • Reporting precise line/column positions in parser error messages
  • Building source maps or spans for compilers, linters, and language tooling
  • Attaching location metadata to AST nodes for later diagnostics

Under The Hood

Architecture - The crate is centered on a single generic struct, LocatedSpan<T, X>, defined in src/lib.rs. It stores the wrapped fragment plus a byte offset and line number, and computes the column lazily from the offset. It implements the nom input traits (InputTake, InputIter, Compare, Offset, and friends) by delegating to the inner fragment while updating position bookkeeping, so nom’s combinators operate on the span transparently. The position combinator returns the current span without consuming input.

Tech Stack - Pure Rust (edition 2018, MSRV 1.65). Depends on nom v8, memchr, and bytecount for fast line counting, with optional stable_deref_trait. Feature flags toggle std/alloc/no_std and SIMD strategies (generic-simd, runtime-dispatch-simd) that pass through to bytecount.

Code Quality - A focused ~770-line lib.rs with inline unit tests in src/tests.rs (39 test functions) plus integration tests. Doc comments include runnable examples, and a FAQ and CHANGELOG accompany the source. Trait implementations are systematic and well-scoped.

API Design - The public surface is intentionally tiny: alias your input to LocatedSpan, sprinkle in the position combinator, and read offset/line/column via accessors like location_offset, location_line, and get_column. Because the span satisfies nom’s input traits, adopting it in an existing parser is nearly mechanical, which keeps the learning curve low for anyone already using nom.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search