Implement keyword matching
All checks were successful
/ build (push) Successful in 1m16s
/ clippy (push) Successful in 1m18s

thank you copilot :3
This commit is contained in:
xqtc 2024-07-24 18:29:51 +02:00
parent fd50d44fb0
commit ebf61390c8
3 changed files with 190 additions and 50 deletions

View file

@ -1,19 +1,176 @@
use std::io::Error;
use crate::tokens::{Image, Material, Sampler, Token, Vector};
impl crate::lex::lexer::Lexer { impl crate::lex::lexer::Lexer {
pub fn is_keyword(word: &str) -> bool { pub fn is_keyword(word: &str) -> Option<Token> {
matches!( let token: Option<Token> = match word {
word, "const" => Some(Token::CONST),
"void" "uniform" => Some(Token::UNIFORM),
| "int" "buffer" => Some(Token::BUFFER),
| "float" "shared" => Some(Token::SHARED),
| "bool" "coherent" => Some(Token::COHERENT),
| "if" "volatile" => Some(Token::VOLATILE),
| "else" "restrict" => Some(Token::RESTRICT),
| "for" "readonly" => Some(Token::READONLY),
| "while" "writeonly" => Some(Token::WRITEONLY),
| "return" "atomic_uint" => Some(Token::ATOMIC_UINT),
| "struct" "layout" => Some(Token::LAYOUT),
| "uniform" "centroid" => Some(Token::CENTROID),
| "varying" "flat" => Some(Token::FLAT),
) "smooth" => Some(Token::SMOOTH),
"noperspective" => Some(Token::NOPERSPECTIVE),
"patch" => Some(Token::PATCH),
"sample" => Some(Token::SAMPLE),
"break" => Some(Token::BREAK),
"continue" => Some(Token::CONTINUE),
"do" => Some(Token::DO),
"for" => Some(Token::FOR),
"while" => Some(Token::WHILE),
"switch" => Some(Token::SWITCH),
"case" => Some(Token::CASE),
"default" => Some(Token::DEFAULT),
"if" => Some(Token::IF),
"else" => Some(Token::ELSE),
"subroutine" => Some(Token::SUBROUTINE),
"in" => Some(Token::IN),
"out" => Some(Token::OUT),
"inout" => Some(Token::INOUT),
"float" => Some(Token::FLOAT),
"double" => Some(Token::DOUBLE),
"int" => Some(Token::INT),
"void" => Some(Token::VOID),
"bool" => Some(Token::BOOL),
"true" => Some(Token::BOOLCONSTANT(true)),
"false" => Some(Token::BOOLCONSTANT(false)),
"invariant" => Some(Token::INVARIANT),
"precise" => Some(Token::PRECISE),
"discard" => Some(Token::DISCARD),
"return" => Some(Token::RETURN),
"mat2" => Some(Token::Material(Material::MAT2)),
"mat3" => Some(Token::Material(Material::MAT3)),
"mat4" => Some(Token::Material(Material::MAT4)),
"dmat2" => Some(Token::Material(Material::DMAT2)),
"dmat3" => Some(Token::Material(Material::DMAT3)),
"dmat4" => Some(Token::Material(Material::DMAT4)),
"mat2x2" => Some(Token::Material(Material::MAT2X2)),
"mat2x3" => Some(Token::Material(Material::MAT2X3)),
"mat2x4" => Some(Token::Material(Material::MAT2X4)),
"dmat2x2" => Some(Token::Material(Material::DMAT2X2)),
"dmat2x3" => Some(Token::Material(Material::DMAT2X3)),
"dmat2x4" => Some(Token::Material(Material::DMAT2X4)),
"mat3x2" => Some(Token::Material(Material::MAT3X2)),
"mat3x3" => Some(Token::Material(Material::MAT3X3)),
"mat3x4" => Some(Token::Material(Material::MAT3X4)),
"dmat3x2" => Some(Token::Material(Material::DMAT3X2)),
"dmat3x3" => Some(Token::Material(Material::DMAT3X3)),
"dmat3x4" => Some(Token::Material(Material::DMAT3X4)),
"mat4x2" => Some(Token::Material(Material::MAT4X2)),
"mat4x3" => Some(Token::Material(Material::MAT4X3)),
"mat4x4" => Some(Token::Material(Material::MAT4X4)),
"dmat4x2" => Some(Token::Material(Material::DMAT4X2)),
"dmat4x3" => Some(Token::Material(Material::DMAT4X3)),
"dmat4x4" => Some(Token::Material(Material::DMAT4X4)),
"vec2" => Some(Token::Vector(Vector::VEC2)),
"vec3" => Some(Token::Vector(Vector::VEC3)),
"vec4" => Some(Token::Vector(Vector::VEC4)),
"ivec2" => Some(Token::Vector(Vector::IVEC2)),
"ivec3" => Some(Token::Vector(Vector::IVEC3)),
"ivec4" => Some(Token::Vector(Vector::IVEC4)),
"bvec2" => Some(Token::Vector(Vector::BVEC2)),
"bvec3" => Some(Token::Vector(Vector::BVEC3)),
"bvec4" => Some(Token::Vector(Vector::BVEC4)),
"dvec2" => Some(Token::Vector(Vector::DVEC2)),
"dvec3" => Some(Token::Vector(Vector::DVEC3)),
"dvec4" => Some(Token::Vector(Vector::DVEC4)),
"uint" => Some(Token::UINT),
"uvec2" => Some(Token::Vector(Vector::UVEC2)),
"uvec3" => Some(Token::Vector(Vector::UVEC3)),
"uvec4" => Some(Token::Vector(Vector::UVEC4)),
"lowp" => Some(Token::LOW_PRECISION),
"mediump" => Some(Token::MEDIUM_PRECISION),
"highp" => Some(Token::HIGH_PRECISION),
"precision" => Some(Token::PRECISION),
"sampler1D" => Some(Token::Sampler(Sampler::SAMPLER1D)),
"sampler2D" => Some(Token::Sampler(Sampler::SAMPLER2D)),
"sampler3D" => Some(Token::Sampler(Sampler::SAMPLER3D)),
"samplerCube" => Some(Token::Sampler(Sampler::SAMPLERCUBE)),
"sampler1DShadow" => Some(Token::Sampler(Sampler::SAMPLER1DSHADOW)),
"sampler2DShadow" => Some(Token::Sampler(Sampler::SAMPLER2DSHADOW)),
"samplerCubeShadow" => Some(Token::Sampler(Sampler::SAMPLERCUBESHADOW)),
"sampler1DArray" => Some(Token::Sampler(Sampler::SAMPLER1DARRAY)),
"sampler2DArray" => Some(Token::Sampler(Sampler::SAMPLER2DARRAY)),
"sampler1DArrayShadow" => Some(Token::Sampler(Sampler::SAMPLER1DARRAYSHADOW)),
"sampler2DArrayShadow" => Some(Token::Sampler(Sampler::SAMPLER2DARRAYSHADOW)),
"isampler1D" => Some(Token::Sampler(Sampler::ISAMPLER1D)),
"isampler2D" => Some(Token::Sampler(Sampler::ISAMPLER2D)),
"isampler3D" => Some(Token::Sampler(Sampler::ISAMPLER3D)),
"isamplerCube" => Some(Token::Sampler(Sampler::ISAMPLERCUBE)),
"isampler1DArray" => Some(Token::Sampler(Sampler::ISAMPLER1DARRAY)),
"isampler2DArray" => Some(Token::Sampler(Sampler::ISAMPLER2DARRAY)),
"usampler1D" => Some(Token::Sampler(Sampler::USAMPLER1D)),
"usampler2D" => Some(Token::Sampler(Sampler::USAMPLER2D)),
"usampler3D" => Some(Token::Sampler(Sampler::USAMPLER3D)),
"usamplerCube" => Some(Token::Sampler(Sampler::USAMPLERCUBE)),
"usampler1DArray" => Some(Token::Sampler(Sampler::USAMPLER1DARRAY)),
"usampler2DArray" => Some(Token::Sampler(Sampler::USAMPLER2DARRAY)),
"sampler2DRect" => Some(Token::Sampler(Sampler::SAMPLER2DRECT)),
"sampler2DRectShadow" => Some(Token::Sampler(Sampler::SAMPLER2DRECTSHADOW)),
"isampler2DRect" => Some(Token::Sampler(Sampler::ISAMPLER2DRECT)),
"usampler2DRect" => Some(Token::Sampler(Sampler::USAMPLER2DRECT)),
"samplerBuffer" => Some(Token::Sampler(Sampler::SAMPLERBUFFER)),
"isamplerBuffer" => Some(Token::Sampler(Sampler::ISAMPLERBUFFER)),
"usamplerBuffer" => Some(Token::Sampler(Sampler::USAMPLERBUFFER)),
"sampler2DMS" => Some(Token::Sampler(Sampler::SAMPLER2DMS)),
"isampler2DMS" => Some(Token::Sampler(Sampler::ISAMPLER2DMS)),
"usampler2DMS" => Some(Token::Sampler(Sampler::USAMPLER2DMS)),
"sampler2DMSArray" => Some(Token::Sampler(Sampler::SAMPLER2DMSARRAY)),
"isampler2DMSArray" => Some(Token::Sampler(Sampler::ISAMPLER2DMSARRAY)),
"usampler2DMSArray" => Some(Token::Sampler(Sampler::USAMPLER2DMSARRAY)),
"samplerCubeArray" => Some(Token::Sampler(Sampler::SAMPLERCUBEARRAY)),
"samplerCubeArrayShadow" => Some(Token::Sampler(Sampler::SAMPLERCUBEARRAYSHADOW)),
"isamplerCubeArray" => Some(Token::Sampler(Sampler::ISAMPLERCUBEARRAY)),
"usamplerCubeArray" => Some(Token::Sampler(Sampler::USAMPLERCUBEARRAY)),
"image1D" => Some(Token::Image(Image::IMAGE1D)),
"iimage1D" => Some(Token::Image(Image::IIMAGE1D)),
"uimage1D" => Some(Token::Image(Image::UIMAGE1D)),
"image2D" => Some(Token::Image(Image::IMAGE2D)),
"iimage2D" => Some(Token::Image(Image::IIMAGE2D)),
"uimage2D" => Some(Token::Image(Image::UIMAGE2D)),
"image3D" => Some(Token::Image(Image::IMAGE3D)),
"iimage3D" => Some(Token::Image(Image::IIMAGE3D)),
"uimage3D" => Some(Token::Image(Image::UIMAGE3D)),
"image2DRect" => Some(Token::Image(Image::IMAGE2DRECT)),
"iimage2DRect" => Some(Token::Image(Image::IIMAGE2DRECT)),
"uimage2DRect" => Some(Token::Image(Image::UIMAGE2DRECT)),
"imageCube" => Some(Token::Image(Image::IMAGECUBE)),
"iimageCube" => Some(Token::Image(Image::IIMAGECUBE)),
"uimageCube" => Some(Token::Image(Image::UIMAGECUBE)),
"imageBuffer" => Some(Token::Image(Image::IMAGEBUFFER)),
"iimageBuffer" => Some(Token::Image(Image::IIMAGEBUFFER)),
"uimageBuffer" => Some(Token::Image(Image::UIMAGEBUFFER)),
"image1DArray" => Some(Token::Image(Image::IMAGE1DARRAY)),
"iimage1DArray" => Some(Token::Image(Image::IIMAGE1DARRAY)),
"uimage1DArray" => Some(Token::Image(Image::UIMAGE1DARRAY)),
"image2DArray" => Some(Token::Image(Image::IMAGE2DARRAY)),
"iimage2DArray" => Some(Token::Image(Image::IIMAGE2DARRAY)),
"uimage2DArray" => Some(Token::Image(Image::UIMAGE2DARRAY)),
"imageCubeArray" => Some(Token::Image(Image::IMAGECUBEARRAY)),
"iimageCubeArray" => Some(Token::Image(Image::IIMAGECUBEARRAY)),
"uimageCubeArray" => Some(Token::Image(Image::UIMAGECUBEARRAY)),
"image2DMS" => Some(Token::Image(Image::IMAGE2DMS)),
"iimage2DMS" => Some(Token::Image(Image::IIMAGE2DMS)),
"uimage2DMS" => Some(Token::Image(Image::UIMAGE2DMS)),
"image2DMSArray" => Some(Token::Image(Image::IMAGE2DMSARRAY)),
"iimage2DMSArray" => Some(Token::Image(Image::IIMAGE2DMSARRAY)),
"uimage2DMSArray" => Some(Token::Image(Image::UIMAGE2DMSARRAY)),
"struct" => Some(Token::STRUCT),
_ => None,
};
if let Some(token) = token {
Some(token)
} else {
Some(Token::IDENTIFIER)
}
} }
} }

