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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
extern crate pest;
use pest_derive::Parser;
use pest::Parser;
use std::{env, fs};
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use pest::iterators::{Pair, Pairs};
use serde_json;
use serde_derive::{Serialize, Deserialize};
use uuid::Uuid;
pub mod message;
pub use message::*;
pub mod templating;
pub use templating::*;
pub use lazy_static::lazy_static;
#[derive(Parser)]
#[grammar = "pax.pest"]
pub struct PaxParser;
pub trait PathQualifiable {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
let fully_qualified_path = module_path!().to_owned() + "::" + atomic_self_type;
fully_qualified_path
}
}
impl PathQualifiable for usize {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"usize".to_string()
}
}
impl PathQualifiable for f64 {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"f64".to_string()
}
}
impl PathQualifiable for i64 {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"i64".to_string()
}
}
impl PathQualifiable for std::string::String {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"std::string::String".to_string()
}
}
impl<T> PathQualifiable for std::rc::Rc<T> {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"std::rc::Rc".to_string()
}
}
impl<T> PathQualifiable for std::vec::Vec<T> {
fn get_fully_qualified_path(atomic_self_type: &str) -> String {
"std::vec::Vec".to_string()
}
}
static PRELUDE_TYPES: [&'static str; 5] = [
"std::rc::Rc",
"std::vec::Vec",
"usize",
"i64",
"u64",
];
pub fn get_fully_qualified_prelude_type(identifier: &str) -> String {
(PRELUDE_TYPES.iter().find(|pt|{
pt.ends_with(identifier)
}).expect("`get_fully_resolved_prelude_type` called on a non-prelude type")).to_string()
}
pub fn is_prelude_type(identifier: &str) -> bool {
for x in PRELUDE_TYPES.iter() {
if x.ends_with(identifier) {
return true;
}
}
false
}
pub enum PaxContents {
FilePath(String),
Inline(String),
}
pub fn get_primitive_definition(pascal_identifier: &str, module_path: &str, source_id: &str, property_definitions: &Vec<PropertyDefinition>) -> ComponentDefinition {
let modified_module_path = if module_path.starts_with("parser") {
module_path.replacen("parser", "crate", 1)
} else {
module_path.to_string()
};
ComponentDefinition {
source_id: source_id.to_string(),
pascal_identifier: pascal_identifier.to_string(),
template: None,
settings: None,
root_template_node_id: None,
module_path: modified_module_path,
property_definitions: property_definitions.to_vec(),
}
}
pub fn parse_pascal_identifiers_from_component_definition_string(pax: &str) -> Vec<String> {
let pax_component_definition = PaxParser::parse(Rule::pax_component_definition, pax)
.expect(&format!("unsuccessful parse from {}", &pax)) .next().unwrap(); let pascal_identifiers = Rc::new(RefCell::new(HashSet::new()));
pax_component_definition.into_inner().for_each(|pair|{
match pair.as_rule() {
Rule::root_tag_pair => {
recurse_visit_tag_pairs_for_pascal_identifiers(
pair.into_inner().next().unwrap(),
Rc::clone(&pascal_identifiers),
);
}
_ => {}
}
});
let unwrapped_hashmap = Rc::try_unwrap(pascal_identifiers).unwrap().into_inner();
unwrapped_hashmap.into_iter().collect()
}
fn recurse_visit_tag_pairs_for_pascal_identifiers(any_tag_pair: Pair<Rule>, pascal_identifiers: Rc<RefCell<HashSet<String>>>) {
match any_tag_pair.as_rule() {
Rule::matched_tag => {
let matched_tag = any_tag_pair;
let open_tag = matched_tag.clone().into_inner().next().unwrap();
let pascal_identifier = open_tag.into_inner().next().unwrap().as_str();
pascal_identifiers.borrow_mut().insert(pascal_identifier.to_string());
let prospective_inner_nodes = matched_tag.clone().into_inner().nth(1).unwrap();
match prospective_inner_nodes.as_rule() {
Rule::inner_nodes => {
let inner_nodes = prospective_inner_nodes;
inner_nodes.into_inner()
.for_each(|sub_tag_pair|{
match sub_tag_pair.as_rule() {
Rule::matched_tag | Rule::self_closing_tag | Rule::statement_control_flow => {
recurse_visit_tag_pairs_for_pascal_identifiers(sub_tag_pair, Rc::clone(&pascal_identifiers));
},
Rule::node_inner_content => {
}
_ => {unreachable!("Parsing error 88779273: {:?}", sub_tag_pair.as_rule());},
}
}
)
},
_ => {unreachable!("Parsing error 45834823: {:?}", matched_tag.clone().into_inner())}
}
},
Rule::self_closing_tag => {
let pascal_identifier = any_tag_pair.into_inner().next().unwrap().as_str();
pascal_identifiers.borrow_mut().insert(pascal_identifier.to_string());
},
Rule::statement_control_flow => {
let matched_tag = any_tag_pair.into_inner().next().unwrap();
let mut n = 1;
match matched_tag.as_rule() {
Rule::statement_if => {
n = 2;
},
Rule::statement_for => {
n = 2;
},
Rule::statement_slot => {
n = 0;
},
_ => {
unreachable!("Parsing error 944491032: {:?}", matched_tag.as_rule());
}
};
let prospective_inner_nodes = matched_tag.into_inner().nth(n).expect("WRONG nth");
match prospective_inner_nodes.as_rule() {
Rule::inner_nodes => {
let inner_nodes = prospective_inner_nodes;
inner_nodes.into_inner()
.for_each(|sub_tag_pair|{
recurse_visit_tag_pairs_for_pascal_identifiers(sub_tag_pair, Rc::clone(&pascal_identifiers));
})
},
Rule::expression_body => {
},
_ => {unreachable!("Parsing error 4449292922: {:?}", prospective_inner_nodes.as_rule());}
}
},
_ => {unreachable!("Parsing error 123123121: {:?}", any_tag_pair.as_rule());}
}
}
fn parse_template_from_component_definition_string(ctx: &mut TemplateParseContext, pax: &str) {
let pax_component_definition = PaxParser::parse(Rule::pax_component_definition, pax)
.expect(&format!("unsuccessful parse from {}", &pax)) .next().unwrap(); pax_component_definition.into_inner().for_each(|pair|{
match pair.as_rule() {
Rule::root_tag_pair => {
ctx.children_id_tracking_stack.push(vec![]);
recurse_visit_tag_pairs_for_template(
ctx,
pair.into_inner().next().unwrap(),
);
}
_ => {}
}
});
}
struct TemplateParseContext {
pub template_node_definitions: Vec<TemplateNodeDefinition>,
pub is_root: bool,
pub pascal_identifier_to_component_id_map: HashMap<String, String>,
pub root_template_node_id: Option<String>,
pub children_id_tracking_stack: Vec<Vec<String>>,
}
fn recurse_visit_tag_pairs_for_template(ctx: &mut TemplateParseContext, any_tag_pair: Pair<Rule>) {
match any_tag_pair.as_rule() {
Rule::matched_tag => {
let matched_tag = any_tag_pair;
let mut open_tag = matched_tag.clone().into_inner().next().unwrap().into_inner();
let pascal_identifier = open_tag.next().unwrap().as_str();
let new_id = create_uuid();
if ctx.is_root {
ctx.root_template_node_id = Some(new_id.clone());
}
ctx.is_root = false;
let mut parents_children_id_list = ctx.children_id_tracking_stack.pop().unwrap();
parents_children_id_list.push(new_id.clone());
ctx.children_id_tracking_stack.push(parents_children_id_list);
ctx.children_id_tracking_stack.push(vec![]);
let prospective_inner_nodes = matched_tag.into_inner().nth(1).unwrap();
match prospective_inner_nodes.as_rule() {
Rule::inner_nodes => {
let inner_nodes = prospective_inner_nodes;
inner_nodes.into_inner()
.for_each(|sub_tag_pair|{
recurse_visit_tag_pairs_for_template(ctx, sub_tag_pair);
})
},
_ => {panic!("wrong prospective inner nodes (or nth)")}
}
let template_node = TemplateNodeDefinition {
id: new_id,
component_id: ctx.pascal_identifier_to_component_id_map.get(pascal_identifier).expect(&format!("Template key not found {}", &pascal_identifier)).to_string(),
inline_attributes: parse_inline_attribute_from_final_pairs_of_tag(open_tag),
children_ids: ctx.children_id_tracking_stack.pop().unwrap(),
};
ctx.template_node_definitions.push(template_node);
},
Rule::self_closing_tag => {
let mut tag_pairs = any_tag_pair.into_inner();
let pascal_identifier = tag_pairs.next().unwrap().as_str();
let new_id = create_uuid();
if ctx.is_root {
ctx.root_template_node_id = Some(new_id.clone());
}
ctx.is_root = false;
let mut parents_children_id_list = ctx.children_id_tracking_stack.pop().unwrap();
parents_children_id_list.push(new_id.clone());
ctx.children_id_tracking_stack.push(parents_children_id_list);
let template_node = TemplateNodeDefinition {
id: new_id,
component_id: ctx.pascal_identifier_to_component_id_map.get(pascal_identifier).expect(&format!("Template key not found {}", &pascal_identifier)).to_string(),
inline_attributes: parse_inline_attribute_from_final_pairs_of_tag(tag_pairs),
children_ids: vec![]
};
ctx.template_node_definitions.push(template_node);
},
Rule::statement_control_flow => {
let matched_tag = any_tag_pair.into_inner().next().unwrap();
let new_id = create_uuid();
let mut parents_children_id_list = ctx.children_id_tracking_stack.pop().unwrap();
parents_children_id_list.push(new_id.clone());
ctx.children_id_tracking_stack.push(parents_children_id_list);
ctx.children_id_tracking_stack.push(vec![]);
let template_node = match matched_tag.as_rule() {
Rule::statement_if => {
TemplateNodeDefinition {
id: new_id,
component_id: "Conditional".to_string(),
inline_attributes: None,children_ids: ctx.children_id_tracking_stack.pop().unwrap(),
}
},
Rule::statement_for => {
TemplateNodeDefinition {
id: new_id,
component_id: "Repeat".to_string(),
inline_attributes: None,children_ids: ctx.children_id_tracking_stack.pop().unwrap(),
}
},
Rule::statement_slot => {
TemplateNodeDefinition {
id: new_id,
component_id: "Slot".to_string(),
inline_attributes: None,children_ids: ctx.children_id_tracking_stack.pop().unwrap(),
}
},
_ => {
unreachable!("Parsing error 883427242: {:?}", matched_tag.as_rule());;
}
};
ctx.template_node_definitions.push(template_node);
},
Rule::node_inner_content => {
},
_ => {unreachable!("Parsing error 2232444421: {:?}", any_tag_pair.as_rule());}
}
}
fn parse_inline_attribute_from_final_pairs_of_tag ( final_pairs_of_tag: Pairs<Rule>) -> Option<Vec<(String, AttributeValueDefinition)>> {
let vec : Vec<(String, AttributeValueDefinition)> = final_pairs_of_tag.map(|attribute_key_value_pair|{
match attribute_key_value_pair.clone().into_inner().next().unwrap().as_rule() {
Rule::attribute_event_binding => {
let mut kv = attribute_key_value_pair.into_inner();
let mut attribute_event_binding = kv.next().unwrap().into_inner();
let event_id = attribute_event_binding.next().unwrap().as_str().to_string();
let symbolic_binding = attribute_event_binding.next().unwrap().as_str().to_string();
(event_id, AttributeValueDefinition::EventBindingTarget(symbolic_binding))
},
_ => { let mut kv = attribute_key_value_pair.into_inner();
let key = kv.next().unwrap().as_str().to_string();
let mut raw_value = kv.next().unwrap().into_inner().next().unwrap();
let value = match raw_value.as_rule() {
Rule::literal_value => {AttributeValueDefinition::LiteralValue(raw_value.as_str().to_string())},
Rule::expression => {AttributeValueDefinition::Expression(raw_value.as_str().to_string())},
Rule::identifier => {AttributeValueDefinition::Identifier(raw_value.as_str().to_string())},
_ => {unreachable!("Parsing error 3342638857230: {:?}", raw_value.as_rule());}
};
(key, value)
}
}
}).collect();
if vec.len() > 0 {
Some(vec)
}else{
None
}
}
fn handle_number_string(num_string: &str) -> Number {
if num_string.contains(".") {
Number::Float(num_string.parse::<f64>().unwrap())
} else {
Number::Int(num_string.parse::<isize>().unwrap())
}
}
fn handle_unit_string(unit_string: &str) -> Unit {
match unit_string {
"px" => Unit::Pixels,
"%" => Unit::Percent,
_ => {unimplemented!("Only px or % are currently supported as units")}
}
}
fn derive_literal_value_from_pair(literal_value_pair: Pair<Rule>) -> SettingsLiteralValue {
let inner_literal = literal_value_pair.into_inner().next().unwrap();
match inner_literal.as_rule() {
Rule::literal_number_with_unit => {
let mut tokens = inner_literal.into_inner();
let num_str = tokens.next().unwrap().as_str();
let unit_str = tokens.next().unwrap().as_str();
SettingsLiteralValue::LiteralNumberWithUnit(handle_number_string(num_str), handle_unit_string(unit_str))
},
Rule::literal_number => {
let num_str = inner_literal.as_str();
SettingsLiteralValue::LiteralNumber(handle_number_string(num_str))
},
Rule::literal_tuple => {
unimplemented!("literal tuples aren't supported but should be. fix me!")
},
Rule::string => {
SettingsLiteralValue::String(inner_literal.as_str().to_string())
},
_ => {unimplemented!()}
}
}
fn derive_settings_value_definition_from_literal_object_pair(mut literal_object: Pair<Rule>) -> SettingsLiteralBlockDefinition {
let mut literal_object_pairs = literal_object.into_inner();
let explicit_type_pascal_identifier = match literal_object_pairs.peek().unwrap().as_rule() {
Rule::pascal_identifier => {
Some(literal_object_pairs.next().unwrap().as_str().to_string())
},
_ => { None }
};
SettingsLiteralBlockDefinition {
explicit_type_pascal_identifier,
settings_key_value_pairs: literal_object_pairs.map(|settings_key_value_pair| {
let mut pairs = settings_key_value_pair.into_inner();
let setting_key = pairs.next().unwrap().as_str().to_string();
let raw_value = pairs.next().unwrap().into_inner().next().unwrap();
let setting_value = match raw_value.as_rule() {
Rule::literal_value => { SettingsValueDefinition::Literal(derive_literal_value_from_pair(raw_value))},
Rule::literal_object => { SettingsValueDefinition::Block(
derive_settings_value_definition_from_literal_object_pair(raw_value)
)},
Rule::expression => { SettingsValueDefinition::Expression(raw_value.as_str().to_string())},
_ => {unreachable!("Parsing error 231453468: {:?}", raw_value.as_rule());}
};
(setting_key, setting_value)
}).collect()
}
}
fn parse_settings_from_component_definition_string(pax: &str) -> Option<Vec<SettingsSelectorBlockDefinition>> {
let pax_component_definition = PaxParser::parse(Rule::pax_component_definition, pax)
.expect(&format!("unsuccessful parse from {}", &pax)) .next().unwrap(); let mut ret : Vec<SettingsSelectorBlockDefinition> = vec![];
pax_component_definition.into_inner().for_each(|top_level_pair|{
match top_level_pair.as_rule() {
Rule::settings_block_declaration => {
let mut selector_block_definitions: Vec<SettingsSelectorBlockDefinition> = top_level_pair.into_inner().map(|selector_block| {
let mut selector_block_pairs = selector_block.into_inner();
let selector = selector_block_pairs.next().unwrap().to_string();
let mut literal_object = selector_block_pairs.next().unwrap();
SettingsSelectorBlockDefinition {
selector,
value_block: derive_settings_value_definition_from_literal_object_pair(literal_object),
}
}).collect();
ret.extend(selector_block_definitions);
}
_ => {}
}
});
Some(ret)
}
pub fn create_uuid() -> String {
Uuid::new_v4().to_string()
}
pub struct ManifestContext {
pub visited_source_ids: HashSet<String>,
pub root_component_id: String,
pub component_definitions: Vec<ComponentDefinition>,
pub template_map: HashMap<String, String>,
pub all_property_definitions: HashMap<String, Vec<PropertyDefinition>>,
}
impl Default for ManifestContext {
fn default() -> Self {
Self {
root_component_id: "".into(),
visited_source_ids: HashSet::new(),
component_definitions: vec![],
template_map: HashMap::new(),
all_property_definitions: HashMap::new(),
}
}
}
pub fn parse_full_component_definition_string(mut ctx: ManifestContext, pax: &str, pascal_identifier: &str, is_root: bool, template_map: HashMap<String, String>, source_id: &str, module_path: &str) -> (ManifestContext, ComponentDefinition) {
let ast = PaxParser::parse(Rule::pax_component_definition, pax)
.expect(&format!("unsuccessful parse from {}", &pax)) .next().unwrap(); if is_root {
ctx.root_component_id = source_id.to_string();
}
let mut tpc = TemplateParseContext {
pascal_identifier_to_component_id_map: template_map,
template_node_definitions: vec![],
root_template_node_id: None,
is_root,
children_id_tracking_stack: vec![],
};
parse_template_from_component_definition_string(&mut tpc, pax);
let modified_module_path = if module_path.starts_with("parser") {
module_path.replacen("parser", "crate", 1)
} else {
module_path.to_string()
};
let property_definitions = ctx.all_property_definitions.get(source_id).unwrap().clone();
let mut new_def = ComponentDefinition {
source_id: source_id.into(),
pascal_identifier: pascal_identifier.to_string(),
template: Some(tpc.template_node_definitions),
settings: parse_settings_from_component_definition_string(pax),
module_path: modified_module_path,
root_template_node_id: tpc.root_template_node_id,
property_definitions,
};
(ctx, new_def)
}