diff --git a/include/Nazara/Shader/ShaderLangParser.hpp b/include/Nazara/Shader/ShaderLangParser.hpp index cb46e30a6..6bafa69fd 100644 --- a/include/Nazara/Shader/ShaderLangParser.hpp +++ b/include/Nazara/Shader/ShaderLangParser.hpp @@ -85,6 +85,10 @@ namespace Nz::ShaderLang std::string ParseModuleName(); ShaderAst::ExpressionPtr ParseType(); + template void HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Attribute&& attribute); + template void HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Attribute&& attribute, T defaultValue); + template void HandleUniqueStringAttribute(ShaderAst::ExpressionValue& targetAttribute, Attribute&& attribute, const std::unordered_map& map, std::optional defaultValue = {}); + static int GetTokenPrecedence(TokenType token); struct Context diff --git a/include/Nazara/Shader/ShaderLangSourceLocation.inl b/include/Nazara/Shader/ShaderLangSourceLocation.inl index 7766a0d84..7d587f3ed 100644 --- a/include/Nazara/Shader/ShaderLangSourceLocation.inl +++ b/include/Nazara/Shader/ShaderLangSourceLocation.inl @@ -58,7 +58,7 @@ namespace Nz::ShaderLang assert(rightLocation.endLine >= startLine); endLine = rightLocation.endLine; assert(rightLocation.endLine > startLine || rightLocation.endColumn >= startColumn); - endColumn = endColumn; + endColumn = rightLocation.endColumn; } inline SourceLocation SourceLocation::BuildFromTo(const SourceLocation& leftSource, const SourceLocation& rightSource) diff --git a/src/Nazara/Shader/Ast/AstCloner.cpp b/src/Nazara/Shader/Ast/AstCloner.cpp index a5d52016e..911a170a6 100644 --- a/src/Nazara/Shader/Ast/AstCloner.cpp +++ b/src/Nazara/Shader/Ast/AstCloner.cpp @@ -424,7 +424,6 @@ namespace Nz::ShaderAst auto clone = std::make_unique(); clone->targetType = Clone(node.targetType); - for (auto& expr : node.expressions) for (std::size_t expressionIndex = 0; expressionIndex < node.expressions.size(); ++expressionIndex) { auto& expr = node.expressions[expressionIndex]; diff --git a/src/Nazara/Shader/Ast/AstSerializer.cpp b/src/Nazara/Shader/Ast/AstSerializer.cpp index 1d73ce8e1..2bda21c62 100644 --- a/src/Nazara/Shader/Ast/AstSerializer.cpp +++ b/src/Nazara/Shader/Ast/AstSerializer.cpp @@ -870,6 +870,7 @@ namespace Nz::ShaderAst type = ShaderAst::Type{ containedTypeIndex }; + break; } case 10: //< FunctionType @@ -880,6 +881,7 @@ namespace Nz::ShaderAst type = FunctionType { funcIndex }; + break; } case 11: //< IntrinsicFunctionType @@ -890,6 +892,7 @@ namespace Nz::ShaderAst type = IntrinsicFunctionType { intrinsicType }; + break; } case 12: //< MethodType @@ -906,6 +909,7 @@ namespace Nz::ShaderAst methodType.methodIndex = methodIndex; type = std::move(methodType); + break; } case 13: //< AliasType diff --git a/src/Nazara/Shader/ShaderLangParser.cpp b/src/Nazara/Shader/ShaderLangParser.cpp index c77a90b3b..8348e9057 100644 --- a/src/Nazara/Shader/ShaderLangParser.cpp +++ b/src/Nazara/Shader/ShaderLangParser.cpp @@ -58,59 +58,6 @@ namespace Nz::ShaderLang { "hint", ShaderAst::LoopUnroll::Hint }, { "never", ShaderAst::LoopUnroll::Never } }; - - template - void HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute) - { - if (targetAttribute.HasValue()) - throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; - - if (!attribute.args) - throw ParserAttributeMissingParameterError{ attribute.sourceLocation, attribute.type }; - - targetAttribute = std::move(attribute.args); - } - - template - void HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute, T defaultValue) - { - if (targetAttribute.HasValue()) - throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; - - if (attribute.args) - targetAttribute = std::move(attribute.args); - else - targetAttribute = std::move(defaultValue); - } - - template - void HandleUniqueStringAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute, const std::unordered_map& map, std::optional defaultValue = {}) - { - if (targetAttribute.HasValue()) - throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; - - //FIXME: This should be handled with global values at sanitization stage - if (attribute.args) - { - if (attribute.args->GetType() != ShaderAst::NodeType::IdentifierExpression) - throw ParserAttributeParameterIdentifierError{ attribute.args->sourceLocation, attribute.type }; - - const std::string& exprStr = static_cast(*attribute.args).identifier; - - auto it = map.find(exprStr); - if (it == map.end()) - throw ParserAttributeInvalidParameterError{ attribute.args->sourceLocation, exprStr, attribute.type }; - - targetAttribute = it->second; - } - else - { - if (!defaultValue) - throw ParserAttributeMissingParameterError{ attribute.sourceLocation, attribute.type }; - - targetAttribute = defaultValue.value(); - } - } } ShaderAst::ModulePtr Parser::Parse(const std::vector& tokens) @@ -1524,6 +1471,59 @@ namespace Nz::ShaderLang return ParseExpression(); } + template + void Parser::HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute) + { + if (targetAttribute.HasValue()) + throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; + + if (!attribute.args) + throw ParserAttributeMissingParameterError{ attribute.sourceLocation, attribute.type }; + + targetAttribute = std::move(attribute.args); + } + + template + void Parser::HandleUniqueAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute, T defaultValue) + { + if (targetAttribute.HasValue()) + throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; + + if (attribute.args) + targetAttribute = std::move(attribute.args); + else + targetAttribute = std::move(defaultValue); + } + + template + void Parser::HandleUniqueStringAttribute(ShaderAst::ExpressionValue& targetAttribute, Parser::Attribute&& attribute, const std::unordered_map& map, std::optional defaultValue) + { + if (targetAttribute.HasValue()) + throw ParserAttributeMultipleUniqueError{ attribute.sourceLocation, attribute.type }; + + //FIXME: This should be handled with global values at sanitization stage + if (attribute.args) + { + if (attribute.args->GetType() != ShaderAst::NodeType::IdentifierExpression) + throw ParserAttributeParameterIdentifierError{ attribute.args->sourceLocation, attribute.type }; + + const std::string& exprStr = static_cast(*attribute.args).identifier; + + auto it = map.find(exprStr); + if (it == map.end()) + throw ParserAttributeInvalidParameterError{ attribute.args->sourceLocation, exprStr, attribute.type }; + + targetAttribute = it->second; + } + else + { + if (!defaultValue) + throw ParserAttributeMissingParameterError{ attribute.sourceLocation, attribute.type }; + + targetAttribute = defaultValue.value(); + } + } + int Parser::GetTokenPrecedence(TokenType token) { switch (token)