ECExpressions.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include "ECInstanceIterable.h"
11 #include "ECInstance.h"
12 
13 
14 #define EXPR_TYPEDEFS(_name_) \
15  BEGIN_BENTLEY_ECOBJECT_NAMESPACE \
16  struct _name_; \
17  typedef _name_ * _name_##P; \
18  typedef _name_ & _name_##R; \
19  typedef _name_ const* _name_##CP; \
20  typedef _name_ const& _name_##CR; \
21  END_BENTLEY_ECOBJECT_NAMESPACE
22 
23 EXPR_TYPEDEFS(ArgumentTreeNode)
24 EXPR_TYPEDEFS(ArithmeticNode)
25 EXPR_TYPEDEFS(UnitSpecNode)
26 EXPR_TYPEDEFS(BinaryNode)
27 EXPR_TYPEDEFS(CallNode)
28 EXPR_TYPEDEFS(LambdaNode)
29 EXPR_TYPEDEFS(ComparisonNode)
30 EXPR_TYPEDEFS(ConcatenateNode)
32 EXPR_TYPEDEFS(CustomSymbol)
33 EXPR_TYPEDEFS(DivideNode)
34 EXPR_TYPEDEFS(DotNode)
35 EXPR_TYPEDEFS(ErrorNode)
36 EXPR_TYPEDEFS(EvaluationResult)
39 EXPR_TYPEDEFS(ExpressionType)
40 EXPR_TYPEDEFS(IValueListResult)
41 EXPR_TYPEDEFS(LambdaValue)
42 EXPR_TYPEDEFS(IdentNode)
43 EXPR_TYPEDEFS(IIfNode)
46 EXPR_TYPEDEFS(LBracketNode)
48 EXPR_TYPEDEFS(LogicalNode)
49 EXPR_TYPEDEFS(MethodReference)
51 EXPR_TYPEDEFS(MultiplyNode)
54 EXPR_TYPEDEFS(PlusMinusNode)
55 EXPR_TYPEDEFS(PrimaryListNode)
56 EXPR_TYPEDEFS(ReferenceResult)
57 EXPR_TYPEDEFS(ResolvedTypeNode)
58 EXPR_TYPEDEFS(ShiftNode)
61 EXPR_TYPEDEFS(UnaryArithmeticNode)
64 EXPR_TYPEDEFS(IECSymbolProvider)
65 
67 
68 typedef RefCountedPtr<ArgumentTreeNode> ArgumentTreeNodePtr;
69 typedef RefCountedPtr<CallNode> CallNodePtr;
70 typedef RefCountedPtr<LambdaNode> LambdaNodePtr;
72 typedef RefCountedPtr<DotNode> DotNodePtr;
73 typedef RefCountedPtr<ErrorNode> ErrorNodePtr;
74 typedef RefCountedPtr<ExpressionType> ExpressionTypePtr;
75 typedef RefCountedPtr<IValueListResult> IValueListResultPtr;
76 typedef RefCountedPtr<LambdaValue> LambdaValuePtr;
78 typedef RefCountedPtr<IdentNode> IdentNodePtr;
79 typedef RefCountedPtr<UnitSpecNode> UnitSpecNodePtr;
82 typedef RefCountedPtr<LBracketNode> LBracketNodePtr;
83 typedef RefCountedPtr<Lexer> LexerPtr;
84 typedef RefCountedPtr<MethodReference> MethodReferencePtr;
87 typedef RefCountedPtr<PrimaryListNode> PrimaryListNodePtr;
88 typedef RefCountedPtr<ResolvedTypeNode> ResolvedTypeNodePtr;
93 
94 typedef bvector<NodeP> NodeVector;
95 typedef bvector<NodeCP> NodeCPVector;
96 typedef NodeVector::iterator NodeVectorIterator;
97 
98 typedef bvector<NodePtr> NodePtrVector;
99 typedef NodePtrVector::iterator NodePtrVectorIterator;
100 
101 typedef bvector<EvaluationResult> EvaluationResultVector;
102 typedef EvaluationResultVector::iterator EvaluationResultVectorIterator;
103 
104 
105 typedef ExpressionStatus (*ExpressionStaticMethod_t)(EvaluationResult& evalResult, EvaluationResultVector& arguments);
106 typedef ExpressionStatus (*ExpressionInstanceMethod_t)(EvaluationResult& evalResult, ECInstanceListCR instanceList, EvaluationResultVector& arguments);
107 // NB: We could generalize Instance methods to take any EvaluationResult as the calling object, but we'd have to refactor all our existing instance method implementations to
108 // check that the caller is an ECInstanceList...rather keep value list methods separate from instance list methods...
109 typedef ExpressionStatus (*ExpressionValueListMethod_t)(EvaluationResult& evalResult, IValueListResultCR valueList, EvaluationResultVector& arguments);
110 
111 
114 
115 
116 
117 /*=================================================================================**/
121 {
122 
126  ECOBJECTS_EXPORT bool AllowsTypeConversion() const;
127 
130  ECOBJECTS_EXPORT bool EnforcesUnits() const;
131 
137  ECOBJECTS_EXPORT bool EnforceGlobalRepresentation () const;
138 
141  ECOBJECTS_EXPORT EvaluationOptions GetEvaluationOptions() const;
142 
146  ECOBJECTS_EXPORT void SetEvaluationOptions (EvaluationOptions options);
147 }; // End of class ExpressionContext
148 
149 //=================================================================================//
151 //+===============+===============+===============+===============+===============+======//
153  {
154 public:
159  ECOBJECTS_EXPORT static InstanceListExpressionContextPtr Create (bvector<IECInstancePtr> const& instances, ExpressionContextP outer = NULL);
160  };
161 
162 /*---------------------------------------------------------------------------------**/
167  {
168 public:
173 
176  ECOBJECTS_EXPORT void SetInstance (IECInstanceCR instance);
177 
180  ECOBJECTS_EXPORT void SetInstances (ECInstanceListCR instances);
181  };
182 
183 /*=================================================================================**/
187 {
188 public:
190  ECOBJECTS_EXPORT BentleyStatus AddSymbol (SymbolR symbol);
193 }; // End of class SymbolExpressionContext
194 
195 
196 /*=================================================================================**/
202 {
203 
204 }; // End of class Symbol
205 
206 /*=================================================================================**/
211 {
212 
213 public:
218  ECOBJECTS_EXPORT static ContextSymbolPtr CreateContextSymbol(wchar_t const* name, ExpressionContextR context);
219 };
220 
221 /*=================================================================================**/
226 {
227 public:
229  ECOBJECTS_EXPORT static MethodSymbolPtr Create(wchar_t const* name, ExpressionStaticMethod_t staticMethod, ExpressionInstanceMethod_t instanceMethod);
230 };
231 
232 
234  {
236  TOKEN_Null = 16, // Null
240  TOKEN_Is = 20,
244  TOKEN_Slash = 43, // /
245  TOKEN_Comma = 44, // ,
246  TOKEN_IntegerDivide = 45, // \ -- backslash
247  TOKEN_LParen = 46, // (
248  TOKEN_RParen = 47, // )
250  TOKEN_And = 50, // And
251  TOKEN_AndAlso = 51, // AndAlso -- short circuiting And
252  TOKEN_Or = 52, // Or
253  TOKEN_OrElse = 53, // OrElse -- short circuiting Or
255  TOKEN_Mod = 55, // Mod
256  TOKEN_ShiftLeft = 56, // <<
257  TOKEN_ShiftRight = 57, // >>
258  TOKEN_Colon = 58, // : decorates a sub-expression with a unit specification ("expr:UnitName[::Factor[::Offset]]")
264  TOKEN_NotEqual = 64, // <>
265  TOKEN_Not = 65, // "Not"
266  TOKEN_Xor = 66, // "Xor"
268  TOKEN_LeftBracket = 69, // [
270  TOKEN_Dot = 71, // .
271  TOKEN_IIf = 72, // IFF
272  TOKEN_LCurly = 73, // {
273  TOKEN_RCurly = 74, // }
274 
275  TOKEN_If = 75,
279  TOKEN_End = 79,
281 
282  TOKEN_Lambda = 85, // => e.g. "SomeArray.Select (X => X.Name)", "SomeArray.AnyMatches (val => val > 10)"
283 
285 
286  TOKEN_Error = 181,
287  TOKEN_Ident = 182,
294  TOKEN_UnitsConst = 190, // what is this used for...anything...?
302  };
303 
304 
305 /*=================================================================================**/
310 {
312  ECOBJECTS_EXPORT static ValueSymbolPtr Create(wchar_t const* name, ECN::ECValueCR exprValue);
313 
314 }; // End of ValueSymbol
315 
316 /*=================================================================================**/
320 struct NodeVisitor
321 {
322  virtual ~NodeVisitor() {}
324  virtual bool OpenParens() = 0;
326  virtual bool CloseParens() = 0;
328  virtual bool StartArrayIndex(NodeCR node) = 0;
330  virtual bool EndArrayIndex(NodeCR node) = 0;
332  virtual bool StartArguments(NodeCR node) = 0;
334  virtual bool EndArguments(NodeCR node) = 0;
336  virtual bool Comma() = 0;
338  virtual bool ProcessNode(NodeCR node) = 0;
340  virtual bool ProcessUnits (UnitSpecCR units) = 0;
341 };
342 
343 /*=================================================================================**/
348  {
349 public:
351  ECOBJECTS_EXPORT static NodePtr ParseValueExpressionAndCreateTree(wchar_t const* expression);
353  ECOBJECTS_EXPORT static NodePtr ParseAssignmentExpressionAndCreateTree(wchar_t const* expression);
354 
355 }; // End of ECEvaluator class
356 
357 /*=================================================================================**/
361 {
363  ECOBJECTS_EXPORT ExpressionStatus GetECValue (ECN::ECValueR ecValue);
364 };
365 
366 /*=================================================================================**/
371 {
372 private:
373  ExpressionContextPtr m_context;
374 
375 protected:
377  ExpressionStatus PerformArithmeticPromotion(ECN::PrimitiveType&targetType, ResolvedTypeNodePtr& left, ResolvedTypeNodePtr& right);
379  ExpressionStatus PerformJunctionPromotion(ECN::PrimitiveType&targetType, ResolvedTypeNodePtr& left, ResolvedTypeNodePtr& right);
381  ExpressionStatus PromoteToType(ResolvedTypeNodePtr& node, ECN::PrimitiveType targetType);
383  ExpressionStatus PromoteToString(ResolvedTypeNodePtr& node);
385  ExpressionResolver(ExpressionContextR context) { m_context = &context; }
386 
387 public:
389  ExpressionContextCR GetExpressionContext() const { return *m_context; }
391  ExpressionContextR GetExpressionContextR() const { return *m_context; }
392 
394  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolvePrimaryList (PrimaryListNodeR primaryList);
396  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveUnaryArithmeticNode (UnaryArithmeticNodeCR node);
398  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveMultiplyNode (MultiplyNodeCR node);
400  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveDivideNode (DivideNodeCR node);
402  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolvePlusMinusNode (PlusMinusNodeCR node);
404  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveConcatenateNode (ConcatenateNodeCR node);
406  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveComparisonNode (ComparisonNodeCR node);
408  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveLogicalNode (LogicalNodeCR node);
410  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveShiftNode (ShiftNodeCR node);
412  ECOBJECTS_EXPORT virtual ResolvedTypeNodePtr _ResolveIIfNode (IIfNodeCR node);
413 };
414 
415 /*=================================================================================**/
419 {
420 public:
425 
427  ECOBJECTS_EXPORT ExpressionStatus GetValue(ValueResultPtr& valueResult, ExpressionContextR context);
428 
430  ECOBJECTS_EXPORT bool Traverse(NodeVisitorR visitor) const;
431 
433  ECOBJECTS_EXPORT WString ToString() const;
434 
436  ECOBJECTS_EXPORT WString ToExpressionString() const;
437 }; // End of struct Node
438 
441 
NodeVector::iterator NodeVectorIterator
Definition: ECExpressions.h:96
Definition: ECExpressions.h:250
IIfNode const & IIfNodeCR
Definition: ECExpressions.h:43
bvector< NodeCP > NodeCPVector
Definition: ECExpressions.h:95
UnitSpec const & UnitSpecCR
Definition: ECObjects.h:102
Definition: ECExpressions.h:278
Definition: ECExpressions.h:243
Definition: ECExpressions.h:262
bvector< NodePtr > NodePtrVector
Definition: ECExpressions.h:98
Definition: ECExpressions.h:270
PrimaryListNode & PrimaryListNodeR
Definition: ECExpressions.h:55
Definition: ECExpressions.h:263
Definition: ECExpressions.h:252
Definition: ECExpressions.h:268
Definition: ECExpressions.h:248
Definition: ECExpressions.h:265
LogicalNode const & LogicalNodeCR
Definition: ECExpressions.h:48
Definition: ECExpressions.h:286
A context in which multiple IECInstances provide the context for expression evaluation.
Definition: ECExpressions.h:152
ExpressionContextR GetExpressionContextR() const
Returns the ExpressionContext associated with this ExpressionResolver.
Definition: ECExpressions.h:391
Definition: ECExpressions.h:241
Definition: ECExpressions.h:290
ExpressionStatus(* ExpressionInstanceMethod_t)(EvaluationResult &evalResult, ECInstanceListCR instanceList, EvaluationResultVector &arguments)
Definition: ECExpressions.h:106
ExpressionResolver(ExpressionContextR context)
Constructs ExpressionResolver from the specified ExpressionContext.
Definition: ECExpressions.h:385
Base class for all symbol types.
Definition: ECExpressions.h:201
Definition: ECExpressions.h:242
Definition: ECExpressions.h:293
Definition: ECExpressions.h:298
Definition: ECExpressions.h:295
Definition: ECExpressions.h:247
ExpressionStatus
Enumerates the possible return values for evaluating an expression or its value.
Definition: ECObjects.h:340
Definition: ECExpressions.h:297
Definition: ECExpressions.h:292
ECValue const & ECValueCR
Definition: ECObjects.h:46
virtual ~NodeVisitor()
Definition: ECExpressions.h:322
Definition: ECExpressions.h:288
Definition: ECExpressions.h:296
The context in which an expression is evaluated.
Definition: ECExpressions.h:120
ComparisonNode const & ComparisonNodeCR
Definition: ECExpressions.h:29
ExpressionStatus(* ExpressionValueListMethod_t)(EvaluationResult &evalResult, IValueListResultCR valueList, EvaluationResultVector &arguments)
Definition: ECExpressions.h:109
#define NULL
Definition: Bentley.h:157
Definition: ECExpressions.h:264
An InstanceListExpressionContext which simply wraps one or more IECInstances.
Definition: ECExpressions.h:166
UnaryArithmeticNode const & UnaryArithmeticNodeCR
Definition: ECExpressions.h:61
Definition: ECExpressions.h:289
#define ECOBJECTS_EXPORT
Definition: ECObjects.h:20
Definition: ECExpressions.h:259
Parses an EC expression string to produce an expression tree which can be used to evaluate the expres...
Definition: ECExpressions.h:347
PrimitiveType
Enumeration of primitive datatypes supported by native "ECObjects" implementation.
Definition: ECObjects.h:322
Definition: ECExpressions.h:287
Definition: ECExpressions.h:235
Visitor interface for an in-order traversal of the Nodes of an ECExpression tree. ...
Definition: ECExpressions.h:320
Definition: ECExpressions.h:279
Definition: ECExpressions.h:301
Node const * NodeCP
Definition: ECExpressions.h:52
Definition: ECExpressions.h:240
Definition: ECExpressions.h:267
ExpressionToken
Definition: ECExpressions.h:233
Definition: ECExpressions.h:280
Definition: ECExpressions.h:260
bvector< EvaluationResult > EvaluationResultVector
Definition: ECExpressions.h:101
Definition: ECExpressions.h:251
BentleyStatus
Definition: Bentley.h:208
Definition: ECExpressions.h:244
Definition: ECExpressions.h:236
A shared pointer template for reference-counted objects.
Definition: RefCounted.h:119
DivideNode const & DivideNodeCR
Definition: ECExpressions.h:33
Definition: ECExpressions.h:257
IECInstance const & IECInstanceCR
Definition: ECObjects.h:67
EvaluationOptions
Options to be used when evaluating an ECExpression.
Definition: ECObjects.h:261
Definition: ECExpressions.h:254
bvector< NodeP > NodeVector
Definition: ECExpressions.h:94
Variant-like object used to set and retrieve property values in ECInstances.
Definition: ECValue.h:73
Definition: ECExpressions.h:253
Definition: ECExpressions.h:282
Definition: ECExpressions.h:300
#define END_BENTLEY_ECOBJECT_NAMESPACE
Definition: DgnPlatform.h:41
Definition: ECExpressions.h:277
Definition: ECExpressions.h:258
ExpressionStatus(* ExpressionStaticMethod_t)(EvaluationResult &evalResult, EvaluationResultVector &arguments)
Definition: ECExpressions.h:105
Used to introduce a named method into the context.
Definition: ECExpressions.h:225
ECInstanceList const & ECInstanceListCR
Definition: ECInstance.h:609
Definition: ECExpressions.h:275
Definition: ECExpressions.h:239
Definition: ECExpressions.h:256
NodePtrVector::iterator NodePtrVectorIterator
Definition: ECExpressions.h:99
Concrete class that can be used to implement the reference-counting pattern.
Definition: RefCounted.h:109
Definition: ECExpressions.h:246
Definition: ECExpressions.h:245
EvaluationResultVector::iterator EvaluationResultVectorIterator
Definition: ECExpressions.h:102
Definition: ECExpressions.h:299
An object which can optimize an ECExpression tree by resolving constant sub-expressions to literal va...
Definition: ECExpressions.h:370
Definition: ECExpressions.h:273
Defines an expression tree for a parsed EC expression.
Definition: ECExpressions.h:418
Definition: ECExpressions.h:255
PlusMinusNode const & PlusMinusNodeCR
Definition: ECExpressions.h:54
IValueListResult const & IValueListResultCR
Definition: ECExpressions.h:40
Definition: ECExpressions.h:237
ShiftNode const & ShiftNodeCR
Definition: ECExpressions.h:58
Holds the result of evaluating an EC expression.
Definition: ECExpressions.h:360
ExpressionContextCR GetExpressionContext() const
Returns the ExpressionContext associated with this ExpressionResolver.
Definition: ECExpressions.h:389
Definition: ECExpressions.h:271
Definition: ECExpressions.h:291
Definition: ECExpressions.h:266
ConcatenateNode const & ConcatenateNodeCR
Definition: ECExpressions.h:30
Used to give a name to an instance.
Definition: ECExpressions.h:210
ExpressionContext const & ExpressionContextCR
Definition: ECExpressions.h:37
Definition: ECExpressions.h:294
Definition: ECExpressions.h:272
Definition: ECExpressions.h:269
#define EXPR_TYPEDEFS(_name_)
Definition: ECExpressions.h:14
Definition: ECExpressions.h:238
Used to introduce a named value into the context.
Definition: ECExpressions.h:309
MultiplyNode const & MultiplyNodeCR
Definition: ECExpressions.h:51
Definition: ECExpressions.h:276
A context which provides a set of symbols for expression evaluation.
Definition: ECExpressions.h:186
Node const & NodeCR
Definition: ECExpressions.h:52
Definition: ECExpressions.h:284
Definition: ECExpressions.h:261
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:51
#define BEGIN_BENTLEY_ECOBJECT_NAMESPACE
Definition: DgnPlatform.h:40
Definition: ECExpressions.h:249

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.