#json-parser #spans #packrat #json-text #white-space #position #items

textparse

A library to declaratively implement parsers that are based on Packrat Parsing

3 unstable releases

0.1.0 Dec 25, 2022
0.0.2 Nov 21, 2022
0.0.1 Nov 7, 2022

#2696 in Parser implementations

Download history 7/week @ 2023-12-18 19/week @ 2023-12-25 8/week @ 2024-02-12 7/week @ 2024-02-19 28/week @ 2024-02-26 10/week @ 2024-03-04 15/week @ 2024-03-11 6/week @ 2024-03-18 49/week @ 2024-04-01

74 downloads per month
Used in 2 crates (via ffmml)

MIT/Apache

29KB
781 lines

textparse

textparse Documentation Actions Status License

A Rust library to declaratively implement parsers that are based on Packrat Parsing.

Examples

The following code implements a parser for a JSON subset format:

use textparse::{
    components::{AnyChar, Char, Digit, Eos, Items, NonEmpty, Not, Str, While, Whitespace},
    Parse, Parser, Position, Span,
};

#[derive(Clone, Span, Parse)]
struct JsonValue(WithoutWhitespaces<JsonValueInner>);

#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON value")]
enum JsonValueInner {
    Null(JsonNull),
    String(JsonString),
    Number(JsonNumber),
    Array(JsonArray),
    Object(JsonObject),
}

#[derive(Clone, Span, Parse)]
struct JsonNull(Str<'n', 'u', 'l', 'l'>);

#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON string")]
struct JsonString(Char<'"'>, While<(Not<Char<'"'>>, AnyChar)>, Char<'"'>);

#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON number")]
struct JsonNumber(NonEmpty<While<Digit>>);

#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON array")]
struct JsonArray(Char<'['>, Csv<JsonValue>, Char<']'>);

#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON object")]
struct JsonObject(Char<'{'>, Csv<JsonObjectItem>, Char<'}'>);

#[derive(Clone, Span, Parse)]
struct JsonObjectItem(WithoutWhitespaces<JsonString>, Char<':'>, JsonValue);

#[derive(Clone, Span, Parse)]
struct Csv<T>(Items<T, Char<','>>);

#[derive(Clone, Span, Parse)]
struct WithoutWhitespaces<T>(While<Whitespace>, T, While<Whitespace>);

You can run the above parser via examples/check_json.rs as follows:

$ echo '["foo",null,{"key": "value"}, 123]' | cargo run --example check_json
OK: the input string is a JSON text.

$ echo '["foo" null]' | cargo run --example check_json
Error: expected one of ',', or ']'
  --> <STDIN>:1:8
  |
1 | ["foo" null]
  |        ^ expected one of ',', or ']'

Dependencies

~3.5MB
~72K SLoC