static public Dictionary <string, Double> GetNameValueTable(IXamlNameResolver nameResolver, string expression, out List <string> unknownNames) { unknownNames = new List <string>(); var allNames = new List <string>(); var nameValueTable = new Dictionary <string, Double>(); var scanner = new MathExpressionScanner(expression); foreach (MathExpressionToken scannerTok in scanner.Scan()) { switch (scannerTok.TokenType) { case MathTokenType.Name: if (!allNames.Contains(scannerTok.Name)) { allNames.Add(scannerTok.Name); } break; } } foreach (string name in allNames) { bool isFullyInit; object obj = nameResolver.Resolve(name, out isFullyInit); if (obj == null) { if (nameResolver.IsFixupTokenAvailable) { unknownNames.Add(name); } else { throw new InvalidOperationException("Could not resolve name {0}"); } } else if (!isFullyInit) { throw new InvalidOperationException(String.Format("Name {0} is not fully initialized", name)); } else { var num = obj as Number; if (num == null) { throw new InvalidOperationException(String.Format("The Name {0} is of type {1} does not resolve to a Number", name, obj.GetType())); } nameValueTable.Add(name, num.Value); } } if (unknownNames.Count == 0) { unknownNames = null; } return(nameValueTable); }
// Comp ::= Exp // Comp ::= Exp == Exp // Exp ::= Term // Exp ::= Term (+/-) Exp // Term ::= Fac // Term ::= Fac (*|/) Term // Fac ::= Number // Fac ::= ( Comp ) public Double Parse(string expression) { _scanner = new MathExpressionScanner(expression); _tokens = _scanner.Scan().GetEnumerator(); _tokens.MoveNext(); CurrentToken = _tokens.Current; Double value = P_Comparison(); return(value); }
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string text = (string)value; if (text == null) { throw new ArgumentException("Missing String Value", "value"); } var nameResolver = (IXamlNameResolver)context; if (nameResolver == null) { throw new ArgumentException("Missing IXamlNameResolver", "context"); } var names = new List <string>(); var scanner = new MathExpressionScanner(text); foreach (MathExpressionToken scannerTok in scanner.Scan()) { switch (scannerTok.TokenType) { case MathTokenType.Name: if (!names.Contains(scannerTok.Name)) { names.Add(scannerTok.Name); } break; } } var unknownNameList = new List <string>(); var nameValueTable = ExpressionServices.GetNameValueTable(nameResolver, text, out unknownNameList); if (unknownNameList != null && unknownNameList.Count > 0) { object fixup = nameResolver.GetFixupToken(unknownNameList); return(fixup); } // if we have the values of all the names, // hand it to the expresssion parser and let 'er rip! // MathExpressionParser parser = new MathExpressionParser(nameValueTable); Double number = parser.Parse(text); return(number); }