celestial_hub_compass/lexer/traits/
display.rs

1use crate::lexer::{self, tokens::Token, Lexer, LexicalError};
2
3impl std::fmt::Display for LexicalError {
4  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
5    match self {
6      LexicalError::InvalidToken => write!(f, "Invalid token"),
7      LexicalError::WrongType { error, help } => {
8        for lexer::ErrorTip { message, location } in error {
9          writeln!(f, "error: {message:?} at {location:?}")?;
10        }
11        match help {
12          Some(help) => writeln!(f, "help: {help:?}"),
13          None => Ok(()),
14        }
15      }
16      LexicalError::UnknownVariable { error, help } => {
17        for lexer::ErrorTip { message, location } in error {
18          writeln!(f, "error: {message:?} at {location:?}")?;
19        }
20        match help {
21          Some(help) => writeln!(f, "help: {help:?}"),
22          None => Ok(()),
23        }
24      }
25      LexicalError::UnknownFunction { error, help } => {
26        for lexer::ErrorTip { message, location } in error {
27          writeln!(f, "error: {message:?} at {location:?}")?;
28        }
29        match help {
30          Some(help) => writeln!(f, "help: {help:?}"),
31          None => Ok(()),
32        }
33      }
34      LexicalError::WrongArgumentCount { error, help } => {
35        for lexer::ErrorTip { message, location } in error {
36          writeln!(f, "error: {message:?} at {location:?}")?;
37        }
38        match help {
39          Some(help) => writeln!(f, "help: {help:?}"),
40          None => Ok(()),
41        }
42      }
43      LexicalError::FunctionIsBuiltin { error, help } => {
44        for lexer::ErrorTip { message, location } in error {
45          writeln!(f, "error: {message:?} at {location:?}")?;
46        }
47        match help {
48          Some(help) => writeln!(f, "help: {help:?}"),
49          None => Ok(()),
50        }
51      }
52      LexicalError::UnusedValue { error, help } => {
53        for lexer::ErrorTip { message, location } in error {
54          writeln!(f, "error: {message:?} at {location:?}")?;
55        }
56        match help {
57          Some(help) => writeln!(f, "help: {help:?}"),
58          None => Ok(()),
59        }
60      }
61    }
62  }
63}
64
65impl<'input> std::fmt::Display for Lexer<'input> {
66  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
67    for (token, span) in self.token_stream.clone().spanned() {
68      let token = token.unwrap();
69      writeln!(f, "{{ {:?} {:?} }}", token, span)?;
70    }
71
72    Ok(())
73  }
74}
75
76impl std::fmt::Display for Token {
77  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
78    write!(f, "{:?}", self)
79  }
80}