/// <summary> /// Evaluates the given expression string and returns the result /// </summary> /// <param name="input"></param> /// <param name="location"></param> /// <param name="state"></param> /// <param name="visiting"></param> /// <returns></returns> private string EvaluateEmbeddedExpressions(string input, Location location, Hashtable state, Stack visiting) { if (input == null) { return(null); } if (input.IndexOf('$') < 0) { return(input); } try { StringBuilder output = new StringBuilder(input.Length); ExpressionTokenizer tokenizer = new ExpressionTokenizer(); ExpressionEvaluator eval = new ExpressionEvaluator(Project, this, state, visiting); tokenizer.IgnoreWhitespace = false; tokenizer.SingleCharacterMode = true; tokenizer.InitTokenizer(input); while (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) { if (tokenizer.CurrentToken == ExpressionTokenizer.TokenType.Dollar) { tokenizer.GetNextToken(); if (tokenizer.CurrentToken == ExpressionTokenizer.TokenType.LeftCurlyBrace) { tokenizer.IgnoreWhitespace = true; tokenizer.SingleCharacterMode = false; tokenizer.GetNextToken(); string val = Convert.ToString(eval.Evaluate(tokenizer), CultureInfo.InvariantCulture); output.Append(val); tokenizer.IgnoreWhitespace = false; if (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.RightCurlyBrace) { throw new ExpressionParseException("'}' expected", tokenizer.CurrentPosition.CharIndex); } tokenizer.SingleCharacterMode = true; tokenizer.GetNextToken(); } else { output.Append('$'); if (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) { output.Append(tokenizer.TokenText); tokenizer.GetNextToken(); } } } else { output.Append(tokenizer.TokenText); tokenizer.GetNextToken(); } } return(output.ToString()); } catch (ExpressionParseException ex) { StringBuilder errorMessage = new StringBuilder(); string reformattedInput = input; // replace CR, LF and TAB with a space reformattedInput = reformattedInput.Replace('\n', ' '); reformattedInput = reformattedInput.Replace('\r', ' '); reformattedInput = reformattedInput.Replace('\t', ' '); errorMessage.Append(ex.Message); errorMessage.Append(Environment.NewLine); string label = "Expression: "; errorMessage.Append(label); errorMessage.Append(reformattedInput); int p0 = ex.StartPos; int p1 = ex.EndPos; if (p0 != -1 || p1 != -1) { errorMessage.Append(Environment.NewLine); if (p1 == -1) { p1 = p0 + 1; } for (int i = 0; i < p0 + label.Length; ++i) { errorMessage.Append(' '); } for (int i = p0; i < p1; ++i) { errorMessage.Append('^'); } } throw new BuildException(errorMessage.ToString(), location, ex.InnerException); } }
/// <summary> /// Evaluates the given expression string and returns the result /// </summary> /// <param name="input"></param> /// <param name="location"></param> /// <param name="state"></param> /// <param name="visiting"></param> /// <returns></returns> private string EvaluateEmbeddedExpressions(string input, Location location, Hashtable state, Stack visiting) { if (input == null) { return null; } if (input.IndexOf('$') < 0) { return input; } try { StringBuilder output = new StringBuilder(input.Length); ExpressionTokenizer tokenizer = new ExpressionTokenizer(); ExpressionEvaluator eval = new ExpressionEvaluator(Project, this, state, visiting); tokenizer.IgnoreWhitespace = false; tokenizer.SingleCharacterMode = true; tokenizer.InitTokenizer(input); while (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) { if (tokenizer.CurrentToken == ExpressionTokenizer.TokenType.Dollar) { tokenizer.GetNextToken(); if (tokenizer.CurrentToken == ExpressionTokenizer.TokenType.LeftCurlyBrace) { tokenizer.IgnoreWhitespace = true; tokenizer.SingleCharacterMode = false; tokenizer.GetNextToken(); string val = Convert.ToString(eval.Evaluate(tokenizer), CultureInfo.InvariantCulture); output.Append(val); tokenizer.IgnoreWhitespace = false; if (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.RightCurlyBrace) { throw new ExpressionParseException("'}' expected", tokenizer.CurrentPosition.CharIndex); } tokenizer.SingleCharacterMode = true; tokenizer.GetNextToken(); } else { output.Append('$'); if (tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) { output.Append(tokenizer.TokenText); tokenizer.GetNextToken(); } } } else { output.Append(tokenizer.TokenText); tokenizer.GetNextToken(); } } return output.ToString(); } catch (ExpressionParseException ex) { StringBuilder errorMessage = new StringBuilder(); string reformattedInput = input; // replace CR, LF and TAB with a space reformattedInput = reformattedInput.Replace('\n', ' '); reformattedInput = reformattedInput.Replace('\r', ' '); reformattedInput = reformattedInput.Replace('\t', ' '); errorMessage.Append(ex.Message); errorMessage.Append(Environment.NewLine); string label = "Expression: "; errorMessage.Append(label); errorMessage.Append(reformattedInput); int p0 = ex.StartPos; int p1 = ex.EndPos; if (p0 != -1 || p1 != -1) { errorMessage.Append(Environment.NewLine); if (p1 == -1) p1 = p0 + 1; for (int i = 0; i < p0 + label.Length; ++i) errorMessage.Append(' '); for (int i = p0; i < p1; ++i) errorMessage.Append('^'); } throw new BuildException(errorMessage.ToString(), location, ex.InnerException); } }