1use std::fmt::Pointer;
2use std::ops::Deref;
3use crate::parser::Parser;
4use crate::token::TokenType;
5
6pub enum NodeType {
7 Root,
8 ObjectExpression,
9 ObjectProperty,
10 ArrayExpression,
11 StringLiteral,
12 NumericLiteral,
13 BooleanLiteral,
14 NullLiteral,
15}
16
17impl NodeType {
18 pub fn to_string(&self) -> String {
19 match self {
20 NodeType::Root => "Root".to_string(),
21 NodeType::ObjectExpression => "ObjectExpression".to_string(),
22 NodeType::ObjectProperty => "ObjectProperty".to_string(),
23 NodeType::ArrayExpression => "ArrayExpression".to_string(),
24 NodeType::StringLiteral => "StringLiteral".to_string(),
25 NodeType::NumericLiteral => "NumericLiteral".to_string(),
26 NodeType::BooleanLiteral => "BooleanLiteral".to_string(),
27 NodeType::NullLiteral => "NullLiteral".to_string(),
28 }
29 }
30}
31
32pub enum NodeChild {
33 KV { key: Node, value: Node },
34 List(Vec<Node>),
35 Value(String),
36 Null,
37}
38
39pub struct Node {
40 pub node_type: NodeType,
41 pub start: usize,
42 pub end: usize,
43 pub children: Box<NodeChild>,
44}
45
46impl Node {
47 pub fn new(node_type: NodeType) -> Self {
48 Node {
49 node_type,
50 start: 0,
51 end: 0,
52 children: Box::new(NodeChild::Null),
53 }
54 }
55
56 pub fn create(node_type: NodeType, children: NodeChild, start: usize, end: usize) -> Self {
57 Node {
58 node_type,
59 start,
60 end,
61 children: Box::new(children),
62 }
63 }
64
65 pub fn get_children_as_value(&self) -> &String {
66 match self.children.deref() {
67 NodeChild::Value(x) => x,
68 _ => panic!("Invalid visit children of Node")
69 }
70 }
71
72 pub fn get_children_as_list(&self) -> &Vec<Node> {
73 match self.children.deref() {
74 NodeChild::List(x) => x,
75 _ => panic!("Invalid visit children of Node")
76 }
77 }
78
79 pub fn get_children_as_kv(&self) -> (&Node, &Node) {
80 match self.children.deref() {
81 NodeChild::KV { key, value } => (key, value),
82 _ => panic!("Invalid visit children of Node")
83 }
84 }
85
86 pub fn to_string(&self) -> String {
87 format!("Node:{} ({}, {})", self.node_type.to_string(), self.start, self.end)
88 }
89}
90
91impl Parser {
92 pub fn parse_node(&mut self) -> Node {
93 let current_token = &self.current_token;
94 let start_pos = current_token.start;
95 let value = current_token.value.to_string();
96
97 let mut node = match current_token.token_type {
98 TokenType::BracesStart => self.parse_object_expression(),
99 TokenType::BracketsStart => self.parse_array_expression(),
100 TokenType::String => {
101 let node = Node::create(
102 NodeType::StringLiteral,
103 NodeChild::Value(value),
104 current_token.start,
105 current_token.end,
106 );
107 self.move_next();
108 node
109 }
110 TokenType::Number => {
111 let node = Node::create(
112 NodeType::NumericLiteral,
113 NodeChild::Value(value),
114 current_token.start,
115 current_token.end,
116 );
117 self.move_next();
118 node
119 }
120 TokenType::Word => {
121 if value == "null" {
122 let node = Node::create(
123 NodeType::NullLiteral,
124 NodeChild::Value(value),
125 current_token.start,
126 current_token.end,
127 );
128 self.move_next();
129 node
130 } else if value == "true" || value == "false" {
131 let node = Node::create(
132 NodeType::BooleanLiteral,
133 NodeChild::Value(value),
134 current_token.start,
135 current_token.end,
136 );
137 self.move_next();
138 node
139 } else {
140 self.unexpected_token(current_token);
141 }
142 }
143 _ => self.unexpected_token(current_token)
144 };
145
146 node.start = start_pos;
147 node.end = self.last_token.end;
148 node
149 }
150}