/// Read expression from string, evaluate it and return the value public object Eval(string expression) { if (expression == null) { return(null); } if (_exprCache == null) { _exprCache = new PrecompiledCache(128); } IOperation ex = _exprCache[expression]; if (ex == null) { using (var sr = new ParsingReader(new StringReader(expression))) { ex = new Parser(TokenSpecialCharacters).Parse(sr); sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); _exprCache[expression] = ex; } } if (ex == null) { return(null); } return(Eval(ex)); }
/// <summary> /// Try to get variable value /// </summary> /// <param name="key">Variable name</param> /// <param name="value">Where to store found variable value</param> /// <returns>true if variable is set</returns> public override bool TryGetValue(string key, out object value) { if (!IsCalculated(key)) { return(base.TryGetValue(key, out value)); } using (var sr = new ParsingReader(key)) { sr.SkipWhiteSpace(); value = sr.ReadNumber(); if (value != null) { sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); return(true); } } value = EvalMulti(key); return(true); }
/// <summary> /// Load script from string /// </summary> /// <param name="scriptXml">script as XML</param> public void Load(string scriptXml) { Items.Clear(); bool close = true; var r = new ParsingReader(new StringReader(scriptXml)); try { r.SkipWhiteSpace(); if (r.Peek() == '<') { // Get a script file XmlReaderSettings rs = new XmlReaderSettings(); rs.IgnoreWhitespace = false; rs.ValidationType = ValidationType.None; using (XmlReader xr = XmlReader.Create(r, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment })) { close = false; Load(xr); return; } } if (r.Peek() == '=') { r.Read(); Items.Add(new Eval { Value = r.ReadToEnd() }); return; } Items.Add(new Code { Value = r.ReadToEnd() }); return; } finally { if (close) { r.Close(); } } }
/// <summary> /// Try to get variable value /// </summary> /// <param name="key">Variable name</param> /// <param name="value">Where to store found variable value</param> /// <returns>true if variable is set</returns> public override bool TryGetValue(string key, out object value) { if (!IsCalculated(key)) { return(base.TryGetValue(key, out value)); } using (var sr = new ParsingReader(key)) { sr.SkipWhiteSpace(); value = sr.ReadNumber(); if (value != null) { sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); return(true); } } if (!string.IsNullOrEmpty(key)) { switch (key[0]) { case '%': EnvironmentVariableTarget target = getTarget(ref key); value = Environment.GetEnvironmentVariable(key, target); return(value != null); case '~': value = Find(key.Substring(1), true); return(true); } } value = EvalMulti(key); return(true); }
/// Read multi-expression (like ${a|b|=3+5}) from the string, evaluate it and return the value public object EvalMulti(string multiExpression) { if (multiExpression == null) { return(null); } if (_exprCache == null) { _exprCache = new PrecompiledCache(128); } IOperation o = _exprCache[multiExpression]; if (o == null) { using (var sr = new ParsingReader(new StringReader(multiExpression))) { o = new Parser(TokenSpecialCharacters).ParseMulti(sr); sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); _exprCache[multiExpression] = o; } } return(Eval(o)); }
private object expandVars(TransformRules rules, string s) { string begin; string end; if ((rules & TransformRules.ExpandDual) == TransformRules.ExpandDual) { begin = "${{"; end = "}}"; } else if ((rules & TransformRules.ExpandDualSquare) == TransformRules.ExpandDualSquare) { begin = "[["; end = "]]"; } else if ((rules & TransformRules.ExpandSquare) == TransformRules.ExpandSquare) { begin = "["; end = "]"; } else if ((rules & TransformRules.Expand) == TransformRules.Expand) { begin = "${"; end = "}"; } else { return(s); } if (s.IndexOf(begin, StringComparison.Ordinal) != -1) { StringBuilder sbNew = new StringBuilder(); using (var sr = new ParsingReader(new StringReader(s))) { int ptr = 0; bool first = true; while (!sr.IsEOF) { char ch = (char)sr.Read(); if (ch != begin[ptr]) { sbNew.Append(begin, 0, ptr); sbNew.Append(ch); ptr = 0; first = false; continue; } ptr++; if (ptr < begin.Length) { continue; } if (sr.Peek() == '{' || sr.Peek() == '[') { sbNew.Append(begin); ptr = 0; first = false; continue; } // object sv = EvalMulti(sr); sv = ((rules & TransformRules.ExpandTrimOnly) == TransformRules.ExpandTrimOnly) ? Utils.TransformStr(Utils.To <string>(sv), rules & TransformRules.TrimMask) : sv; sv = ((rules & TransformRules.ExpandReplaceOnly) == TransformRules.ExpandReplaceOnly) ? Utils.TransformStr(Utils.To <string>(sv), rules & TransformRules.ReplaceMask) : sv; // Now read the trailing stuff sr.SkipWhiteSpace(); for (ptr = 0; ptr < end.Length; ++ptr) { sr.ReadAndThrowIfNot(end[ptr]); } if (sr.IsEOF && first) { return(sv); } ptr = 0; first = false; sbNew.Append(Utils.To <string>(sv)); } for (int i = 0; i < ptr; ++i) { sbNew.Append(begin[i]); } } return(sbNew.ToString()); } return(s); }
private IEnumerable <Vars> getTransformedRows(string columnsOverride, string sortOverride, string whereExpression) { bool overrideSort = (sortOverride != null); if (!overrideSort) { sortOverride = Context.TransformStr(SortColumns, Transform); } // ColumnsInfo ci = new ColumnsInfo(); // Load CSV, if present string text = GetTransformedValueStr(); string quote = Context.TransformStr(Quote, Transform); string separator = Context.TransformStr(Separator, Transform); char? quoteChar = string.IsNullOrEmpty(quote) ? (char?)null : quote[0]; bool trim = (Options & RowsetOptions.Trim) != 0; string colNames = Context.TransformStr(Columns, Transform);; // Read additional column names from the first CSV row, if specified ParsingReader csvReader = null; if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text.Trim())) { csvReader = new ParsingReader(new StringReader(text)); if ((Options & RowsetOptions.Header) == RowsetOptions.Header) { csvReader.SkipWhiteSpace(); colNames = Context.TransformStr(csvReader.ReadLine(), Verbatim ? TransformRules.None : Transform); } } char sep = string.IsNullOrEmpty(separator) ? '\0' : separator[0]; // Add these columns if (!string.IsNullOrEmpty(colNames)) { ci.AddParsed(Context, colNames, quoteChar, sep, trim); } // Add extra columns if (Cols != null) { foreach (var col in Cols) { ColumnInfo cc = new ColumnInfo(); cc.Name = Context.TransformStr(col.Name, col.Transform); cc.Value = Context.TransformStr(col.Value, col.Transform); cc.IsDefault = (col.Default != null); if (cc.IsDefault) { cc.Default = Context.Transform(col.Default, col.Transform); } object t = Context.Transform(col.Type, col.Transform); if (t != null) { if (t is Type) { cc.Type = (Type)t; } else { cc.Type = Context.FindType(t.ToString()); } } ci.Add(cc); } } SortInfos si = new SortInfos(sortOverride, quoteChar, sep, trim); if (!overrideSort && SortCols != null) { foreach (var col in SortCols) { var ss = new SortInfo { Name = Context.TransformStr(col.Name, col.Transform), Sort = col.Sort }; var cmp = Context.Transform(col.Comparer, Transform); if (cmp != null) { object c1 = null; if (cmp is Type) { ss.Comparer = (IComparer)Utils.CreateInstance((Type)cmp); } else if (cmp is string && !string.IsNullOrEmpty((string)cmp)) { string smp = (string)cmp; if (smp.ToUpperInvariant() == "IGNORECASE" || smp.ToUpperInvariant() == "NOCASE" || smp.ToUpperInvariant() == "IC") { ss.Comparer = StringComparer.CurrentCultureIgnoreCase; } else if (Utils.TryGetProperty(null, typeof(StringComparer), smp, null, false, out c1)) { ss.Comparer = (IComparer)c1; } else { ss.Comparer = (IComparer)Utils.CreateInstance(Context.FindType(smp)); } } else { ss.Comparer = (IComparer)cmp; } } si.Add(ss); } } var ret = si.SortAndFilter(Context, columnsOverride, whereExpression, getTransformedRowsInternal(ci, csvReader)); if (columnsOverride == null) { return(ret); } ci = new ColumnsInfo(); ci.AddParsed(Context, columnsOverride, quoteChar, separator[0], trim); return(ci.Process(ret)); }