ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.cpp
1// #################################################################################################
2// ALib C++ Library
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 "Symbol 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;
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{
42 // use ALib standard formatter, if no dedicated instance was given.
43 if(!formatter.Get())
45
46 // parse format string
47 integer nonExprPortionStart= 0;
48 integer parsePos = 0;
49 while(parsePos < originalFormatString.Length() )
50 {
51 // has next parse position?
52 // Note: if bracket is found at the end of string, we just ignore this here. An according
53 // exception is thrown in formatter later.
54 if( ( parsePos= originalFormatString.IndexOf( A_CHAR('{'), parsePos ) ) < 0
55 || parsePos == originalFormatString.Length() - 1 )
56 {
57 formatStringStripped << originalFormatString.Substring( nonExprPortionStart );
58 break;
59 }
60
61 // double Escape character? -> ignore
62 ++parsePos;
63 if( originalFormatString[parsePos] == A_CHAR('{') )
64 {
65 ++parsePos;
66 continue;
67 }
68
69 // add current portion to format string
70 formatStringStripped << originalFormatString.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
71
72 // Either find separator character or closing bracket of placeholder
73 integer endPos= parsePos;
74 while( endPos < originalFormatString.Length() -1
75 && originalFormatString[endPos] != separatorChar
76 && originalFormatString[endPos] != A_CHAR('}') )
77 ++endPos;
78
79 // extract expression string and set start of non-expression portion
80 String expressionString= originalFormatString.Substring( parsePos, endPos - parsePos );
81 nonExprPortionStart= endPos;
82 if( originalFormatString[endPos] == separatorChar )
83 ++nonExprPortionStart;
84
85 // add expression
86 try
87 {
88 expressions.emplace_back( compiler->Compile( expressionString ) );
89 }
90 catch( Exception& e)
91 {
94 throw;
95 }
96 }
97}
98
99
101{
102 // evaluate expressions and collect boxes
103 auto& results= stdFormatter->GetArgContainer();
104 results.Add( formatStringStripped );
105
106 try
107 {
108 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo )
109 results.Add( expressions[expressionNo]->Evaluate( scope ) );
110 }
111 catch( Exception& e)
112 {
114 expressions.size() + 1, originalFormatString );
115 throw;
116 }
117
118 try
119 {
120 stdFormatter->FormatArgs( target );
121 }
122 catch(Exception& e)
123 {
126 throw;
127 }
128}
129
130} // namespace [alib::expressions::util]
131
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.
ALIB_DLL ExpressionFormatter(const String formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
ALIB_DLL void Format(AString &target, expressions::Scope &scope)
const AString originalFormatString
The original format string. Used only for exception information.
std::vector< Expression > expressions
The expressions functions to receive the format data.
static ALIB_DLL SPFormatter Default
#define ALIB_CALLER_NULLED
Definition alib.inl:1010
#define A_CHAR(STR)
Utility types of camp ALib Expressions.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
exceptions::Exception Exception
Type alias in namespace alib.
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.