From 9aaa9f1e6df9c91d7e1914130c8eb346f8fe7f71 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 5 Oct 2023 08:18:33 -0400 Subject: [PATCH] Fix simple value parsing --- src/interface.rs | 13 +++++-------- src/value/mod.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/interface.rs b/src/interface.rs index 20e9cbe..57addc5 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -99,17 +99,14 @@ impl<'context, 'code> Evaluator<'context, 'code> { } fn run(self) -> Vec> { - let mut cursor_0 = self.tree.walk(); - let mut cursor_1 = self.tree.walk(); - let node = cursor_0.node(); - let item_count = node.child_count(); + let mut cursor = self.tree.walk(); + let root_node = cursor.node(); + let item_count = root_node.child_count(); let mut results = Vec::with_capacity(item_count); - println!("{}", node.to_sexp()); + println!("{}", root_node.to_sexp()); - assert_eq!(cursor_0.node().kind(), "root"); - - for item_node in node.children(&mut cursor_0) { + for item_node in root_node.children(&mut cursor) { let item_result = Item::from_syntax_node(item_node, self.source); match item_result { diff --git a/src/value/mod.rs b/src/value/mod.rs index c25a243..6db6f64 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -54,9 +54,31 @@ impl Value { let child = node.child(0).unwrap(); match child.kind() { - "integer" | "float" | "boolean" | "string" | "empty" => { - Ok(Value::from_syntax_node(child, source)?) + "integer" => { + let bytes = &source[child.byte_range()]; + let raw_value = bytes.parse::().unwrap(); + + Ok(Value::Integer(raw_value)) } + "float" => { + let bytes = &source[child.byte_range()]; + let raw_value = bytes.parse::().unwrap(); + + Ok(Value::Float(raw_value)) + } + "string" => { + let byte_range_without_quotes = child.start_byte() - 1..child.end_byte(); + let text = &source[byte_range_without_quotes]; + + Ok(Value::String(text.to_string())) + } + "boolean" => { + let bytes = &source[child.byte_range()]; + let raw_value = bytes.parse::().unwrap(); + + Ok(Value::Boolean(raw_value)) + } + "empty" => Ok(Value::Empty), "list" => { let item_count = child.named_child_count(); let mut values = Vec::with_capacity(item_count);