#brackets #parse-tree #parse #tree #parser

bracket_parse

simple parser for bracketed lists with quoted and unquoted strings -- Now deprecated, favouring Gobble instead which can do everything this does much tidier

5 releases

Uses old Rust 2015

0.1.4 Jun 11, 2020
0.1.3 Aug 3, 2018
0.1.2 Aug 3, 2018
0.1.1 Aug 2, 2018
0.1.0 Aug 2, 2018

#1882 in Parser implementations

MIT license

16KB
410 lines

Bracket Parse

This is intended to convert a bracketd lisp/prolog like string into a tree where each bracket is considered a parent of some kind

Coming in v0.1.4

Handle Jsonish Objects - May need another Variant

Changes in v0.1.3

Iterator on &Bracket

Changes v0.1.2

Added tail_n for tail chain skipping as tail().tail() drops the borrow. Added tail_h for tail(n).head(), again to avoid borrow drops().

Impl Display for Bracket //TODO Escape strings safely


lib.rs:

Bracket Parse

A Utility for parsing Bracketed lists and sets of strings.

It is now deprecated, as Gobble does everything it does better. This was one of my first Rust projects and it shows.

It is a relatively lazy way of parsing items from a bracketed string,

"hello(peter,dave)" is easy for it to handle, as are nested brackets.

The above will result in something like

Branch[Leaf("hello"),Branch[Leaf("peter"),Leaf("dave")]]

This is not intended super extensible right now, though contributions are welcome.

The list can also be constructed relatively simply by using chained builder type methods

use bracket_parse::{Bracket,br};
use bracket_parse::Bracket::{Leaf,Branch};
use std::str::FromStr;

let str1 = Bracket::from_str("hello(peter,dave)").unwrap();

//Standard Build method
let basic1 = Branch(vec![Leaf("hello".to_string()),
                        Branch(vec![Leaf("peter".to_string()),
                                    Leaf("dave".to_string())])]);

//Chaining Build method
let chain1 = br().sib_lf("hello")
                   .sib(br().sib_lf("peter").sib_lf("dave"));

assert_eq!(str1,basic1);
assert_eq!(str1,chain1);

It can also handle string input with escapes. Quotes are removed and the string item is considered a single Leaf value;

use bracket_parse::{Bracket,br,lf};
use std::str::FromStr;

let bk = Bracket::from_str(r#""hello" 'matt"' "and \"friends\"""#).unwrap();
let chn = br().sib_lf("hello").sib_lf("matt\"").sib_lf("and \"friends\"");
assert_eq!(bk,chn);

No runtime deps