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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use crate::token_source::TokenSource;
use crate::Parser;
use biome_diagnostics::console::fmt::Display;
use biome_diagnostics::console::{markup, MarkupBuf};
use biome_diagnostics::location::AsSpan;
use biome_diagnostics::{Advices, Diagnostic, Location, LogCategory, MessageAndDescription, Visit};
use biome_rowan::{SyntaxKind, TextLen, TextRange};
use std::cmp::Ordering;

/// A specialized diagnostic for the parser
///
/// Parser diagnostics are always **errors**.
///
/// A parser diagnostics structured in this way:
/// 1. a mandatory message and a mandatory [TextRange]
/// 2. a list of details, useful to give more information and context around the error
/// 3. a hint, which should tell the user how they could fix their issue
///
/// These information **are printed in this exact order**.
///
#[derive(Debug, Diagnostic, Clone)]
#[diagnostic(category = "parse", severity = Error)]
pub struct ParseDiagnostic {
    /// The location where the error is occurred
    #[location(span)]
    span: Option<TextRange>,
    #[message]
    #[description]
    message: MessageAndDescription,
    #[advice]
    advice: ParserAdvice,
}

/// Possible details related to the diagnostic
#[derive(Debug, Default, Clone)]
struct ParserAdvice {
    advice_list: Vec<ParserAdviceKind>,
}

/// The structure of the advice. A message that gives details, a possible range so
/// the diagnostic is able to highlight the part of the code we want to explain.
#[derive(Debug, Clone)]
struct ParserAdviceDetail {
    /// A message that should explain this detail
    message: MarkupBuf,
    /// An optional range that should highlight the details of the code
    span: Option<TextRange>,
}

#[derive(Debug, Clone)]
enum ParserAdviceKind {
    /// A list a possible details that can be attached to the diagnostic.
    /// Useful to explain the nature errors.
    Detail(ParserAdviceDetail),
    /// A message for the user that should tell the user how to fix the issue
    Hint(MarkupBuf),
    List(MarkupBuf, Vec<MarkupBuf>),
}

impl ParserAdvice {
    fn add_detail(&mut self, message: impl Display, range: impl AsSpan) {
        self.advice_list
            .push(ParserAdviceKind::Detail(ParserAdviceDetail {
                message: markup! { {message} }.to_owned(),
                span: range.as_span(),
            }));
    }

    fn add_hint(&mut self, message: impl Display) {
        self.advice_list
            .push(ParserAdviceKind::Hint(markup! { { message } }.to_owned()));
    }

    fn add_hint_with_alternatives(&mut self, message: impl Display, alternatives: &[impl Display]) {
        self.advice_list.push(ParserAdviceKind::List(
            markup! {{message}}.to_owned(),
            alternatives
                .iter()
                .map(|msg| markup! {{msg}}.to_owned())
                .collect(),
        ))
    }
}

impl Advices for ParserAdvice {
    fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
        for advice_kind in &self.advice_list {
            match advice_kind {
                ParserAdviceKind::Detail(detail) => {
                    let ParserAdviceDetail { span, message } = detail;
                    visitor.record_log(LogCategory::Info, message)?;

                    let location = Location::builder().span(span).build();
                    visitor.record_frame(location)?;
                }
                ParserAdviceKind::Hint(hint) => {
                    visitor.record_log(LogCategory::Info, hint)?;
                }
                ParserAdviceKind::List(message, list) => {
                    visitor.record_log(LogCategory::Info, message)?;

                    let list: Vec<_> = list
                        .iter()
                        .map(|suggestion| suggestion as &dyn Display)
                        .collect();
                    visitor.record_list(&list)?;
                }
            }
        }

        Ok(())
    }
}

impl ParseDiagnostic {
    pub fn new(message: impl Display, span: impl AsSpan) -> Self {
        Self {
            span: span.as_span(),
            message: MessageAndDescription::from(markup! { {message} }.to_owned()),
            advice: ParserAdvice::default(),
        }
    }

