Shader: Fix some errors

This commit is contained in:
SirLynix 2022-03-29 08:42:08 +02:00 committed by Jérôme Leclercq
parent 1afc599e3d
commit 35f6240786
5 changed files with 62 additions and 55 deletions

View File

@ -85,6 +85,10 @@ namespace Nz::ShaderLang
std::string ParseModuleName();
ShaderAst::ExpressionPtr ParseType();
template<typename T> void HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& targetAttribute, Attribute&& attribute);
template<typename T> void HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& targetAttribute, Attribute&& attribute, T defaultValue);
template<typename T> void HandleUniqueStringAttribute(ShaderAst::ExpressionValue<T>& targetAttribute, Attribute&& attribute, const std::unordered_map<std::string, T>& map, std::optional<T> defaultValue = {});
static int GetTokenPrecedence(TokenType token);
struct Context

View File

@ -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)

View File

@ -424,7 +424,6 @@ namespace Nz::ShaderAst
auto clone = std::make_unique<CastExpression>();
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];

View File

@ -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

View File

@ -58,59 +58,6 @@ namespace Nz::ShaderLang
{ "hint", ShaderAst::LoopUnroll::Hint },
{ "never", ShaderAst::LoopUnroll::Never }
};
template<typename T>
void HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& 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<typename T>
void HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& 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<typename T>
void HandleUniqueStringAttribute(ShaderAst::ExpressionValue<T>& targetAttribute, Parser::Attribute&& attribute, const std::unordered_map<std::string, T>& map, std::optional<T> 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<ShaderAst::IdentifierExpression&>(*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<Token>& tokens)
@ -1524,6 +1471,59 @@ namespace Nz::ShaderLang
return ParseExpression();
}
template<typename T>
void Parser::HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& 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<typename T>
void Parser::HandleUniqueAttribute(ShaderAst::ExpressionValue<T>& 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<typename T>
void Parser::HandleUniqueStringAttribute(ShaderAst::ExpressionValue<T>& targetAttribute, Parser::Attribute&& attribute, const std::unordered_map<std::string, T>& map, std::optional<T> 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<ShaderAst::IdentifierExpression&>(*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)