sponge_lib/
decompose_tokens.rs1use std::error::Error;
2
3#[derive(Debug, Clone)]
4pub enum Token {
5 Keyword(String),
6 Identifier(String),
7 Symbol(char),
8 Literal(i32),
9}
10
11pub fn decompose(content: &str) -> Result<Vec<Token>, Box<dyn Error>> {
12 let mut tokens = Vec::new();
13 let words = content.split_whitespace();
14
15 for word in words {
16 match word {
17 "fn" | "let" | "mut" | "i32" | "main" => tokens.push(Token::Keyword(word.to_string())),
18 "{" | "}" | "(" | ")" | ":" | ";" | "=" => {
19 for c in word.chars() {
20 tokens.push(Token::Symbol(c));
21 }
22 }
23 _ => {
24 if let Ok(literal) = word.parse::<i32>() {
25 tokens.push(Token::Literal(literal));
26 } else {
27 tokens.push(Token::Identifier(word.to_string()));
28 }
29 }
30 }
31 }
32
33 Ok(tokens)
34}