Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Like.cs
2using System.Text.RegularExpressions;
8
10{
16 public delegate string ExpressionTransform(string Expression);
17
22 {
32 : base(Left, Right, Start, Length, Expression)
33 {
34 }
35
40
49 {
50 if (!(Left.AssociatedObjectValue is string sl))
51 throw new ScriptRuntimeException("String values expected.", this);
52
53 if (!(Right.AssociatedObjectValue is string sr))
54 throw new ScriptRuntimeException("String values expected.", this);
55
56 Match M;
57
59 if (!(h is null))
60 sr = h(sr);
61
62 M = this.Matches(sl, sr, out string[] GroupNames);
63
64 if (M.Success)
65 {
66 if (!(GroupNames is null))
67 {
68 foreach (string GroupName in GroupNames)
69 {
70 Group G = M.Groups[GroupName];
71 if (G.Success)
72 {
73 string Value = G.Value;
74
75 if (Expression.TryParse(Value, out double d))
76 Variables[GroupName] = d;
77 else
78 Variables[GroupName] = Value;
79
80 Variables[GroupName + "_STR"] = Value;
81 Variables[GroupName + "_POS"] = G.Index;
82 Variables[GroupName + "_LEN"] = G.Length;
83 }
84 }
85 }
86
87 if (this.partialMatch || (M.Index == 0 && M.Length == sl.Length))
88 return BooleanValue.True;
89 }
90
91 return BooleanValue.False;
92 }
93
101 protected Match Matches(string Value, string Expression, out string[] GroupNames)
102 {
103 lock (this.synchObject)
104 {
105 if (this.lastExpression is null || Expression != this.lastExpression)
106 {
107 this.regex = new Regex(Expression, this.options);
108 this.lastExpression = Expression;
109
110 ChunkedList<string> Names = null;
111
112 foreach (string s in this.regex.GetGroupNames())
113 {
114 if (!int.TryParse(s, out int _))
115 {
116 if (Names is null)
117 Names = new ChunkedList<string>();
118
119 Names.Add(s);
120 }
121 }
122
123 if (Names is null)
124 this.groupNames = null;
125 else
126 this.groupNames = Names.ToArray();
127 }
128
129 GroupNames = this.groupNames;
130
131 return this.regex.Match(Value);
132 }
133 }
134
135 private Regex regex = null;
136 private string[] groupNames = null;
137 private string lastExpression = null;
138 private RegexOptions options = RegexOptions.Singleline;
139 private bool partialMatch = false;
140 private readonly object synchObject = new object();
141
145 public RegexOptions Options
146 {
147 get => this.options;
148 set
149 {
150 lock (this.synchObject)
151 {
152 this.options = value;
153 this.lastExpression = null;
154 }
155 }
156 }
157
161 public bool PartialMatch
162 {
163 get => this.partialMatch;
164 set => this.partialMatch = value;
165 }
166
173 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
174 {
175 if (!(CheckAgainst.AssociatedObjectValue is string sl))
176 return PatternMatchResult.NoMatch;
177
178 if (this.right is ConstantElement RightConstant &&
179 RightConstant.Constant.AssociatedObjectValue is string sr)
180 {
182 if (!(h is null))
183 sr = h(sr);
184
185 Match M = this.Matches(sl, sr, out string[] GroupNames);
186
187 if (M.Success)
188 {
189 if (!(GroupNames is null))
190 {
191 foreach (string GroupName in GroupNames)
192 {
193 Group G = M.Groups[GroupName];
194 if (G.Success)
195 {
196 string Value = G.Value;
197 object ObjValue;
198
199 if (Expression.TryParse(Value, out double d))
200 ObjValue = d;
201 else
202 ObjValue = Value;
203
204 if (AlreadyFound.TryGetValue(GroupName, out IElement E) &&
205 !E.AssociatedObjectValue.Equals(ObjValue))
206 {
207 return PatternMatchResult.NoMatch;
208 }
209
210 AlreadyFound[GroupName] = Expression.Encapsulate(ObjValue);
211 }
212 }
213 }
214
215 return this.left.PatternMatch(CheckAgainst, AlreadyFound);
216 }
217 }
218
219 return PatternMatchResult.NoMatch;
220 }
221
222 }
223}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
static bool TryParse(string s, out double Value)
Tries to parse a double-precision floating-point value.
Definition: Expression.cs:4781
ScriptNode right
Right operand.
ScriptNode left
Left operand.
Base class for binary scalar operators.
Represents a constant element value.
IElement Constant
Constant value.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Boolean-valued number.
Definition: BooleanValue.cs:12
static readonly BooleanValue False
Constant false value.
static readonly BooleanValue True
Constant true value.
RegexOptions Options
Options for regular expression. Default is RegexOptions.Singleline.
Definition: Like.cs:146
ExpressionTransform TransformExpression
Event raised before performing comparison. Can be used to transform expression.
Definition: Like.cs:39
bool PartialMatch
If a partial match is sufficient for operator to return true. (Default=false)
Definition: Like.cs:162
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: Like.cs:173
Match Matches(string Value, string Expression, out string[] GroupNames)
Checks a value against a regular expression.
Definition: Like.cs:101
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: Like.cs:48
Like(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Like
Definition: Like.cs:31
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Definition: ImplTypes.g.cs:58
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
delegate string ExpressionTransform(string Expression)
Delegate for expression transforms.