1
0

Add list support

This commit is contained in:
Jeff 2023-09-29 08:59:09 -04:00
parent 3182e7c860
commit b308c1852f
2 changed files with 27 additions and 13 deletions

View File

@ -263,8 +263,6 @@ struct Operation {
impl Operation { impl Operation {
fn new(node: Node, source: &str) -> Result<Self> { fn new(node: Node, source: &str) -> Result<Self> {
println!("{node:?}");
let first_child = node.child(0).unwrap(); let first_child = node.child(0).unwrap();
let second_child = node.child(1).unwrap(); let second_child = node.child(1).unwrap();
let third_child = node.child(2).unwrap(); let third_child = node.child(2).unwrap();

View File

@ -49,18 +49,15 @@ pub enum Value {
impl Value { impl Value {
pub fn new(node: Node, source: &str) -> Result<Self> { pub fn new(node: Node, source: &str) -> Result<Self> {
let child = node.child(0).unwrap(); let node = if node.kind() == "value" {
node.child(0).unwrap()
} else {
node
};
if node.kind() != "value" { let value_snippet = &source[node.byte_range()];
return Err(Error::UnexpectedSourceNode {
expected: "value",
actual: node.kind(),
});
}
let value_snippet = &source[child.byte_range()]; match node.kind() {
match child.kind() {
"integer" => { "integer" => {
let raw = value_snippet.parse::<i64>().unwrap_or_default(); let raw = value_snippet.parse::<i64>().unwrap_or_default();
@ -81,10 +78,29 @@ impl Value {
Ok(Value::Float(raw)) 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), "empty" => Ok(Value::Empty),
_ => Err(Error::UnexpectedSourceNode { _ => Err(Error::UnexpectedSourceNode {
expected: "integer, string, boolean or empty", expected: "integer, string, boolean or empty",
actual: child.kind(), actual: node.kind(),
}), }),
} }
} }