@@ -42,6 +42,7 @@ public class MathEvaluator : IDisposable
42
42
private Stack < double > _parameters ;
43
43
private List < string > _innerFunctions ;
44
44
private uint _nestedFunctionDepth ;
45
+ private uint _nestedGroupDepth ;
45
46
private StringReader _expressionReader ;
46
47
private VariableDictionary _variables ;
47
48
private ReadOnlyCollection < string > _functions ;
@@ -64,6 +65,7 @@ public MathEvaluator()
64
65
_calculationStack = new Stack < double > ( ) ;
65
66
_parameters = new Stack < double > ( 2 ) ;
66
67
_nestedFunctionDepth = 0 ;
68
+ _nestedGroupDepth = 0 ;
67
69
}
68
70
69
71
@@ -105,6 +107,7 @@ public double Evaluate(string expression)
105
107
_expressionReader = new StringReader ( expression ) ;
106
108
_symbolStack . Clear ( ) ;
107
109
_nestedFunctionDepth = 0 ;
110
+ _nestedGroupDepth = 0 ;
108
111
_expressionQueue . Clear ( ) ;
109
112
110
113
ParseExpressionToQueue ( ) ;
@@ -268,6 +271,7 @@ private bool TryStartGroup()
268
271
}
269
272
270
273
_symbolStack . Push ( _currentChar . ToString ( ) ) ;
274
+ _nestedGroupDepth ++ ;
271
275
return true ;
272
276
}
273
277
@@ -276,7 +280,8 @@ private bool TryComma()
276
280
if ( _currentChar != ',' )
277
281
return false ;
278
282
279
- if ( _nestedFunctionDepth <= 0 )
283
+ if ( _nestedFunctionDepth <= 0 ||
284
+ _nestedFunctionDepth < _nestedGroupDepth )
280
285
{
281
286
throw new ParseException ( Resources . InvalidCharacterEncountered + _currentChar ) ;
282
287
}
@@ -309,7 +314,7 @@ private bool TryEndGroup()
309
314
310
315
bool hasStart = false ;
311
316
312
- while ( _symbolStack . Count > 0 )
317
+ while ( _symbolStack . Count > 0 )
313
318
{
314
319
string p = _symbolStack . Pop ( ) ;
315
320
if ( p == "(" )
@@ -328,6 +333,8 @@ private bool TryEndGroup()
328
333
_nestedFunctionDepth -- ;
329
334
}
330
335
336
+ _nestedGroupDepth -- ;
337
+
331
338
break ;
332
339
}
333
340
@@ -467,6 +474,12 @@ private double CalculateFromQueue()
467
474
}
468
475
469
476
result = _calculationStack . Pop ( ) ;
477
+
478
+ if ( _calculationStack . Any ( ) )
479
+ {
480
+ throw new ParseException ( String . Format ( "{0}Items '{1}' were remaining on calculation stack." , Resources . InvalidSymbolOnStack , string . Join ( ", " , _calculationStack ) ) ) ;
481
+ }
482
+
470
483
return result ;
471
484
}
472
485
0 commit comments