summaryrefslogtreecommitdiff
path: root/tests/src/parser.rs
blob: b2aa01da9dbcbb2a512c95cf90f5a3cae233ce01 (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
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
use std::fmt::Debug;

use typstc::func::Scope;
use typstc::size::Size;
use typstc::syntax::*;
use typstc::{function, parse};

mod spanless;
use spanless::SpanlessEq;


/// The result of a single test case.
enum Case {
    Okay,
    Failed {
        line: usize,
        src: &'static str,
        expected: String,
        found: String,
    }
}

/// Test all tests.
fn test(tests: Vec<(&str, Vec<Case>)>) {
    println!();

    let mut errors = false;

    let len = tests.len();
    println!("Running {} test{}", len, if len > 1 { "s" } else { "" });

    for (file, cases) in tests {
        print!("Testing: {}. ", file);

        let mut okay = 0;
        let mut failed = 0;

        for case in cases {
            match case {
                Case::Okay => okay += 1,
                Case::Failed { line, src, expected, found } => {
                    println!();
                    println!(" ❌  Case failed in file {}.rs in line {}.", file, line);
                    println!("   - Source:   {:?}", src);
                    println!("   - Expected: {}", expected);
                    println!("   - Found:    {}", found);

                    failed += 1;
                }
            }
        }

        // Print a small summary.
        print!("{} okay, {} failed.", okay, failed);
        if failed == 0 {
            print!(" ✔")
        } else {
            errors = true;
        }

        println!();
    }

    println!();

    if errors {
        std::process::exit(-1);
    }
}

/// The main test macro.
macro_rules! tokens {
    ($($task:ident $src:expr =>($line:expr)=> [$($e:tt)*])*) => ({
        vec![$({
            let (okay, expected, found) = case!($task $src, [$($e)*]);
            if okay {
                Case::Okay
            } else {
                Case::Failed {
                    line: $line,
                    src: $src,
                    expected: format(expected),
                    found: format(found),
                }
            }
        }),*]
    });
}

//// Indented formatting for failed cases.
fn format(thing: impl Debug) -> String {
    format!("{:#?}", thing).replace('\n', "\n     ")
}

/// Evaluates a single test.
macro_rules! case {
    (t $($rest:tt)*) => (case!(@tokenize SpanlessEq::spanless_eq, $($rest)*));
    (ts $($rest:tt)*) => (case!(@tokenize PartialEq::eq, $($rest)*));

    (@tokenize $cmp:expr, $src:expr, [$($e:tt)*]) => ({
        let expected = list!(tokens [$($e)*]);
        let found = tokenize($src).collect::<Vec<_>>();
        ($cmp(&found, &expected), expected, found)
    });

    (p $($rest:tt)*) => (case!(@parse SpanlessEq::spanless_eq, $($rest)*));
    (ps $($rest:tt)*) => (case!(@parse PartialEq::eq, $($rest)*));

    (@parse $cmp:expr, $src:expr, [$($e:tt)*]) => ({
        let expected = SyntaxTree { nodes: list!(nodes [$($e)*]) };
        let found = parse($src, ParseContext { scope: &scope() }).0;
        ($cmp(&found, &expected), expected, found)
    });

    (c $src:expr, [$($e:tt)*]) => ({
        let expected = Colorization { tokens: list!(colors [$($e)*]) };
        let found = parse($src, ParseContext { scope: &scope() }).1;
        (expected == found, expected, found)
    });

    (e $src:expr, [$($e:tt)*]) => ({
        let errors = list!([$($e)*]).into_iter()
            .map(|s| s.map(|m| m.to_string()))
            .collect();

        let expected = ErrorMap { errors };
        let found = parse($src, ParseContext { scope: &scope() }).2;
        (expected == found, expected, found)
    });
}

/// A scope containing the `DebugFn` as a fallback.
fn scope() -> Scope {
    Scope::with_debug::<DebugFn>()
}

/// Parses possibly-spanned lists of token or node expressions.
macro_rules! list {
    (expr [$($item:expr),* $(,)?]) => ({
        #[allow(unused_imports)]
        use cuts::expr::*;
        Tuple { items: vec![$(zspan($item)),*] }
    });

    (expr [$($key:expr =>($_:expr)=> $value:expr),* $(,)?]) => ({
        #[allow(unused_imports)]
        use cuts::expr::*;
        Object {
            pairs: vec![$(Pair {
                key: zspan(Ident($key.to_string())),
                value: zspan($value),
            }),*]
        }
    });

    ($cut:ident [$($e:tt)*]) => ({
        #[allow(unused_imports)]
        use cuts::$cut::*;
        list!([$($e)*])
    });

    ([$(($sl:tt:$sc:tt, $el:tt:$ec:tt, $v:expr)),* $(,)?]) => ({
        vec![
            $(Spanned { v: $v, span: Span {
                start: Position { line: $sl, column: $sc },
                end:   Position { line: $el, column: $ec },
            }}),*
        ]
    });

    ([$($e:tt)*]) => (vec![$($e)*].into_iter().map(zspan).collect::<Vec<_>>());
}

/// Composes a function expression.
macro_rules! func {
    ($name:expr $(,pos: [$($p:tt)*])? $(,key: [$($k:tt)*])?; $($b:tt)*) => ({
        #![allow(unused_mut, unused_assignments)]

        let mut positional = Tuple::new();
        let mut keyword = Object::new();

        $(positional = list!(expr [$($p)*]);)?
        $(keyword = list!(expr [$($k)*]);)?

        Node::Func(FuncCall(Box::new(DebugFn {
            header: FuncHeader {
                name: zspan(Ident($name.to_string())),
                args: FuncArgs {
                    positional,
                    keyword,
                },
            },
            body: func!(@body $($b)*),
        })))
    });

    (@body Some($($b:tt)*)) => (Some(SyntaxTree { nodes: list!(nodes $($b)*) }));
    (@body None) => (None);
}

function! {
    /// Most functions in the tests are parsed into the debug function for easy
    /// inspection of arguments and body.
    #[derive(Debug, PartialEq)]
    pub struct DebugFn {
        header: FuncHeader,
        body: Option<SyntaxTree>,
    }

    parse(header, body, ctx) {
        let cloned = header.clone();
        header.args.clear();
        DebugFn {
            header: cloned,
            body: parse!(optional: body, ctx),
        }
    }

    layout() { vec![] }
}

/// Span an element with a zero span.
fn zspan<T>(v: T) -> Spanned<T> {
    Spanned { v, span: Span::ZERO }
}

/// Abbreviations for tokens, nodes, colors and expressions.
#[allow(non_snake_case, dead_code)]
mod cuts {
    pub mod tokens {
        pub use typstc::syntax::Token::{
            Whitespace as W,
            LineComment as LC,
            BlockComment as BC,
            StarSlash as SS,
            LeftBracket as LB,
            RightBracket as RB,
            LeftParen as LP,
            RightParen as RP,
            LeftBrace as LBR,
            RightBrace as RBR,
            Colon as CL,
            Comma as CM,
            Equals as EQ,
            ExprIdent as ID,
            ExprStr as STR,
            ExprSize as SIZE,
            ExprNumber as NUM,
            ExprBool as BOOL,
            Star as S,
            Underscore as U,
            Backtick as B,
            Text as T,
        };
    }

    pub mod nodes {
        use typstc::syntax::Node;

        pub use Node::{
            Space as S,
            Newline as N,
            ToggleItalic as I,
            ToggleBolder as B,
            ToggleMonospace as M,
        };

        pub fn T(text: &str) -> Node {
            Node::Text(text.to_string())
        }
    }

    pub mod colors {
        pub use typstc::syntax::ColorToken::{
            Comment as C,
            Bracket as B,
            FuncName as FN,
            Colon as CL,
            Key as K,
            Equals as EQ,
            Comma as CM,
            Paren as P,
            Brace as BR,
            ExprIdent as ID,
            ExprStr as STR,
            ExprNumber as NUM,
            ExprSize as SIZE,
            ExprBool as BOOL,
            Bold as BD,
            Italic as IT,
            Monospace as MS,
            Invalid as INV,
        };
    }

    pub mod expr {
        use typstc::syntax::{Expression, Ident};

        pub use Expression::{
            Number as NUM,
            Size as SIZE,
            Bool as BOOL,
        };

        pub fn ID(text: &str) -> Expression {
            Expression::Ident(Ident(text.to_string()))
        }

        pub fn STR(text: &str) -> Expression {
            Expression::Str(text.to_string())
        }
    }
}

fn main() {
    test(include!("../cache/parser-tests.rs"))
}