Skip to main content

oak_prolog/parser/
element_type.rs

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