oak_prolog/parser/element_type.rs
1use oak_core::{ElementType, UniversalElementRole};
2
3/// Prolog element types.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum PrologElementType {
7 // Whitespace and comments
8 /// Whitespace.
9 Whitespace,
10 /// Newline.
11 Newline,
12 /// Comment.
13 Comment,
14
15 // Literals
16 /// Atom.
17 Atom,
18 /// Integer.
19 Integer,
20 /// Float.
21 Float,
22 /// String.
23 String,
24 /// Variable.
25 Variable,
26
27 // Operators
28 /// Unify `=`.
29 Unify, // =
30 /// Not unify `\=`.
31 NotUnify, // \=
32 /// Equal `==`.
33 Equal, // ==
34 /// Not equal `\==`.
35 NotEqual, // \==
36 /// Arithmetic equal `=:=`.
37 ArithEqual, // =:=
38 /// Arithmetic not equal `=\=`.
39 ArithNotEqual, // =\=
40 /// Less than `<`.
41 Less, // <
42 /// Greater than `>`.
43 Greater, // >
44 /// Less than or equal `=<`.
45 LessEqual, // =<
46 /// Greater than or equal `>=`.
47 GreaterEqual, // >=
48 /// Is `is`.
49 Is, // is
50 /// Plus `+`.
51 Plus, // +
52 /// Minus `-`.
53 Minus, // -
54 /// Multiply `*`.
55 Multiply, // *
56 /// Divide `/`.
57 Divide, // /
58 /// Integer divide `//`.
59 IntDivide, // //
60 /// Modulo `mod`.
61 Modulo, // mod
62 /// Power `**`.
63 Power, // **
64 /// Bitwise AND `/\`.
65 BitwiseAnd, // /\
66 /// Bitwise OR `\/`.
67 BitwiseOr, // \/
68 /// Bitwise XOR `xor`.
69 BitwiseXor, // xor
70 /// Bitwise NOT `\`.
71 BitwiseNot, // \
72 /// Left shift `<<`.
73 LeftShift, // <<
74 /// Right shift `>>`.
75 RightShift, // >>
76
77 // Punctuation
78 /// Left parenthesis `(`.
79 LeftParen, // (
80 /// Right parenthesis `)`.
81 RightParen, // )
82 /// Left bracket `[`.
83 LeftBracket, // [
84 /// Right bracket `]`.
85 RightBracket, // ]
86 /// Left brace `{`.
87 LeftBrace, // {
88 /// Right brace `}`.
89 RightBrace, // }
90 /// Comma `,`.
91 Comma, // ,
92 /// Dot `.`.
93 Dot, // .
94 /// Pipe `|`.
95 Pipe, // |
96 /// Semicolon `;`.
97 Semicolon, // ;
98 /// Cut `!`.
99 Cut, // !
100 /// Question mark `?`.
101 Question, // ?
102 /// Colon `:`.
103 Colon, // :
104 /// Colon minus `:-`.
105 ColonMinus, // :-
106 /// Question minus `?-`.
107 QuestionMinus, // ?-
108
109 // Special constructs
110 /// Functor.
111 Functor,
112 /// Clause.
113 Clause,
114 /// Rule.
115 Rule,
116 /// Fact.
117 Fact,
118 /// Query.
119 Query,
120 /// Directive.
121 Directive,
122 /// List.
123 List,
124 /// Structure.
125 Structure,
126
127 // Special
128 /// Root.
129 Root,
130 /// Error.
131 Error,
132 /// End of file.
133 Eof,
134}
135
136impl PrologElementType {
137 /// Checks if this type is a token.
138 pub fn is_token(&self) -> bool {
139 !self.is_element()
140 }
141
142 /// Checks if this type is a structural element.
143 pub fn is_element(&self) -> bool {
144 matches!(self, Self::Root | Self::Functor | Self::Clause | Self::Rule | Self::Fact | Self::Query | Self::Directive | Self::List | Self::Structure)
145 }
146}
147
148impl ElementType for PrologElementType {
149 type Role = UniversalElementRole;
150
151 fn role(&self) -> Self::Role {
152 match self {
153 _ => UniversalElementRole::None,
154 }
155 }
156}
157
158impl From<crate::lexer::token_type::PrologTokenType> for PrologElementType {
159 fn from(token: crate::lexer::token_type::PrologTokenType) -> Self {
160 unsafe { std::mem::transmute(token) }
161 }
162}