    pub fn new_single_node(name: &str, range: TextRange, p: &impl Parser) -> Self {
        let names = format!("{} {}", article_for(name), name);
        let msg = if p.source().text().text_len() <= range.start() {
            format!("Expected {} but instead found the end of the file.", names)
        } else {
            format!("Expected {} but instead found '{}'.", names, p.text(range))
        };
        Self {
            span: range.as_span(),
            message: MessageAndDescription::from(msg),
            advice: ParserAdvice::default(),
        }
        .with_detail(range, format!("Expected {} here.", names))
    }

    pub fn new_with_any(names: &[&str], range: TextRange, p: &impl Parser) -> Self {
        debug_assert!(names.len() > 1, "Requires at least 2 names");

        if names.len() < 2 {
            return Self::new_single_node(names.first().unwrap_or(&"<missing>"), range, p);
        }

        let mut joined_names = String::new();

        for (index, name) in names.iter().enumerate() {
            if index > 0 {
                joined_names.push_str(", ");
            }

            if index == names.len() - 1 {
                joined_names.push_str("or ");
            }

            joined_names.push_str(article_for(name));
            joined_names.push(' ');
            joined_names.push_str(name);
        }

        let msg = if p.source().text().text_len() <= range.start() {
            format!(
                "Expected {} but instead found the end of the file.",
                joined_names
            )
        } else {
            format!(
                "Expected {} but instead found '{}'.",
                joined_names,
                p.text(range)
            )
        };

        Self {
            span: range.as_span(),
            message: MessageAndDescription::from(msg),
            advice: ParserAdvice::default(),
        }
        .with_detail(range, format!("Expected {} here.", joined_names))
    }

    pub const fn is_error(&self) -> bool {
        true
    }

    /// Use this API if you want to highlight more code frame, to help to explain where's the error.
    ///
    /// A detail is printed **after the actual error** and before the hint.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use biome_console::fmt::{Termcolor};
    /// # use biome_console::markup;
    /// # use biome_diagnostics::{DiagnosticExt, PrintDiagnostic, console::fmt::Formatter};
    /// # use biome_parser::diagnostic::ParseDiagnostic;
    /// # use biome_rowan::{TextSize, TextRange};
    /// # use std::fmt::Write;
    ///
    /// let source = "const a";
    /// let range = TextRange::new(TextSize::from(0), TextSize::from(5));
    /// let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
    ///     .with_detail(TextRange::new(TextSize::from(6), TextSize::from(7)), "This is reason why it's broken");
    ///
    /// let mut write = biome_diagnostics::termcolor::Buffer::no_color();
    /// let error = diagnostic
    ///     .clone()
    ///     .with_file_path("example.js")
    ///     .with_file_source_code(source.to_string());
    /// Formatter::new(&mut Termcolor(&mut write))
    ///     .write_markup(markup! {
    ///     {PrintDiagnostic::verbose(&error)}
    /// })
    ///     .expect("failed to emit diagnostic");
    ///
    /// let mut result = String::new();
    /// write!(
    ///     result,
    ///     "{}",
    ///     std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
    /// ).expect("");
    pub fn with_detail(mut self, range: impl AsSpan, message: impl Display) -> Self {
        self.advice.add_detail(message, range.as_span());
        self
    }

    /// Small message that should suggest the user how they could fix the error
    ///
    /// Hints are rendered a **last part** of the diagnostics
    ///
    /// ## Examples
    ///
    /// ```
    /// # use biome_console::fmt::{Termcolor};
    /// # use biome_console::markup;
    /// # use biome_diagnostics::{DiagnosticExt, PrintDiagnostic, console::fmt::Formatter};
    /// # use biome_parser::diagnostic::ParseDiagnostic;
    /// # use biome_rowan::{TextSize, TextRange};
    /// # use std::fmt::Write;
    ///
    /// let source = "const a";
    /// let range = TextRange::new(TextSize::from(0), TextSize::from(5));
    /// let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
    ///     .with_hint("You should delete the code");
    ///
    /// let mut write = biome_diagnostics::termcolor::Buffer::no_color();
    /// let error = diagnostic
    ///     .clone()
    ///     .with_file_path("example.js")
    ///     .with_file_source_code(source.to_string());
    /// Formatter::new(&mut Termcolor(&mut write))
    ///     .write_markup(markup! {
    ///     {PrintDiagnostic::verbose(&error)}
    /// })
    ///     .expect("failed to emit diagnostic");
    ///
    /// let mut result = String::new();
    /// write!(
    ///     result,
    ///     "{}",
    ///     std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
    /// ).expect("");
    ///
    /// assert!(result.contains("× this is wrong!"));
    /// assert!(result.contains("i You should delete the code"));
    /// assert!(result.contains("> 1 │ const a"));
    /// ```
    ///
    pub fn with_hint(mut self, message: impl Display) -> Self {
        self.advice.add_hint(message);
        self
    }

