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
use crate::lexer::{self, tokens::Token, Lexer, LexicalError};

impl std::fmt::Display for LexicalError {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    match self {
      LexicalError::InvalidToken => write!(f, "Invalid token"),
      LexicalError::WrongType { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
      LexicalError::UnknownVariable { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
      LexicalError::UnknownFunction { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
      LexicalError::WrongArgumentCount { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
      LexicalError::FunctionIsBuiltin { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
      LexicalError::UnusedValue { error, help } => {
        for lexer::ErrorTip { message, location } in error {
          writeln!(f, "error: {message:?} at {location:?}")?;
        }
        match help {
          Some(help) => writeln!(f, "help: {help:?}"),
          None => Ok(()),
        }
      }
    }
  }
}

impl<'input> std::fmt::Display for Lexer<'input> {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    for (token, span) in self.token_stream.clone().spanned() {
      let token = token.unwrap();
      writeln!(f, "{{ {:?} {:?} }}", token, span)?;
    }

    Ok(())
  }
}

impl std::fmt::Display for Token {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    write!(f, "{:?}", self)
  }
}