// Checks whether there is an argument separator (e.g. ',') before the end of the // function call. E.g. returns true for "a,b)" and "a(b,c),d)" and false for "b),c". public static bool SeparatorExists(ParsingScript script) { if (!script.StillValid()) { return(false); } int argumentList = 0; for (int i = script.Pointer; i < script.Size(); i++) { char ch = script.At(i); switch (ch) { case Constants.NEXT_ARG: return(true); case Constants.START_ARG: argumentList++; break; case Constants.END_STATEMENT: case Constants.END_GROUP: case Constants.END_ARG: if (--argumentList < 0) { return(false); } break; } } return(false); }
public static string GetBodyBetween(ParsingScript script, char open = Constants.START_ARG, char close = Constants.END_ARG) { // We are supposed to be one char after the beginning of the string, i.e. // we must not have the opening char as the first one. StringBuilder sb = new StringBuilder(script.Size()); int braces = 0; bool inQuotes = false; bool inQuotes1 = false; bool inQuotes2 = false; bool checkBraces = true; char prev = Constants.EMPTY; char prevprev = Constants.EMPTY; for (; script.StillValid(); script.Forward()) { char ch = script.Current; if (close != Constants.QUOTE) { checkBraces = !inQuotes; if (ch == Constants.QUOTE && !inQuotes1 && (prev != '\\' || prevprev == '\\')) { inQuotes = inQuotes2 = !inQuotes2; } if (ch == Constants.QUOTE1 && !inQuotes2 && (prev != '\\' || prevprev == '\\')) { inQuotes = inQuotes1 = !inQuotes1; } } if (string.IsNullOrWhiteSpace(ch.ToString()) && sb.Length == 0) { continue; } else if (checkBraces && ch == open) { braces++; } else if (checkBraces && ch == close) { braces--; } sb.Append(ch); prevprev = prev; prev = ch; if (braces < 0) { if (ch == close) { sb.Remove(sb.Length - 1, 1); } break; } } return(sb.ToString()); }
public static string GetBodyBetween(ParsingScript script, char open, char close) { // We are supposed to be one char after the beginning of the string, i.e. // we must not have the opening char as the first one. StringBuilder sb = new StringBuilder(script.Size()); int braces = 0; for (; script.StillValid(); script.Forward()) { char ch = script.Current; if (string.IsNullOrWhiteSpace(ch.ToString()) && sb.Length == 0) { continue; } else if (ch == open) { braces++; } else if (ch == close) { braces--; } sb.Append(ch); if (braces == -1) { if (ch == close) { sb.Remove(sb.Length - 1, 1); } break; } } return(sb.ToString()); }
public static string GetToken(ParsingScript script, char [] to) { char curr = script.TryCurrent(); char prev = script.TryPrev(); if (!to.Contains(Constants.SPACE)) { // Skip a leading space unless we are inside of quotes while (curr == Constants.SPACE && prev != Constants.QUOTE) { script.Forward(); curr = script.TryCurrent(); prev = script.TryPrev(); } } // String in quotes bool inQuotes = curr == Constants.QUOTE; if (inQuotes) { int qend = script.Find(Constants.QUOTE, script.Pointer + 1); if (qend == -1) { throw new ArgumentException("Unmatched quotes in [" + script.FromPrev() + "]"); } string result = script.Substr(script.Pointer + 1, qend - script.Pointer - 1); script.Pointer = qend + 1; return(result); } script.MoveForwardIf(Constants.QUOTE); int end = script.FindFirstOf(to); end = end < 0 ? script.Size() : end; // Skip found characters that have a backslash before. while (end > 0 && end + 1 < script.Size() && script.String [end - 1] == '\\') { end = script.FindFirstOf(to, end + 1); } end = end < 0 ? script.Size() : end; if (script.At(end - 1) == Constants.QUOTE) { end--; } string var = script.Substr(script.Pointer, end - script.Pointer); // \"yes\" --> "yes" var = var.Replace("\\\"", "\""); script.Pointer = end; script.MoveForwardIf(Constants.QUOTE, Constants.SPACE); return(var); }