CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Scanner.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
11
15
19
23
24#include <functional>
25
27
28 // Scanner base class.
29 class Scanner {
30 private:
32 char mChr = 0;
33
34 Log* mLog = nullptr;
35
39
41
42 // Active commentary string (in front of the next token).
45 bool mCommentFirstLine = false;
46
47 public:
48 Scanner(Log* log = nullptr);
49 virtual ~Scanner() = default;
50
51 // Starts scanning the specified source code.
52 bool scanSource(const SourceCodePtr& source);
53
54 // Pushes the specified token string onto the stack where further tokens will be parsed from the top of the stack.
55 void pushTokenString(const TokenPtrString& tokenString);
57
58 // Returns the iterator of the top most token string on the stack.
59 [[nodiscard]] TokenPtrString::ConstIterator getTopTokenStringIterator() const;
60
61 // Scanes the source code for the next token
62 virtual TokenPtr next() = 0;
63
64 // Returns the token previously returned by the "Next" function.
66
67 // Returns the token previously returned by the "Next" function.
69
70 // Returns the start position of the token previously returned by the "Next" function.
71 [[nodiscard]] inline const SourcePosition& getPosition() const { return mNextStartPos; }
72
73 // Returns the source code which is currently being scanned.
74 [[nodiscard]] inline SourceCode* getSource() const { return mSource.get(); }
75
76 // Returns the source code which is currently being scanned.
77 [[nodiscard]] inline const SourceCodePtr& getSharedSource() const { return mSource; }
78
79 // Returns the active commentary string, which is in front of the next token.
80 [[nodiscard]] inline const String& getComment() const { return mComment; }
81
82 protected:
84
86
88
89 virtual TokenPtr scanToken() = 0;
90
91 char take(char chr);
92 char takeIt();
93
94 TokenPtr make(const Token::Types& type, bool takeChr = false);
95 TokenPtr make(const Token::Types& type, String& spell, bool takeChr = false);
96 TokenPtr make(const Token::Types& type, String& spell, const SourcePosition& pos, bool takeChr = false);
97
98 protected: // Report Handling
99 // Throws an instance of the exception Report class.
100 [[noreturn]] void error(const String& msg);
101
102 // Throws an 'unexpected character' error.
104
105 // Throws an 'unexpected character' error with suggestion which character was expected.
107
108 // Throws an 'unexpected end-of-stream' error.
110
111 protected: // Scanning
112 // Ignores all characters which comply the specified predicate.
113 void ignore(const UniqueFunction<bool(char) const>& pred);
115
124
126
127 protected: // Helper functions
128 // Returns true if the next character is a new-line character (i.e. '\n' or '\r').
129 [[nodiscard]] inline bool isNewLine() const { return (mChr == '\n' || mChr == '\r'); }
130
131 // Returns true if the next character is equal to the specified character.
132 [[nodiscard]] inline bool is(const char chr) const { return (mChr == chr); }
133
134 // Returns the next character.
135 [[nodiscard]] inline char chr() const { return mChr; }
136
137 // Returns the next character as 'unsigned char'.
138 [[nodiscard]] inline unsigned char uChr() const { return static_cast<unsigned char>(mChr); }
139
140 private:
142
143 void appendComment(const String& s);
145 };
146
148
149} // namespace CeresEngine::ShaderCompiler
Log base class.
Definition Log.hpp:19
Definition Scanner.hpp:29
bool scanSource(const SourceCodePtr &source)
const String & getComment() const
Definition Scanner.hpp:80
TokenPtr nextToken(bool scanComments, bool scanWhiteSpaces)
void ignoreWhiteSpaces(bool includeNewLines=true)
TokenPtr scanNumber(bool startWithPeriod=false)
TokenPtr scanWhiteSpaces(bool includeNewLines=true)
TokenPtr nextTokenScan(bool scanComments, bool scanWhiteSpaces)
Log * mLog
Definition Scanner.hpp:34
SourcePosition mNextStartPos
Definition Scanner.hpp:36
TokenPtr mPrevToken
Definition Scanner.hpp:38
void pushTokenString(const TokenPtrString &tokenString)
bool isNewLine() const
Definition Scanner.hpp:129
const SourcePosition & getPosition() const
Definition Scanner.hpp:71
Vector< TokenPtrString::ConstIterator > mTokenStringItStack
Definition Scanner.hpp:40
SourceCode * getSource() const
Definition Scanner.hpp:74
String mComment
Definition Scanner.hpp:43
TokenPtr scanVarArg(String &spell)
unsigned char uChr() const
Definition Scanner.hpp:138
bool mCommentFirstLine
Definition Scanner.hpp:45
UInt32 mCommentStartPos
Definition Scanner.hpp:44
TokenPtr mActiveToken
Definition Scanner.hpp:37
const SourceCodePtr & getSharedSource() const
Definition Scanner.hpp:77
void appendComment(const String &s)
void errorUnexpected(char expectedChar)
char mChr
Definition Scanner.hpp:32
TokenPtr make(const Token::Types &type, bool takeChr=false)
void appendMultiLineComment(const String &s)
bool is(const char chr) const
Definition Scanner.hpp:132
TokenPtr make(const Token::Types &type, String &spell, const SourcePosition &pos, bool takeChr=false)
TokenPtr make(const Token::Types &type, String &spell, bool takeChr=false)
TokenPtr scanCommentBlock(bool scanComments)
SourceCodePtr mSource
Definition Scanner.hpp:31
void ignore(const UniqueFunction< bool(char) const > &pred)
bool scanDigitSequence(String &spell)
void error(const String &msg)
TokenPtr scanCommentLine(bool scanComments)
char chr() const
Definition Scanner.hpp:135
TokenPtrString::ConstIterator getTopTokenStringIterator() const
Definition SourceCode.hpp:24
Definition SourcePosition.hpp:30
Types
Definition Token.hpp:24
Definition AST.hpp:33
SPtr< SourceCode > SourceCodePtr
Definition SourceCode.hpp:66
SPtr< Token > TokenPtr
Definition Token.hpp:174
SPtr< Scanner > ScannerPtr
Definition Scanner.hpp:147
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::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
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