new fixes for lexing process, tests to go with them

This commit is contained in:
Aidan Hahn 2021-09-18 16:48:24 -07:00
parent 5ed8a054d3
commit 72598c2168
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 43 additions and 9 deletions

View file

@ -111,6 +111,11 @@ fn process(document: String) -> Result<Ast, String> {
match c { match c {
// add a new Seg reference to the stack // add a new Seg reference to the stack
'(' => { '(' => {
if is_str {
token.push(c);
continue
}
if token != "" { if token != "" {
return Err("list started in middle of another token".to_string()); return Err("list started in middle of another token".to_string());
} }
@ -135,7 +140,7 @@ fn process(document: String) -> Result<Ast, String> {
} }
// add to token // add to token
_ => { _ => {
token.push(c) token.push(c);
} }
} }
@ -149,11 +154,13 @@ fn process(document: String) -> Result<Ast, String> {
let mut current_seg_ref = ref_stack.pop().unwrap(); let mut current_seg_ref = ref_stack.pop().unwrap();
let mut obj; let mut obj;
if token.len() > 0 { if is_str {
if is_str { obj = Ctr::String(token);
obj = Ctr::String(token); is_str = false;
is_str = false; token = String::new();
} else if token == "true" { list_append(current_seg_ref.clone(), obj);
} else if token.len() > 0 {
if token == "true" {
obj = Ctr::Bool(true); obj = Ctr::Bool(true);
} else if token == "false" { } else if token == "false" {
obj = Ctr::Bool(false); obj = Ctr::Bool(false);

View file

@ -41,14 +41,13 @@ pub fn get_echo() -> Function {
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => () Ctr::None => ()
} }
string.push_str(" ");
return true; return true;
}) { }) {
// TODO: use custom logger // TODO: use custom logger
println!("Echo failure!") println!("Echo failure!")
} }
return Ctr::String(string);
return Ctr::String(string);
} }
) )
}; };

View file

@ -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] #[test]
fn test_unmatched_list_delim_flat() { fn test_unmatched_list_delim_flat() {
let document: &str = "(one two"; let document: &str = "(one two";