extern crate honeycomb; use honeycomb::{ atoms::{list, rec, seq_no_ws, space}, language::{identifier, token}, Parser, }; use std::collections::HashMap; #[test] fn markup_test() { let mut map = HashMap::new(); map.insert( String::from("row"), Markup::Array(vec![ Markup::Value(String::from("1")), Markup::Value(String::from("2")), Markup::Value(String::from("3")), ]), ); assert_eq!( (markup() * (1..)).parse( r#" row { - 1 - 2 - 3 }"# ), Ok(vec![Markup::Object(map)]) ); } #[derive(Clone, PartialEq, Debug)] pub enum Markup { Value(String), Object(HashMap), Array(Vec), } fn object() -> Parser { space() >> (((space() >> identifier() & (seq_no_ws("{") >> markup() << (seq_no_ws("}") << space()))) * (1..)) - |obj: Vec<(String, Markup)>| { let mut map = HashMap::new(); for (s, y) in obj { map.insert(s, y); } Markup::Object(map) }) << space() } fn array() -> Parser { seq_no_ws("-") >> list(rec(markup), seq_no_ws("-")) - Markup::Array } fn value() -> Parser { (space() >> token() << space()) - Markup::Value } fn markup() -> Parser { rec(object) | rec(array) | rec(value) }