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
// structured style markup (language)
// a SIMPLE regex parsed language which compiles into CSS

#[derive(Debug, Clone)]
pub struct SSMProgram {
    pub viewport_restriction: String,
    pub uses: Vec<SSMUseBlock>,
    pub members: Vec<SSMMemberBlock>,
    pub sets: Vec<SSMSetBlock>,
    pub errors: Vec<SSMError>,
}

#[derive(Debug, Clone)]
pub struct SSMError {
    pub message: String,
    pub flagged: String, // flagged text
}

#[derive(Debug, Clone)]
pub struct SSMUseBlock {
    // a "USE" block defines information about the program being parsed, such as the version:
    // USE ssm 1.0
    pub reference: String,
}

#[derive(Debug, Clone)]
pub struct SSMMemberBlock {
    // a "MEMBER" is a CSS selector reference which stores the selector by a custom name
    pub by: String,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct SSMSetBlock {
    // a "SET" block is used to set a property to a value on a member
    pub property: String,
    pub value: String,
    pub for_member: Option<String>,
    pub at: Option<String>, // (for animations, corresponds to the animation stage)
}

// parse an ssm into a program tree
fn program_tree(input: String) -> SSMProgram {
    let mut program: SSMProgram = SSMProgram {
        viewport_restriction: String::new(),
        uses: Vec::new(),
        members: Vec::new(),
        sets: Vec::new(),
        errors: Vec::new(),
    };

    // ...

    // regex
    let when_regex = regex::RegexBuilder::new(r"^(WHEN)\s(?<RESTRICTION>.*?)($|\%)")
        .multi_line(true)
        .build()
        .unwrap();

    let use_regex = regex::RegexBuilder::new(r"^(USE)\s(?<REFERENCE>.*?)($|\%)")
        .multi_line(true)
        .build()
        .unwrap();

    let member_regex =
        regex::RegexBuilder::new(r"^(MEMBER)\s(?<SELECTOR>.*?)(\sNAMED\s)(?<NAME>.*?)($|\%)")
            .multi_line(true)
            .build()
            .unwrap();

    let set_regex = regex::RegexBuilder::new(
        r"^(SET)\s(?<PROPERTY>.*?)(TO)(?<VALUE>.*?)(FOR)\s(?<MEMBER>.*?)($|\%)",
    )
    .multi_line(true)
    .build()
    .unwrap();

    let set_at_regex = regex::RegexBuilder::new(
        r"^(SET)\s(?<PROPERTY>.*?)(TO)(?<VALUE>.*?)(AT)\s(?<AT>.*?)($|\%)",
    )
    .multi_line(true)
    .build()
    .unwrap();

    // matches
    for capture in when_regex.captures_iter(&input) {
        // WHEN is used to define a custom viewport restriction (media query)
        let restriction = capture.name("RESTRICTION").unwrap().as_str();
        program.viewport_restriction = restriction.to_string();
    }

    for capture in use_regex.captures_iter(&input) {
        let reference = capture.name("REFERENCE").unwrap().as_str();
        program.uses.push(SSMUseBlock {
            reference: reference.to_string(),
        });
    }

    for capture in member_regex.captures_iter(&input) {
        let selector = capture.name("SELECTOR").unwrap().as_str();
        let name = capture.name("NAME").unwrap().as_str();

        // make sure member isn't already registered
        let existing = program.members.iter().find(|m| m.name == name);

        if existing.is_some() {
            program.errors.push(SSMError {
                message: String::from(
                    "a member with this name is already registered in this environment",
                ),
                flagged: name.to_string(),
            });

            continue;
        }

        // ...
        program.members.push(SSMMemberBlock {
            by: selector.to_string(),
            name: name.to_string(),
        });
    }

    for capture in set_regex.captures_iter(&input) {
        let property = capture.name("PROPERTY").unwrap().as_str().trim();
        let value = capture.name("VALUE").unwrap().as_str().trim();
        let member = capture.name("MEMBER").unwrap().as_str().trim();

        // ...
        program.sets.push(SSMSetBlock {
            property: property.to_string(),
            value: value.to_string(),
            for_member: Option::Some(member.to_string()),
            at: Option::None,
        });
    }

    for capture in set_at_regex.captures_iter(&input) {
        let property = capture.name("PROPERTY").unwrap().as_str().trim();
        let value = capture.name("VALUE").unwrap().as_str().trim();
        let at = capture.name("AT").unwrap().as_str().trim();

        program.sets.push(SSMSetBlock {
            property: property.to_string(),
            value: value.to_string(),
            for_member: Option::None,
            at: Option::Some(at.to_string()),
        });
    }

    // return
    return program;
}

// parse an ssm program and return the compiled CSS
pub fn parse_ssm_program(input: String) -> String {
    let mut out: String = String::new();

    // get tree
    let tree: SSMProgram = program_tree(input);
    let mut parsed_sets: Vec<&SSMSetBlock> = Vec::new();

    // ...
    for member in tree.members.iter() {
        // get all sets
        let sets = tree
            .sets
            .iter()
            // TODO: remove the .clone() when filtering sets
            .filter(|s| s.for_member == Option::Some(member.name.clone()));

        // build out
        let mut member_out = format!(
            r"{}[named='{}'] {{}}
{} {{REMOVE}}}}",
            member.by, member.name, member.by
        );

