private bool check(char ch) { if (_reader.Peek() == ch) { _reader.Read(); return(true); } return(false); }
/// Parse command line into the list of arguments. Mimics .NET command line parsing logic where possible public static string[] SplitArgs(string commandLine) { if (commandLine == null) { return(null); } bool quoted = false; List <string> args = new List <string>(); StringBuilder sb = new StringBuilder(); using (var reader = new ParsingReader(new StringReader(commandLine))) { int n; do { n = reader.Read(); if (n == '"') { quoted = true; while (!reader.IsEOF) { var ch = (char)reader.Read(); if (ch == '"') { break; } sb.Append(ch); } } else if (n == -1 || char.IsWhiteSpace((char)n)) { if (sb.Length > 0 || quoted) { args.Add(sb.ToString()); } sb.Length = 0; quoted = false; } else { sb.Append((char)n); } } while (n != -1); } return(args.ToArray()); }
/// <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> /// Parse stream and return the expression tree /// </summary> /// <param name="r">Stream</param> /// <returns>Parsed expression or null if no expression is found on stream</returns> public IOperation Parse(ParsingReader r) { List <IOperation> data = new List <IOperation>(); while (!r.IsEOF) { TokenQueue tokenQueue = new TokenQueue(this, r); var ex = parseSingleStatement(tokenQueue, -1); if (ex == null) { break; } if (data.Count > 0) { data.Add(new Operations.OperationPop()); } data.Add(ex); r.SkipWhiteSpaceAndComments(); if (r.Peek() != ';') { break; } do { r.Read(); r.SkipWhiteSpaceAndComments(); } while (r.Peek() == ';'); } if (data.Count == 0) { return(null); } if (data.Count == 1) { return(data[0]); } return(new Operations.OperationExpression(data.ToArray())); }
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); }
/// Parse multi-expression, like ${a|b|=2+3} public IOperation ParseMulti(ParsingReader reader) { Operations.OperationVariableAccess va = new Operations.OperationVariableAccess(); StringBuilder sb = new StringBuilder(); bool orMet = true; int n; while ((n = reader.Peek()) != -1) { var q = (char)n; if (q == '}' || q == ')' || q == ']') { break; } switch (q) { case '|': reader.Read(); va.AddName(sb.ToString()); sb.Length = 0; orMet = true; break; case '\'': case '\"': case '`': orMet = false; if (sb.Length != 0) { reader.ThrowParsingException("Quote must be a first character"); } va.AddValue(reader.ReadQuote()); reader.SkipWhiteSpaceAndComments(); if (reader.Peek() != '}' && reader.Peek() != '|') { reader.ThrowParsingException("Unexpected character '" + (char)reader.Peek() + "'"); } sb.Length = 0; break; case '=': orMet = false; reader.Read(); var v = Parse(reader); if (v != null) { va.AddExpression(v); } else { va.AddValue(null); } break; default: orMet = false; sb.Append(q); reader.Read(); break; } } if (sb.Length > 0 || orMet) { va.AddName(sb.ToString()); } return(va); }