    /// A message that also allows to list of alternatives in case a fixed range of values/characters are expected.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use biome_console::fmt::{Termcolor};
    /// # use biome_console::markup;
    /// # use biome_diagnostics::{DiagnosticExt, PrintDiagnostic, console::fmt::Formatter};
    /// # use biome_parser::diagnostic::ParseDiagnostic;
    /// # use biome_rowan::{TextSize, TextRange};
    /// # use std::fmt::Write;
    ///
    /// let source = "const a";
    /// let range = TextRange::new(TextSize::from(0), TextSize::from(5));
    /// let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
    ///     .with_alternatives("Expected one of the following values:", &["foo", "bar"]);
    ///
    /// let mut write = biome_diagnostics::termcolor::Buffer::no_color();
    /// let error = diagnostic
    ///     .clone()
    ///     .with_file_path("example.js")
    ///     .with_file_source_code(source.to_string());
    /// Formatter::new(&mut Termcolor(&mut write))
    ///     .write_markup(markup! {
    ///     {PrintDiagnostic::verbose(&error)}
    /// })
    ///     .expect("failed to emit diagnostic");
    ///
    /// let mut result = String::new();
    /// write!(
    ///     result,
    ///     "{}",
    ///     std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
    /// ).expect("");
    ///
    /// assert!(result.contains("× this is wrong!"));
    /// assert!(result.contains("i Expected one of the following values:"));
    /// assert!(result.contains("- foo"));
    /// assert!(result.contains("- bar"));
    /// ```
    ///
    pub fn with_alternatives(
        mut self,
        message: impl Display,
        alternatives: &[impl Display],
    ) -> Self {
        self.advice
            .add_hint_with_alternatives(message, alternatives);
        self
    }

    /// Retrieves the range that belongs to the diagnostic
    pub(crate) fn diagnostic_range(&self) -> Option<&TextRange> {
        self.span.as_ref()
    }
}

pub trait ToDiagnostic<P>
where
    P: Parser,
{
    fn into_diagnostic(self, p: &P) -> ParseDiagnostic;
}

impl<P: Parser> ToDiagnostic<P> for ParseDiagnostic {
    fn into_diagnostic(self, _: &P) -> ParseDiagnostic {
        self
    }
}

#[must_use]
pub fn expected_token<K>(token: K) -> ExpectedToken
where
    K: SyntaxKind,
{
    ExpectedToken(
        token
            .to_string()
            .expect("Expected token to be a punctuation or keyword."),
    )
}

#[must_use]
pub fn expected_token_any<K: SyntaxKind>(tokens: &[K]) -> ExpectedTokens {
    use std::fmt::Write;
    let mut expected = String::new();

    for (index, token) in tokens.iter().enumerate() {
        if index > 0 {
            expected.push_str(", ");
        }

        if index == tokens.len() - 1 {
            expected.push_str("or ");
        }

        let _ = write!(
            &mut expected,
            "'{}'",
            token
                .to_string()
                .expect("Expected token to be a punctuation or keyword.")
        );
    }

    ExpectedTokens(expected)
}

