From 04a3435200b9c6c52cbd48953a1de607d3bda042 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 10 Dec 2024 16:45:59 +0100 Subject: [PATCH] Fix shaders parsing to skip comments in between special macros --- .../Parser/ShaderProcessing.cpp | 36 ++----------------- Source/Engine/Utilities/TextProcessing.cpp | 35 ++++++++++++++++++ Source/Engine/Utilities/TextProcessing.h | 20 +++++------ 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/Source/Engine/ShadersCompilation/Parser/ShaderProcessing.cpp b/Source/Engine/ShadersCompilation/Parser/ShaderProcessing.cpp index fea60c8e7..e65961038 100644 --- a/Source/Engine/ShadersCompilation/Parser/ShaderProcessing.cpp +++ b/Source/Engine/ShadersCompilation/Parser/ShaderProcessing.cpp @@ -66,10 +66,6 @@ void ShaderProcessing::Parser::init() bool ShaderProcessing::Parser::process() { const Token defineToken("#define"); - const Separator singleLineCommentSeparator('/', '/'); - const Separator multiLineCommentSeparator('/', '*'); - - // TODO: split parsing into two phrases: comments preprocessing and parsing // Read whole source code Token token; @@ -77,36 +73,8 @@ bool ShaderProcessing::Parser::process() { text.ReadToken(&token); - // Single line comment - if (token.Separator == singleLineCommentSeparator) - { - // Read whole line - text.ReadLine(); - } - // Multi line comment - else if (token.Separator == multiLineCommentSeparator) - { - // Read tokens until end sequence - char prev = ' '; - char c; - while (text.CanRead()) - { - c = text.ReadChar(); - if (prev == '*' && c == '/') - { - break; - } - prev = c; - } - - // Check if comment is valid (has end before file end) - if (!text.CanRead()) - { - OnWarning(TEXT("Missing multiline comment ending")); - } - } - // Preprocessor definition - else if (token == defineToken) + // Preprocessor definition + if (token == defineToken) { // Skip text.ReadLine(); diff --git a/Source/Engine/Utilities/TextProcessing.cpp b/Source/Engine/Utilities/TextProcessing.cpp index fb971559d..50faeb262 100644 --- a/Source/Engine/Utilities/TextProcessing.cpp +++ b/Source/Engine/Utilities/TextProcessing.cpp @@ -1,6 +1,7 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. #include "TextProcessing.h" +#include "Engine/Core/Log.h" TextProcessing::TextProcessing(const char* input, int32 length) : _buffer(input) @@ -54,6 +55,8 @@ void TextProcessing::Setup_HLSL() 32, }; Whitespaces.Set(whitespaces, ARRAY_COUNT(whitespaces)); + SingleLineComment = SeparatorData('/', '/'); + MultiLineCommentSeparator = SeparatorData('/', '*'); } char TextProcessing::ReadChar() @@ -123,6 +126,38 @@ void TextProcessing::ReadToken(Token* token) token->Separator = Separators[s]; ReadChar(); + + // Check for comments + if (token->Separator == SingleLineComment) + { + // Read whole line + ReadLine(); + + // Read another token + ReadToken(token); + } + else if (token->Separator == MultiLineCommentSeparator) + { + // Read tokens until end sequence + char prev = ' '; + while (CanRead()) + { + c = ReadChar(); + if (prev == '*' && c == '/') + break; + prev = c; + } + + // Check if comment is valid (has end before file end) + if (!CanRead()) + { + LOG(Warning, "Missing multiline comment ending"); + } + + // Read another token + ReadToken(token); + } + return; } } diff --git a/Source/Engine/Utilities/TextProcessing.h b/Source/Engine/Utilities/TextProcessing.h index d26f52814..fa804a132 100644 --- a/Source/Engine/Utilities/TextProcessing.h +++ b/Source/Engine/Utilities/TextProcessing.h @@ -9,13 +9,13 @@ #include "Engine/Platform/StringUtils.h" /// -/// Helper class to fast ANSI text processing (tokenization, reading, streaming etc.) +/// Helper class to fast ANSI text processing (tokenization, reading, streaming etc.). /// class FLAXENGINE_API TextProcessing : public NonCopyable { public: /// - /// Separator structure + /// Separator structure. /// struct SeparatorData { @@ -200,18 +200,21 @@ public: public: /// - /// Array with all token separators + /// Array with all token separators. /// Array Separators; /// /// - /// Array with all white characters + /// Array with all white characters. /// Array Whitespaces; + SeparatorData SingleLineComment; + SeparatorData MultiLineCommentSeparator; + public: /// - /// Set separators and white chars for HLSL language + /// Sets up separators and white chars for HLSL language. /// void Setup_HLSL(); @@ -219,25 +222,22 @@ public: /// /// Returns true if there are still characters in the buffer and can read data from it /// - /// True if can read data, otherwise false FORCE_INLINE bool CanRead() const { return _position < _length; } /// - /// Peeks single character without moving forward in the buffer + /// Peeks a single character without moving forward in the buffer. /// - /// First character FORCE_INLINE char PeekChar() const { return *_cursor; } /// - /// Gets current line number + /// Gets the current line number. /// - /// Current line number FORCE_INLINE int32 GetLine() const { return _line;