TODO: define a `consume` function to not keep repeating the peeks_tokens.next() all the time
(tokens: Vec<tokens::Token>)
| 119 | |
| 120 | // TODO: define a `consume` function to not keep repeating the peeks_tokens.next() all the time |
| 121 | pub fn parse(tokens: Vec<tokens::Token>) -> Vec<Function> { |
| 122 | let mut peeks_tokens = tokens.clone().into_iter().enumerate().peekable(); |
| 123 | let mut functions: Vec<Function> = vec![]; |
| 124 | |
| 125 | while let Some((index, token)) = peeks_tokens.peek() { |
| 126 | if let tokens::Token::Identifier(identifier) = token { |
| 127 | match identifier.as_str() { |
| 128 | "letter" | "letters" => { |
| 129 | let (func_tokens, right_pos_idx) = slice_until_end_func(&tokens, &index); |
| 130 | |
| 131 | let casing = find_casing_parameter(func_tokens, "upcase".to_string()); |
| 132 | let select = find_int_parameter(func_tokens, "select".to_string()); |
| 133 | |
| 134 | if identifier == "letter" { |
| 135 | functions.push(Function::Letter { |
| 136 | casing: casing.clone(), |
| 137 | select: select.clone(), |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | if identifier == "letters" { |
| 142 | functions.push(Function::Letters { |
| 143 | casing: casing.clone(), |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | peeks_tokens.nth(right_pos_idx + 1); |
| 148 | } |
| 149 | "number" => { |
| 150 | let (func_tokens, right_pos_idx) = slice_until_end_func(&tokens, &index); |
| 151 | |
| 152 | let select = find_int_parameter(func_tokens, "select".to_string()); |
| 153 | |
| 154 | functions.push(Function::Number { |
| 155 | select: select.clone(), |
| 156 | }); |
| 157 | |
| 158 | peeks_tokens.nth(right_pos_idx + 1); |
| 159 | } |
| 160 | "numbers" => { |
| 161 | peeks_tokens.next(); |
| 162 | functions.push(Function::Numbers) |
| 163 | } |
| 164 | "glob" => { |
| 165 | let (func_tokens, right_pos_idx) = slice_until_end_func(&tokens, &index); |
| 166 | if func_tokens |
| 167 | == [ |
| 168 | tokens::Token::LeftParen, |
| 169 | tokens::Token::Parameter("rest".to_string()), |
| 170 | tokens::Token::Equal, |
| 171 | tokens::Token::True, |
| 172 | tokens::Token::RightParen, |
| 173 | ] |
| 174 | { |
| 175 | functions.push(Function::Glob { rest: true }); |
| 176 | } |
| 177 | |
| 178 | if func_tokens |
nothing calls this directly
no test coverage detected