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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use super::printer::Printer;
use std::rc::Rc;
use std::cell::UnsafeCell;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::mem;

/** Print Items */

pub struct PrintItems {
    pub(super) first_node: Option<PrintItemPath>,
    last_node: Option<PrintItemPath>,
}

impl PrintItems {
    pub fn new() -> PrintItems {
        PrintItems {
            first_node: None,
            last_node: None,
        }
    }

    pub fn into_rc_path(self) -> Option<PrintItemPath> {
        self.first_node
    }

    pub fn push_item(&mut self, item: PrintItem) {
        self.push_item_internal(item);
    }

    // seems marginally faster to inline this? probably not worth it
    #[inline]
    fn push_item_internal(&mut self, item: PrintItem) {
        let node = Rc::new(PrintNodeCell::new(item));
        if let Some(first_node) = &self.first_node {
            let new_last_node = node.get_last_next().unwrap_or(node.clone());
            self.last_node.as_ref().unwrap_or(first_node).set_next(Some(node));
            self.last_node = Some(new_last_node);
        } else {
            self.last_node = node.get_last_next();
            self.first_node = Some(node);
        }
    }
}

impl PrintItems {
    pub fn extend(&mut self, items: PrintItems) {
        if let Some(first_node) = &self.first_node {
            self.last_node.as_ref().unwrap_or(first_node).set_next(items.first_node.clone());
            self.last_node = items.last_node.or(items.first_node.or(self.last_node.clone())); // todo: fix this
        } else {
            self.first_node = items.first_node;
            self.last_node = items.last_node;
        }
    }

    pub fn push_str(&mut self, item: &str) {
        self.push_item_internal(PrintItem::String(Rc::from(StringContainer::new(String::from(item)))));
    }

    pub fn push_condition(&mut self, condition: Condition) {
        self.push_item_internal(PrintItem::Condition(Rc::from(condition)));
    }

    pub fn push_info(&mut self, info: Info) {
        self.push_item_internal(PrintItem::Info(Rc::from(info)));
    }

    pub fn push_signal(&mut self, signal: Signal) {
        self.push_item_internal(PrintItem::Signal(signal));
    }

    pub fn push_path(&mut self, path: PrintItemPath) {
        self.push_item_internal(PrintItem::RcPath(path))
    }

    pub fn push_optional_path(&mut self, path: Option<PrintItemPath>) {
        if let Some(path) = path {
            self.push_path(path);
        }
    }

    pub fn is_empty(&self) -> bool {
        self.first_node.is_none()
    }

    // todo: clean this up
    #[cfg(debug_assertions)]
    pub fn get_as_text(&self) -> String {
        return if let Some(first_node) = &self.first_node {
            get_items_as_text(first_node.clone(), String::from(""))
        } else {
            String::new()
        };

        fn get_items_as_text(items: PrintItemPath, indent_text: String) -> String {
            let mut text = String::new();
            for item in PrintItemsIterator::new(items) {
                match item {
                    PrintItem::Signal(signal) => text.push_str(&get_line(format!("Signal::{:?}", signal), &indent_text)),
                    PrintItem::Info(info) => text.push_str(&get_line(format!("Info: {}", info.name), &indent_text)),
                    PrintItem::Condition(condition) => {
                        text.push_str(&get_line(format!("Condition: {}", condition.name), &indent_text));
                        if let Some(true_path) = &condition.true_path {
                            text.push_str(&get_line(String::from("  true:"), &indent_text));
                            text.push_str(&get_items_as_text(true_path.clone(), format!("{}    ", &indent_text)));
                        }
                        if let Some(false_path) = &condition.false_path {
                            text.push_str(&get_line(String::from("  false:"), &indent_text));
                            text.push_str(&get_items_as_text(false_path.clone(), format!("{}    ", &indent_text)));
                        }
                    },
                    PrintItem::String(str_text) => text.push_str(&get_line(format!("`{}`", str_text.text.to_string()), &indent_text)),
                    PrintItem::RcPath(path) => text.push_str(&get_items_as_text(path.clone(), indent_text.clone())),
                }
            }

            return text;

            fn get_line(text: String, indent_text: &String) -> String {
                format!("{}{}\n", indent_text, text)
            }
        }
    }

    pub fn iter(&self) -> PrintItemsIterator {
        PrintItemsIterator {
            node: self.first_node.clone(),
        }
    }
}

pub struct PrintItemsIterator {
    node: Option<PrintItemPath>,
}

impl PrintItemsIterator {
    pub fn new(path: PrintItemPath) -> PrintItemsIterator {
        PrintItemsIterator {
            node: Some(path),
        }
    }
}

