ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
13#endif // !DOXYGEN
14
15/// Utility types of camp \alib_expressions.
17
19 Compiler* pCompiler,
20 SPFormatter formatter,
21 character separatorChar )
22: compiler ( pCompiler )
23, stdFormatter ( formatter )
24, originalFormatString( pFormatString )
25{
26 // use ALib standard formatter, if no dedicated instance was given.
27 if(!formatter.Get())
29
30 // parse format string
31 integer nonExprPortionStart= 0;
32 integer parsePos = 0;
33 while(parsePos < originalFormatString.Length() )
34 {
35 // has next parse position?
36 // Note: if bracket is found at the end of string, we just ignore this here. An according
37 // exception is thrown in formatter later.
38 if( ( parsePos= originalFormatString.IndexOf( A_CHAR('{'), parsePos ) ) < 0
39 || parsePos == originalFormatString.Length() - 1 )
40 {
41 formatStringStripped << originalFormatString.Substring( nonExprPortionStart );
42 break;
43 }
44
45 // double Escape character? -> ignore
46 ++parsePos;
47 if( originalFormatString[parsePos] == A_CHAR('{') )
48 {
49 ++parsePos;
50 continue;
51 }
52
53 // add current portion to format string
54 formatStringStripped << originalFormatString.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
55
56 // Either find separator character or closing bracket of placeholder
57 integer endPos= parsePos;
58 while( endPos < originalFormatString.Length() -1
59 && originalFormatString[endPos] != separatorChar
60 && originalFormatString[endPos] != A_CHAR('}') )
61 ++endPos;
62
63 // extract expression string and set start of non-expression portion
64 String expressionString= originalFormatString.Substring( parsePos, endPos - parsePos );
65 nonExprPortionStart= endPos;
66 if( originalFormatString[endPos] == separatorChar )
67 ++nonExprPortionStart;
68
69 // add expression
70 try
71 {
72 expressions.emplace_back( compiler->Compile( expressionString ) );
73 }
74 catch( Exception& e)
75 {
78 throw;
79 }
80 }
81}
82
83
85{
86 // evaluate expressions and collect boxes
87 auto& results= stdFormatter->GetArgContainer();
88 results.Add( formatStringStripped );
89
90 try
91 {
92 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo )
93 results.Add( expressions[expressionNo]->Evaluate( scope ) );
94 }
95 catch( Exception& e)
96 {
98 expressions.size() + 1, originalFormatString );
99 throw;
100 }
101
102 try
103 {
104 stdFormatter->FormatArgs( target );
105 }
106 catch(Exception& e)
107 {
109 originalFormatString );
110 throw;
111 }
112}
113
114} // namespace [alib::expressions::util]
115
virtual ALIB_API Expression Compile(const String &expressionString)
Definition compiler.cpp:242
Compiler * compiler
The expression compiler (as given in the constructor).
AString formatStringStripped
The resulting format string passed to ALib formatters.
const AString originalFormatString
The original format string. Used only for exception information.
ALIB_API ExpressionFormatter(const String formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
std::vector< Expression > expressions
The expressions functions to receive the format data.
ALIB_API void Format(AString &target, expressions::Scope &scope)
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
static ALIB_API SPFormatter Default
constexpr integer Length() const
Definition string.hpp:326
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:406
#define ALIB_CALLER_NULLED
Definition alib.hpp:1173
#define A_CHAR(STR)
Utility types of camp ALib Expressions.
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273