summaryrefslogtreecommitdiff
path: root/src/syntax/tree.rs
blob: 41a03faebef97a706981daf47f4e8d4d68100100 (plain) (blame)
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
//! The syntax tree.

use std::any::Any;
use std::fmt::Debug;

use crate::layout::Layout;
use super::span::SpanVec;

/// A list of nodes which forms a tree together with the nodes' children.
pub type SyntaxTree = SpanVec<SyntaxNode>;

/// A syntax node, which encompasses a single logical entity of parsed source
/// code.
#[derive(Debug, Clone)]
pub enum SyntaxNode {
    /// Whitespace containing less than two newlines.
    Space,
    /// Whitespace with more than two newlines.
    Parbreak,
    /// A forced line break.
    Linebreak,
    /// Plain text.
    Text(String),
    /// Lines of raw text.
    Raw(Vec<String>),
    /// Italics were enabled / disabled.
    ToggleItalic,
    /// Bolder was enabled / disabled.
    ToggleBolder,
    /// A subtree, typically a function invocation.
    Dyn(Box<dyn DynamicNode>),
}

impl PartialEq for SyntaxNode {
    fn eq(&self, other: &SyntaxNode) -> bool {
        use SyntaxNode::*;
        match (self, other) {
            (Space, Space) => true,
            (Parbreak, Parbreak) => true,
            (Linebreak, Linebreak) => true,
            (Text(a), Text(b)) => a == b,
            (Raw(a), Raw(b)) => a == b,
            (ToggleItalic, ToggleItalic) => true,
            (ToggleBolder, ToggleBolder) => true,
            (Dyn(a), Dyn(b)) => a == b,
            _ => false,
        }
    }
}

/// Dynamic syntax nodes.
///
/// *Note*: This is automatically implemented for all types which are
/// `Debug + Clone + PartialEq`, `Layout` and `'static`.
pub trait DynamicNode: Debug + Layout {
    /// Convert into a `dyn Any`.
    fn as_any(&self) -> &dyn Any;

    /// Check for equality with another dynamic node.
    fn dyn_eq(&self, other: &dyn DynamicNode) -> bool;

    /// Clone into a boxed node trait object.
    fn box_clone(&self) -> Box<dyn DynamicNode>;
}

impl dyn DynamicNode {
    /// Downcast this dynamic node to a concrete node.
    pub fn downcast<N>(&self) -> Option<&N> where N: DynamicNode + 'static {
        self.as_any().downcast_ref::<N>()
    }
}

impl PartialEq for dyn DynamicNode {
    fn eq(&self, other: &dyn DynamicNode) -> bool {
        self.dyn_eq(other)
    }
}

impl Clone for Box<dyn DynamicNode> {
    fn clone(&self) -> Self {
        self.box_clone()
    }
}

impl<T> DynamicNode for T where T: Debug + PartialEq + Clone + Layout + 'static {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn dyn_eq(&self, other: &dyn DynamicNode) -> bool {
        match other.as_any().downcast_ref::<Self>() {
            Some(other) => self == other,
            None => false,
        }
    }

    fn box_clone(&self) -> Box<dyn DynamicNode> {
        Box::new(self.clone())
    }
}