litcheck_filecheck/parse/lexer/
token.rs

1use std::{borrow::Cow, fmt};
2
3use crate::ast::{Check, CheckModifier};
4
5use super::LexerError;
6
7/// The token type produced by [Lexer]
8#[derive(Debug, Clone)]
9pub enum Token<'input> {
10    Eof,
11    Lf,
12    Error(LexerError),
13    Check(Check),
14    Modifier(CheckModifier),
15    Comment(Cow<'input, str>),
16    Raw(&'input str),
17    Ident(&'input str),
18    Num(i64),
19    // '[['
20    MatchStart,
21    // ']]'
22    MatchEnd,
23    // '{{'
24    RegexStart,
25    // '}}'
26    RegexEnd,
27    // '{'
28    LBrace,
29    // '}'
30    RBrace,
31    // '('
32    LParen,
33    // ')'
34    RParen,
35    // '#'
36    Hash,
37    // '$'
38    Dollar,
39    // '%'
40    Percent,
41    // '@'
42    At,
43    // ','
44    Comma,
45    // '.'
46    Dot,
47    // ':'
48    Colon,
49    // '+'
50    Plus,
51    // '-'
52    Minus,
53    // '='
54    Equal,
55    // '=='
56    Equals,
57    // LITERAL
58    Literal,
59    // LINE
60    Line,
61    // add
62    Add,
63    // sub
64    Sub,
65    // mul
66    Mul,
67    // div
68    Div,
69    // min
70    Min,
71    // max
72    Max,
73}
74impl<'input> Token<'input> {
75    pub fn from_keyword_or_ident(s: &'input str) -> Self {
76        match s {
77            "LITERAL" => Self::Literal,
78            "LINE" => Self::Line,
79            "add" => Self::Add,
80            "sub" => Self::Sub,
81            "mul" => Self::Mul,
82            "div" => Self::Div,
83            "min" => Self::Min,
84            "max" => Self::Max,
85            _ => Self::Ident(s),
86        }
87    }
88}
89impl<'input> Eq for Token<'input> {}
90impl<'input> PartialEq for Token<'input> {
91    fn eq(&self, other: &Token<'input>) -> bool {
92        match (self, other) {
93            (Self::Error(_), Self::Error(_)) => true,
94            (Self::Check(a), Self::Check(b)) => a == b,
95            (Self::Modifier(a), Self::Modifier(b)) => a == b,
96            (Self::Comment(a), Self::Comment(b)) => a == b,
97            (Self::Raw(a), Self::Raw(b)) => a == b,
98            (Self::Ident(a), Self::Ident(b)) => a == b,
99            (Self::Num(a), Self::Num(b)) => a == b,
100            (a, b) => core::mem::discriminant(a) == core::mem::discriminant(b),
101        }
102    }
103}
104impl<'input> fmt::Display for Token<'input> {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        use std::fmt::Write;
107        match self {
108            Self::Eof => f.write_str("end of file"),
109            Self::Lf => f.write_str("newline"),
110            Self::Error(_) => f.write_str("error"),
111            Self::Check(c) => write!(f, "{c}"),
112            Self::Modifier(c) => write!(f, "{c:?}"),
113            Self::Comment(_) => write!(f, "a comment"),
114            Self::Raw(s) => f.write_str(s),
115            Self::Ident(s) => f.write_str(s),
116            Self::Num(n) => write!(f, "{n}"),
117            Self::MatchStart => f.write_str("[["),
118            Self::MatchEnd => f.write_str("]]"),
119            Self::RegexStart => f.write_str("{{"),
120            Self::RegexEnd => f.write_str("}}"),
121            Self::LBrace => f.write_char('{'),
122            Self::RBrace => f.write_char('}'),
123            Self::LParen => f.write_char('('),
124            Self::RParen => f.write_char(')'),
125            Self::Hash => f.write_char('#'),
126            Self::Dollar => f.write_char('$'),
127            Self::Percent => f.write_char('%'),
128            Self::At => f.write_char('@'),
129            Self::Colon => f.write_char(':'),
130            Self::Comma => f.write_char(','),
131            Self::Dot => f.write_char('.'),
132            Self::Plus => f.write_char('+'),
133            Self::Minus => f.write_char('-'),
134            Self::Equal => f.write_str("="),
135            Self::Equals => f.write_str("=="),
136            Self::Line => f.write_str("LINE"),
137            Self::Literal => f.write_str("LITERAL"),
138            Self::Add => f.write_str("add"),
139            Self::Sub => f.write_str("sub"),
140            Self::Mul => f.write_str("mul"),
141            Self::Div => f.write_str("div"),
142            Self::Min => f.write_str("min"),
143            Self::Max => f.write_str("max"),
144        }
145    }
146}