Crates.io | starlane-parse |
lib.rs | starlane-parse |
version | 0.3.14 |
source | src |
created_at | 2024-09-18 20:39:20.436274 |
updated_at | 2024-09-18 20:39:20.436274 |
description | Is a collection of utilities for making using the great [nom](https://crates.io/crates/nom) parser combinator easier to use. |
homepage | https://starlane.io |
repository | https://github.com/cosmic-initiative/cosmic-initiative.git |
max_upload_size | |
id | 1379611 |
size | 24,988 |
Is a collection of utilities for making using the great nom parser combinator easier to use.
cosmic-nom
is part of The Cosmic Initiative
a WebAssembly orchestration framework--although you can use it for any project you want.
cosmic-nom
synthesizes the utilities of two other derivatives of nom:
cosmic-nom
uses for improved error handling)cosmic-nom
uses to locate error spans in the parsed content)To use cosmic-nom
you must accept an input implementing trait starlane_parse::Span
and return a result of type starlane_parse::Res
:
pub fn name<I:Span>( input:I ) -> Res<I,I> {
alpha(input)
}
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 starlane_parse::new_span("scott williams")
name(new_span("scott williams"));
use result
to transform your Res
into a regular Result<O,E>
let name: I = result(name(new_span("scott williams")))?;
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"))))?;
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.