/// split tokens by comma. Comma in parenthesis/brace/bracket would be skipped.
split tokens by comma. Comma in parenthesis/brace/bracket would be skipped.
split tokens by comma . Comma in parenthesis / brace / bracket would be skipped .
fn split_by_comma(code: Code) -> Vec<Vec<Token>> {
    let mut tokens = Vec::new();
    let mut iter = code.tokens.iter().peekable();
    // use some counters to ensure the target comma is not in parenthesis/brace/bracket pairs.
    let mut paren_n = 0;
    let mut brace_n = 0;
    let mut bracket_n = 0;
    loop {
        let mut inner = Vec::new();
        loop {
            match iter.next() {
                Some(next) => {
                    match next {
                        Token::Symbol(Symbol::Comma) if paren_n == 0 && brace_n == 0 && bracket_n == 0 => break,
                        Token::Symbol(Symbol::LParen) => paren_n += 1,
                        Token::Symbol(Symbol::RParen) => paren_n -= 1,
                        Token::Symbol(Symbol::LBrace) => brace_n += 1,
                        Token::Symbol(Symbol::RBrace) => brace_n -= 1,
                        Token::Symbol(Symbol::LBracket) => bracket_n += 1,
                        Token::Symbol(Symbol::RBracket) => bracket_n -= 1,
                        _ => ()
                    }
                    inner.push(next.clone());
                }
                None => break
            }
        }
        tokens.push(inner);
        if iter.peek().is_none() {
            break;
        }
    }
    tokens
}
fn split_by_comma ( code : Code ) -> Vec < Vec < Token > > { let mut tokens = Vec :: new ( ) ; let mut iter = code . tokens . iter ( ) . peekable ( ) ; // use some counters to ensure the target comma is not in parenthesis/brace/bracket pairs. let mut paren_n = 0 ; let mut brace_n = 0 ; let mut bracket_n = 0 ; loop { let mut inner = Vec :: new ( ) ; loop { match iter . next ( ) { Some ( next ) => { match next { Token :: Symbol ( Symbol :: Comma ) if paren_n == 0 && brace_n == 0 && bracket_n == 0 => break , Token :: Symbol ( Symbol :: LParen ) => paren_n += 1 , Token :: Symbol ( Symbol :: RParen ) => paren_n -= 1 , Token :: Symbol ( Symbol :: LBrace ) => brace_n += 1 , Token :: Symbol ( Symbol :: RBrace ) => brace_n -= 1 , Token :: Symbol ( Symbol :: LBracket ) => bracket_n += 1 , Token :: Symbol ( Symbol :: RBracket ) => bracket_n -= 1 , _ => ( ) } inner . push ( next . clone ( ) ) ; } None => break } } tokens . push ( inner ) ; if iter . peek ( ) . is_none ( ) { break ; } } tokens }
split tokens by comma.
split tokens by comma .
code
Code