Finish challenge 16 and problem set 2.

This commit is contained in:
2022-06-28 15:56:31 -04:00
parent fd6f9464cc
commit 956d75ab4e
3 changed files with 58 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ pub enum Token {
Identifier(String),
Equal,
Ampersand,
Semicolon,
}
pub type Tokens = Vec<Token>;
@@ -19,6 +20,9 @@ pub fn parse_key_value(text: &str) -> HashMap<String, String> {
[Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Ampersand] => {
result.insert(key.to_string(), value.to_string());
}
[Token::Identifier(key), Token::Equal, Token::Identifier(value), Token::Semicolon] => {
result.insert(key.to_string(), value.to_string());
}
[Token::Identifier(key), Token::Equal, Token::Identifier(value)] => {
result.insert(key.to_string(), value.to_string());
}
@@ -40,6 +44,8 @@ fn scan(code: &str, mut ix: usize, mut tokens: Tokens) -> Tokens {
tokens.push(Token::Ampersand);
} else if c == '=' {
tokens.push(Token::Equal);
} else if c == ';' {
tokens.push(Token::Semicolon);
} else {
panic!("Unexpected char '{}' at index {}", c, ix);
}
@@ -62,4 +68,4 @@ fn scan_identifier(code: &str, mut ix: usize, mut tokens: Tokens) -> Tokens {
scan(code, ix, tokens)
}
const SPECIAL_CHARS: &[char] = &['.', '@'];
const SPECIAL_CHARS: &[char] = &['.', '@', '%'];