logic_parser/lexing/
token.rs

1/// Enum representing all the tokens the Lexer can produce
2#[derive(Debug, PartialEq, Clone)]
3pub enum TokenKind {
4    Identifier(String),
5    Literal(bool),
6    Not,
7    And,
8    Or,
9    Implies,
10    IfAndOnlyIf,
11    OpenParen,
12    CloseParen
13}
14
15/// An [`Span`] represents a range of characters in the source code
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct Span {
18    pub start: usize,
19    pub end: usize
20}
21
22/// Represents the tokens that the [`Lexer`] will produce and the [`Parser`]
23/// will consume
24#[derive(Debug, Clone)]
25pub struct Token {
26    pub kind: TokenKind,
27    pub span: Span
28}
29
30impl Token {
31    pub fn new<T>(kind: TokenKind, span: T) -> Token
32    where T: Into<Span> {
33        Token { kind, span: span.into() }
34    }
35}
36
37// -> Other traits implementations
38
39impl std::fmt::Display for TokenKind {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            TokenKind::Identifier(i) => write!(f, "Identifier({})", i),
43            TokenKind::Literal(l) => write!(f, "Literal({})", l),
44            TokenKind::Not => write!(f, "Not()"),
45            TokenKind::And => write!(f, "And()"),
46            TokenKind::Or => write!(f, "Or()"),
47            TokenKind::Implies => write!(f, "Implies()"),
48            TokenKind::IfAndOnlyIf => write!(f, "IfAndOnlyIf()"),
49            TokenKind::OpenParen => write!(f, "OpenParen()"),
50            TokenKind::CloseParen => write!(f, "CloseParen()"),
51        }
52    }
53}
54
55impl From<(usize, usize)> for Span {
56    fn from(value: (usize, usize)) -> Self {
57        Span { start: value.0, end: value.1 }
58    }
59}