2024-01-15 22:59:02 +00:00
|
|
|
pub mod lexer;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod lexer_tests {
|
|
|
|
use super::lexer::{self, Token, Class};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key() {
|
|
|
|
let tree = lexer::tokenize("key = \"value\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key_array() {
|
|
|
|
let tree = lexer::tokenize("key = [ \"\\\"value\", 6 ]");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let t1 = Token { class: Class::SEPARATOR, value: Some("[".to_string()) };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("\"value".to_string()) };
|
|
|
|
let t2 = Token { class: Class::SEPARATOR, value: Some(",".to_string()) };
|
|
|
|
let strn2 = Token { class: Class::INTEGER, value: Some("6".to_string()) };
|
|
|
|
let t3 = Token { class: Class::SEPARATOR, value: Some("]".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, t1, strn, t2, strn2, t3];
|
|
|
|
assert_eq!(tree, manual_tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key_unknown_token() {
|
|
|
|
let tree = lexer::tokenize("key = ^");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::UNKNOWN, value: Some("^".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, strn];
|
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key_escaped_double_quote_in_string() {
|
|
|
|
let tree = lexer::tokenize("key = \"\\\"value\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
2024-01-16 20:25:09 +00:00
|
|
|
let strn = Token { class: Class::STRING, value: Some("\"value".to_string()) };
|
2024-01-15 22:59:02 +00:00
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integers() {
|
|
|
|
let tree = lexer::tokenize("key = 123567\r\nkey2 = -400");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::INTEGER, value: Some("123567".to_string()) };
|
|
|
|
let nl = Token { class: Class::NEWLINE, value: None };
|
|
|
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
|
|
|
let op2 = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn2 = Token { class: Class::INTEGER, value: Some("-400".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn booleans() {
|
|
|
|
let tree = lexer::tokenize("key = true\r\nkey2 = FALSE");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::TRUE, value: None };
|
|
|
|
let nl = Token { class: Class::NEWLINE, value: None };
|
|
|
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
|
|
|
let op2 = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn2 = Token { class: Class::FALSE, value: None };
|
|
|
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key_space_in_string() {
|
|
|
|
let tree = lexer::tokenize("key = \"a b c\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("a b c".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_key_double_space_in_string() {
|
|
|
|
let tree = lexer::tokenize("key = \"a b c d\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("a b c d".to_string()) };
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn triple_key() {
|
|
|
|
let tree = lexer::tokenize("key = \"value\"\r\nkey2 = \"value2\"\r\nkey3 = \"value3\"");
|
|
|
|
|
|
|
|
let nl = Token { class: Class::NEWLINE, value: None };
|
|
|
|
let nl2 = Token { class: Class::NEWLINE, value: None };
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
|
|
|
let op2 = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn2 = Token { class: Class::STRING, value: Some("value2".to_string()) };
|
|
|
|
let id3 = Token { class: Class::IDENTIFIER, value: Some("key3".to_string()) };
|
|
|
|
let op3 = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn3 = Token { class: Class::STRING, value: Some("value3".to_string()) };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2, nl2, id3, op3, strn3];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment() {
|
|
|
|
let tree = lexer::tokenize("# This is a comment\r\nkey = \"value\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_comment() {
|
|
|
|
let tree = lexer::tokenize("# This is a comment\r\nkey = \"value\" # I'm inline!");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_comment_no_spaces() {
|
|
|
|
let tree = lexer::tokenize("#This is a comment\r\nkey = \"value\" #I'm inline!");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_identifier() {
|
|
|
|
let tree = lexer::tokenize("\"key\" = \"value\"");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, strn];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_equals() {
|
|
|
|
let tree = lexer::tokenize("\"key\" = =");
|
|
|
|
|
|
|
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
|
|
|
let op = Token { class: Class::EQUALS, value: None };
|
|
|
|
let op2 = Token { class: Class::EQUALS, value: None };
|
|
|
|
|
|
|
|
let manual_tree = vec![id, op, op2];
|
2024-01-16 20:25:09 +00:00
|
|
|
assert_eq!(tree, manual_tree);
|
2024-01-15 22:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|