UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ExpressionParserTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "ExpressionParserTypesFwd.h" // IWYU pragma: export
8#include "Containers/Array.h"
10#include "Templates/Function.h"
11#include "Containers/Set.h"
12#include "Containers/Map.h"
13#include "Misc/Optional.h"
16#include "Misc/Guid.h"
18#include <type_traits>
19
20namespace Impl
21{
23}
24
27{
29 : Text(InText)
30 {
31 }
32
34};
35
37template <typename CharType>
39{
40public:
41 TStringToken() = default;
42
45 {
46 return TString<CharType>::ConstructFromPtrSize(TokenStart, (int32)(TokenEnd - TokenStart));
47 }
48
50 bool IsValid() const
51 {
52 return TokenEnd != TokenStart;
53 }
54
56 const CharType* GetTokenStartPos() const
57 {
58 return TokenStart;
59 }
60
62 const CharType* GetTokenEndPos() const
63 {
64 return TokenEnd;
65 }
66
69 {
70 return CharacterIndex;
71 }
72
75 {
76 return LineNumber;
77 }
78
80 void Accumulate(const TStringToken& InToken)
81 {
82 if (InToken.TokenEnd > TokenEnd)
83 {
84 TokenEnd = InToken.TokenEnd;
85 }
86 }
87
88protected:
89 friend class TTokenStream<CharType>;
90
91 explicit TStringToken(const CharType* InStart, int32 Line = 0, int32 Character = 0)
96 {
97 }
98
99 const CharType* TokenStart = nullptr;
100 const CharType* TokenEnd = nullptr;
103};
104
106enum class EParseState
107{
109 Continue,
111 StopAfter,
113 StopBefore,
115 Cancel,
116};
117
119template <typename CharType>
121{
122public:
130
134
137
140
143
146
147public:
149 CORE_API explicit TTokenStream(const CharType* In);
150
152 CORE_API CharType PeekChar(int32 Offset = 0) const;
153
156
158 CORE_API bool IsReadPosValid(const CharType* InPos, int32 MinNumChars = 1) const;
159
161 CORE_API bool IsEmpty() const;
162
164 CORE_API int32 GetPosition() const;
165
166 const CharType* GetStart() const
167 {
168 return Start;
169 }
170 const CharType* GetRead() const
171 {
172 return ReadPos;
173 }
174 const CharType* GetEnd() const
175 {
176 return End;
177 }
178
180 CORE_API FString GetErrorContext() const;
181
183 CORE_API void SetReadPos(const TStringToken<CharType>& Token);
184
185private:
187 const CharType* Start;
189 const CharType* End;
191 const CharType* ReadPos;
192};
193
196#define DEFINE_EXPRESSION_NODE_TYPE(TYPE, ...) \
197template<> struct TGetExpressionNodeTypeId<TYPE>\
198{\
199 static const FGuid& GetTypeId()\
200 {\
201 static FGuid Global(__VA_ARGS__);\
202 return Global;\
203 }\
204};
205template<typename T> struct TGetExpressionNodeTypeId;
206
208DEFINE_EXPRESSION_NODE_TYPE(bool, 0xCACBC715, 0x505A6B4A, 0x8808809F, 0x897AA5F6)
210
217{
218public:
220 FExpressionNode() = default;
221
223 template <
224 typename T
225 UE_REQUIRES(!std::is_convertible_v<T*, FExpressionNode*>)
226 >
227 FExpressionNode(T In);
228
230
231 // Movable, but non copyable
236
238 CORE_API const FGuid& GetTypeId() const;
239
241 template<typename T>
242 const T* Cast() const;
243
246
247private:
249 static constexpr uint32 MaxStackAllocationSize = 64 - sizeof(FGuid);
250
254
256 FGuid TypeId;
257 alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) uint8 InlineBytes[MaxStackAllocationSize];
258};
259
261template <typename CharType>
274
276template <typename CharType>
292
295{
299
301 {
302 return A.OperatorType == B.OperatorType &&
303 A.LeftOperandType == B.LeftOperandType &&
304 A.RightOperandType == B.RightOperandType;
305 }
306
312};
313
315template <typename ContextType, typename CharType>
317{
319 FExpressionResult ExecPreUnary(const TExpressionToken<CharType>& Operator, const TExpressionToken<CharType>& R, const ContextType* Context) const;
321 FExpressionResult ExecPostUnary(const TExpressionToken<CharType>& Operator, const TExpressionToken<CharType>& L, const ContextType* Context) const;
325 bool ShouldShortCircuit(const TExpressionToken<CharType>& Operator, const TExpressionToken<CharType>& L, const ContextType* Context) const;
326
347 template<typename OperatorType, typename FuncType>
348 void MapPreUnary(FuncType InFunc);
349
354 template<typename OperatorType, typename FuncType>
355 void MapPostUnary(FuncType InFunc);
356
388 template<typename OperatorType, typename FuncType>
389 void MapBinary(FuncType InFunc);
390
391 template<typename OperatorType, typename FuncType>
392 void MapShortCircuit(FuncType InFunc);
393
394public:
397 using FShortCircuit = TFunction<bool(const FExpressionNode&, const ContextType* Context)>;
398
399private:
405};
406
410template <typename CharType>
422template <typename ContextType, typename CharType>
424{
426 : Operators(InOperators)
427 , Context(InContext)
428 {
429 }
430
432 {
433 return Operators.ExecPreUnary(Operator, R, Context);
434 }
436 {
437 return Operators.ExecPostUnary(Operator, L, Context);
438 }
440 {
441 return Operators.ExecBinary(Operator, L, R, Context);
442 }
443 virtual bool ShouldShortCircuit(const TExpressionToken<CharType>& Operator, const TExpressionToken<CharType>& L) const override
444 {
445 return Operators.ShouldShortCircuit(Operator, L, Context);
446 }
447
448private:
449 const TOperatorJumpTable<ContextType>& Operators;
450 const ContextType* Context;
451};
452
454template <typename CharType>
486
492template <typename CharType>
494
495
497template <typename CharType>
499{
500public:
501 TTokenDefinitions() = default;
502
505
508
511
514
515private:
517
518private:
519 bool bIgnoreWhitespace = false;
521};
522
523
528{
531};
532
554
557{
558public:
560 template<typename StartGroupType, typename EndGroupType>
565
567 template<typename ExpressionNodeType>
572
574 template<typename ExpressionNodeType>
579
587 template<typename ExpressionNodeType>
589 {
590#if DO_CHECK
591 for (TMap<FGuid, FOpParameters>::TConstIterator It(BinaryOperators); It; ++It)
592 {
593 const FOpParameters& CurValue = It.Value();
594
595 if (CurValue.Precedence == InPrecedence)
596 {
597 // Operators of the same precedence, must all have the same associativity
599 }
600 }
601#endif
602
604 }
605
606public:
607
609 CORE_API const FGuid* GetGrouping(const FGuid& TypeId) const;
610
612 CORE_API bool HasPreUnaryOperator(const FGuid& TypeId) const;
613
615 CORE_API bool HasPostUnaryOperator(const FGuid& TypeId) const;
616
619
620private:
621
622 TMap<FGuid, FGuid> Groupings;
623 TSet<FGuid> PreUnaryOperators;
624 TSet<FGuid> PostUnaryOperators;
625 TMap<FGuid, FOpParameters> BinaryOperators;
626};
627
628extern template class TTokenStream<ANSICHAR>;
629extern template class TTokenStream<UTF8CHAR>;
630extern template class TTokenStream<WIDECHAR>;
631
632extern template class TExpressionTokenConsumer<ANSICHAR>;
633extern template class TExpressionTokenConsumer<UTF8CHAR>;
634extern template class TExpressionTokenConsumer<WIDECHAR>;
635
636extern template class TTokenDefinitions<ANSICHAR>;
637extern template class TTokenDefinitions<WIDECHAR>;
638extern template class TTokenDefinitions<UTF8CHAR>;
639
640#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_6
642#endif
643
644#include "Misc/ExpressionParserTypes.inl" // IWYU pragma: export
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
TCopyQualifiersFromTo_T< From, To > * Cast(From *Src)
Definition Casts.h:95
TValueOrError< FExpressionNode, FExpressionError > FExpressionResult
Definition ExpressionParserTypesFwd.h:35
EAssociativity
Definition ExpressionParserTypes.h:528
#define DEFINE_EXPRESSION_NODE_TYPE(TYPE,...)
Definition ExpressionParserTypes.h:196
EParseState
Definition ExpressionParserTypes.h:107
const bool
Definition NetworkReplayStreaming.h:178
#define UE_REQUIRES(...)
Definition Requires.h:86
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32 Offset
Definition VulkanMemory.cpp:4033
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ExpressionParserTypes.h:557
CORE_API const FGuid * GetGrouping(const FGuid &TypeId) const
Definition ExpressionParser.cpp:454
void DefinePreUnaryOperator()
Definition ExpressionParserTypes.h:568
void DefineBinaryOperator(int32 InPrecedence, EAssociativity InAssociativity=EAssociativity::RightToLeft, bool bCanShortCircuit=false)
Definition ExpressionParserTypes.h:588
void DefineGrouping()
Definition ExpressionParserTypes.h:561
void DefinePostUnaryOperator()
Definition ExpressionParserTypes.h:575
CORE_API bool HasPostUnaryOperator(const FGuid &TypeId) const
Definition ExpressionParser.cpp:464
CORE_API const FOpParameters * GetBinaryOperatorDefParameters(const FGuid &TypeId) const
Definition ExpressionParser.cpp:469
CORE_API bool HasPreUnaryOperator(const FGuid &TypeId) const
Definition ExpressionParser.cpp:459
Definition ExpressionParserTypes.h:217
FExpressionNode()=default
FExpressionNode(const FExpressionNode &)=delete
FExpressionNode & operator=(const FExpressionNode &)=delete
Definition Text.h:385
Definition Array.h:670
Definition ExpressionParserTypes.h:456
TExpressionTokenConsumer & operator=(TExpressionTokenConsumer &&)=delete
CORE_API TTokenStream< CharType > & GetStream()
Definition ExpressionParser.cpp:303
TExpressionTokenConsumer(TExpressionTokenConsumer &&)=delete
~TExpressionTokenConsumer()=default
CORE_API TArray< TExpressionToken< CharType > > Extract()
Definition ExpressionParser.cpp:288
TExpressionTokenConsumer(const TExpressionTokenConsumer &)=delete
TExpressionTokenConsumer & operator=(const TExpressionTokenConsumer &)=delete
Definition ExpressionParserTypes.h:263
TStringToken< CharType > Context
Definition ExpressionParserTypes.h:272
FExpressionNode Node
Definition ExpressionParserTypes.h:271
TExpressionToken(const TStringToken< CharType > &InContext, FExpressionNode InNode)
Definition ExpressionParserTypes.h:265
Definition AssetRegistryState.h:50
Definition AndroidPlatformMisc.h:14
Definition UnrealString.h.inl:34
Definition ExpressionParserTypes.h:39
bool IsValid() const
Definition ExpressionParserTypes.h:50
int32 LineNumber
Definition ExpressionParserTypes.h:101
int32 GetLineNumber() const
Definition ExpressionParserTypes.h:74
TStringToken(const CharType *InStart, int32 Line=0, int32 Character=0)
Definition ExpressionParserTypes.h:91
TStringToken()=default
const CharType * GetTokenStartPos() const
Definition ExpressionParserTypes.h:56
int32 CharacterIndex
Definition ExpressionParserTypes.h:102
const CharType * TokenEnd
Definition ExpressionParserTypes.h:100
const CharType * TokenStart
Definition ExpressionParserTypes.h:99
TString< CharType > GetString() const
Definition ExpressionParserTypes.h:44
const CharType * GetTokenEndPos() const
Definition ExpressionParserTypes.h:62
int32 GetCharacterIndex() const
Definition ExpressionParserTypes.h:68
void Accumulate(const TStringToken &InToken)
Definition ExpressionParserTypes.h:80
Definition ExpressionParserTypes.h:499
CORE_API void IgnoreWhitespace()
Definition ExpressionParser.cpp:309
CORE_API bool DoesIgnoreWhitespace()
Definition ExpressionParser.cpp:321
TTokenDefinitions()=default
CORE_API TOptional< FExpressionError > ConsumeTokens(TExpressionTokenConsumer< CharType > &Consumer) const
Definition ExpressionParser.cpp:373
CORE_API void DefineToken(TFunction< TExpressionDefinition< CharType > > &&Definition)
Definition ExpressionParser.cpp:315
Definition ExpressionParserTypes.h:121
CORE_API CharType PeekChar(int32 Offset=0) const
Definition ExpressionParser.cpp:29
CORE_API void SetReadPos(const TStringToken< CharType > &Token)
Definition ExpressionParser.cpp:273
CORE_API TOptional< TStringToken< CharType > > GenerateToken(int32 NumChars, TStringToken< CharType > *Accumulate=nullptr) const
Definition ExpressionParser.cpp:254
CORE_API bool IsReadPosValid(const CharType *InPos, int32 MinNumChars=1) const
Definition ExpressionParser.cpp:23
CORE_API int32 CharsRemaining() const
Definition ExpressionParser.cpp:40
CORE_API TOptional< TStringToken< CharType > > ParseSymbol(TStringToken< CharType > *Accumulate=nullptr) const
Definition ExpressionParser.cpp:132
CORE_API TOptional< TStringToken< CharType > > ParseWhitespace(TStringToken< CharType > *Accumulate=nullptr) const
Definition ExpressionParser.cpp:241
CORE_API TOptional< TStringToken< CharType > > ParseToken(TFunctionRef< EParseState(CharType)> Pred, TStringToken< CharType > *Accumulate=nullptr) const
Definition ExpressionParser.cpp:86
const CharType * GetRead() const
Definition ExpressionParserTypes.h:170
CORE_API FString GetErrorContext() const
Definition ExpressionParser.cpp:58
CORE_API TOptional< TStringToken< CharType > > ParseTokenIgnoreCase(const CharType *Symbol, TStringToken< CharType > *Accumulate=nullptr) const
Definition ExpressionParser.cpp:213
CORE_API int32 GetPosition() const
Definition ExpressionParser.cpp:52
CORE_API bool IsEmpty() const
Definition ExpressionParser.cpp:46
const CharType * GetEnd() const
Definition ExpressionParserTypes.h:174
const CharType * GetStart() const
Definition ExpressionParserTypes.h:166
Definition ValueOrError.h:58
Definition ExpressionParserTypes.h:21
Definition ExpressionParserTypes.h:27
FExpressionError(const FText &InText)
Definition ExpressionParserTypes.h:28
FText Text
Definition ExpressionParserTypes.h:33
Definition Guid.h:109
Definition ExpressionParserTypes.h:537
int32 Precedence
Definition ExpressionParserTypes.h:539
bool bCanShortCircuit
Definition ExpressionParserTypes.h:545
FOpParameters(int32 InPrecedence, EAssociativity InAssociativity, bool bInCanShortCircuit)
Definition ExpressionParserTypes.h:547
EAssociativity Associativity
Definition ExpressionParserTypes.h:542
Definition ExpressionParserTypes.h:295
friend bool operator==(const FOperatorFunctionID &A, const FOperatorFunctionID &B)
Definition ExpressionParserTypes.h:300
FGuid LeftOperandType
Definition ExpressionParserTypes.h:297
FGuid OperatorType
Definition ExpressionParserTypes.h:296
friend uint32 GetTypeHash(const FOperatorFunctionID &In)
Definition ExpressionParserTypes.h:307
FGuid RightOperandType
Definition ExpressionParserTypes.h:298
Definition ExpressionParserTypes.inl:18
Definition ExpressionParserTypes.h:278
TOptional< int32 > ShortCircuitIndex
Definition ExpressionParserTypes.h:290
EType
Definition ExpressionParserTypes.h:280
@ PreUnaryOperator
Definition ExpressionParserTypes.h:280
@ Benign
Definition ExpressionParserTypes.h:280
@ ShortCircuit
Definition ExpressionParserTypes.h:280
@ PostUnaryOperator
Definition ExpressionParserTypes.h:280
@ BinaryOperator
Definition ExpressionParserTypes.h:280
@ Operand
Definition ExpressionParserTypes.h:280
TCompiledToken(EType InType, TExpressionToken< CharType > InToken, TOptional< int32 > InShortCircuitIndex=TOptional< int32 >())
Definition ExpressionParserTypes.h:282
EType Type
Definition ExpressionParserTypes.h:289
Definition ExpressionParserTypes.h:205
Definition ExpressionParserTypes.h:412
virtual FExpressionResult ExecPreUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &R) const =0
virtual FExpressionResult ExecBinary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L, const TExpressionToken< CharType > &R) const =0
virtual bool ShouldShortCircuit(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L) const =0
virtual FExpressionResult ExecPostUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L) const =0
Definition ExpressionParserTypes.h:424
virtual FExpressionResult ExecPreUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &R) const override
Definition ExpressionParserTypes.h:431
virtual FExpressionResult ExecBinary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L, const TExpressionToken< CharType > &R) const override
Definition ExpressionParserTypes.h:439
virtual FExpressionResult ExecPostUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L) const override
Definition ExpressionParserTypes.h:435
TOperatorEvaluationEnvironment(const TOperatorJumpTable< ContextType, CharType > &InOperators, const ContextType *InContext)
Definition ExpressionParserTypes.h:425
virtual bool ShouldShortCircuit(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L) const override
Definition ExpressionParserTypes.h:443
Definition ExpressionParserTypes.h:317
FExpressionResult ExecPreUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &R, const ContextType *Context) const
Definition ExpressionParserTypes.inl:316
void MapPostUnary(FuncType InFunc)
Definition ExpressionParserTypes.inl:362
FExpressionResult ExecPostUnary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L, const ContextType *Context) const
Definition ExpressionParserTypes.inl:331
bool ShouldShortCircuit(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L, const ContextType *Context) const
Definition ExpressionParserTypes.inl:304
FExpressionResult ExecBinary(const TExpressionToken< CharType > &Operator, const TExpressionToken< CharType > &L, const TExpressionToken< CharType > &R, const ContextType *Context) const
Definition ExpressionParserTypes.inl:288
void MapPreUnary(FuncType InFunc)
Definition ExpressionParserTypes.inl:347
void MapBinary(FuncType InFunc)
Definition ExpressionParserTypes.inl:377
void MapShortCircuit(FuncType InFunc)
Definition ExpressionParserTypes.inl:393
Definition Optional.h:131