From 72598c2168ee5cfd528657cc94fcfaa91c812227 Mon Sep 17 00:00:00 2001 From: Aidan Hahn Date: Sat, 18 Sep 2021 16:48:24 -0700 Subject: [PATCH] new fixes for lexing process, tests to go with them --- src/lex.rs | 19 +++++++++++++------ src/str.rs | 5 ++--- tests/test_lex.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/lex.rs b/src/lex.rs index 5999232..156443f 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -111,6 +111,11 @@ fn process(document: String) -> Result { match c { // add a new Seg reference to the stack '(' => { + if is_str { + token.push(c); + continue + } + if token != "" { return Err("list started in middle of another token".to_string()); } @@ -135,7 +140,7 @@ fn process(document: String) -> Result { } // add to token _ => { - token.push(c) + token.push(c); } } @@ -149,11 +154,13 @@ fn process(document: String) -> Result { let mut current_seg_ref = ref_stack.pop().unwrap(); let mut obj; - if token.len() > 0 { - if is_str { - obj = Ctr::String(token); - is_str = false; - } else if token == "true" { + if is_str { + obj = Ctr::String(token); + is_str = false; + token = String::new(); + list_append(current_seg_ref.clone(), obj); + } else if token.len() > 0 { + if token == "true" { obj = Ctr::Bool(true); } else if token == "false" { obj = Ctr::Bool(false); diff --git a/src/str.rs b/src/str.rs index 6969ece..64c43c9 100644 --- a/src/str.rs +++ b/src/str.rs @@ -41,14 +41,13 @@ pub fn get_echo() -> Function { Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::None => () } - + string.push_str(" "); return true; }) { // TODO: use custom logger println!("Echo failure!") } - - return Ctr::String(string); + return Ctr::String(string); } ) }; diff --git a/tests/test_lex.rs b/tests/test_lex.rs index 9b5c643..52fe60c 100644 --- a/tests/test_lex.rs +++ b/tests/test_lex.rs @@ -58,6 +58,34 @@ mod lex_tests { } } + #[test] + fn test_list_delim_in_str() { + let document: &str = "('(')"; + match lex(document.to_string()) { + Ok(tree) => { + assert_eq!(ast_to_string(tree), document); + }, + Err(s) => { + print!("{}\n", s); + assert!(false); + } + } + } + + #[test] + fn test_empty_string() { + let document: &str = "('')"; + match lex(document.to_string()) { + Ok(tree) => { + assert_eq!(ast_to_string(tree), document); + }, + Err(s) => { + print!("{}\n", s); + assert!(false); + } + } + } + #[test] fn test_unmatched_list_delim_flat() { let document: &str = "(one two";