#parser-combinator #nom #utilities

cosmic-nom

Is a collection of utilities for making using the great nom parser combinator easier to use

27 releases

0.3.14 Aug 31, 2024
0.3.13 Aug 31, 2024
0.3.6 Nov 21, 2022
0.3.3 Oct 31, 2022
0.3.0 Sep 20, 2022

#755 in Parser implementations

Download history 14/week @ 2024-08-16 12/week @ 2024-08-23 744/week @ 2024-08-30 64/week @ 2024-09-06 24/week @ 2024-09-13 31/week @ 2024-09-20 18/week @ 2024-09-27 8/week @ 2024-10-04 22/week @ 2024-10-11 10/week @ 2024-10-18 2/week @ 2024-10-25 33/week @ 2024-11-01 14/week @ 2024-11-08 11/week @ 2024-11-15 13/week @ 2024-11-22 12/week @ 2024-11-29

51 downloads per month
Used in 11 crates (5 directly)

MIT license

25KB
772 lines

COSMIC NOM

Is a collection of utilities for making using the great nom parser combinator easier to use.

THE COSMIC INITIATIVE

cosmic-nom is part of The Cosmic Initiative a WebAssembly orchestration framework--although you can use it for any project you want.

A DERIVATIVE OF DERIVATIVE WORKS

cosmic-nom synthesizes the utilities of two other derivatives of nom:

  • nom-supreme A collection of excellent utilities for nom (which cosmic-nom uses for improved error handling)
  • nom_locate A special input type for nom to locate tokens (which cosmic-nom uses to locate error spans in the parsed content)

COSMIC NOMS CONTRIBUTIONS

IMPLEMENTATION

To use cosmic-nom you must accept an input implementing trait cosmic_nom::Span and return a result of type cosmic_nom::Res:

pub fn name<I:Span>( input:I ) -> Res<I,I> {
  alpha(input)
}

SPAN

Since It is hard to compose a combinator if your input type doesn't implement all of the traits used in every condition: InputLength, InputTakeAtPosition, AsBytes, etc... So cosmic-nom provides a single trait that also supports location captures via nom_locate and seems to work with every combinator in the complete package (not tested on streaming).

pub trait Span:
    Clone
    + ToString
    + AsBytes
    + Slice<Range<usize>>
    + Slice<RangeTo<usize>>
    + Slice<RangeFrom<usize>>
    + InputLength
    + Offset
    + InputTake
    + InputIter<Item = char>
    + InputTakeAtPosition<Item = char>
    + Compare<&'static str>
    + FindSubstring<&'static str>
    + core::fmt::Debug

To create a span call cosmic_nom::new_span("scott williams")

name(new_span("scott williams"));

RESULT

use result to transform your Res into a regular Result<O,E>

let name: I = result(name(new_span("scott williams")))?;

LOG

and you can wrap your result in a log() which will output to stdout if an error occurs:

let name: I = log(result(name(new_span("scott williams"))))?;

IMPORTANT!

Be warned that in the service of making things easier and more reportable cosmic-nom makes nom a little less awesome in some other ways... for instance nom has great efficiencies due to its "zero-copy" input strategy and it seems to accomplish this by passing and slicing a single &str around... cosmic-nom wraps input in an Arc and still takes great care to minimize overhead, but it breaks with the pure spirit of nom in order to provide a little ease of use.

Dependencies

~2.4–3MB
~68K SLoC