squiid_parser/
tokens.rs

1use logos::Logos;
2
3#[derive(Logos, Copy, Clone, Hash, Debug)]
4#[logos(skip r"[ \t\n\f]+")]
5#[logos(subpattern identifier=r"[_a-zA-Z][_0-9a-zA-Z]*")]
6#[logos(subpattern float=r"[0-9]*\.[0-9]+")]
7pub enum Token<'a> {
8    /// Identifier followed by an opening parenthesis
9    #[regex(r"(?&identifier)\(")]
10    Function(&'a str),
11    #[token(",")]
12    Comma(&'a str),
13
14    /// Identifier
15    #[regex(r"(?&identifier)")]
16    VariableAssign(&'a str),
17    /// $ followed by identifier
18    #[regex(r"\$(?&identifier)")]
19    VariableRecal(&'a str),
20    /// # followed by identifier
21    #[regex(r"#(?&identifier)")]
22    Constant(&'a str),
23
24    /// optional int/float
25    ///
26    /// an e followed by an option + or -
27    ///
28    /// 1 or more digits (the number following the e)
29    ///
30    /// an optional decimal point followed by 1 or more digits (3.1) or (.1)
31    #[regex(r"[0-9]*\.?[0-9]+([eE][-+]?\d+(\.\d+)?)", priority = 3)]
32    ScientificNotation(&'a str),
33    #[regex("(?&float)+", priority = 2)]
34    Float(&'a str),
35    #[regex(r"[0-9]+", priority = 1)]
36    Int(&'a str),
37
38    /// An @ signifies the previous answer
39    #[token("@")]
40    PrevAns(&'a str),
41
42    #[token("(")]
43    LParen(&'a str),
44    #[token(")")]
45    RParen(&'a str),
46    #[token("=")]
47    Equal(&'a str),
48    #[token("^")]
49    Power(&'a str),
50    #[token("*")]
51    Multiply(&'a str),
52    #[token("/")]
53    Divide(&'a str),
54    #[token("%")]
55    Modulo(&'a str),
56    #[token("+")]
57    Add(&'a str),
58    // this can be the unary operator (-3) or the binary operator (3-4)
59    #[token("-")]
60    Subtract(&'a str),
61
62    // Logic Operators
63    #[token(">")]
64    GreaterThan(&'a str),
65    #[token("<")]
66    LessThan(&'a str),
67    #[token(">=")]
68    GreaterThanEqualTo(&'a str),
69    #[token("<=")]
70    LessThanEqualTo(&'a str),
71    #[token("==")]
72    EqualTo(&'a str),
73
74    /// This cannot be a token, it is used for differentiation between minus and negative later on in parsing
75    Negative(&'a str),
76}
77
78/// `PartialEq` implementation that ignores the content of the enum
79impl PartialEq for Token<'_> {
80    fn eq(&self, other: &Self) -> bool {
81        std::mem::discriminant(self) == std::mem::discriminant(other)
82    }
83}