Skip to main content

oak_prolog/lexer/
token_type.rs

1use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type PrologToken = Token<PrologTokenType>;
6
7impl PrologTokenType {
8    pub fn is_token(&self) -> bool {
9        !self.is_element()
10    }
11
12    pub fn is_element(&self) -> bool {
13        matches!(self, Self::Root | Self::Functor | Self::Clause | Self::Rule | Self::Fact | Self::Query | Self::Directive | Self::List | Self::Structure)
14    }
15}
16
17impl TokenType for PrologTokenType {
18    type Role = UniversalTokenRole;
19    const END_OF_STREAM: Self = Self::Error;
20
21    fn is_ignored(&self) -> bool {
22        false
23    }
24
25    fn role(&self) -> Self::Role {
26        match self {
27            _ => UniversalTokenRole::None,
28        }
29    }
30}
31
32#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34pub enum PrologTokenType {
35    // Whitespace and comments
36    Whitespace,
37    Newline,
38    Comment,
39
40    // Literals
41    Atom,
42    Integer,
43    Float,
44    String,
45    Variable,
46
47    // Operators
48    Unify,         // =
49    NotUnify,      // \=
50    Equal,         // ==
51    NotEqual,      // \==
52    ArithEqual,    // =:=
53    ArithNotEqual, // =\=
54    Less,          // <
55    Greater,       // >
56    LessEqual,     // =<
57    GreaterEqual,  // >=
58    Is,            // is
59    Plus,          // +
60    Minus,         // -
61    Multiply,      // *
62    Divide,        // /
63    IntDivide,     // //
64    Modulo,        // mod
65    Power,         // **
66    BitwiseAnd,    // /\
67    BitwiseOr,     // \/
68    BitwiseXor,    // xor
69    BitwiseNot,    // \
70    LeftShift,     // <<
71    RightShift,    // >>
72
73    // Punctuation
74    LeftParen,     // (
75    RightParen,    // )
76    LeftBracket,   // [
77    RightBracket,  // ]
78    LeftBrace,     // {
79    RightBrace,    // }
80    Comma,         // ,
81    Dot,           // .
82    Pipe,          // |
83    Semicolon,     // ;
84    Cut,           // !
85    Question,      // ?
86    Colon,         // :
87    ColonMinus,    // :-
88    QuestionMinus, // ?-
89
90    // Special constructs
91    Functor,
92    Clause,
93    Rule,
94    Fact,
95    Query,
96    Directive,
97    List,
98    Structure,
99
100    // Special
101    Root,
102    Error,
103    Eof,
104}