Refactor how idents are flattened
This commit is contained in:
parent
3404073ed8
commit
705ae0c810
@ -46,6 +46,33 @@ fn parse_hex(s: String) -> Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn flatten_ident<I: Iterator<Item = Token>>(toks: &mut Peekable<I>, 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::<String>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
TokenKind::Ident(ref i) => {
|
||||||
|
toks.next();
|
||||||
|
s.push_str(i)
|
||||||
|
}
|
||||||
|
TokenKind::Number(ref n) => {
|
||||||
|
toks.next();
|
||||||
|
s.push_str(n)
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
pub fn from_tokens<I: Iterator<Item = Token>>(
|
pub fn from_tokens<I: Iterator<Item = Token>>(
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
@ -151,32 +178,7 @@ impl Value {
|
|||||||
TokenKind::Symbol(Symbol::BitAnd) => {
|
TokenKind::Symbol(Symbol::BitAnd) => {
|
||||||
Some(Value::Ident(String::from("&"), QuoteKind::None))
|
Some(Value::Ident(String::from("&"), QuoteKind::None))
|
||||||
}
|
}
|
||||||
TokenKind::Symbol(Symbol::Hash) => {
|
TokenKind::Symbol(Symbol::Hash) => Some(parse_hex(flatten_ident(toks, scope))),
|
||||||
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::<String>(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
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::Interpolation => {
|
// TokenKind::Interpolation => {
|
||||||
// Some(Value::Ident(
|
// Some(Value::Ident(
|
||||||
// parse_interpolation(toks, scope)
|
// parse_interpolation(toks, scope)
|
||||||
@ -187,24 +189,7 @@ impl Value {
|
|||||||
// ))
|
// ))
|
||||||
// }
|
// }
|
||||||
TokenKind::Ident(mut s) => {
|
TokenKind::Ident(mut s) => {
|
||||||
while let Some(tok) = toks.peek() {
|
s.push_str(&flatten_ident(toks, scope));
|
||||||
match tok.kind.clone() {
|
|
||||||
TokenKind::Interpolation => {
|
|
||||||
toks.next();
|
|
||||||
s.push_str(
|
|
||||||
&parse_interpolation(toks, scope)
|
|
||||||
.iter()
|
|
||||||
.map(|x| x.kind.to_string())
|
|
||||||
.collect::<String>(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
TokenKind::Ident(ref i) => {
|
|
||||||
toks.next();
|
|
||||||
s.push_str(i)
|
|
||||||
}
|
|
||||||
_ => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match toks.peek() {
|
match toks.peek() {
|
||||||
Some(Token {
|
Some(Token {
|
||||||
kind: TokenKind::Symbol(Symbol::OpenParen),
|
kind: TokenKind::Symbol(Symbol::OpenParen),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user