1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::{collections::BTreeMap, ops::Range};

use serde::{Deserialize, Serialize};
use tree_sitter::Node;

use crate::{
    AbstractTree, Error, Expression, Function, Identifier, Item, Result, Table, Value, ValueType,
    VariableMap,
};

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct ValueNode {
    value_type: ValueType,
    start_byte: usize,
    end_byte: usize,
}

impl ValueNode {
    pub fn new(value_type: ValueType, start_byte: usize, end_byte: usize) -> Self {
        Self {
            value_type,
            start_byte,
            end_byte,
        }
    }

    pub fn byte_range(&self) -> Range<usize> {
        self.start_byte..self.end_byte
    }
}

impl AbstractTree for ValueNode {
    fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
        debug_assert_eq!("value", node.kind());

        let child = node.child(0).unwrap();
        let value_type = match child.kind() {
            "integer" => ValueType::Integer,
            "float" => ValueType::Float,
            "string" => ValueType::String,
            "boolean" => ValueType::Boolean,
            "empty" => ValueType::Empty,
            "list" => {
                let mut child_nodes = Vec::new();

                for index in 1..child.child_count() - 1 {
                    let child_syntax_node = child.child(index).unwrap();

                    if child_syntax_node.is_named() {
                        let expression = Expression::from_syntax_node(source, child_syntax_node)?;
                        child_nodes.push(expression);
                    }
                }

                ValueType::ListExact(child_nodes)
            }
            "table" => {
                let child_count = child.child_count();
                let mut column_names = Vec::new();

                let expression_node = child.child(child_count - 1).unwrap();
                let expression = Expression::from_syntax_node(source, expression_node)?;

                for index in 2..child.child_count() - 2 {
                    let node = child.child(index).unwrap();

                    if node.is_named() {
                        let identifier = Identifier::from_syntax_node(source, node)?;

                        column_names.push(identifier)
                    }
                }

                ValueType::Table {
                    column_names,
                    rows: Box::new(expression),
                }
            }
            "map" => {
                let mut child_nodes = BTreeMap::new();
                let mut current_key = "".to_string();

                for index in 0..child.child_count() - 1 {
                    let child_syntax_node = child.child(index).unwrap();

                    if child_syntax_node.kind() == "identifier" {
                        current_key =
                            Identifier::from_syntax_node(source, child_syntax_node)?.take_inner();
                    }

                    if child_syntax_node.kind() == "expression" {
                        let key = current_key.clone();
                        let expression = Expression::from_syntax_node(source, child_syntax_node)?;

                        child_nodes.insert(key, expression);
                    }
                }

                ValueType::Map(child_nodes)
            }
            "function" => {
                let mut identifiers = Vec::new();

                let item_node = child.child(child.child_count() - 2).unwrap();
                let item = Item::from_syntax_node(source, item_node)?;

                for index in 1..child.child_count() - 3 {
                    let child_node = child.child(index).unwrap();

                    if child_node.kind() == "identifier" {
                        let identifier = Identifier::from_syntax_node(source, child_node)?;

                        identifiers.push(identifier);
                    }
                }

                let function = Function::new(identifiers, item);

                ValueType::Function(function)
            }
            _ => {
                return Err(Error::UnexpectedSyntaxNode {
                    expected:
                        "string, integer, float, boolean, list, table, map, function or empty",
                    actual: child.kind(),
                    location: child.start_position(),
                    relevant_source: source[child.byte_range()].to_string(),
                })
            }
        };

        Ok(ValueNode {
            value_type,
            start_byte: child.start_byte(),
            end_byte: child.end_byte(),
        })
    }

    fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
        let value_source = &source[self.byte_range()];
        let value = match &self.value_type {
            ValueType::Any => todo!(),
            ValueType::String => {
                let without_quotes = &value_source[1..value_source.len() - 1];

                Value::String(without_quotes.to_string())
            }
            ValueType::Float => Value::Float(value_source.parse().unwrap()),
            ValueType::Integer => Value::Integer(value_source.parse().unwrap()),
            ValueType::Boolean => Value::Boolean(value_source.parse().unwrap()),
            ValueType::ListExact(nodes) => {
                let mut values = Vec::with_capacity(nodes.len());

                for node in nodes {
                    let value = node.run(source, context)?;

                    values.push(value);
                }

                Value::List(values)
            }
            ValueType::Empty => Value::Empty,
            ValueType::Map(nodes) => {
                let mut values = VariableMap::new();

                for (key, node) in nodes {
                    let value = node.run(source, context)?;

                    values.set_value(key.clone(), value)?;
                }

                Value::Map(values)
            }
            ValueType::Table {
                column_names,
                rows: row_expression,
            } => {
                let mut headers = Vec::with_capacity(column_names.len());
                let mut rows = Vec::new();

                for identifier in column_names {
                    let name = identifier.inner().clone();

                    headers.push(name)
                }

                let _row_values = row_expression.run(source, context)?;
                let row_values = _row_values.as_list()?;

                for value in row_values {
                    let row = value.as_list()?.clone();

                    rows.push(row)
                }

                let table = Table::from_raw_parts(headers, rows);

                Value::Table(table)
            }
            ValueType::Function(function) => Value::Function(function.clone()),
        };

        Ok(value)
    }
}