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
use regex::Regex;
use teo_teon::value::Value;
use crate::ast::argument_list::ArgumentList;
use crate::ast::expression::Expression;
use crate::ast::identifier::Identifier;
use crate::{declare_container_node, declare_node, impl_container_node_defaults, impl_node_defaults, node_child_fn, node_children_iter, node_children_iter_fn, node_optional_child_fn};
use crate::ast::named_expression::NamedExpression;
use crate::ast::punctuations::Punctuation;
use crate::format::Writer;
use crate::traits::write::Write;

declare_node!(NumericLiteral, pub(crate) value: Value, pub(crate) display: String);

impl_node_defaults!(NumericLiteral);

impl Write for NumericLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_content(self, self.display.as_str());
    }
}

declare_node!(StringLiteral, pub(crate) value: String, pub(crate) display: String);

impl_node_defaults!(StringLiteral);

impl Write for StringLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_content(self, self.display.as_str());
    }
}

declare_node!(RegexLiteral, pub(crate) value: Regex, pub(crate) display: String);

impl_node_defaults!(RegexLiteral);

impl Write for RegexLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_content(self, self.display.as_str());
    }
}

declare_node!(BoolLiteral, pub(crate) value: bool);

impl_node_defaults!(BoolLiteral);

impl Write for BoolLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_content(self, if self.value { "true" } else { "false" });
    }
}

declare_node!(NullLiteral);

impl_node_defaults!(NullLiteral);

impl Write for NullLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_content(self, "null")
    }
}

declare_container_node!(EnumVariantLiteral,
    pub(crate) identifier: usize,
    pub(crate) argument_list: Option<usize>,
);

impl_container_node_defaults!(EnumVariantLiteral);

impl EnumVariantLiteral {

    node_child_fn!(identifier, Identifier);

    node_optional_child_fn!(argument_list, ArgumentList);

    pub fn unwrap_enumerable_enum_member_strings(&self) -> Option<Vec<&str>> {
        Some(vec![self.identifier().name()])
    }

    pub fn unwrap_enumerable_enum_member_string(&self) -> Option<&str> {
        Some(self.identifier().name())
    }
}

impl Write for EnumVariantLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values())
    }
}

declare_container_node!(TupleLiteral, pub(crate) expressions: Vec<usize>);

impl_container_node_defaults!(TupleLiteral);

node_children_iter!(TupleLiteral, Expression, TupleLiteralExpressionsIter, expressions);

impl TupleLiteral {

    node_children_iter_fn!(expressions, TupleLiteralExpressionsIter);
}

impl Write for TupleLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values())
    }
}

declare_container_node!(ArrayLiteral, pub(crate) expressions: Vec<usize>);

impl_container_node_defaults!(ArrayLiteral);

node_children_iter!(ArrayLiteral, Expression, ArrayLiteralExpressionsIter, expressions);

impl ArrayLiteral {

    node_children_iter_fn!(expressions, ArrayLiteralExpressionsIter);

    pub fn unwrap_enumerable_enum_member_strings(&self) -> Option<Vec<&str>> {
        let mut result = vec![];
        for expression in self.expressions() {
            if let Some(r) = expression.unwrap_enumerable_enum_member_string() {
                result.push(r);
            }
        }
        Some(result)
    }

    pub fn unwrap_enumerable_enum_member_string(&self) -> Option<&str> {
        if self.expressions.len() < 1 {
            None
        } else {
            self.expressions().next().unwrap().unwrap_enumerable_enum_member_string()
        }
    }
}

impl Write for ArrayLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values())
    }
}

declare_container_node!(DictionaryLiteral,
    pub(crate) expressions: Vec<usize>,
    pub(crate) namespace_path: Vec<usize>,
    pub(crate) is_config_field: bool,
    pub(crate) close_block: usize,
);

impl_container_node_defaults!(DictionaryLiteral);

node_children_iter!(DictionaryLiteral, NamedExpression, DictionaryLiteralExpressionsIter, expressions);

impl DictionaryLiteral {

    node_children_iter_fn!(expressions, DictionaryLiteralExpressionsIter);

    node_child_fn!(close_block, Punctuation);

}

impl Write for DictionaryLiteral {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values())
    }
}