impl Iterator for PrintItemsIterator {
    type Item = PrintItem;

    fn next(&mut self) -> Option<PrintItem> {
        let node = self.node.take();

        match node {
            Some(node) => {
                self.node = node.get_next();
                Some(node.get_item())
            },
            None => None
        }
    }
}

impl Into<PrintItems> for &str {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        items.push_str(self);
        items
    }
}

impl Into<PrintItems> for String {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        items.push_str(&self);
        items
    }
}

impl Into<PrintItems> for &String {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        items.push_str(self);
        items
    }
}

impl Into<PrintItems> for Condition {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        items.push_condition(self);
        items
    }
}

impl Into<PrintItems> for Signal {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        items.push_signal(self);
        items
    }
}

impl Into<PrintItems> for Option<PrintItemPath> {
    fn into(self) -> PrintItems {
        let mut items = PrintItems::new();
        if let Some(path) = self {
            items.push_path(path);
        }
        items
    }
}

/** Print Node */

pub struct PrintNode {
    pub(super) next: Option<PrintItemPath>,
    pub(super) item: PrintItem,
}

impl Drop for PrintNode {
    // Implement a custom drop in order to prevent a stack overflow error when dropping objects of this type
    fn drop(&mut self) {
        let mut next = mem::replace(&mut self.next, None);

        loop {
            next = match next {
                Some(node) => match Rc::try_unwrap(node) {
                    Ok(node) => node.take_next(),
                    Err(_) => break,
                },
                None => break
            }
        }
    }
}

impl PrintNode {
    fn new(item: PrintItem) -> PrintNode {
        PrintNode {
            item,
            next: None,
        }
    }

    fn set_next(&mut self, new_next: Option<PrintItemPath>) {
        let past_next = mem::replace(&mut self.next, new_next.clone());

        if let Some(past_next) = past_next {
            if let Some(new_next) = new_next {
                new_next.get_last_next().unwrap_or(new_next).set_next(Some(past_next));
            }
        }
    }
}

/// A fast implementation of RefCell<PrintNode> that avoids runtime checks on borrows.
pub struct PrintNodeCell {
    value: UnsafeCell<PrintNode>,
}

impl PrintNodeCell {
    pub(super) fn new(item: PrintItem) -> PrintNodeCell {
        PrintNodeCell {
            value: UnsafeCell::new(PrintNode::new(item))
        }
    }

    #[inline]
    pub(super) fn get_item(&self) -> PrintItem {
        unsafe {
            (*self.value.get()).item.clone()
        }
    }

    #[inline]
    pub(super) fn get_next(&self) -> Option<PrintItemPath> {
        unsafe {
            (*self.value.get()).next.clone()
        }
    }

    #[inline]
    pub(super) fn set_next(&self, new_next: Option<PrintItemPath>) {
        unsafe {
            (*self.value.get()).set_next(new_next);
        }
    }

    #[inline]
    pub(super) fn get_last_next(&self) -> Option<PrintItemPath> {
        let mut current = self.get_next();
        loop {
            if let Some(last) = &current {
                if let Some(next) = last.get_next() {
                    current.replace(next);
                    continue;
                }
            }
            break;
        }

        return current;
    }

    /// Gets the node unsafely. Be careful when using this and ensure no mutation is
    /// happening during the borrow.
    #[inline]
    pub(super) unsafe fn get_node(&self) -> *mut PrintNode {
        self.value.get()
    }

    #[inline]
    pub fn take_next(self) -> Option<PrintItemPath> {
        self.value.into_inner().next.take()
    }
}

pub type PrintItemPath = Rc<PrintNodeCell>;

/* Print item and kinds */

/// The different items the printer could encounter.
#[derive(Clone)]
pub enum PrintItem {
    String(Rc<StringContainer>),
    Condition(Rc<Condition>),
    Info(Rc<Info>),
    Signal(Signal),
    RcPath(PrintItemPath),
}

