CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Parser.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 "Scanner.hpp"
11
13
21
23
28
30
32
33 // Syntax parser base class.
34 class Parser {
35 protected:
36 struct ParsingState {
37 bool activeTemplate; // If true, '<' and '>' will not be parsed as a binary operator.
38 };
39
40 private:
46
49
50 Log* mLog = nullptr;
52
56
58 const UInt32 unexpectedTokenLimit = 3; //< this should never be less than 1
59
60 bool mEnableWarnings = false;
61
62 public:
63 virtual ~Parser() = default;
64
65 protected:
68
69 Parser(Log* log);
70
71 protected: // Report Handling
72 void error(const String& msg, const SourceArea& area, bool breakWithException = true);
73 void error(const String& msg, const Token* token, bool breakWithException = true);
74 void error(const String& msg, bool prevToken = true, bool breakWithException = true);
75
76 void errorUnexpected(const String& hint = "", const Token* token = nullptr, bool breakWithException = false);
77 void errorUnexpected(const Tokens type, const Token* token = nullptr, bool breakWithException = false);
78
79 void errorInternal(const String& msg, const String& procName);
80
81 void warning(const String& msg, const SourceArea& area);
82 void warning(const String& msg, const Token* token);
83 void warning(const String& msg, bool prevToken = true);
84
85 void enableWarnings(bool enable);
86
87 protected: // Scanner
88 virtual ScannerPtr makeScanner() = 0;
89
90 virtual void pushScannerSource(const SourceCodePtr& source, const String& filename = "");
91 virtual bool popScannerSource();
92
94
95 // Returns the current token scanner.
97
98 // Returns the filename for the current scanner source.
100
101 TokenPtr accept(const Tokens type);
102 TokenPtr accept(const Tokens type, const String& spell);
104
105 // Pushes the specified token string onto the stack where further tokens will be parsed from the top of the stack.
106 void pushTokenString(const TokenPtrString& tokenString);
108
109 // Ignores the next tokens if they are white spaces and optionally new lines.
110 void ignoreWhiteSpaces(bool includeNewLines = false, bool includeComments = false);
112
113 protected: // Source area
114 // Sets the source area of the specified AST to area of the 'areaOriginAST' (if not null), and updates the area with the previous scanner token.
115 template<typename T> const T& updateSourceArea(const T& ast, const AST* areaOriginAST = nullptr) {
116 if(areaOriginAST) {
117 ast->area = areaOriginAST->area;
118 }
119 ast->area.update(getScanner().getPreviousToken()->getSourceArea());
120 return ast;
121 }
122
123 // Sets the source area of the specified AST to area of the first origin and updates the area with last origin.
124 template<typename T> const T& updateSourceArea(const T& ast, const ASTPtr& firstAreaOriginAST, const ASTPtr& lastAreaOriginAST) {
125 ast->area = firstAreaOriginAST->area;
126 ast->area.update(*lastAreaOriginAST);
127 return ast;
128 }
129
130 // Sets the source area offset of the specified AST to the source position of the previous scanner token.
131 template<typename T> const T& updateSourceAreaOffset(const T& ast) {
132 ast->area.offset(getScanner().getPreviousToken()->getPosition());
133 return ast;
134 }
135
136 protected: // Parsing
139
145
148
162
164
166 [[nodiscard]] Int32 parseIntLiteral(const String& valueStr, const Token* token = nullptr);
167
168 protected: // Common
169 // Returns the log pointer or null if no log was defined.
170 [[nodiscard]] inline Log* getLog() const { return mLog; }
171
172 // Returns a reference to the report handler.
174
175 // Returns a reference to the name mangling options.
177
178 // Returns a pointer to the name mangling prefix the specified identifier conflicts with, or null if no conflict exists.
179 [[nodiscard]] const String* findNameManglingPrefix(const String& ident) const;
180
181 // Makes a new shared pointer of the specified AST node class.
182 template<typename T, typename... Args> SPtr<T> make(Args&&... args) {
183 return ce_shared_new<T>(getScanner().getPosition(), std::forward<Args>(args)...);
184 }
185
186 // Returns the current token.
187 [[nodiscard]] inline const TokenPtr& getToken() const { return mToken; }
188
189 // Returns the type of the next token.
190 [[nodiscard]] inline Tokens getTokenType() const { return getToken()->getType(); }
191
192 // Returns true if the next token is from the specified type.
193 [[nodiscard]] inline bool is(const Tokens type) const { return (getTokenType() == type); }
194
195 // Returns true if the next token is from the specified type and has the specified spelling.
196 [[nodiscard]] inline bool is(const Tokens type, const String& spell) const { return (getTokenType() == type && getToken()->getSpelling() == spell); }
197
198 private:
199 // Parse left-to-right associative binary expression.
201
203
204 void assertTokenType(const Tokens type);
206 };
207
208} // namespace CeresEngine::ShaderCompiler
Log base class.
Definition Log.hpp:19
Definition Parser.hpp:34
TokenPtr accept(const Tokens type, const String &spell)
bool is(const Tokens type) const
Definition Parser.hpp:193
bool mEnableWarnings
Definition Parser.hpp:60
InitializerList< BinaryOp > BinaryOpList
Definition Parser.hpp:67
Stack< ASTPtr > mPreParsedASTStack
Definition Parser.hpp:55
TernaryExpressionPtr parseTernaryExpression(const ExpressionPtr &condExpression)
void warning(const String &msg, const Token *token)
const String * findNameManglingPrefix(const String &ident) const
void assertTokenSpell(const String &spell)
NameMangling mNameMangling
Definition Parser.hpp:48
TokenPtr mToken
Definition Parser.hpp:51
Stack< ScannerStackEntry > mScannerStack
Definition Parser.hpp:53
Log * mLog
Definition Parser.hpp:50
void pushParsingState(const ParsingState &state)
virtual ScannerPtr makeScanner()=0
ReportHandler & getReportHandler()
Definition Parser.hpp:173
UInt32 mUnexpectedTokenCounter
Definition Parser.hpp:57
void errorUnexpected(const Tokens type, const Token *token=nullptr, bool breakWithException=false)
ExpressionPtr parseLtrBinaryExpression(const UniqueFunction< ExpressionPtr() const > &parseSubExpressionFunc, const BinaryOpList &binaryOps)
const UInt32 unexpectedTokenLimit
Definition Parser.hpp:58
NameMangling & getNameMangling()
Definition Parser.hpp:176
void errorInternal(const String &msg, const String &procName)
ReportHandler mReportHandler
Definition Parser.hpp:47
bool is(const Tokens type, const String &spell) const
Definition Parser.hpp:196
void warning(const String &msg, bool prevToken=true)
void error(const String &msg, bool prevToken=true, bool breakWithException=true)
TokenPtr accept(const Tokens type)
Stack< ParsingState > mParsingStateStack
Definition Parser.hpp:54
Int32 parseIntLiteral(TokenPtr token=nullptr)
void pushPreParsedAST(const ASTPtr &ast)
Pushes the specified AST node onto the stack of pre-parsed AST nodes.
Int32 parseIntLiteral(const String &valueStr, const Token *token=nullptr)
const T & updateSourceAreaOffset(const T &ast)
Definition Parser.hpp:131
const T & updateSourceArea(const T &ast, const ASTPtr &firstAreaOriginAST, const ASTPtr &lastAreaOriginAST)
Definition Parser.hpp:124
void ignoreWhiteSpaces(bool includeNewLines=false, bool includeComments=false)
virtual ExpressionPtr parsePrimaryExpression()=0
virtual void pushScannerSource(const SourceCodePtr &source, const String &filename="")
void pushTokenString(const TokenPtrString &tokenString)
Tokens getTokenType() const
Definition Parser.hpp:190
void warning(const String &msg, const SourceArea &area)
ParsingState getActiveParsingState() const
const T & updateSourceArea(const T &ast, const AST *areaOriginAST=nullptr)
Definition Parser.hpp:115
const TokenPtr & getToken() const
Definition Parser.hpp:187
SPtr< T > make(Args &&... args)
Definition Parser.hpp:182
void errorUnexpected(const String &hint="", const Token *token=nullptr, bool breakWithException=false)
Log * getLog() const
Definition Parser.hpp:170
void assertTokenType(const Tokens type)
void error(const String &msg, const SourceArea &area, bool breakWithException=true)
void error(const String &msg, const Token *token, bool breakWithException=true)
Definition ReportHandler.hpp:31
Definition Scanner.hpp:29
Definition SourceArea.hpp:20
Definition Token.hpp:21
Types
Definition Token.hpp:24
Definition AST.hpp:33
SPtr< Expression > ExpressionPtr
Definition Visitor.hpp:26
SPtr< SourceCode > SourceCodePtr
Definition SourceCode.hpp:66
SPtr< TernaryExpression > TernaryExpressionPtr
Definition Visitor.hpp:70
SPtr< AST > ASTPtr
Definition Visitor.hpp:23
SPtr< Token > TokenPtr
Definition Token.hpp:174
SPtr< Scanner > ScannerPtr
Definition Scanner.hpp:147
std::stack< T, Container > Stack
The Stack class is a container adapter that gives the programmer the functionality of a stack - speci...
Definition Stack.hpp:18
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
std::initializer_list< T > InitializerList
An object of type InitializerList<T> is a lightweight proxy object that provides access to an array o...
Definition InitializerList.hpp:40
std::int32_t Int32
Definition DataTypes.hpp:21
struct CeresEngine::GLState state
std::uint32_t UInt32
Definition DataTypes.hpp:23
FunctionBase< true, false, fu2::capacity_default, true, false, Signatures... > UniqueFunction
An owning non copyable function wrapper for arbitrary callable types.
Definition Function.hpp:59
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
Definition Parser.hpp:41
ScannerPtr scanner
Definition Parser.hpp:42
TokenPtr nextToken
Definition Parser.hpp:44
String filename
Definition Parser.hpp:43