2024-07-11 12:03:42 +02:00
|
|
|
//! A simple lexer for GLSL.
|
|
|
|
//!
|
|
|
|
//! Adheres to the GLSL 440. Read the spec
|
|
|
|
//! [here](https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.40.pdf).
|
|
|
|
//! ## Example
|
|
|
|
//! ```
|
|
|
|
//! use glsl_lexer::*;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let source = r#"
|
|
|
|
//! #version 440
|
|
|
|
//! uniform float time;
|
|
|
|
//! void main() {
|
|
|
|
//! 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);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
//! # WIP THAT SHIT STILL WONKY AF
|
2024-07-10 19:06:08 +02:00
|
|
|
|
2024-07-21 20:40:47 +02:00
|
|
|
#![allow(dead_code)]
|
2024-07-12 10:19:00 +02:00
|
|
|
|
2024-07-21 20:40:47 +02:00
|
|
|
pub mod lex;
|
2024-07-21 17:42:15 +02:00
|
|
|
mod tokens;
|
|
|
|
mod util;
|
2024-07-11 19:33:56 +02:00
|
|
|
|
2024-07-12 10:19:00 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2024-07-10 19:06:08 +02:00
|
|
|
pub enum Token {
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `float`
|
2024-07-10 19:06:08 +02:00
|
|
|
Identifier(String),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `uniform`
|
2024-07-10 19:06:08 +02:00
|
|
|
Keyword(String),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `13`
|
2024-07-10 19:06:08 +02:00
|
|
|
IntegerLiteral(i64),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `3.5` or `.5`
|
2024-07-10 19:06:08 +02:00
|
|
|
FloatLiteral(f64),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `+`
|
2024-07-10 19:06:08 +02:00
|
|
|
Operator(String),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `{`
|
2024-07-10 19:06:08 +02:00
|
|
|
Symbol(char),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Should be self-explanatory
|
2024-07-10 19:06:08 +02:00
|
|
|
Whitespace,
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Something like `// uwu`
|
2024-07-10 19:06:08 +02:00
|
|
|
Comment(String),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// Shrouded in mystery
|
2024-07-10 19:06:08 +02:00
|
|
|
Unknown(char),
|
2024-07-11 12:03:42 +02:00
|
|
|
/// End Of File
|
2024-07-10 21:37:34 +02:00
|
|
|
EOF,
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Lexer {
|
2024-07-11 12:03:42 +02:00
|
|
|
/// GLSL source
|
|
|
|
pub input: Vec<char>,
|
|
|
|
/// Position in source
|
|
|
|
pub position: usize,
|
|
|
|
/// [`char`] under position
|
|
|
|
pub current_char: Option<char>,
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 17:55:43 +02:00
|
|
|
/// Instantiates Lexer and retrieves Tokens of given source.
|
|
|
|
/// Returns Arc<[Token]>
|
2024-07-12 19:04:15 +02:00
|
|
|
/// # Example:
|
|
|
|
/// ```
|
|
|
|
/// let lexed: Arc<[Token]> = lex!(r#"
|
|
|
|
/// #version 440
|
|
|
|
/// uniform float time;
|
|
|
|
/// void main() {
|
|
|
|
/// gl_FragColor = vec4(1.0, 0.5, 0.2, 1.0);
|
|
|
|
/// }
|
|
|
|
/// "#);
|
|
|
|
/// ```
|
2024-07-12 17:55:43 +02:00
|
|
|
/// Equivalent to:
|
|
|
|
/// ```
|
|
|
|
/// let source = "some source";
|
|
|
|
/// let lexed = Lexer::new(source).get_tokens();
|
|
|
|
/// ```
|
2024-07-21 17:42:15 +02:00
|
|
|
|
2024-07-21 20:40:47 +02:00
|
|
|
// /// Parses the source given the [`Lexer`] upon initialization and returns a vector of [`Token`].
|
|
|
|
// /// # Example:
|
|
|
|
// /// ```
|
|
|
|
// /// use glsl_lexer::*;
|
|
|
|
// /// let source = r#"
|
|
|
|
// /// #version 440
|
|
|
|
// /// uniform float time;
|
|
|
|
// /// void main() {
|
|
|
|
// /// 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);
|
|
|
|
// ///```
|
|
|
|
// // We are using Arc<[Token]> as return type for cheaper cloning of the returned value
|
|
|
|
// pub fn get_tokens(&mut self) -> Arc<[Token]> {
|
|
|
|
// let mut tokens = Vec::new();
|
|
|
|
// while let Some(c) = self.current_char {
|
|
|
|
// if c.is_whitespace() {
|
|
|
|
// self.consume_whitespace();
|
|
|
|
// tokens.push(Token::Whitespace);
|
|
|
|
// } else if c.is_alphabetic() || c == '_' {
|
|
|
|
// tokens.push(self.consume_identifier_or_keyword());
|
|
|
|
// } else if c.is_ascii_digit() {
|
|
|
|
// tokens.push(self.consume_number());
|
|
|
|
// } else if c == '/' && self.peek() == Some('/') {
|
|
|
|
// tokens.push(self.consume_comment());
|
|
|
|
// } else {
|
|
|
|
// match c {
|
|
|
|
// // TODO Implement operands like +=
|
|
|
|
// '+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' | '!' | '=' | '<' | '>' | '?' => {
|
|
|
|
// tokens.push(self.consume_operator());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// '{' | '}' | '(' | ')' | '#' | ',' | ';' => {
|
|
|
|
// tokens.push(self.consume_symbol());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// '.' => {
|
|
|
|
// tokens.push(self.consume_number());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// _ => {
|
|
|
|
// tokens.push(self.consume_unknown());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// tokens.push(Token::EOF);
|
|
|
|
// let ret: Arc<[Token]> = tokens.into();
|
|
|
|
// ret
|
|
|
|
// }
|
2024-07-10 19:06:08 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-07-21 20:40:47 +02:00
|
|
|
use super::lex::lexer::Lexer;
|
|
|
|
use super::tokens::Token;
|
2024-07-10 19:06:08 +02:00
|
|
|
use super::*;
|
2024-07-10 21:37:34 +02:00
|
|
|
use log::info;
|
|
|
|
|
|
|
|
fn init() {
|
|
|
|
std::env::set_var("RUST_LOG", "INFO");
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
}
|
2024-07-10 19:06:08 +02:00
|
|
|
|
|
|
|
#[test]
|
2024-07-10 19:28:54 +02:00
|
|
|
fn whitespace() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 19:06:08 +02:00
|
|
|
let source_code = " \t\n";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[Whitespace] Tokens: {:#?}", tokens);
|
2024-07-12 10:19:00 +02:00
|
|
|
assert_eq!(tokens, vec![Token::Whitespace, Token::EOF].into());
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-10 19:28:54 +02:00
|
|
|
fn identifier() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 19:06:08 +02:00
|
|
|
let source_code = "variableName";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[Identifier] Tokens: {:#?}", tokens);
|
2024-07-10 19:06:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
2024-07-12 10:19:00 +02:00
|
|
|
vec![Token::Identifier("variableName".to_string()), Token::EOF].into()
|
2024-07-10 19:06:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-10 19:28:54 +02:00
|
|
|
fn keyword() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 19:06:08 +02:00
|
|
|
let source_code = "uniform";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[Keyword] Tokens: {:#?}", tokens);
|
2024-07-10 19:06:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
2024-07-12 10:19:00 +02:00
|
|
|
vec![Token::Keyword("uniform".to_string()), Token::EOF].into()
|
2024-07-10 19:06:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-10 19:28:54 +02:00
|
|
|
fn integer_literal() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 19:06:08 +02:00
|
|
|
let source_code = "12345";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[IntegerLiteral] Tokens: {:#?}", tokens);
|
2024-07-12 10:19:00 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![Token::IntegerLiteral(12345), Token::EOF].into()
|
|
|
|
);
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-07-10 19:28:54 +02:00
|
|
|
fn float_literal() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-11 19:33:56 +02:00
|
|
|
let source_code = "123.4504";
|
2024-07-10 19:06:08 +02:00
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[FloatLiteral] Tokens: {:#?}", tokens);
|
2024-07-12 10:19:00 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![Token::FloatLiteral(123.4504), Token::EOF].into()
|
|
|
|
);
|
2024-07-11 19:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float_shorthand() {
|
|
|
|
init();
|
|
|
|
let source_code = ".4504";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
|
|
|
info!("[FloatLiteral Shorthand] Tokens: {:#?}", tokens);
|
2024-07-12 10:19:00 +02:00
|
|
|
assert_eq!(tokens, vec![Token::FloatLiteral(0.4504), Token::EOF].into());
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|
2024-07-10 20:47:10 +02:00
|
|
|
|
2024-07-12 00:13:32 +02:00
|
|
|
#[test]
|
2024-07-12 10:19:00 +02:00
|
|
|
fn swizzling() {
|
2024-07-12 00:13:32 +02:00
|
|
|
init();
|
|
|
|
let source_code = "abcd.xyz";
|
|
|
|
let mut lexer = Lexer::new(source_code);
|
|
|
|
let tokens = lexer.get_tokens();
|
|
|
|
info!("[Swizzling] Tokens: {:#?}", tokens);
|
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![
|
|
|
|
Token::Identifier("abcd".to_string()),
|
|
|
|
Token::Symbol('.'),
|
|
|
|
Token::Identifier("xyz".to_string()),
|
|
|
|
Token::EOF
|
|
|
|
]
|
2024-07-12 10:19:00 +02:00
|
|
|
.into()
|
2024-07-12 00:13:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:59:11 +02:00
|
|
|
#[test]
|
|
|
|
fn test_operator() {
|
|
|
|
init();
|
|
|
|
let source = "+-*/%&|^!=<>?";
|
|
|
|
let mut lexer = Lexer::new(source);
|
|
|
|
let tokens = lexer.get_tokens();
|
|
|
|
info!("[Operator] Tokens: {:#?}", tokens);
|
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![
|
|
|
|
Token::Operator("+".to_string()),
|
|
|
|
Token::Operator("-".to_string()),
|
|
|
|
Token::Operator("*".to_string()),
|
|
|
|
Token::Operator("/".to_string()),
|
|
|
|
Token::Operator("%".to_string()),
|
|
|
|
Token::Operator("&".to_string()),
|
|
|
|
Token::Operator("|".to_string()),
|
|
|
|
Token::Operator("^".to_string()),
|
|
|
|
Token::Operator("!".to_string()),
|
|
|
|
Token::Operator("=".to_string()),
|
|
|
|
Token::Operator("<".to_string()),
|
|
|
|
Token::Operator(">".to_string()),
|
|
|
|
Token::Operator("?".to_string()),
|
|
|
|
Token::EOF,
|
|
|
|
]
|
2024-07-12 10:19:00 +02:00
|
|
|
.into()
|
2024-07-11 17:59:11 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-10 20:47:10 +02:00
|
|
|
#[test]
|
|
|
|
fn test_single_line_comment() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 20:47:10 +02:00
|
|
|
let source = "// This is a comment\n";
|
|
|
|
let mut lexer = Lexer::new(source);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[Comment] Tokens: {:#?}", tokens);
|
2024-07-10 20:47:10 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![
|
|
|
|
Token::Comment("// This is a comment".to_string()),
|
|
|
|
Token::Whitespace,
|
2024-07-10 21:37:34 +02:00
|
|
|
Token::EOF,
|
2024-07-10 20:47:10 +02:00
|
|
|
]
|
2024-07-12 10:19:00 +02:00
|
|
|
.into()
|
2024-07-10 20:47:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I hope that does it. Writing this test was pain.
|
|
|
|
#[test]
|
|
|
|
fn complex_source() {
|
2024-07-10 21:37:34 +02:00
|
|
|
init();
|
2024-07-10 20:47:10 +02:00
|
|
|
let source = r#"
|
|
|
|
uniform float time;
|
|
|
|
void main() {
|
|
|
|
gl_FragColor = vec4(1.0, 0.5, 0.2, 1.0);
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let mut lexer = Lexer::new(source);
|
|
|
|
let tokens = lexer.get_tokens();
|
2024-07-10 21:37:34 +02:00
|
|
|
info!("[Complex Source] Tokens: {:#?}", tokens);
|
2024-07-10 20:47:10 +02:00
|
|
|
assert_eq!(
|
|
|
|
tokens,
|
|
|
|
vec![
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Keyword("uniform".to_string()),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Keyword("float".to_string()),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Identifier("time".to_string()),
|
|
|
|
Token::Symbol(';'),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Keyword("void".to_string()),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Identifier("main".to_string()),
|
|
|
|
Token::Symbol('('),
|
|
|
|
Token::Symbol(')'),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Symbol('{'),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Identifier("gl_FragColor".to_string()),
|
|
|
|
Token::Whitespace,
|
2024-07-11 17:59:11 +02:00
|
|
|
Token::Operator('='.to_string()),
|
2024-07-10 20:47:10 +02:00
|
|
|
Token::Whitespace,
|
|
|
|
Token::Identifier("vec4".to_string()),
|
|
|
|
Token::Symbol('('),
|
|
|
|
Token::FloatLiteral(1.0),
|
|
|
|
Token::Symbol(','),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::FloatLiteral(0.5),
|
|
|
|
Token::Symbol(','),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::FloatLiteral(0.2),
|
|
|
|
Token::Symbol(','),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::FloatLiteral(1.0),
|
|
|
|
Token::Symbol(')'),
|
|
|
|
Token::Symbol(';'),
|
|
|
|
Token::Whitespace,
|
|
|
|
Token::Symbol('}'),
|
|
|
|
Token::Whitespace,
|
2024-07-10 21:37:34 +02:00
|
|
|
Token::EOF,
|
2024-07-10 20:47:10 +02:00
|
|
|
]
|
2024-07-12 10:19:00 +02:00
|
|
|
.into()
|
2024-07-10 20:47:10 +02:00
|
|
|
);
|
|
|
|
}
|
2024-07-10 19:06:08 +02:00
|
|
|
}
|