diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-07-31 20:27:46 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-07-31 20:27:46 +0300 |
commit | 61b985db80a15e230476af54b0ef8783fd8cbf63 (patch) | |
tree | 4c9f34f66652550bcb606c73d2eda22c9c26821b | |
parent | 50382d13bcbd750f0330da0d526345a085c4d666 (diff) | |
download | myslip-61b985db80a15e230476af54b0ef8783fd8cbf63.tar.gz myslip-61b985db80a15e230476af54b0ef8783fd8cbf63.zip |
Added parse_to_ast for public use from other modules
-rw-r--r-- | src/parse/parsetree.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/parse/parsetree.rs b/src/parse/parsetree.rs index 46317a4..7e1c80d 100644 --- a/src/parse/parsetree.rs +++ b/src/parse/parsetree.rs @@ -151,6 +151,15 @@ fn tokens_to_ast_inner( Ok((input.into(), res)) } +pub fn parse_to_ast(s: &str) -> Result<SExp, String> { + let tokens = match tokenize(s) { + Ok(t) => Ok(t), + Err(e) => Err(format!("error in tokenization: {}", e)), + }?; + + tokens_to_ast(tokens.into()) +} + #[cfg(test)] mod private_parsing_tests { @@ -318,4 +327,18 @@ mod private_parsing_tests { ); } + #[test] + fn test_parse_to_ast() { + assert_eq!( + parse_to_ast("(* 2 (+ 3 4))"), + Ok(scons(scons( + Mul, + scons( + 2, + scons(scons(Add, scons(3, scons(4, Nil))), Nil) + ) + ), Nil)) + ); + } + } |