leap-lang 0.3.0

Leap language parser
Documentation
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
use crate::{
    leaptypes::{Comment, CommentType, LeapEnum, LeapStruct, LeapType, Name, ValueType},
    parser::{commentsparser, parser::Parser},
};

#[derive(Debug)]
struct Block {
    start: usize,
    next_start: usize,
    trail_indent: usize,
    new_section: bool,
    text: String,
}

pub fn format(data: &str) -> Option<String> {
    let types = Parser::parse(data).ok()?;
    let mut formatted: Vec<Block> = vec![];
    for next_type in &types {
        let mut lines = format_type(next_type);
        if !lines.is_empty() {
            if let Some(last) = formatted.last_mut() {
                last.next_start = lines.last().unwrap().start;
            }
            update_trail_indent(&mut lines);
            formatted.append(&mut lines);
        }
    }
    if let Some(last) = formatted.last_mut() {
        last.next_start = data.len();
    }
    let mut comments = commentsparser::parse(data).into_iter().peekable();
    let mut result = vec![];
    for b in formatted {
        while comments
            .peek()
            .map_or(false, |c| c.position.start < b.start)
        {
            let indent = if b.new_section { 0 } else { 4 };
            result.push(format_comment(&comments.next().unwrap(), indent));
        }
        let has_trail_comment = comments.peek().map_or(false, |c| match c.comment_type {
            CommentType::Trail => b.start < c.position.start && c.position.start < b.next_start,
            _ => false,
        });
        if has_trail_comment {
            let indent = b.trail_indent - b.text.len();
            let mut text = b.text;
            text.push_str(&format_comment(&comments.next().unwrap(), indent));
            result.push(text);
        } else {
            result.push(b.text);
        }
    }
    for c in comments {
        result.push(format_comment(&c, 0));
    }
    // remove repeating separators and separators at the start
    let mut new_result: Vec<String> = vec![];
    for r in result {
        if r.is_empty() {
            // push only if last is not empty and array is not empty
            let last_separator_or_empty = new_result.last().map(|s| s.is_empty()).unwrap_or(true);
            if !last_separator_or_empty {
                new_result.push(r)
            }
        } else {
            new_result.push(r);
        }
    }
    result = new_result;
    // remove separators at the end
    if result.last().map(|s| s.is_empty()).unwrap_or(false) {
        result.pop();
    }
    let mut result = result.join("\n");
    // newline at the end
    if !result.is_empty() {
        result.push('\n');
    }
    Some(result)
}

fn update_trail_indent(lines: &mut [Block]) {
    let max_len = lines.iter().map(|b| b.text.len()).max().unwrap_or(0);
    let indent = (max_len / 4) * 4 + 4;
    for b in lines {
        b.trail_indent = indent;
    }
}

fn format_type(leap_type: &LeapType) -> Vec<Block> {
    let mut lines = vec![];
    let mut type_lines = match leap_type {
        LeapType::Struct(s) => format_struct(s),
        LeapType::Enum(e) => format_enum(e),
    };
    lines.append(&mut type_lines);
    lines
}

fn format_struct(leap_struct: &LeapStruct) -> Vec<Block> {
    let mut lines = vec![];
    let text = format!(
        ".struct {}{}",
        leap_struct.name.get(),
        format_type_args(&leap_struct.args)
    );
    let next_start = if let Some(last) = leap_struct.props.last() {
        last.position.start
    } else {
        leap_struct.position.end()
    };
    lines.push(Block {
        start: leap_struct.position.start,
        next_start,
        trail_indent: 0,
        new_section: true,
        text,
    });
    for i in 0..leap_struct.props.len() {
        let prop = &leap_struct.props[i];
        let next_prop = leap_struct.props.get(i + 1);
        let text = format!(
            "    {}: {}",
            prop.name.get(),
            format_prop_type(&prop.prop_type)
        );
        let next_start = if let Some(next) = next_prop {
            next.position.start
        } else {
            prop.position.end()
        };
        lines.push(Block {
            start: prop.position.start,
            next_start,
            trail_indent: 0,
            new_section: false,
            text,
        })
    }
    lines
}

fn format_enum(leap_enum: &LeapEnum) -> Vec<Block> {
    let mut lines = vec![];
    let text = format!(
        ".enum {}{}",
        leap_enum.name.get(),
        format_type_args(&leap_enum.args)
    );
    let next_start = if let Some(last) = leap_enum.variants.last() {
        last.position.start
    } else {
        leap_enum.position.end()
    };
    lines.push(Block {
        start: leap_enum.position.start,
        next_start,
        trail_indent: 0,
        new_section: true,
        text,
    });
    for i in 0..leap_enum.variants.len() {
        let variant = &leap_enum.variants[i];
        let next_var = leap_enum.variants.get(i + 1);
        let text = if variant.name.get() == variant.prop_type.name() {
            format!("    {}", format_prop_type(&variant.prop_type))
        } else {
            format!(
                "    {}: {}",
                variant.name.get(),
                format_prop_type(&variant.prop_type)
            )
        };
        let next_start = if let Some(next) = next_var {
            next.position.start
        } else {
            variant.position.end()
        };
        lines.push(Block {
            start: variant.position.start,
            next_start,
            trail_indent: 0,
            new_section: false,
            text,
        })
    }
    lines
}

fn format_type_args(args: &[Name]) -> String {
    if args.is_empty() {
        "".to_owned()
    } else {
        let mut tokens = vec![];
        for name in args {
            tokens.push(name.get());
        }
        let tokens = tokens.join(" ");
        format!("[{}]", tokens)
    }
}