#[derive(Clone, PartialEq, Copy, Debug)]
pub enum Signal {
    /// Signal that a new line should occur based on the printer settings.
    NewLine,
    /// Signal that a tab should occur based on the printer settings.
    Tab,
    /// Signal that the current location could be a newline when
    /// exceeding the line width.
    PossibleNewLine,
    /// Signal that the current location should be a space, but
    /// could be a newline if exceeding the line width.
    SpaceOrNewLine,
    /// Expect the next character to be a newline. If it's not, force a newline.
    ExpectNewLine,
    /// Queue a start indent to be set after the next written item.
    QueueStartIndent,
    /// Signal the start of a section that should be indented.
    StartIndent,
    /// Signal the end of a section that should be indented.
    FinishIndent,
    /// Signal the start of a group of print items that have a lower precedence
    /// for being broken up with a newline for exceeding the line width.
    StartNewLineGroup,
    /// Signal the end of a newline group.
    FinishNewLineGroup,
    /// Signal that a single indent should occur based on the printer settings.
    SingleIndent,
    /// Signal to the printer that it should stop using indentation.
    StartIgnoringIndent,
    /// Signal to the printer that it should start using indentation again.
    FinishIgnoringIndent,
    /// Signal to the printer that it shouldn't print any new lines.
    StartForceNoNewLines,
    /// Signal to the printer that it should finish not printing any new lines.
    FinishForceNoNewLines,
    /// Signal that a space should occur if not trailing.
    SpaceIfNotTrailing,
}

/// Can be used to get information at a certain location being printed. These
/// can be resolved by providing the info object to a condition context's
/// get_resolved_info(&info) method.
#[derive(Clone, PartialEq, Copy, Debug)]
pub struct Info {
    /// Unique identifier.
    id: usize,
    /// Name for debugging purposes.
    #[cfg(debug_assertions)]
    name: &'static str,
}

static INFO_COUNTER: AtomicUsize = AtomicUsize::new(0);

impl Info {
    pub fn new(_name: &'static str) -> Info {
        Info {
            id: INFO_COUNTER.fetch_add(1, Ordering::SeqCst),
            #[cfg(debug_assertions)]
            name: _name
        }
    }

    #[inline]
    pub fn get_unique_id(&self) -> usize {
        self.id
    }

    #[inline]
    pub fn get_name(&self) -> &'static str {
        #[cfg(debug_assertions)]
        return self.name;
        #[cfg(not(debug_assertions))]
        return "";
    }
}

/// Conditionally print items based on a condition.
///
/// These conditions are extremely flexible and can even be resolved based on
/// information found later on in the file.
#[derive(Clone)]
pub struct Condition {
    /// Unique identifier.
    id: usize,
    /// Name for debugging purposes.
    #[cfg(debug_assertions)]
    name: &'static str,
    /// If a reference has been created for the condition via `get_reference()`. If so, the printer
    /// will store the condition and it will be retrievable via a condition resolver.
    pub(super) is_stored: bool,
    /// The condition to resolve.
    pub(super) condition: Rc<Box<ConditionResolver>>,
    /// The items to print when the condition is true.
    pub(super) true_path: Option<PrintItemPath>,
    /// The items to print when the condition is false or undefined (not yet resolved).
    pub(super) false_path: Option<PrintItemPath>,
    /// Any infos that should cause the re-evaluation of this condition.
    /// This is only done on request for performance reasons.
    pub(super) dependent_infos: Option<Vec<Info>>,
}

static CONDITION_COUNTER: AtomicUsize = AtomicUsize::new(0);

impl Condition {
    pub fn new(name: &'static str, properties: ConditionProperties) -> Condition {
        Condition::new_internal(name, properties, None)
    }

    pub fn new_true() -> Condition {
        Condition::new_internal("trueCondition", ConditionProperties {
            condition: Rc::new(Box::new(|_| Some(true))),
            true_path: None,
            false_path: None,
        }, None)
    }

    pub fn new_false() -> Condition {
        Condition::new_internal("falseCondition", ConditionProperties {
            condition: Rc::new(Box::new(|_| Some(false))),
            true_path: None,
            false_path: None,
        }, None)
    }

    pub fn new_with_dependent_infos(name: &'static str, properties: ConditionProperties, dependent_infos: Vec<Info>) -> Condition {
        Condition::new_internal(name, properties, Some(dependent_infos))
    }

    fn new_internal(_name: &'static str, properties: ConditionProperties, dependent_infos: Option<Vec<Info>>) -> Condition {
        Condition {
            id: CONDITION_COUNTER.fetch_add(1, Ordering::SeqCst),
            is_stored: dependent_infos.is_some(),
            #[cfg(debug_assertions)]
            name: _name,
            condition: properties.condition,
            true_path: properties.true_path.map(|x| x.first_node).flatten(),
            false_path: properties.false_path.map(|x| x.first_node).flatten(),
            dependent_infos,
        }
    }

    #[inline]
    pub fn get_unique_id(&self) -> usize {
        self.id
    }

