From 705ae0c810e6c35a0f3873895188a5cd513125bb Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 8 Feb 2020 16:49:44 -0500 Subject: [PATCH] Refactor how idents are flattened --- src/value/parse.rs | 73 ++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/src/value/parse.rs b/src/value/parse.rs index 1f18ceb..a0c3f1f 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -46,6 +46,33 @@ fn parse_hex(s: String) -> Value { } } +fn flatten_ident>(toks: &mut Peekable, scope: &Scope) -> String { + let mut s = String::new(); + while let Some(tok) = toks.peek() { + match tok.kind.clone() { + TokenKind::Interpolation => { + toks.next(); + s.push_str( + &parse_interpolation(toks, scope) + .iter() + .map(|x| x.kind.to_string()) + .collect::(), + ) + } + TokenKind::Ident(ref i) => { + toks.next(); + s.push_str(i) + } + TokenKind::Number(ref n) => { + toks.next(); + s.push_str(n) + } + _ => break, + } + } + s +} + impl Value { pub fn from_tokens>( toks: &mut Peekable, @@ -151,32 +178,7 @@ impl Value { TokenKind::Symbol(Symbol::BitAnd) => { Some(Value::Ident(String::from("&"), QuoteKind::None)) } - TokenKind::Symbol(Symbol::Hash) => { - let mut s = String::new(); - while let Some(tok) = toks.peek() { - match tok.kind.clone() { - TokenKind::Interpolation => { - toks.next(); - s.push_str( - &parse_interpolation(toks, scope) - .iter() - .map(|x| x.kind.to_string()) - .collect::(), - ) - } - TokenKind::Ident(ref i) => { - toks.next(); - s.push_str(i) - } - TokenKind::Number(ref n) => { - toks.next(); - s.push_str(n) - } - _ => break, - } - } - Some(parse_hex(s)) - } + TokenKind::Symbol(Symbol::Hash) => Some(parse_hex(flatten_ident(toks, scope))), // TokenKind::Interpolation => { // Some(Value::Ident( // parse_interpolation(toks, scope) @@ -187,24 +189,7 @@ impl Value { // )) // } TokenKind::Ident(mut s) => { - while let Some(tok) = toks.peek() { - match tok.kind.clone() { - TokenKind::Interpolation => { - toks.next(); - s.push_str( - &parse_interpolation(toks, scope) - .iter() - .map(|x| x.kind.to_string()) - .collect::(), - ) - } - TokenKind::Ident(ref i) => { - toks.next(); - s.push_str(i) - } - _ => break, - } - } + s.push_str(&flatten_ident(toks, scope)); match toks.peek() { Some(Token { kind: TokenKind::Symbol(Symbol::OpenParen),