fn format_prop_type(prop_type: &ValueType) -> String {
    match prop_type {
        ValueType::Simple(t) => t.name(),
        ValueType::List(t) => format!("list[{}]", format_prop_type(t)),
        ValueType::TypeArg(n) => n.get().to_owned(),
        ValueType::LeapType { name, args } => {
            if args.is_empty() {
                name.get().to_owned()
            } else {
                let args = args
                    .iter()
                    .map(format_prop_type)
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("{}[{}]", name.get(), args)
            }
        }
    }
}

fn format_comment(comment: &Comment, indent: usize) -> String {
    let indent = String::from_utf8(vec![b' '; indent]).unwrap();
    match comment.comment_type {
        CommentType::Line | CommentType::Trail => format!("{}/-- {}", indent, comment.comment),
        CommentType::Separator => "".to_owned(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_update_trail_indent() {
        let mut blocks = vec![Block {
            start: 0,
            next_start: 0,
            trail_indent: 0,
            new_section: false,
            text: "aaaa".to_owned(),
        }];
        update_trail_indent(&mut blocks);
        assert_eq!(blocks[0].trail_indent, 8);
        let mut blocks = vec![Block {
            start: 0,
            next_start: 0,
            trail_indent: 0,
            new_section: false,
            text: "aaaaaaa".to_owned(),
        }];
        update_trail_indent(&mut blocks);
        assert_eq!(blocks[0].trail_indent, 8);
    }

    #[test]
    fn test_format() {
        assert_eq!(format(".struct    s1").unwrap(), ".struct s1\n");
        assert_eq!(
            format(".struct    s1 [  a   b   ]").unwrap(),
            ".struct s1[a b]\n"
        );
        assert_eq!(
            format(".struct    s1  a:   int").unwrap(),
            ".struct s1\n    a: int\n"
        );
        assert_eq!(format(".enum    e1    s1").unwrap(), ".enum e1\n    s1\n");
        assert_eq!(
            format(".enum    e1    aaa:    s1").unwrap(),
            ".enum e1\n    aaa: s1\n"
        );
        assert_eq!(
            format(".enum    e1[a   b]    s1  v2[ a   b ]").unwrap(),
            ".enum e1[a b]\n    s1\n    v2[a b]\n"
        );
    }

    #[test]
    fn test_format_with_comments() {
        assert_eq!(format("").unwrap(), "");
        assert_eq!(
            format("/ text\n.struct    s1").unwrap(),
            "/-- text\n.struct s1\n"
        );
        assert_eq!(
            format("/- text\n.struct    s1").unwrap(),
            "/-- text\n.struct s1\n"
        );
        assert_eq!(
            format("/-- text\n.struct    s1").unwrap(),
            "/-- text\n.struct s1\n"
        );
        assert_eq!(
            format("/--- text\n.struct    s1").unwrap(),
            "/-- - text\n.struct s1\n"
        );
        assert_eq!(
            format("/-- -- text\n.struct    s1").unwrap(),
            "/-- -- text\n.struct s1\n"
        );
        assert_eq!(
            format("/ text\n\n.struct    s1").unwrap(),
            "/-- text\n\n.struct s1\n"
        );
        assert_eq!(
            format("\n\n\n\n.struct  \n\n\n\n  s1\n\n\n\n").unwrap(),
            ".struct s1\n"
        );
        assert_eq!(
            format("/ text\n\n\n.struct    s1").unwrap(),
            "/-- text\n\n.struct s1\n"
        );
        assert_eq!(
            format("/ text     \n\n\n.struct    s1").unwrap(),
            "/-- text\n\n.struct s1\n"
        );

        assert_eq!(
            format(".struct s1 / text").unwrap(),
            ".struct s1  /-- text\n"
        );
        assert_eq!(format(".enum s1 / text").unwrap(), ".enum s1    /-- text\n");

        assert_eq!(
            format(".struct s1\n/ text").unwrap(),
            ".struct s1\n/-- text\n"
        );
        assert_eq!(format(".enum s1\n/ text").unwrap(), ".enum s1\n/-- text\n");

        assert_eq!(
            format(".struct s1\nv: int / text").unwrap(),
            ".struct s1\n    v: int  /-- text\n"
        );
        assert_eq!(
            format(".enum s1\nval / text").unwrap(),
            ".enum s1\n    val     /-- text\n"
        );

        assert_eq!(
            format(".struct s1\n/ text\nv: int").unwrap(),
            ".struct s1\n    /-- text\n    v: int\n"
        );
        assert_eq!(
            format(".enum aaaaaa\n/ text\nval").unwrap(),
            ".enum aaaaaa\n    /-- text\n    val\n"
        );

        assert_eq!(
            format(".struct s1\n/ text\n\n\n/ text\nv: int").unwrap(),
            ".struct s1\n    /-- text\n\n    /-- text\n    v: int\n"
        );
    }

    #[test]
    fn test_format_complex() {
        let formatted = format(
            "
        / text1 text
        / tttt2

        /text3

        .struct some-my-struct / text4
        /text5

        /text6
        /text7
        v1: list[int] /text8
        v2: list[list[int]]

        /text9
        /text10

        .enum value-enum

        / test11

        val1
        val2

        / text12
        ",
        )
        .unwrap();
        let expected = "/-- text1 text
/-- tttt2

/-- text3

.struct some-my-struct  /-- text4
    /-- text5

    /-- text6
    /-- text7
    v1: list[int]       /-- text8
    v2: list[list[int]]

/-- text9
/-- text10

.enum value-enum

    /-- test11

    val1
    val2

/-- text12
";
        assert_eq!(formatted, expected);
        // if format formatted, result should be same
        assert_eq!(format(&formatted).unwrap(), expected);
    }
}