    #[inline]
    pub fn get_name(&self) -> &'static str {
        #[cfg(debug_assertions)]
        return self.name;
        #[cfg(not(debug_assertions))]
        return "condition";
    }

    #[inline]
    pub fn get_true_path(&self) -> &Option<PrintItemPath> {
        &self.true_path
    }

    #[inline]
    pub fn get_false_path(&self) -> &Option<PrintItemPath> {
        &self.false_path
    }

    #[inline]
    pub(super) fn resolve(&self, context: &mut ConditionResolverContext) -> Option<bool> {
        (self.condition)(context)
    }

    pub fn get_reference(&mut self) -> ConditionReference {
        self.is_stored = true;
        ConditionReference::new(self.get_name(), self.id)
    }
}

#[derive(Clone, PartialEq, Copy, Debug)]
pub struct ConditionReference {
    #[cfg(debug_assertions)]
    pub(super) name: &'static str,
    pub(super) id: usize,
}

impl ConditionReference {
    pub(super) fn new(_name: &'static str, id: usize) -> ConditionReference {
        ConditionReference {
            #[cfg(debug_assertions)]
            name: _name,
            id,
        }
    }

    #[inline]
    pub(super) fn get_name(&self) -> &'static str {
        #[cfg(debug_assertions)]
        return self.name;
        #[cfg(not(debug_assertions))]
        return "conditionRef";
    }

    /// Creates a condition resolver that checks the value of the condition this references.
    pub fn create_resolver(&self) -> impl Fn(&mut ConditionResolverContext) -> Option<bool> + Clone + 'static {
        let captured_self = self.clone();
        move |condition_context: &mut ConditionResolverContext| {
            condition_context.get_resolved_condition(&captured_self)
        }
    }
}

/// Properties for the condition.
pub struct ConditionProperties {
    /// The condition to resolve.
    pub condition: Rc<Box<ConditionResolver>>,
    /// The items to print when the condition is true.
    pub true_path: Option<PrintItems>,
    /// The items to print when the condition is false or undefined (not yet resolved).
    pub false_path: Option<PrintItems>,
}

/// Function used to resolve a condition.
pub type ConditionResolver = dyn Fn(&mut ConditionResolverContext) -> Option<bool>;

/// Context used when resolving a condition.
pub struct ConditionResolverContext<'a> {
    printer: &'a mut Printer,
    /// Gets the writer info at the condition's location.
    pub writer_info: WriterInfo,
}

impl<'a> ConditionResolverContext<'a> {
    pub(super) fn new(printer: &'a mut Printer, writer_info: WriterInfo) -> Self {
        ConditionResolverContext {
            printer,
            writer_info,
        }
    }

    /// Gets if a condition was true, false, or returns undefined when not yet resolved.
    /// A condition reference can be retrieved by calling the `get_reference()` on a condition.
    pub fn get_resolved_condition(&mut self, condition_reference: &ConditionReference) -> Option<bool> {
        self.printer.get_resolved_condition(condition_reference)
    }

    /// Gets the writer info at a specified info or returns undefined when not yet resolved.
    pub fn get_resolved_info(&self, info: &Info) -> Option<&WriterInfo> {
        self.printer.get_resolved_info(info)
    }

    /// Clears the info result from being stored.
    pub fn clear_info(&mut self, info: &Info) {
        self.printer.clear_info(info)
    }

    /// Gets if the provided info has moved positions since the last check.
    /// Returns None when the info can't be resolved. Returns Some(false) the first time this is called.
    pub fn has_info_moved(&mut self, info: &Info) -> Option<bool> {
        self.printer.has_info_moved(info)
    }
}

/// A container that holds the string's value and character count.
#[derive(Clone)]
pub struct StringContainer {
    /// The string value.
    pub text: String,
    /// The cached character count.
    /// It is much faster to cache this than to recompute it all the time.
    pub(super) char_count: u32,
}

impl StringContainer {
    /// Creates a new string container.
    pub fn new(text: String) -> StringContainer {
        let char_count = text.chars().count() as u32;
        StringContainer {
            text,
            char_count
        }
    }
}

/// Information about a certain location being printed.
#[derive(Clone, Debug)]
pub struct WriterInfo {
    pub line_number: u32,
    pub column_number: u32,
    pub indent_level: u8,
    pub line_start_indent_level: u8,
    pub line_start_column_number: u32,
}

impl WriterInfo {
    /// Gets if the current column number equals the line start column number.
    pub fn is_start_of_line(&self) -> bool {
        self.column_number == self.line_start_column_number
    }

    /// Gets the line and column number.
    pub fn get_line_and_column(&self) -> (u32, u32) {
        (self.line_number, self.column_number)
    }
}