Skip to main content

oak_j/parser/
element_type.rs

1use crate::lexer::JTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use std::sync::Arc;
4
5/// Type alias for J syntax tree elements.
6pub type JElement<'a> = Arc<GreenNode<'a, JElementType>>;
7
8/// All possible element types in the J syntax tree.
9#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum JElementType {
12    /// Root node
13    Root,
14    /// Compilation unit
15    CompilationUnit,
16
17    /// Sentence
18    Sentence,
19
20    /// Assignment
21    Assignment,
22
23    /// Expression
24    Expression,
25
26    /// Verb
27    Verb,
28
29    /// Noun
30    Noun,
31
32    /// Adverb
33    Adverb,
34
35    /// Conjunction
36    Conjunction,
37
38    /// Parenthesized expression
39    Group,
40}
41
42impl From<JTokenType> for JElementType {
43    fn from(token: JTokenType) -> Self {
44        match token {
45            JTokenType::Identifier => Self::Noun,
46            JTokenType::NumberLiteral => Self::Noun,
47            JTokenType::StringLiteral => Self::Noun,
48            JTokenType::IsGlobal | JTokenType::IsLocal => Self::Assignment,
49            _ => Self::Expression,
50        }
51    }
52}
53
54impl ElementType for JElementType {
55    type Role = UniversalElementRole;
56
57    fn role(&self) -> Self::Role {
58        match self {
59            Self::Root | Self::CompilationUnit => UniversalElementRole::Root,
60            Self::Sentence => UniversalElementRole::Statement,
61            Self::Assignment => UniversalElementRole::Binding,
62            Self::Expression => UniversalElementRole::Expression,
63            _ => UniversalElementRole::None,
64        }
65    }
66}