2222 not be applied to this library, at least for now. There is a list of unimplemented features placed
2323 below
2424
25+ ##Configuration
26+
27+ There are list of available options presented below. To enable some option just use #define directive before inclusion library's header file.
28+ Do not forget to provide this definitions in header files to provide access to specific types in case you need to use them.
29+
30+ \code
31+ #define TCPP_SOME_EXAMPLE_CONFIG
32+ //... other configs
33+ #define TCPP_IMPLEMENTATION
34+ #include "tcppLibrary.hpp
35+ \endcode
36+
37+ * TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED: The config determines which approach is used to provide processed output for a user.
38+ Originally Preprocessor::Process returns output data as a std::string's instance. **TokensOutputStream** is returned with Preprocessor::Process
39+ in case of TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED is defined
40+
2541 \todo Implement support of char literals
2642 \todo Improve existing performance for massive input files
2743 \todo Add support of integral literals like L, u, etc
2844 \todo Implement built-in directives like #pragma, #error and others
29- \todo Provide support of variadic macros
3045*/
3146
3247#pragma once
@@ -180,9 +195,37 @@ namespace tcpp
180195 } TToken, *TTokenPtr;
181196
182197
183- using TTokensSequence = std::vector<TToken>;
198+ using TTokensSequence = std::vector<TToken>;
199+ using TTokensSequenceIter = TTokensSequence::iterator;
200+ using TTokensSequenceConstIter = TTokensSequence::const_iterator;
184201
185202
203+ #ifdef TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED
204+
205+ class TokensOutputStream
206+ {
207+ public:
208+ explicit TokensOutputStream (TTokensSequence tokens) TCPP_NOEXCEPT;
209+
210+ // Methods to provide support of iterating over the sequence using for loop
211+ TTokensSequenceIter begin () TCPP_NOEXCEPT;
212+ TTokensSequenceIter end () TCPP_NOEXCEPT;
213+ TTokensSequenceConstIter begin () const TCPP_NOEXCEPT;
214+ TTokensSequenceConstIter end () const TCPP_NOEXCEPT;
215+
216+ const TToken& GetNextToken () TCPP_NOEXCEPT;
217+ const TToken& PeekNextToken (size_t offset = 1 ) TCPP_NOEXCEPT;
218+ bool HasNextToken () const TCPP_NOEXCEPT;
219+
220+ const TTokensSequence& GetSequence () const TCPP_NOEXCEPT;
221+ private:
222+ TTokensSequence mTokens {};
223+ private:
224+ TTokensSequenceIter mCurrIt {};
225+ };
226+
227+ #endif
228+
186229 /* !
187230 class Lexer
188231
@@ -340,6 +383,14 @@ namespace tcpp
340383 } TIfStackEntry, *TIfStackEntryPtr;
341384
342385 using TIfStack = std::stack<TIfStackEntry>;
386+
387+ using TPreprocessResult =
388+ #ifdef TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED
389+ TokensOutputStream;
390+ #else
391+ TTokensSequence;
392+ #endif
393+
343394 public:
344395 Preprocessor () TCPP_NOEXCEPT = delete ;
345396 Preprocessor (const Preprocessor&) TCPP_NOEXCEPT = delete ;
@@ -348,8 +399,8 @@ namespace tcpp
348399
349400 bool AddCustomDirectiveHandler (const std::string& directive, const TDirectiveHandler& handler) TCPP_NOEXCEPT;
350401
351- TTokensSequence Process () TCPP_NOEXCEPT;
352- static std::string ToString (const TTokensSequence & tokens) TCPP_NOEXCEPT;
402+ TPreprocessResult Process () TCPP_NOEXCEPT;
403+ static std::string ToString (const TPreprocessResult & tokens) TCPP_NOEXCEPT;
353404
354405 Preprocessor& operator = (const Preprocessor&) TCPP_NOEXCEPT = delete ;
355406
@@ -1157,7 +1208,7 @@ namespace tcpp
11571208 }
11581209
11591210
1160- TTokensSequence Preprocessor::Process () TCPP_NOEXCEPT
1211+ Preprocessor::TPreprocessResult Preprocessor::Process () TCPP_NOEXCEPT
11611212 {
11621213 TCPP_ASSERT (mpLexer);
11631214
@@ -1304,10 +1355,14 @@ namespace tcpp
13041355 }
13051356 }
13061357
1358+ #ifdef TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED
1359+ return TokensOutputStream{ processedTokens };
1360+ #else
13071361 return processedTokens;
1362+ #endif
13081363 }
13091364
1310- std::string Preprocessor::ToString (const TTokensSequence & tokens) TCPP_NOEXCEPT
1365+ std::string Preprocessor::ToString (const TPreprocessResult & tokens) TCPP_NOEXCEPT
13111366 {
13121367 std::string output = EMPTY_STR_VALUE;
13131368
@@ -2211,5 +2266,66 @@ namespace tcpp
22112266 return !mConditionalBlocksStack .empty () && (mConditionalBlocksStack .top ().mShouldBeSkipped || !mConditionalBlocksStack .top ().mIsParentBlockActive );
22122267 }
22132268
2269+
2270+ #ifdef TCPP_OUTPUT_TOKENS_EXTENSION_ENABLED
2271+
2272+ TokensOutputStream::TokensOutputStream (TTokensSequence tokens) TCPP_NOEXCEPT
2273+ : mTokens(std::move(tokens)), mCurrIt(mTokens .begin())
2274+ {
2275+ }
2276+
2277+ TTokensSequenceIter TokensOutputStream::begin () TCPP_NOEXCEPT
2278+ {
2279+ return mTokens .begin ();
2280+ }
2281+
2282+ TTokensSequenceIter TokensOutputStream::end () TCPP_NOEXCEPT
2283+ {
2284+ return mTokens .end ();
2285+ }
2286+
2287+ TTokensSequenceConstIter TokensOutputStream::begin () const TCPP_NOEXCEPT
2288+ {
2289+ return mTokens .cbegin ();
2290+ }
2291+
2292+ TTokensSequenceConstIter TokensOutputStream::end () const TCPP_NOEXCEPT
2293+ {
2294+ return mTokens .cend ();
2295+ }
2296+
2297+ const TToken& TokensOutputStream::GetNextToken () TCPP_NOEXCEPT
2298+ {
2299+ if (mCurrIt == mTokens .end ())
2300+ {
2301+ return mTokens .back ();
2302+ }
2303+
2304+ return *mCurrIt ++;
2305+ }
2306+
2307+ const TToken& TokensOutputStream::PeekNextToken (size_t offset) TCPP_NOEXCEPT
2308+ {
2309+ TTokensSequenceIter peekIt = mCurrIt + offset;
2310+ if (peekIt == mTokens .end ())
2311+ {
2312+ return mTokens .back ();
2313+ }
2314+
2315+ return *peekIt;
2316+ }
2317+
2318+ bool TokensOutputStream::HasNextToken () const TCPP_NOEXCEPT
2319+ {
2320+ return mCurrIt != mTokens .end ();
2321+ }
2322+
2323+ const TTokensSequence& TokensOutputStream::GetSequence () const TCPP_NOEXCEPT
2324+ {
2325+ return mTokens ;
2326+ }
2327+
2328+ #endif
2329+
22142330#endif
22152331}
0 commit comments