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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#![allow(unused)]
use std::fs;
use regex::Regex;
#[derive(Debug)]
pub enum OkmlType {
String(String),
Boolean(bool),
Integer(i64),
Float(f64),
SubList(Vec<Okml>),
Null,
}
impl OkmlType {
pub fn unwrap_string(&self) -> &String {
match self {
OkmlType::String(s) => s,
_ => panic!("called `unwrap_string` on a non-String value"),
}
}
pub fn unwrap_bool(&self) -> bool {
match self {
OkmlType::Boolean(b) => *b,
_ => panic!("called `unwrap_bool` on a non-Boolean value"),
}
}
pub fn unwrap_integer(&self) -> i64 {
match self {
OkmlType::Integer(i) => *i,
_ => panic!("called `unwrap_integer` on a non-Integer value"),
}
}
pub fn unwrap_float(&self) -> f64 {
match self {
OkmlType::Float(f) => *f,
_ => panic!("called `unwrap_float` on a non-Float value"),
}
}
pub fn unwrap_sublist(&self) -> &Vec<Okml> {
match self {
OkmlType::SubList(v) => v,
_ => panic!("called `unwrap_sublist` on a non-SubList value"),
}
}
pub fn is_null(&self) -> bool {
matches!(self, OkmlType::Null)
}
}
#[derive(Debug)]
pub struct Okml {
key: String,
value: OkmlType,
}
impl Okml {
pub fn read_from_file(file_path: &str) -> Vec<Okml>{
let extension = &file_path[file_path.len()-4..file_path.len()];
if extension != "okml" {
println!("File is not in .okml {extension}");
std::process::exit(-1);
}
println!("Okml File Loaded: {}", file_path);
let mut content = fs::read_to_string(file_path)
.expect("Should've read the file");
let mut processed_content = pre_process(content.clone());
let re = Regex::new(r"[ \t\n]+").unwrap();
let tokens: Vec<&str> = re.split(&processed_content).filter(|s| !s.is_empty()).collect();
let (parsed_ast, _) = parse(tokens, 0);
parsed_ast
}
pub fn read_from_string(content: &str) -> Vec<Okml> {
let mut processed_content = pre_process(content.to_string());
let re = Regex::new(r"[ \t\n]+").unwrap();
let tokens: Vec<&str> = re.split(&processed_content).filter(|s| !s.is_empty()).collect();
let (parsed_ast, _) = parse(tokens, 0);
parsed_ast
}
pub fn key(&self) -> &str{
let key = &self.key;
return &key[..key.len()-1]
}
pub fn value(&self) -> &OkmlType {
return &self.value
}
}
fn pre_process(mut content: String) -> String{
let mut chars: Vec<char> = content.chars().collect();
let mut result = Vec::with_capacity(chars.len());
let mut next_char: Option<&char>;
for (index, ¤t_char) in chars.iter().enumerate() {
next_char = chars.get(index+1);
if current_char == ' ' && next_char == Some(&':') {
continue;
}
if current_char == ':'
&& next_char != Some(&' ')
&& chars.get(index.wrapping_sub(1)) != Some(&'\\')
{
result.push(current_char);
result.push(' ');
} else if current_char == '{' && chars.get(index-1) != Some(&' '){
result.push(' ');
result.push(current_char);
} else if current_char == '`' && (next_char != Some(&'`')) {
result.push(current_char);
result.push('\n');
} else if current_char == '`' && (chars.get(index-1) != Some(&'\n')) {
// result.push('\n');
result.push(current_char);
}
else {
result.push(current_char);
}
}
chars = result;
let mut rc: String = chars.into_iter().collect();
return rc;
}
fn parse(tokens: Vec<&str>, index: usize) -> (Vec<Okml>, usize) {
let token_len = tokens.len();
let mut parent: Vec<Okml> = Default::default();
let count_braces: u64 = 0;
let mut index = 0;
while index < token_len-1 {
let current = tokens.get(index).unwrap();
let mut value = tokens.get(index+1).unwrap();
// parse (sub_list_key { )
if value == &"{" {
let mut child: Vec<Okml> = Default::default();
let mut new_index = 0;
(child, new_index) = parse_list(tokens.clone(), index);
let mut child_to_push: Okml = Okml {
key: current.to_string(),
value: OkmlType::SubList(child)
};
parent.push(child_to_push);
index = new_index-1;
continue;
}
// parse (key: value)
if let Some(':') = current.chars().last() {
if value == &"```" {
let mut _current: &str;
let mut result: String = Default::default();
while index < token_len{
index = index+1;
_current = tokens.get(index+1).unwrap();
if _current == "```" {
break;
}
result.push_str(_current);
result.push_str(" ");
}
parent.push(Okml{
key: current.to_string(),
value: OkmlType::String(result.clone())
});
} else {
let mut return_value: OkmlType;
match value {
&"true" | &"yes" => return_value = OkmlType::Boolean(true),
&"false" | &"no" => return_value = OkmlType::Boolean(false),
&"null" => return_value = OkmlType::Null,
&&_ => {
if let Ok(parsed) = value.parse::<i64>() {
return_value = OkmlType::Integer(parsed);
} else if let Ok(parsed) = value.parse::<f64>() {
return_value = OkmlType::Float(parsed);
} else {
return_value = OkmlType::String(value.to_string());
}
}
}
// match value
parent.push(Okml{
key: current.to_string(),
value: (return_value)
});
}
}
index +=1
}
return (parent, index)
}
fn parse_list(tokens: Vec<&str>, mut index: usize) -> (Vec<Okml>, usize) {
let mut parent: Vec<Okml> = Default::default();
index = index +2;
let mut current = tokens.get(index).unwrap();
if current == &"}" {
return (parent, index)
}
let mut value = tokens.get(index+1).unwrap();
while current != &"}" {
current = tokens.get(index).unwrap();
value = tokens.get(index+1).unwrap();
if current == &"}" || value == &"}" {
break;
}
if value == &"{" {
let mut new_index: usize = 0;
let mut child: Vec<Okml> = Default::default();
(child, new_index) = parse_list(tokens.clone(), index);
parent.push(Okml {
key: current.to_string(),
value: OkmlType::SubList(child)
});
index = new_index-1;
} else {
if value == &"```" {
let mut _current: &str;
let mut result: String = Default::default();
while index < tokens.len(){
index = index+1;
_current = tokens.get(index+1).unwrap();
if _current == "```" {
break;
}
result.push_str(_current);
result.push_str(" ");
}
parent.push(Okml{
key: current.to_string(),
value: OkmlType::String(result.clone())
});
} else {
let mut return_value: OkmlType;
match value {
&"true" | &"yes" => return_value = OkmlType::Boolean(true),
&"false" | &"no" => return_value = OkmlType::Boolean(false),
&"null" => return_value = OkmlType::Null,
&&_ => {
if let Ok(parsed) = value.parse::<i64>() {
return_value = OkmlType::Integer(parsed);
} else if let Ok(parsed) = value.parse::<f64>() {
return_value = OkmlType::Float(parsed);
} else {
return_value = OkmlType::String(value.to_string());
}
}
}
// match value
parent.push(Okml{
key: current.to_string(),
value: (return_value)
});
}
}
index = index+2;
}
(parent, index)
}
|