ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.cpp
1//##################################################################################################
2// ALib C++ Framework
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Configuration MACRO ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Expressions;
20 import ALib.Expressions.Impl;
21 import ALib.Characters.Functions;
22 import ALib.Strings;
23# if ALIB_CAMP
24 import ALib.Camp.Base;
25# endif
26#else
28# include "ALib.Camp.Base.H"
29#endif
30//========================================== Implementation ========================================
31/// Utility types of camp \alib_expressions.
33
35 Compiler* pCompiler,
36 SPFormatter formatter,
37 character separatorChar )
38: compiler ( pCompiler )
39, stdFormatter ( formatter )
40, originalFormatString( pFormatString ) {
41 // use ALib standard formatter, if no dedicated instance was given.
42 if(!formatter.Get())
44
45 String256 escapedFS; escapedFS.DbgDisableBufferReplacementWarning();
46 StringEscaperStandard().Escape(pFormatString, escapedFS, EMPTY_STRING);
47
48 // parse format string
49 integer nonExprPortionStart= 0;
50 integer parsePos = 0;
51 while(parsePos < escapedFS.Length() ) {
52 // has a next parse position?
53 // Note: if bracket is found at the end of string, we just ignore this here. An according
54 // exception is thrown in formatter later.
55 if( ( parsePos= escapedFS.IndexOf( A_CHAR('{'), parsePos ) ) < 0
56 || parsePos == escapedFS.Length() - 1 )
57 {
58 formatStringStripped << escapedFS.Substring( nonExprPortionStart );
59 break;
60 }
61
62 // double Escape character? -> ignore
63 ++parsePos;
64 if( escapedFS[parsePos] == A_CHAR('{') ) {
65 ++parsePos;
66 continue;
67 }
68
69 // add the current portion to format string
70 formatStringStripped << escapedFS.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
71 formatSubstrings.push_back(formatStringStripped.Length() -1);
72
73 // Either find separator character or closing bracket of placeholder
74 integer endPos= parsePos;
75 while( endPos < escapedFS.Length() -1
76 && escapedFS[endPos] != separatorChar
77 && escapedFS[endPos] != A_CHAR('}') )
78 ++endPos;
79
80 // extract expression string and set start of non-expression portion
81 String expressionString= escapedFS.Substring( parsePos, endPos - parsePos );
82 nonExprPortionStart= endPos;
83 if( escapedFS[endPos] == separatorChar )
84 ++nonExprPortionStart;
85
86 // add expression
87 try { expressions.emplace_back( compiler->Compile( expressionString ) ); }
88 catch( Exception& e) {
90 expressions.size() + 1, escapedFS );
91 throw;
92 }
93 }
94 formatSubstrings.push_back(formatStringStripped.Length());
95}
96
97
99 // evaluate expressions and collect boxes
100 try {
101 // the first string is the start-substring.
102 // Attn: must be done with formatter to unescape the string.
103 stdFormatter->Format( target, formatStringStripped.Substring(0, formatSubstrings[0]));
104 size_t idx= 1;
105
106 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo ) {
107 Box exprResult= expressions[expressionNo]->Evaluate( scope );
108
109 try {
110 String formatString= formatStringStripped.Substring( formatSubstrings[idx-1],
111 formatSubstrings[idx] - formatSubstrings[idx-1] );
112 stdFormatter->Format( target, formatString, exprResult );
113 ++idx;
114 }
115 catch(Exception& e) {
118 throw;
119 }
120 }
121 }
122 catch( Exception& e)
123 {
125 expressions.size() + 1, originalFormatString );
126 throw;
127 }
128
129}
130
131} // namespace [alib::expressions::util]
#define ALIB_CALLER_NULLED
Definition alib.inl:1105
#define A_CHAR(STR)
Definition alib.inl:1325
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
Compiler * compiler
The expression compiler (as given in the constructor).
AString formatStringStripped
The resulting format string passed to ALib formatters.
ExpressionFormatter(const String &formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
void Format(AString &target, expressions::Scope &scope)
const AString originalFormatString
The original format string. Used only for exception information.
std::vector< Expression > expressions
The expression functions to receive the format data.
static SPFormatter DEFAULT
Utility types of camp ALib Expressions.
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::util::StringEscaperStandard StringEscaperStandard
Type alias in namespace alib.
Definition escaper.inl:186
strings::TEscape< character > Escape
Type alias in namespace alib.
Definition format.inl:532
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.inl:2234
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1135
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
exceptions::Exception Exception
Type alias in namespace alib.
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.