ALib C++ Library
Library Version: 2511 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;
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 // parse format string
46 integer nonExprPortionStart= 0;
47 integer parsePos = 0;
48 while(parsePos < originalFormatString.Length() ) {
49 // has next parse position?
50 // Note: if bracket is found at the end of string, we just ignore this here. An according
51 // exception is thrown in formatter later.
52 if( ( parsePos= originalFormatString.IndexOf( A_CHAR('{'), parsePos ) ) < 0
53 || parsePos == originalFormatString.Length() - 1 )
54 {
55 formatStringStripped << originalFormatString.Substring( nonExprPortionStart );
56 break;
57 }
58
59 // double Escape character? -> ignore
60 ++parsePos;
61 if( originalFormatString[parsePos] == A_CHAR('{') ) {
62 ++parsePos;
63 continue;
64 }
65
66 // add current portion to format string
67 formatStringStripped << originalFormatString.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
68
69 // Either find separator character or closing bracket of placeholder
70 integer endPos= parsePos;
71 while( endPos < originalFormatString.Length() -1
72 && originalFormatString[endPos] != separatorChar
73 && originalFormatString[endPos] != A_CHAR('}') )
74 ++endPos;
75
76 // extract expression string and set start of non-expression portion
77 String expressionString= originalFormatString.Substring( parsePos, endPos - parsePos );
78 nonExprPortionStart= endPos;
79 if( originalFormatString[endPos] == separatorChar )
80 ++nonExprPortionStart;
81
82 // add expression
83 try
84 {
85 expressions.emplace_back( compiler->Compile( expressionString ) );
86 }
87 catch( Exception& e)
88 {
91 throw;
92} } }
93
94
96 // evaluate expressions and collect boxes
97 auto& results= stdFormatter->GetArgContainer();
98 results.Add( formatStringStripped );
99
100 try
101 {
102 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo )
103 results.Add( expressions[expressionNo]->Evaluate( scope ) );
104 }
105 catch( Exception& e)
106 {
108 expressions.size() + 1, originalFormatString );
109 throw;
110 }
111
112 try
113 {
114 stdFormatter->FormatArgs( target );
115 }
116 catch(Exception& e)
117 {
120 throw;
121} }
122
123} // namespace [alib::expressions::util]
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:1027
#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:2189
characters::character character
Type alias in namespace alib.