https://github.com/sile/textparse
A Rust library to declaratively implement parsers that are based on Packrat Parsing.
https://github.com/sile/textparse
parser rust
Last synced: about 1 year ago
JSON representation
A Rust library to declaratively implement parsers that are based on Packrat Parsing.
- Host: GitHub
- URL: https://github.com/sile/textparse
- Owner: sile
- License: apache-2.0
- Created: 2022-11-06T03:46:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-25T04:21:15.000Z (over 3 years ago)
- Last Synced: 2024-05-01T23:47:30.744Z (about 2 years ago)
- Topics: parser, rust
- Language: Rust
- Homepage:
- Size: 119 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
textparse
=========
[](https://crates.io/crates/textparse)
[](https://docs.rs/textparse)
[](https://github.com/sile/textparse/actions)

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:
```rust
use textparse::{
components::{AnyChar, Char, Digit, Eos, Items, NonEmpty, Not, Str, While, Whitespace},
Parse, Parser, Position, Span,
};
#[derive(Clone, Span, Parse)]
struct JsonValue(WithoutWhitespaces);
#[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>, AnyChar)>, Char<'"'>);
#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON number")]
struct JsonNumber(NonEmpty>);
#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON array")]
struct JsonArray(Char<'['>, Csv, Char<']'>);
#[derive(Clone, Span, Parse)]
#[parse(name = "a JSON object")]
struct JsonObject(Char<'{'>, Csv, Char<'}'>);
#[derive(Clone, Span, Parse)]
struct JsonObjectItem(WithoutWhitespaces, Char<':'>, JsonValue);
#[derive(Clone, Span, Parse)]
struct Csv(Items>);
#[derive(Clone, Span, Parse)]
struct WithoutWhitespaces(While, T, While);
```
You can run the above parser via [examples/check_json.rs](examples/check_json.rs) as follows:
```console
$ 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 ']'
--> :1:8
|
1 | ["foo" null]
| ^ expected one of ',', or ']'
```