        for set in sets {
            member_out.push_str(&format!("{}: {};", set.property, set.value));
            parsed_sets.push(set); // push set so we don't parse it again
        }

        // add to out
        member_out = member_out.replace("REMOVE}}", "");
        out += &format!("{member_out}}}");
    }

    for set in tree.sets.iter() {
        if parsed_sets.contains(&set) {
            // this set has already been parsed previously in the program, so we're going to ignore it
            continue;
        }

        // build out
        let mut member_out = if set.at.is_some() {
            set.at.as_ref().unwrap().to_owned() + r" {REMOVE}}"
        } else {
            set.for_member.as_ref().unwrap().to_owned()
                + r" {REMOVE}} --ssm-warn: 'unknown member';"
        };

        member_out.push_str(&format!("{}: {};", set.property, set.value));

        // add to out
        member_out = member_out.replace("REMOVE}}", "");
        out += &format!("{member_out}}}");
    }

    // errors
    for error in tree.errors.iter() {
        // add to out
        out += &format!(
            r"body::before {{
                position: absolute;
                bottom: 0;
                left: 0;
                content: '[SSMError]: \'{}\' | {}';
                border-left: solid red 5px;
                background: black;
                padding-left: 1rem;
            }}",
            error.flagged, error.message
        );
    }

    // remove comments
    out = out.replace(r"^@\s(.*?)$", "");

    // handle use
    if tree.uses.len() > 1 {
        // animation definition ("USE ssm::anim {name}")
        let anim_use = tree
            .uses
            .iter()
            .find(|u| u.reference.starts_with("ssm::anim"));

        if anim_use.is_some() {
            let anim_name = anim_use
                .unwrap()
                .reference
                .split("ssm:anim ")
                .skip(1)
                .collect::<String>();

            out = format!("@keyframes {} {{{}}}", anim_name, out);
        }

        // inherit (references that start with "http" are imported)
        for use_statement in tree.uses {
            if !use_statement.reference.starts_with("http") {
                continue;
            };

            out = format!("@import url(\"{}\");{}", use_statement.reference, out);
        }
    }

    // handle viewport_restriction
    if !tree.viewport_restriction.is_empty() {
        out = format!(
            "@media screen and ({}) {{{}}}",
            tree.viewport_restriction, out
        );
    }

    // return
    return out;
}

pub fn parse_ssm_blocks(input: String) -> String {
    // parses all SSM blocks in a Markdown input
    let mut out: String = String::new();

    let ssm_regex = regex::RegexBuilder::new("(ssm\\#)(?<CONTENT>.*?)\\#")
        .multi_line(true)
        .dot_matches_new_line(true)
        .build()
        .unwrap();

    for capture in ssm_regex.captures_iter(&input.clone()) {
        let content = capture.name("CONTENT").unwrap().as_str();

        // compile
        let css = parse_ssm_program(content.to_string());

        // replace
        out += &css;
    }

    // return
    return out;
}