Skip to main content

oak_prolog/kind/
mod.rs

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