brack_sdk_rs/
ast.rs

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
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct InnerNode {
    pub id: String,
    pub children: Vec<AST>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct LeafNode {
    pub id: String,
    pub value: String,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum AST {
    Document(InnerNode),
    Stmt(InnerNode),
    Expr(InnerNode),
    Angle(InnerNode),
    Square(InnerNode),
    Curly(InnerNode),
    Identifier(InnerNode),
    Text(LeafNode),
}

impl AST {
    pub fn children(&self) -> &Vec<AST> {
        match self {
            AST::Document(node)
            | AST::Stmt(node)
            | AST::Expr(node)
            | AST::Angle(node)
            | AST::Square(node)
            | AST::Curly(node)
            | AST::Identifier(node) => &node.children,
            AST::Text(_) => panic!("Leaf node has no children"),
        }
    }

    pub fn value(&self) -> String {
        match self {
            AST::Text(leaf) => leaf.value.clone(),
            AST::Document(_)
            | AST::Stmt(_)
            | AST::Expr(_)
            | AST::Angle(_)
            | AST::Square(_)
            | AST::Curly(_)
            | AST::Identifier(_) => panic!("Inner node has no value"),
        }
    }

    pub fn id(&self) -> String {
        match self {
            AST::Document(node)
            | AST::Stmt(node)
            | AST::Expr(node)
            | AST::Angle(node)
            | AST::Square(node)
            | AST::Curly(node)
            | AST::Identifier(node) => node.id.clone(),
            AST::Text(leaf) => leaf.id.clone(),
        }
    }
}

impl AST {
    pub fn add(&mut self, ast: AST) -> Result<()> {
        match self {
            AST::Document(node)
            | AST::Stmt(node)
            | AST::Expr(node)
            | AST::Angle(node)
            | AST::Square(node)
            | AST::Curly(node)
            | AST::Identifier(node) => {
                node.children.push(ast);
            }
            AST::Text(_) => {
                anyhow::bail!("Cannot add child to leaf node");
            }
        }
        Ok(())
    }

    pub fn get(&self, id: &str) -> Option<&AST> {
        match self {
            AST::Document(node)
            | AST::Stmt(node)
            | AST::Expr(node)
            | AST::Angle(node)
            | AST::Square(node)
            | AST::Curly(node)
            | AST::Identifier(node) => {
                if node.id == id {
                    return Some(self);
                }
                for child in &node.children {
                    if let Some(ast) = child.get(id) {
                        return Some(ast);
                    }
                }
                None
            },
            AST::Text(node) => {
                if node.id == id {
                    return Some(self);
                }
                None
            },
        }
    }
}