summaryrefslogtreecommitdiff
path: root/rust/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/parser.rs')
-rw-r--r--rust/src/parser.rs27
1 files changed, 27 insertions, 0 deletions
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<Value, String> {
+ Ok(Value::Sym(token.to_string()))
+}
+
+fn parse_bool(token: &str) -> Result<Value, String> {
+ token
+ .parse::<bool>()
+ .map(Value::Bool)
+ .map_err(|e| e.to_string())
+}
+
+fn parse_number(token: &str) -> Result<Value, String> {
+ token
+ .parse::<i32>()
+ .map(Value::Num)
+ .map_err(|e| e.to_string())
+}