View file

@ -14,9 +14,6 @@
//! gl_FragColor = vec4(1.0, 0.5, 0.2, 1.0); //! gl_FragColor = vec4(1.0, 0.5, 0.2, 1.0);
//! } //! }
//! "#; //! "#;
//! let mut lexer = glsl_lexer::Lexer::new(&source);
//! let tokens = lexer.get_tokens();
//! dbg!("{}", tokens);
//! } //! }
//! ``` //! ```
@ -27,6 +24,19 @@
pub mod lex; pub mod lex;
mod tokens; mod tokens;
#[cfg(test)]
mod tests {
use super::*;
use crate::lex::lexer::Lexer;
use crate::tokens::{Image, Token};
#[test]
fn keyword() {
let source = "image1D";
let res = Lexer::is_keyword(source);
assert_eq!(Some(Token::Image(Image::IMAGE1D)), res)
}
}
// #[cfg(test)] // #[cfg(test)]
// mod tests { // mod tests {
// use super::lex::lexer::Lexer; // use super::lex::lexer::Lexer;

View file

@ -10,33 +10,6 @@ pub enum Token {
INT, INT,
UINT, UINT,
DOUBLE, DOUBLE,
// MAT2,
// MAT3,
// MAT4,
// MAT2X2,
// MAT2X3,
// MAT2X4,
// MAT3X2,
// MAT3X3,
// MAT3X4,
// MAT4X2,
// MAT4X3,
// MAT4X4,
// DVEC2,
// DVEC3,
// DVEC4,
// DMAT2,
// DMAT3,
// DMAT4,
// DMAT2X2,
// DMAT2X3,
// DMAT2X4,
// DMAT3X2,
// DMAT3X3,
// DMAT3X4,
// DMAT4X2,
// DMAT4X3,
// DMAT4X4,
CENTROID, CENTROID,
IN, IN,
OUT, OUT,
@ -45,6 +18,10 @@ pub enum Token {
PATCH, PATCH,
SAMPLE, SAMPLE,
BUFFER, BUFFER,
Material(Material),
Vector(Vector),
Sampler(Sampler),
Image(Image),
SHARED, SHARED,
COHERENT, COHERENT,
VOLATILE, VOLATILE,
@ -56,10 +33,6 @@ pub enum Token {
SMOOTH, SMOOTH,
LAYOUT, LAYOUT,
ATOMIC_UINT, ATOMIC_UINT,
SAMPLER(Sampler),
VECTOR(Vector),
IMAGE(Image),
MATERIAL(Material),
STRUCT, STRUCT,
VOID, VOID,
WHILE, WHILE,