Initial shaderlang commit

This commit is contained in:
Jérôme Leclercq
2021-02-24 23:51:24 +01:00
parent d59423afca
commit 9a0f201433
7 changed files with 623 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_LANGLEXER_HPP
#define NAZARA_SHADER_LANGLEXER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>
namespace Nz::ShaderLang
{
enum class TokenType
{
#define NAZARA_SHADERLANG_TOKEN(X) X,
#include <Nazara/Shader/ShaderLangTokenList.hpp>
};
struct Token
{
unsigned int column;
unsigned int line;
TokenType type;
std::variant<double, long long, std::string> data;
};
class BadNumber : public std::exception
{
using exception::exception;
};
class NumberOutOfRange : public std::exception
{
using exception::exception;
};
class UnrecognizedToken : public std::exception
{
using exception::exception;
};
NAZARA_SHADER_API std::vector<Token> Tokenize(const std::string_view& str);
NAZARA_SHADER_API const char* ToString(TokenType tokenType);
NAZARA_SHADER_API std::string ToString(const std::vector<Token>& tokens, bool pretty = true);
}
#include <Nazara/Shader/ShaderLangLexer.inl>
#endif

View File

@@ -0,0 +1,12 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/ShaderLangLexer.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_LANGPARSER_HPP
#define NAZARA_SHADER_LANGPARSER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangLexer.hpp>
namespace Nz::ShaderLang
{
class ExpectedToken : public std::exception
{
public:
using exception::exception;
};
class UnexpectedToken : public std::exception
{
public:
using exception::exception;
};
class NAZARA_SHADER_API Parser
{
public:
inline Parser();
~Parser() = default;
void Parse(const std::vector<Token>& tokens);
private:
const Token& Advance();
void Expect(const Token& token, TokenType type);
void ExpectNext(TokenType type);
void ParseFunctionBody();
void ParseFunctionDeclaration();
void ParseFunctionParameter();
const Token& PeekNext();
struct Context
{
std::size_t tokenCount;
std::size_t tokenIndex = 0;
const Token* tokens;
};
Context* m_context;
};
}
#include <Nazara/Shader/ShaderLangParser.inl>
#endif

View File

@@ -0,0 +1,16 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderLang
{
inline Parser::Parser() :
m_context(nullptr)
{
}
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#if !defined(NAZARA_SHADERLANG_TOKEN)
#error You must define NAZARA_SHADERLANG_TOKEN before including this file
#endif
#ifndef NAZARA_SHADERLANG_TOKENT_LAST
#define NAZARA_SHADERLANG_TOKEN_LAST(X) NAZARA_SHADERLANG_TOKEN(X)
#endif
NAZARA_SHADERLANG_TOKEN(Add)
NAZARA_SHADERLANG_TOKEN(BoolFalse)
NAZARA_SHADERLANG_TOKEN(BoolTrue)
NAZARA_SHADERLANG_TOKEN(ClosingParenthesis)
NAZARA_SHADERLANG_TOKEN(ClosingCurlyBracket)
NAZARA_SHADERLANG_TOKEN(Colon)
NAZARA_SHADERLANG_TOKEN(Comma)
NAZARA_SHADERLANG_TOKEN(Divide)
NAZARA_SHADERLANG_TOKEN(Dot)
NAZARA_SHADERLANG_TOKEN(FloatingPointValue)
NAZARA_SHADERLANG_TOKEN(EndOfStream)
NAZARA_SHADERLANG_TOKEN(FunctionDeclaration)
NAZARA_SHADERLANG_TOKEN(FunctionReturn)
NAZARA_SHADERLANG_TOKEN(IntegerValue)
NAZARA_SHADERLANG_TOKEN(Identifier)
NAZARA_SHADERLANG_TOKEN(Multiply)
NAZARA_SHADERLANG_TOKEN(OpenCurlyBracket)
NAZARA_SHADERLANG_TOKEN(OpenParenthesis)
NAZARA_SHADERLANG_TOKEN(Semicolon)
NAZARA_SHADERLANG_TOKEN(Return)
NAZARA_SHADERLANG_TOKEN(Subtract)
#undef NAZARA_SHADERLANG_TOKEN
#undef NAZARA_SHADERLANG_TOKEN_LAST