Skip to main content

oak_gsgl/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2use std::fmt;
3
4/// Element types for the GSGL (Game Shader Graphics Language) language.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum GsglElementType {
8    /// Root node of the AST.
9    Root,
10    /// A source file.
11    SourceFile,
12    /// A function definition.
13    FunctionDefinition,
14    /// A variable declaration.
15    VariableDeclaration,
16    /// A struct definition.
17    StructDefinition,
18    /// A block of code.
19    Block,
20    /// An `if` statement.
21    IfStatement,
22    /// A `for` statement.
23    ForStatement,
24    /// A `while` statement.
25    WhileStatement,
26    /// A `return` statement.
27    ReturnStatement,
28
29    /// `shader` keyword.
30    Shader,
31    /// `vertex` keyword.
32    Vertex,
33    /// `fragment` keyword.
34    Fragment,
35    /// `geometry` keyword.
36    Geometry,
37    /// `compute` keyword.
38    Compute,
39    /// `uniform` keyword.
40    Uniform,
41    /// `attribute` keyword.
42    Attribute,
43    /// `varying` keyword.
44    Varying,
45    /// `in` keyword.
46    In,
47    /// `out` keyword.
48    Out,
49    /// `inout` keyword.
50    Inout,
51    /// `const` keyword.
52    Const,
53    /// `struct` keyword.
54    Struct,
55    /// `if` keyword.
56    If,
57    /// `else` keyword.
58    Else,
59    /// `for` keyword.
60    For,
61    /// `while` keyword.
62    While,
63    /// `do` keyword.
64    Do,
65    /// `break` keyword.
66    Break,
67    /// `continue` keyword.
68    Continue,
69    /// `return` keyword.
70    Return,
71    /// `discard` keyword.
72    Discard,
73    /// `true` keyword.
74    True,
75    /// `false` keyword.
76    False,
77
78    /// `float` type.
79    Float,
80    /// `int` type.
81    Int,
82    /// `bool` type.
83    Bool,
84    /// `vec2` type.
85    Vec2,
86    /// `vec3` type.
87    Vec3,
88    /// `vec4` type.
89    Vec4,
90    /// `mat2` type.
91    Mat2,
92    /// `mat3` type.
93    Mat3,
94    /// `mat4` type.
95    Mat4,
96    /// `sampler2D` type.
97    Sampler2D,
98    /// `samplerCube` type.
99    SamplerCube,
100    /// `void` type.
101    Void,
102
103    /// An identifier.
104    Identifier,
105    /// A number literal.
106    Number,
107    /// A string literal.
108    String,
109
110    /// `+`.
111    Plus,
112    /// `-`.
113    Minus,
114    /// `*`.
115    Star,
116    /// `/`.
117    Slash,
118    /// `%`.
119    Percent,
120    /// `=`.
121    Assign,
122    /// `+=`.
123    PlusAssign,
124    /// `-=`.
125    MinusAssign,
126    /// `*=`.
127    StarAssign,
128    /// `/=`.
129    SlashAssign,
130
131    /// `==`.
132    Eq,
133    /// `!=`.
134    Ne,
135    /// `<`.
136    Lt,
137    /// `<=`.
138    Le,
139    /// `>`.
140    Gt,
141    /// `>=`.
142    Ge,
143
144    /// `&&`.
145    And,
146    /// `||`.
147    Or,
148    /// `!`.
149    Not,
150
151    /// `&`.
152    BitAnd,
153    /// `|`.
154    BitOr,
155    /// `^`.
156    BitXor,
157    /// `~`.
158    BitNot,
159    /// `<<`.
160    LeftShift,
161    /// `>>`.
162    RightShift,
163
164    /// `(`.
165    LeftParen,
166    /// `)`.
167    RightParen,
168    /// `{`.
169    LeftBrace,
170    /// `}`.
171    RightBrace,
172    /// `[`.
173    LeftBracket,
174    /// `]`.
175    RightBracket,
176    /// `;`.
177    Semicolon,
178    /// `,`.
179    Comma,
180    /// `.`.
181    Dot,
182    /// `:`.
183    Colon,
184    /// `?`.
185    Question,
186    /// `#`.
187    Hash,
188    /// `@`.
189    At,
190
191    /// Preprocessor directive.
192    Preprocessor,
193    /// `#include`.
194    Include,
195    /// `#define`.
196    Define,
197    /// `#ifdef`.
198    Ifdef,
199    /// `#ifndef`.
200    Ifndef,
201    /// `#endif`.
202    Endif,
203    /// `#version`.
204    Version,
205
206    /// `sin` function.
207    Sin,
208    /// `cos` function.
209    Cos,
210    /// `tan` function.
211    Tan,
212    /// `sqrt` function.
213    Sqrt,
214    /// `pow` function.
215    Pow,
216    /// `exp` function.
217    Exp,
218    /// `log` function.
219    Log,
220    /// `abs` function.
221    Abs,
222    /// `sign` function.
223    Sign,
224    /// `floor` function.
225    Floor,
226    /// `ceil` function.
227    Ceil,
228    /// `fract` function.
229    Fract,
230    /// `mod` function.
231    Mod,
232    /// `min` function.
233    Min,
234    /// `max` function.
235    Max,
236    /// `clamp` function.
237    Clamp,
238    /// `mix` function.
239    Mix,
240    /// `step` function.
241    Step,
242    /// `smoothstep` function.
243    Smoothstep,
244    /// `length` function.
245    Length,
246    /// `distance` function.
247    Distance,
248    /// `dot` function.
249    DotProduct,
250    /// `cross` function.
251    Cross,
252    /// `normalize` function.
253    Normalize,
254    /// `faceforward` function.
255    Faceforward,
256    /// `reflect` function.
257    Reflect,
258    /// `refract` function.
259    Refract,
260
261    /// Whitespace.
262    Whitespace,
263    /// A comment.
264    Comment,
265    /// A newline.
266    Newline,
267    /// End of stream.
268    Eof,
269    /// An error token.
270    Error,
271
272    /// A function declaration.
273    FunctionDecl,
274    /// A variable declaration.
275    VariableDecl,
276    /// A struct declaration.
277    StructDecl,
278    /// A statement.
279    Statement,
280    /// An expression.
281    Expression,
282    /// A parameter.
283    Parameter,
284    /// An argument.
285    Argument,
286    /// A field access.
287    FieldAccess,
288    /// An array access.
289    ArrayAccess,
290    /// A function call.
291    FunctionCall,
292    /// A binary expression.
293    BinaryExpr,
294    /// A unary expression.
295    UnaryExpr,
296    /// An assignment expression.
297    AssignmentExpr,
298    /// A conditional expression.
299    ConditionalExpr,
300    /// A literal.
301    Literal,
302}
303
304impl GsglElementType {
305    /// Checks if the kind is a keyword.
306    pub fn is_keyword(self) -> bool {
307        matches!(
308            self,
309            Self::Shader
310                | Self::Vertex
311                | Self::Fragment
312                | Self::Geometry
313                | Self::Compute
314                | Self::Uniform
315                | Self::Attribute
316                | Self::Varying
317                | Self::In
318                | Self::Out
319                | Self::Inout
320                | Self::Const
321                | Self::Struct
322                | Self::If
323                | Self::Else
324                | Self::For
325                | Self::While
326                | Self::Do
327                | Self::Break
328                | Self::Continue
329                | Self::Return
330                | Self::Discard
331                | Self::True
332                | Self::False
333        )
334    }
335
336    /// Checks if the kind is a data type.
337    pub fn is_type(self) -> bool {
338        matches!(self, Self::Float | Self::Int | Self::Bool | Self::Vec2 | Self::Vec3 | Self::Vec4 | Self::Mat2 | Self::Mat3 | Self::Mat4 | Self::Sampler2D | Self::SamplerCube | Self::Void)
339    }
340
341    /// Checks if the kind is an operator.
342    pub fn is_operator(self) -> bool {
343        matches!(
344            self,
345            Self::Plus
346                | Self::Minus
347                | Self::Star
348                | Self::Slash
349                | Self::Percent
350                | Self::Assign
351                | Self::PlusAssign
352                | Self::MinusAssign
353                | Self::StarAssign
354                | Self::SlashAssign
355                | Self::Eq
356                | Self::Ne
357                | Self::Lt
358                | Self::Le
359                | Self::Gt
360                | Self::Ge
361                | Self::And
362                | Self::Or
363                | Self::Not
364                | Self::BitAnd
365                | Self::BitOr
366                | Self::BitXor
367                | Self::BitNot
368                | Self::LeftShift
369                | Self::RightShift
370        )
371    }
372}
373
374impl fmt::Display for GsglElementType {
375    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
376        let name = match self {
377            Self::Root => "ROOT",
378            Self::SourceFile => "SOURCE_FILE",
379            Self::Shader => "shader",
380            Self::Vertex => "vertex",
381            Self::Fragment => "fragment",
382            Self::Geometry => "geometry",
383            Self::Compute => "compute",
384            Self::Uniform => "uniform",
385            Self::Attribute => "attribute",
386            Self::Varying => "varying",
387            Self::In => "in",
388            Self::Out => "out",
389            Self::Inout => "inout",
390            Self::Const => "const",
391            Self::Struct => "struct",
392            Self::If => "if",
393            Self::Else => "else",
394            Self::For => "for",
395            Self::While => "while",
396            Self::Do => "do",
397            Self::Break => "break",
398            Self::Continue => "continue",
399            Self::Return => "return",
400            Self::Discard => "discard",
401            Self::True => "true",
402            Self::False => "false",
403            Self::Float => "float",
404            Self::Int => "int",
405            Self::Bool => "bool",
406            Self::Vec2 => "vec2",
407            Self::Vec3 => "vec3",
408            Self::Vec4 => "vec4",
409            Self::Mat2 => "mat2",
410            Self::Mat3 => "mat3",
411            Self::Mat4 => "mat4",
412            Self::Sampler2D => "sampler2D",
413            Self::SamplerCube => "samplerCube",
414            Self::Void => "void",
415            Self::Identifier => "IDENTIFIER",
416            Self::Number => "NUMBER",
417            Self::String => "STRING",
418            Self::Plus => "+",
419            Self::Minus => "-",
420            Self::Star => "*",
421            Self::Slash => "/",
422            Self::Percent => "%",
423            Self::Assign => "=",
424            Self::PlusAssign => "+=",
425            Self::MinusAssign => "-=",
426            Self::StarAssign => "*=",
427            Self::SlashAssign => "/=",
428            Self::Eq => "==",
429            Self::Ne => "!=",
430            Self::Lt => "<",
431            Self::Le => "<=",
432            Self::Gt => ">",
433            Self::Ge => ">=",
434            Self::And => "&&",
435            Self::Or => "||",
436            Self::Not => "!",
437            Self::BitAnd => "&",
438            Self::BitOr => "|",
439            Self::BitXor => "^",
440            Self::BitNot => "~",
441            Self::LeftShift => "<<",
442            Self::RightShift => ">>",
443            Self::LeftParen => "(",
444            Self::RightParen => ")",
445            Self::LeftBrace => "{",
446            Self::RightBrace => "}",
447            Self::LeftBracket => "[",
448            Self::RightBracket => "]",
449            Self::Semicolon => ";",
450            Self::Comma => ",",
451            Self::Dot => ".",
452            Self::Colon => ":",
453            Self::Question => "?",
454            Self::Hash => "#",
455            Self::At => "@",
456            Self::Preprocessor => "PREPROCESSOR",
457            Self::Include => "#include",
458            Self::Define => "#define",
459            Self::Ifdef => "#ifdef",
460            Self::Ifndef => "#ifndef",
461            Self::Endif => "#endif",
462            Self::Version => "#version",
463            Self::Sin => "sin",
464            Self::Cos => "cos",
465            Self::Tan => "tan",
466            Self::Sqrt => "sqrt",
467            Self::Pow => "pow",
468            Self::Abs => "abs",
469            Self::Min => "min",
470            Self::Max => "max",
471            Self::Clamp => "clamp",
472            Self::Mix => "mix",
473            Self::Step => "step",
474            Self::Smoothstep => "smoothstep",
475            Self::Length => "length",
476            Self::Distance => "distance",
477            Self::DotProduct => "dot",
478            Self::Whitespace => "WHITESPACE",
479            Self::Newline => "NEWLINE",
480            Self::Comment => "COMMENT",
481            Self::Error => "ERROR",
482            Self::Eof => "EOF",
483            Self::FunctionDecl => "FUNCTION_DECL",
484            Self::VariableDecl => "VARIABLE_DECL",
485            Self::StructDecl => "STRUCT_DECL",
486            Self::Statement => "STATEMENT",
487            Self::Expression => "EXPRESSION",
488            Self::Parameter => "PARAMETER",
489            Self::Argument => "ARGUMENT",
490            Self::FieldAccess => "FIELD_ACCESS",
491            Self::ArrayAccess => "ARRAY_ACCESS",
492            Self::FunctionCall => "FUNCTION_CALL",
493            Self::BinaryExpr => "BINARY_EXPR",
494            Self::UnaryExpr => "UNARY_EXPR",
495            Self::AssignmentExpr => "ASSIGNMENT_EXPR",
496            Self::ConditionalExpr => "CONDITIONAL_EXPR",
497            Self::Literal => "LITERAL",
498            _ => "UNKNOWN",
499        };
500        write!(f, "{}", name)
501    }
502}
503
504impl ElementType for GsglElementType {
505    type Role = UniversalElementRole;
506
507    fn role(&self) -> Self::Role {
508        match self {
509            Self::Root | Self::SourceFile => UniversalElementRole::Root,
510            Self::FunctionDefinition | Self::FunctionDecl => UniversalElementRole::Definition,
511            Self::VariableDeclaration | Self::VariableDecl => UniversalElementRole::Definition,
512            Self::StructDefinition | Self::StructDecl => UniversalElementRole::Typing,
513            Self::Block => UniversalElementRole::Container,
514            Self::IfStatement => UniversalElementRole::Statement,
515            Self::ForStatement | Self::WhileStatement => UniversalElementRole::Statement,
516            Self::ReturnStatement => UniversalElementRole::Statement,
517            Self::Expression | Self::BinaryExpr | Self::UnaryExpr | Self::AssignmentExpr | Self::ConditionalExpr => UniversalElementRole::Expression,
518            Self::Statement => UniversalElementRole::Statement,
519            _ => UniversalElementRole::None,
520        }
521    }
522}
523
524impl From<crate::lexer::token_type::GsglTokenType> for GsglElementType {
525    fn from(token: crate::lexer::token_type::GsglTokenType) -> Self {
526        use crate::lexer::token_type::GsglTokenType as T;
527        match token {
528            T::Root => Self::Root,
529            T::SourceFile => Self::SourceFile,
530            T::FunctionDefinition => Self::FunctionDefinition,
531            T::VariableDeclaration => Self::VariableDeclaration,
532            T::StructDefinition => Self::StructDefinition,
533            T::Block => Self::Block,
534            T::IfStatement => Self::IfStatement,
535            T::ForStatement => Self::ForStatement,
536            T::WhileStatement => Self::WhileStatement,
537            T::ReturnStatement => Self::ReturnStatement,
538            T::Shader => Self::Shader,
539            T::Vertex => Self::Vertex,
540            T::Fragment => Self::Fragment,
541            T::Geometry => Self::Geometry,
542            T::Compute => Self::Compute,
543            T::Uniform => Self::Uniform,
544            T::Attribute => Self::Attribute,
545            T::Varying => Self::Varying,
546            T::In => Self::In,
547            T::Out => Self::Out,
548            T::Inout => Self::Inout,
549            T::Const => Self::Const,
550            T::Struct => Self::Struct,
551            T::If => Self::If,
552            T::Else => Self::Else,
553            T::For => Self::For,
554            T::While => Self::While,
555            T::Do => Self::Do,
556            T::Break => Self::Break,
557            T::Continue => Self::Continue,
558            T::Return => Self::Return,
559            T::Discard => Self::Discard,
560            T::True => Self::True,
561            T::False => Self::False,
562            T::Float => Self::Float,
563            T::Int => Self::Int,
564            T::Bool => Self::Bool,
565            T::Vec2 => Self::Vec2,
566            T::Vec3 => Self::Vec3,
567            T::Vec4 => Self::Vec4,
568            T::Mat2 => Self::Mat2,
569            T::Mat3 => Self::Mat3,
570            T::Mat4 => Self::Mat4,
571            T::Sampler2D => Self::Sampler2D,
572            T::SamplerCube => Self::SamplerCube,
573            T::Void => Self::Void,
574            T::Identifier => Self::Identifier,
575            T::Number => Self::Number,
576            T::String => Self::String,
577            T::Plus => Self::Plus,
578            T::Minus => Self::Minus,
579            T::Star => Self::Star,
580            T::Slash => Self::Slash,
581            T::Percent => Self::Percent,
582            T::Assign => Self::Assign,
583            T::PlusAssign => Self::PlusAssign,
584            T::MinusAssign => Self::MinusAssign,
585            T::StarAssign => Self::StarAssign,
586            T::SlashAssign => Self::SlashAssign,
587            T::Eq => Self::Eq,
588            T::Ne => Self::Ne,
589            T::Lt => Self::Lt,
590            T::Le => Self::Le,
591            T::Gt => Self::Gt,
592            T::Ge => Self::Ge,
593            T::And => Self::And,
594            T::Or => Self::Or,
595            T::Not => Self::Not,
596            T::BitAnd => Self::BitAnd,
597            T::BitOr => Self::BitOr,
598            T::BitXor => Self::BitXor,
599            T::BitNot => Self::BitNot,
600            T::LeftShift => Self::LeftShift,
601            T::RightShift => Self::RightShift,
602            T::LeftParen => Self::LeftParen,
603            T::RightParen => Self::RightParen,
604            T::LeftBrace => Self::LeftBrace,
605            T::RightBrace => Self::RightBrace,
606            T::LeftBracket => Self::LeftBracket,
607            T::RightBracket => Self::RightBracket,
608            T::Semicolon => Self::Semicolon,
609            T::Comma => Self::Comma,
610            T::Dot => Self::Dot,
611            T::Colon => Self::Colon,
612            T::Question => Self::Question,
613            T::Hash => Self::Hash,
614            T::At => Self::At,
615            T::Preprocessor => Self::Preprocessor,
616            T::Include => Self::Include,
617            T::Define => Self::Define,
618            T::Ifdef => Self::Ifdef,
619            T::Ifndef => Self::Ifndef,
620            T::Endif => Self::Endif,
621            T::Version => Self::Version,
622            T::Sin => Self::Sin,
623            T::Cos => Self::Cos,
624            T::Tan => Self::Tan,
625            T::Sqrt => Self::Sqrt,
626            T::Pow => Self::Pow,
627            T::Exp => Self::Exp,
628            T::Log => Self::Log,
629            T::Abs => Self::Abs,
630            T::Sign => Self::Sign,
631            T::Floor => Self::Floor,
632            T::Ceil => Self::Ceil,
633            T::Fract => Self::Fract,
634            T::Mod => Self::Mod,
635            T::Min => Self::Min,
636            T::Max => Self::Max,
637            T::Clamp => Self::Clamp,
638            T::Mix => Self::Mix,
639            T::Step => Self::Step,
640            T::Smoothstep => Self::Smoothstep,
641            T::Length => Self::Length,
642            T::Distance => Self::Distance,
643            T::DotProduct => Self::DotProduct,
644            T::Cross => Self::Cross,
645            T::Normalize => Self::Normalize,
646            T::Faceforward => Self::Faceforward,
647            T::Reflect => Self::Reflect,
648            T::Refract => Self::Refract,
649            T::Whitespace => Self::Whitespace,
650            T::Comment => Self::Comment,
651            T::Newline => Self::Newline,
652            T::Eof => Self::Eof,
653            T::Error => Self::Error,
654        }
655    }
656}