Skip to main content

oak_r/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))]
7#[repr(u8)]
8pub enum RElementType {
9    // Whitespace and newlines
10    Whitespace,
11    Newline,
12
13    // Comments
14    Comment,
15
16    // Literals
17    StringLiteral,
18    IntegerLiteral,
19    FloatLiteral,
20    BooleanLiteral,
21    NullLiteral,
22    Inf,
23    NaN,
24    NA,
25    NaInteger,
26    NaReal,
27    NaComplex,
28    NaCharacter,
29
30    // Identifiers
31    Identifier,
32
33    // Keywords
34    If,
35    Else,
36    For,
37    In,
38    While,
39    Repeat,
40    Next,
41    Break,
42    Function,
43    Return,
44    True,
45    False,
46    Null,
47
48    // Operators
49    Plus,
50    Minus,
51    Star,
52    Slash,
53    Percent,
54    Caret,
55    Equal,
56    EqualEqual,
57    NotEqual,
58    Less,
59    Greater,
60    LessEqual,
61    GreaterEqual,
62    And,
63    Or,
64    Not,
65    AndAnd,
66    OrOr,
67    Tilde,
68    LeftArrow,
69    RightArrow,
70    DoubleLeftArrow,
71    DoubleRightArrow,
72    Pipe,
73    Operator,
74
75    // Punctuation
76    LeftParen,
77    RightParen,
78    LeftBracket,
79    RightBracket,
80    LeftBrace,
81    RightBrace,
82    Comma,
83    Semicolon,
84    Colon,
85    DoubleColon,
86    TripleColon,
87    Dot,
88    Dollar,
89    At,
90    Question,
91
92    // Root node
93    Root,
94
95    // Expressions
96    Assignment,
97    BinaryExpression,
98    UnaryExpression,
99    LiteralExpression,
100    IdentifierExpression,
101    CallExpression,
102    GroupingExpression,
103    BlockExpression,
104    IfExpression,
105    WhileExpression,
106    ForExpression,
107    RepeatExpression,
108    FunctionDefinition,
109    IndexExpression,
110    MemberExpression,
111    ArgumentList,
112    ParameterList,
113
114    // 错误和结束
115    Error,
116    Eof,
117}
118
119impl RElementType {
120    pub fn is_trivia(self) -> bool {
121        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
122    }
123}
124
125impl ElementType for RElementType {
126    type Role = UniversalElementRole;
127
128    fn role(&self) -> Self::Role {
129        match self {
130            _ => UniversalElementRole::None,
131        }
132    }
133}
134
135impl From<crate::lexer::token_type::RTokenType> for RElementType {
136    fn from(token: crate::lexer::token_type::RTokenType) -> Self {
137        unsafe { std::mem::transmute(token) }
138    }
139}