From b308c1852f76d21ca52c69620975590fd4871818 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 29 Sep 2023 08:59:09 -0400 Subject: [PATCH] Add list support --- src/interface.rs | 2 -- src/value/mod.rs | 38 +++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/interface.rs b/src/interface.rs index d68616f..fa4376c 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -263,8 +263,6 @@ struct Operation { impl Operation { fn new(node: Node, source: &str) -> Result { - println!("{node:?}"); - let first_child = node.child(0).unwrap(); let second_child = node.child(1).unwrap(); let third_child = node.child(2).unwrap(); diff --git a/src/value/mod.rs b/src/value/mod.rs index cdbe91e..5cb163f 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -49,18 +49,15 @@ pub enum Value { impl Value { pub fn new(node: Node, source: &str) -> Result { - let child = node.child(0).unwrap(); + let node = if node.kind() == "value" { + node.child(0).unwrap() + } else { + node + }; - if node.kind() != "value" { - return Err(Error::UnexpectedSourceNode { - expected: "value", - actual: node.kind(), - }); - } + let value_snippet = &source[node.byte_range()]; - let value_snippet = &source[child.byte_range()]; - - match child.kind() { + match node.kind() { "integer" => { let raw = value_snippet.parse::().unwrap_or_default(); @@ -81,10 +78,29 @@ impl Value { Ok(Value::Float(raw)) } + "list" => { + let child_count = node.child_count(); + let mut values = Vec::with_capacity(child_count); + + // Skip the first and last nodes because they are parentheses. + for index in 1..child_count - 1 { + let child = node.child(index).unwrap(); + + if child.kind() == "," { + continue; + } + + let value = Value::new(child, source)?; + + values.push(value) + } + + Ok(Value::List(values)) + } "empty" => Ok(Value::Empty), _ => Err(Error::UnexpectedSourceNode { expected: "integer, string, boolean or empty", - actual: child.kind(), + actual: node.kind(), }), } }