CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
GLSLParser.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "GLSLScanner.hpp"
11
13
15
17
19
21
22 // Syntax parser class for the shading language GLSL.
23 class GLSLParser : public SLParser {
24 private:
26
27 // Symbol table for type name (i.e. structure and typedef identifiers) to detect cast expression, which are not context free.
29
31 bool mIsESSL = false;
32 bool mIsCoreProfile = false;
33
34 public:
35 GLSLParser(Log* log = nullptr);
36
37 ProgramPtr parseSource(const SourceCodePtr& source, const NameMangling& nameMangling, const InputShaderVersion versionIn, bool enableWarnings = false);
38
39 private:
41
42 // Returns true if the current token is a data type.
43 [[nodiscard]] bool isDataType() const;
44
45 // Returns true if the current token is a base data type (i.e. scalar, vector, matrix, or string type denoter).
46 [[nodiscard]] bool isBaseDataType() const;
47
48 // Returns true if the current token is a literal.
49 [[nodiscard]] bool isLiteral() const;
50
51 // Returns true if the current token is part of an arithmetic unary expression, i.e. either '-' or '+'.
53
54 // Returns true if the current token is a modifier of a type specifier.
55 [[nodiscard]] bool isModifier() const;
56
57 // Overrides the token accept function to process all directives before the actual parsing.
58 TokenPtr acceptIt() override;
59
60 // Processes the specified directive (only '#line'-directive are allowed after pre-processing).
61 void processDirective(const String& ident);
65
66 private: // Symbol table
67 // Opens a new scope of the type name symbol table.
68 void openScope();
69
70 // Closes the current scope of the type name symbol table.
71 void closeScope();
72
73 // Registers the specified identifier as type name, to detect cast expressions.
74 void registerTypeName(const String& ident);
75
76 // Returns true if the specified identifier is a valid type name within the current scope.
77 [[nodiscard]] bool isRegisteredTypeName(const String& ident) const;
78
79 private: // Parsing
81
87 VarDeclarationPtr parseVarDeclaration(VarDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr) override;
88
91
92#if 0
93 BufferDeclarationPtr parseBufferDeclaration(BufferDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
94 SamplerDeclarationPtr parseSamplerDeclaration(SamplerDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
95#endif
96 StructDeclarationPtr parseStructDeclaration(bool parseStructtoken = true, const TokenPtr& identifierToken = nullptr);
98 const TokenPtr& identifierToken = nullptr);
100
105#if 0
108#endif
109 BasicDeclarationStatementPtr parseFunctionDeclarationStatement(const TypeSpecifierPtr& returnType = nullptr, const TokenPtr& identifierToken = nullptr);
112#if 0
113 BufferDeclarationStatementPtr ParseBufferDeclarationStatement(const BufferTypeDenoterPtr& typeDenoter = nullptr, const TokenPtr& identifierToken = nullptr);
114 SamplerDeclarationStatementPtr ParseSamplerDeclarationStatement(const SamplerTypeDenoterPtr& typeDenoter = nullptr, const TokenPtr& identifierToken = nullptr);
115#endif
116 VarDeclarationStatementPtr parseVarDeclarationStatement(bool isUniform = false, const TokenPtr& identifierToken = nullptr);
117
120#if 0
122#endif
123
137 CallExpressionPtr parseCallExpressionWithPrefixOpt(const ExpressionPtr& prefixExpression = nullptr, bool isStatic = false, const TokenPtr& identifierToken = nullptr);
139
142#if 0
143 Vector<BufferDeclarationPtr> ParseBufferDeclarationList(BufferDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
144 Vector<SamplerDeclarationPtr> ParseSamplerDeclarationList(SamplerDeclarationStatement* declarationStatementRef, const TokenPtr& identifierToken = nullptr);
145#endif
146
147 TypeDenoterPtr parseTypeDenoter(bool allowVoidType = true, StructDeclarationPtr* structDeclaration = nullptr);
156
163
164 bool parseModifiers(TypeSpecifier* typeSpecifier, bool allowPrimitiveType = false, const TokenPtr& inputtoken = nullptr);
165 };
166
167} // namespace CeresEngine::ShaderCompiler
Definition GLSLParser.hpp:23
TypeNameSymbolTable mTypeNameSymbolTable
Definition GLSLParser.hpp:28
bool mIsCoreProfile
Definition GLSLParser.hpp:32
BufferTypeDenoterPtr parseBufferTypeDenoter()
StatementPtr parseLocalStatement() override
TypeDenoterPtr parseTypeDenoter(bool allowVoidType=true, StructDeclarationPtr *structDeclaration=nullptr)
void processDirective(const String &ident)
StatementPtr parseGlobalStatementPrimary(bool hasAttribs=false)
StatementPtr parseForLoopInitializer() override
ExpressionPtr parseObjectOrCallExpression(const ExpressionPtr &expression=nullptr)
StructTypeDenoterPtr parseStructTypeDenoterWithStructDeclarationOpt(StructDeclarationPtr &structDeclaration)
void registerTypeName(const String &ident)
TypeDenoterPtr parseTypeDenoterPrimary(StructDeclarationPtr *structDeclaration=nullptr)
VoidTypeDenoterPtr parseVoidTypeDenoter()
DataType parseDataType(const String &keyword)
CodeBlockPtr parseCodeBlock() override
CallExpressionPtr parseCallExpressionWithPrefixOpt(const ExpressionPtr &prefixExpression=nullptr, bool isStatic=false, const TokenPtr &identifierToken=nullptr)
VarDeclarationPtr parseVarDeclaration(VarDeclarationStatement *declarationStatementRef, const TokenPtr &identifierToken=nullptr) override
StatementPtr parseGlobalStatementWithTypeSpecifier(const TokenPtr &inputtoken=nullptr)
Vector< StatementPtr > parseGlobalStatementList()
BaseTypeDenoterPtr parseBaseTypeDenoter()
TypeSpecifierPtr parseTypeSpecifier(bool parseVoidType=false, const TokenPtr &inputtoken=nullptr)
TypeDenoterPtr parseTypeDenoterWithStructDeclarationOpt(StructDeclarationPtr &structDeclaration, bool allowVoidType=true)
UnaryExpressionPtr parseUnaryExpression()
ProgramPtr parseProgram(const SourceCodePtr &source)
Int32 mVersion
Definition GLSLParser.hpp:30
TypeSpecifierExpressionPtr parseTypeSpecifierExpression()
VarDeclarationStatementPtr parseVarDeclarationStatement(bool isUniform=false, const TokenPtr &identifierToken=nullptr)
AssignExpressionPtr parseAssignExpression(const ExpressionPtr &expression)
StructDeclarationPtr parseStructDeclaration(bool parseStructtoken=true, const TokenPtr &identifierToken=nullptr)
bool parseModifiers(TypeSpecifier *typeSpecifier, bool allowPrimitiveType=false, const TokenPtr &inputtoken=nullptr)
CallExpressionPtr parseCallExpression(const ObjectExpressionPtr &objectExpression=nullptr, const TypeDenoterPtr &typeDenoter=nullptr)
StructTypeDenoterPtr parseStructTypeDenoter()
ObjectExpressionPtr parseObjectExpression(const ExpressionPtr &expression=nullptr)
BasicDeclarationStatementPtr parseUniformBufferDeclarationStatement(const TokenPtr &identifierToken=nullptr)
bool isRegisteredTypeName(const String &ident) const
SwitchCasePtr parseSwitchCase() override
LiteralExpressionPtr parseLiteralExpression()
UniformBufferDeclarationPtr parseUniformBufferDeclaration(const TokenPtr &identifierToken=nullptr)
BracketExpressionPtr parseBracketExpression()
BasicDeclarationStatementPtr parseFunctionDeclarationStatement(const TypeSpecifierPtr &returnType=nullptr, const TokenPtr &identifierToken=nullptr)
FunctionDeclarationPtr parseFunctionDeclaration(BasicDeclarationStatement *declarationStatementRef, const TypeSpecifierPtr &returnType=nullptr, const TokenPtr &identifierToken=nullptr)
StatementPtr parseGlobalStatementWithLayoutQualifier()
Vector< AttributePtr > parseAttributeList()
SamplerTypeDenoterPtr parseSamplerTypeDenoter()
ExpressionPtr parseTypeSpecifierOrCallExpression()
ExpressionPtr parseExpressionWithSuffixOpt(ExpressionPtr expression)
bool mIsESSL
Definition GLSLParser.hpp:31
VarDeclarationStatementPtr parseParameter() override
ExpressionPtr parsePrimaryExpression() override
ProgramPtr parseSource(const SourceCodePtr &source, const NameMangling &nameMangling, const InputShaderVersion versionIn, bool enableWarnings=false)
CallExpressionPtr parseCallExpressionAsTypeCtor(const TypeDenoterPtr &typeDenoter)
PostUnaryExpressionPtr parsePostUnaryExpression(const ExpressionPtr &expression)
Log base class.
Definition Log.hpp:19
Definition SLParser.hpp:28
Definition AST.hpp:33
SPtr< TypeSpecifier > TypeSpecifierPtr
Definition Visitor.hpp:37
SPtr< LiteralExpression > LiteralExpressionPtr
Definition Visitor.hpp:68
SPtr< SamplerDeclarationStatement > SamplerDeclarationStatementPtr
Definition Visitor.hpp:48
TypeModifier
Definition ASTEnums.hpp:471
SPtr< Expression > ExpressionPtr
Definition Visitor.hpp:26
SPtr< ObjectExpression > ObjectExpressionPtr
Definition Visitor.hpp:76
InputShaderVersion
Input shader version enumeration.
Definition Targets.hpp:40
SPtr< SourceCode > SourceCodePtr
Definition SourceCode.hpp:66
SPtr< TypeSpecifierExpression > TypeSpecifierExpressionPtr
Definition Visitor.hpp:69
SPtr< BufferDeclarationStatement > BufferDeclarationStatementPtr
Definition Visitor.hpp:47
SPtr< BaseTypeDenoter > BaseTypeDenoterPtr
Definition TypeDenoter.hpp:32
SPtr< StructDeclaration > StructDeclarationPtr
Definition Visitor.hpp:42
SPtr< VarDeclaration > VarDeclarationPtr
Definition Visitor.hpp:39
InterpModifier
Definition ASTEnums.hpp:453
SPtr< PostUnaryExpression > PostUnaryExpressionPtr
Definition Visitor.hpp:73
SPtr< SamplerDeclaration > SamplerDeclarationPtr
Definition Visitor.hpp:41
SPtr< BufferTypeDenoter > BufferTypeDenoterPtr
Definition TypeDenoter.hpp:33
SPtr< CodeBlock > CodeBlockPtr
Definition Visitor.hpp:30
SPtr< FunctionDeclaration > FunctionDeclarationPtr
Definition Visitor.hpp:44
SPtr< Statement > StatementPtr
Definition Visitor.hpp:25
SPtr< CallExpression > CallExpressionPtr
Definition Visitor.hpp:74
StorageClass
Definition ASTEnums.hpp:441
SPtr< TypeDenoter > TypeDenoterPtr
Definition TypeDenoter.hpp:29
SPtr< BufferDeclaration > BufferDeclarationPtr
Definition Visitor.hpp:40
SPtr< SwitchCase > SwitchCasePtr
Definition Visitor.hpp:32
DataType
Definition ASTEnums.hpp:159
SPtr< SamplerTypeDenoter > SamplerTypeDenoterPtr
Definition TypeDenoter.hpp:34
SPtr< VoidTypeDenoter > VoidTypeDenoterPtr
Definition TypeDenoter.hpp:30
SPtr< BracketExpression > BracketExpressionPtr
Definition Visitor.hpp:75
SPtr< AssignExpression > AssignExpressionPtr
Definition Visitor.hpp:77
SPtr< StructTypeDenoter > StructTypeDenoterPtr
Definition TypeDenoter.hpp:35
SPtr< VarDeclarationStatement > VarDeclarationStatementPtr
Definition Visitor.hpp:49
SPtr< UnaryExpression > UnaryExpressionPtr
Definition Visitor.hpp:72
SPtr< UniformBufferDeclaration > UniformBufferDeclarationPtr
Definition Visitor.hpp:45
SPtr< Token > TokenPtr
Definition Token.hpp:174
SPtr< Attribute > AttributePtr
Definition Visitor.hpp:31
PrimitiveType
Definition ASTEnums.hpp:425
SPtr< BasicDeclarationStatement > BasicDeclarationStatementPtr
Definition Visitor.hpp:51
SPtr< Program > ProgramPtr
Definition Visitor.hpp:29
SamplerType
Definition ASTEnums.hpp:571
SPtr< Scanner > ScannerPtr
Definition Scanner.hpp:147
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::int32_t Int32
Definition DataTypes.hpp:21
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Name mangling descriptor structure for shader input/output variables (also referred to as "varyings")...
Definition ShaderCompiler.hpp:185