use crate::tokenizer::LexicalTokenType::{ConstInteger, Delimiter, Identifier, Keyword, Operator}; use crate::tokenizer::{zero_lexical_parser, LexicalTokenType}; use zero_parser::parser::ParserContext; mod tokenizer; fn validate_tokens(input: &'static str, tokens: Vec) { let context = ParserContext::new(()); let word: Vec = input.chars().collect(); let (_, actual_tokens) = zero_lexical_parser(context.clone(), word.as_slice()).unwrap(); dbg!(&tokens, &actual_tokens); assert_eq!(tokens.len(), actual_tokens.len()); for (actual, except) in actual_tokens.iter().zip(tokens.iter()) { assert_eq!( &actual.token_type, except, "The literal value of actual token is {}.", actual .literal_value .iter() .map(|x| x.clone()) .collect::() ); } } #[test] fn main_test() { validate_tokens( "int main() { return 0; }", vec![ Keyword, Identifier, Delimiter, Delimiter, Delimiter, Keyword, ConstInteger(0), Delimiter, Delimiter, ], ); } #[test] fn number_test() { validate_tokens("123", vec![ConstInteger(123)]) } #[test] fn hexadecimal_test() { validate_tokens( "// test hexadecimal define int main(){ int a; a = 0xf; return a; }", vec![ Keyword, Identifier, Delimiter, Delimiter, Delimiter, Keyword, Identifier, Delimiter, Identifier, Operator, ConstInteger(15), Delimiter, Keyword, Identifier, Delimiter, Delimiter, ], ); } #[test] fn while_and_if_test() { validate_tokens( " // test while-if int whileIf() { int a; a = 0; int b; b = 0; while (a < 100) { if (a == 5) { b = 25; } else if (a == 10) { b = 42; } else { b = a * 2; } a = a + 1; } return (b); }", vec![ // int whileIf() { Keyword, Identifier, Delimiter, Delimiter, Delimiter, // int a; Keyword, Identifier, Delimiter, // a = 0; Identifier, Operator, ConstInteger(0), Delimiter, // int b; Keyword, Identifier, Delimiter, // b = 0; Identifier, Operator, ConstInteger(0), Delimiter, // while (a < 100) { Keyword, Delimiter, Identifier, Operator, ConstInteger(100), Delimiter, Delimiter, // if (a == 5) { Keyword, Delimiter, Identifier, Operator, ConstInteger(5), Delimiter, Delimiter, // b = 25; Identifier, Operator, ConstInteger(25), Delimiter, // } Delimiter, // else if (a == 10) { Keyword, Keyword, Delimiter, Identifier, Operator, ConstInteger(10), Delimiter, Delimiter, // b = 42; Identifier, Operator, ConstInteger(42), Delimiter, // } Delimiter, // else { Keyword, Delimiter, // b = a * 2; Identifier, Operator, Identifier, Operator, ConstInteger(2), Delimiter, // } Delimiter, // a = a + 1; Identifier, Operator, Identifier, Operator, ConstInteger(1), Delimiter, // } Delimiter, // return (b); Keyword, Delimiter, Identifier, Delimiter, Delimiter, // } Delimiter, ], ); }