From c93770786563a88f3262be9889ca05ad737084f5 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Tue, 29 Jun 2021 14:18:20 +0200 Subject: [PATCH] linked list thingy --- src/config.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/config.rs b/src/config.rs index 2dfe97e..19f9cc2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use super::*; use anyhow::*; +use std::collections::LinkedList; type VarName = String; type AttrValue = String; @@ -96,3 +97,52 @@ pub fn parse_key_values(iter: impl Iterator>) -> HashMap>, +} + +enum ExpressionElement { + Single(Sp), + KeyValue(Vec<(String, Sp)>), +} + +impl Iterator for SExpIterator { + type Item = ExpressionElement; + + fn next(&mut self) -> Option { + let mut data = vec![]; + loop { + match (self.elements.pop_front(), self.elements.pop_front()) { + (Some(Sp(kw_l, Expr::Keyword(kw), kw_r)), Some(value)) => { + data.push((kw, value)); + } + (Some(x), Some(y)) => { + self.elements.push_front(y); + self.elements.push_front(x); + break; + } + (Some(x), None) => { + self.elements.push_front(x); + break; + } + (None, None) => break, + (None, Some(_)) => unreachable!(), + } + } + if data.is_empty() { + Some(ExpressionElement::Single(self.elements.pop_front()?)) + } else { + Some(ExpressionElement::KeyValue(data)) + } + } +} + +/* + ( + foo + bar + :baz "hi" :bat "ho" + [rst arst arst rst]) + +*/