CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
TokenString.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 "Token.hpp"
11
13
14#include <ostream>
15
17
18 void assertReachedEnd(bool reachedEnd);
20
27 template<typename TokenType, typename TokenOfInterestFunctor> class BasicTokenString {
28 public:
31
33 public:
34 ConstIterator() = default;
35
36 ConstIterator(const ConstIterator&) = default;
38
39 ConstIterator(const typename Container::const_iterator& it, const typename Container::const_iterator& itEnd) : mIt{it}, mItEnd{itEnd} {
41 }
42
44 if(mIt != mItEnd) {
45 ++mIt;
47 }
48 return *this;
49 }
50
52 ConstIterator tmp = *this;
53 operator++();
54 return tmp;
55 }
56
57 [[nodiscard]] bool reachedEnd() const { return (mIt == mItEnd); }
58
61 return *((*this)++);
62 }
63
66 assertCurrentTokenType((*mIt)->getType(), type);
67 return acceptIt();
68 }
69
70 const ValueType& operator*() { return *mIt; }
71
72 typename Container::const_iterator operator->() { return mIt; }
73
74 private:
76 while(mIt != mItEnd && !TokenOfInterestFunctor::isOfInterest(*mIt)) {
77 ++mIt;
78 }
79 }
80
81 typename Container::const_iterator mIt, mItEnd;
82 };
83
84 BasicTokenString() = default;
87
89
90 [[nodiscard]] ConstIterator begin() const { return ConstIterator(mTokens.begin(), mTokens.end()); }
91
92 [[nodiscard]] ConstIterator end() const { return ConstIterator(mTokens.end(), mTokens.end()); }
93
94 void pushBack(const TokenType& token) { mTokens.push_back(token); }
95
96 void pushBack(const BasicTokenString& tokenString) { mTokens.insert(mTokens.end(), tokenString.mTokens.begin(), tokenString.mTokens.end()); }
97
98 void popFront() { mTokens.erase(mTokens.begin()); }
99
100 void popBack() { mTokens.pop_back(); }
101
102 [[nodiscard]] const ValueType& front() const { return mTokens.front(); }
103
104 [[nodiscard]] const ValueType& back() const { return mTokens.back(); }
105
106 [[nodiscard]] bool empty() const { return mTokens.empty(); }
107
108 [[nodiscard]] const Container& getTokens() const { return mTokens; }
109
111
112 // Removes all tokens that are not of interest for the specified token string from the front.
113 void trimFront() {
114 while(!empty() && !TokenOfInterestFunctor::isOfInterest(front())) {
115 popFront();
116 }
117 }
118
119 // Removes all tokens that are not of interest for the specified token string from the back.
120 void trimBack() {
121 while(!empty() && !TokenOfInterestFunctor::isOfInterest(back())) {
122 popBack();
123 }
124 }
125
126 private:
128 };
129
130 // ---------------------------------------------------------------------------------------------
131
132 template<typename TokenType, typename TokenOfInterestFunctor>
135 auto lhsIt = lhs.begin();
136 auto rhsIt = rhs.begin();
137
139 for(; (!lhsIt.reachedEnd() && !rhsIt.reachedEnd()); ++lhsIt, ++rhsIt) {
140 auto lhstoken = lhsIt->get();
141 auto rhstoken = rhsIt->get();
142
144 if(lhstoken->getType() != rhstoken->getType()) {
145 return false;
146 }
147
149 if(lhstoken->getSpelling() != rhstoken->getSpelling()) {
150 return false;
151 }
152 }
153
155 return (lhsIt.reachedEnd() && rhsIt.reachedEnd());
156 }
157
158 template<typename TokenType, typename TokenOfInterestFunctor>
162
163 template<typename TokenType, typename TokenOfInterestFunctor>
164 std::ostream& operator<<(std::ostream& lhs, const BasicTokenString<TokenType, TokenOfInterestFunctor>& rhs) {
165 for(const auto& token : rhs.getTokens()) {
166 lhs << token->getSpelling();
167 }
168 return lhs;
169 }
170
172 static bool isOfInterest(const TokenPtr& token);
173 };
174
176
177} // namespace CeresEngine::ShaderCompiler
bool reachedEnd() const
Definition TokenString.hpp:57
ValueType accept(const Token::Types type)
Definition TokenString.hpp:64
ConstIterator & operator=(const ConstIterator &)=default
ConstIterator & operator++()
Definition TokenString.hpp:43
ConstIterator(const typename Container::const_iterator &it, const typename Container::const_iterator &itEnd)
Definition TokenString.hpp:39
void nextTokenOfInterest()
Definition TokenString.hpp:75
const ValueType & operator*()
Definition TokenString.hpp:70
ValueType acceptIt()
Definition TokenString.hpp:59
Container::const_iterator mIt
Definition TokenString.hpp:81
Container::const_iterator mItEnd
Definition TokenString.hpp:81
ConstIterator operator++(int)
Definition TokenString.hpp:51
Container::const_iterator operator->()
Definition TokenString.hpp:72
Token string template class.
Definition TokenString.hpp:27
void trimBack()
Definition TokenString.hpp:120
void popFront()
Definition TokenString.hpp:98
Container & getTokens()
Definition TokenString.hpp:110
ConstIterator begin() const
Definition TokenString.hpp:90
const ValueType & back() const
Definition TokenString.hpp:104
BasicTokenString(const BasicTokenString &)=default
TokenType ValueType
Definition TokenString.hpp:29
void pushBack(const TokenType &token)
Definition TokenString.hpp:94
const ValueType & front() const
Definition TokenString.hpp:102
const Container & getTokens() const
Definition TokenString.hpp:108
ConstIterator end() const
Definition TokenString.hpp:92
bool empty() const
Definition TokenString.hpp:106
BasicTokenString & operator=(const BasicTokenString &)=default
void trimFront()
Definition TokenString.hpp:113
BasicTokenString(const TokenType &token)
Definition TokenString.hpp:88
void popBack()
Definition TokenString.hpp:100
Vector< TokenType > Container
Definition TokenString.hpp:30
Container mTokens
Definition TokenString.hpp:127
void pushBack(const BasicTokenString &tokenString)
Definition TokenString.hpp:96
Types
Definition Token.hpp:24
Definition AST.hpp:33
std::ostream & operator<<(std::ostream &lhs, const BasicTokenString< TokenType, TokenOfInterestFunctor > &rhs)
Definition TokenString.hpp:164
bool operator!=(const Identifier &lhs, const Identifier &rhs)
Definition Identifier.hpp:64
void assertCurrentTokenType(const Token::Types type, const Token::Types expectedType)
bool operator==(const Identifier &lhs, const Identifier &rhs)
Definition Identifier.hpp:60
SPtr< Token > TokenPtr
Definition Token.hpp:174
void assertReachedEnd(bool reachedEnd)
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25