From 72190fe9bbe8b72294d4649d9c3b68f101f2aad2 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Tue, 24 Sep 2024 09:33:37 +0200 Subject: Split module into ast and parser --- rust/src/parser.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 rust/src/parser.rs (limited to 'rust/src/parser.rs') diff --git a/rust/src/parser.rs b/rust/src/parser.rs new file mode 100644 index 0000000..d5d4c30 --- /dev/null +++ b/rust/src/parser.rs @@ -0,0 +1,27 @@ +use crate::ast::*; + +pub fn parse(arg: &str) -> Value { + let token = arg.trim(); + parse_number(token) + .or(parse_bool(token)) + .or(parse_symbol(token)) + .unwrap() +} + +fn parse_symbol(token: &str) -> Result { + Ok(Value::Sym(token.to_string())) +} + +fn parse_bool(token: &str) -> Result { + token + .parse::() + .map(Value::Bool) + .map_err(|e| e.to_string()) +} + +fn parse_number(token: &str) -> Result { + token + .parse::() + .map(Value::Num) + .map_err(|e| e.to_string()) +} -- cgit v1.2.3