pub struct ExpectedToken(&'static str);

impl<P> ToDiagnostic<P> for ExpectedToken
where
    P: Parser,
{
    fn into_diagnostic(self, p: &P) -> ParseDiagnostic {
        if p.cur() == P::Kind::EOF {
            p.err_builder(
                format!("expected `{}` but instead the file ends", self.0),
                p.cur_range(),
            )
            .with_detail(p.cur_range(), "the file ends here")
        } else {
            p.err_builder(
                format!("expected `{}` but instead found `{}`", self.0, p.cur_text()),
                p.cur_range(),
            )
            .with_hint(format!("Remove {}", p.cur_text()))
        }
    }
}

pub struct ExpectedTokens(String);

impl<P> ToDiagnostic<P> for ExpectedTokens
where
    P: Parser,
{
    fn into_diagnostic(self, p: &P) -> ParseDiagnostic {
        if p.cur() == P::Kind::EOF {
            p.err_builder(
                format!("expected {} but instead the file ends", self.0),
                p.cur_range(),
            )
            .with_detail(p.cur_range(), "the file ends here")
        } else {
            p.err_builder(
                format!("expected {} but instead found `{}`", self.0, p.cur_text()),
                p.cur_range(),
            )
            .with_hint(format!("Remove {}", p.cur_text()))
        }
    }
}

/// Creates a diagnostic saying that the node `name` was expected at range
pub fn expected_node(name: &str, range: TextRange, p: &impl Parser) -> ParseDiagnostic {
    ParseDiagnostic::new_single_node(name, range, p)
}

/// Creates a diagnostic saying that any of the nodes in `names` was expected at range
pub fn expected_any(names: &[&str], range: TextRange, p: &impl Parser) -> ParseDiagnostic {
    ParseDiagnostic::new_with_any(names, range, p)
}

/// Creates a diagnostic with message "Unexpected value." and then it lists the values that should be expected.
pub fn expect_one_of(names: &[&str], range: TextRange) -> ParseDiagnostic {
    ParseDiagnostic::new("Unexpected value or character.", range)
        .with_alternatives("Expected one of:", names)
}

fn article_for(name: &str) -> &'static str {
    match name.chars().next() {
        Some('a' | 'e' | 'i' | 'o' | 'u') => "an",
        _ => "a",
    }
}

/// Merges two lists of parser diagnostics. Only keeps the error from the first collection if two start at the same range.
///
/// The two lists must be so sorted by their source range in increasing order.
pub fn merge_diagnostics(
    first: Vec<ParseDiagnostic>,
    second: Vec<ParseDiagnostic>,
) -> Vec<ParseDiagnostic> {
    if first.is_empty() {
        return second;
    }

    if second.is_empty() {
        return first;
    }

    let mut merged = Vec::new();

    let mut first_iter = first.into_iter();
    let mut second_iter = second.into_iter();

    let mut current_first: Option<ParseDiagnostic> = first_iter.next();
    let mut current_second: Option<ParseDiagnostic> = second_iter.next();

    loop {
        match (current_first, current_second) {
            (Some(first_item), Some(second_item)) => {
                let (first, second) = match (
                    first_item.diagnostic_range(),
                    second_item.diagnostic_range(),
                ) {
                    (Some(first_range), Some(second_range)) => {
                        match first_range.start().cmp(&second_range.start()) {
                            Ordering::Less => {
                                merged.push(first_item);
                                (first_iter.next(), Some(second_item))
                            }
                            Ordering::Equal => {
                                // Only keep one error, skip the one from the second list.
                                (Some(first_item), second_iter.next())
                            }
                            Ordering::Greater => {
                                merged.push(second_item);
                                (Some(first_item), second_iter.next())
                            }
                        }
                    }
                    (Some(_), None) => {
                        merged.push(second_item);
                        (Some(first_item), second_iter.next())
                    }
                    (None, Some(_)) => {
                        merged.push(first_item);
                        (first_iter.next(), Some(second_item))
                    }
                    (None, None) => {
                        merged.push(first_item);
                        merged.push(second_item);

                        (first_iter.next(), second_iter.next())
                    }
                };

                current_first = first;
                current_second = second;
            }

            (None, None) => return merged,
            (Some(first_item), None) => {
                merged.push(first_item);
                merged.extend(first_iter);
                return merged;
            }
            (None, Some(second_item)) => {
                merged.push(second_item);
                merged.extend(second_iter);
                return merged;
            }
        }
    }
}