public CFValue(CommandFormat format) { type = ""; text = ""; if (format is CommandFormatText) { text = ((CommandFormatText)format).str; if (format is CommandFormatOptionalText) { type = "optionalText"; } else if (format is CommandFormatRegex) { type = "regexText"; } else { type = "text"; } } else if (format is CommandFormatMarker) { if (format is CommandFormatCommand) { type = "command"; } else if (format is CommandFormatArgument) { type = "argument"; } else if (format is CommandFormatValue) { type = "value"; } } }
/// <summary> /// Parses a command string. /// </summary> /// <param name="cdd">The command string to parse.</param> /// <returns>The parsed command, ready to be invoked.</returns> public KeyValuePair <string, Dictionary <string, string> > ParseCommand(string cdd) { string command = ""; Dictionary <string, string> arguments = new Dictionary <string, string>(); string text = cdd; int position = 0; int commandPos; int firstValuePos = -1; int lastValuePos = -1; for (int ii = 0; ii < parts.Count; ii++) { CommandFormat part = parts[ii]; if (part is CommandFormatMarker) { if (part is CommandFormatCommand) { commandPos = ii; } else if (part is CommandFormatValue) { if (firstValuePos > -1) { lastValuePos = ii; } else { firstValuePos = ii; } } } } int i = 0; string currentArgument = ""; int help = -1; while (position < text.Length) { if (i >= parts.Count) { position = text.Length; command = "+FALSE+"; i = 0; } CommandFormat part = parts[i]; string res = part.CheckValidity(text.Substring(position)); // ok so: // example // COMMAND text[ --] ARGUMENT VALUE text[ --] ARGUMENT VALUE // COMMAND text[{] ARGUMENT text[=] VALUE text[, ] ARGUMENT text[=] VALUE text[}] if (part is CommandFormatMarker) { if (part is CommandFormatCommand) { command = res; help = -1; } else if (part is CommandFormatArgument) { currentArgument = res; help = -1; } else if (part is CommandFormatValue) { arguments[currentArgument] = string.Join("", res.Split('"')); if (i == firstValuePos) { help = lastValuePos; } if (i == lastValuePos) { help = firstValuePos; } } } if (res == "+FALSE+") { if (help > -1) { i = help; if (i >= parts.Count) { position = text.Length; command = "+FALSE+"; } } else { position = text.Length; command = "+FALSE+"; } help = -1; } else { position += res.Length; } i++; } if (command == "+FALSE+") { //lblExampleCommand.Text = "Syntax Error"; return(new KeyValuePair <string, Dictionary <string, string> >()); } else { /*string argvs = "{"; * * foreach (KeyValuePair<string, string> entry in arguments) { * argvs += entry.Key + "=" + entry.Value + ", "; * } * * argvs += "}"; * * lblExampleCommand.Text = command + argvs;*/ return(new KeyValuePair <string, Dictionary <string, string> >(command, arguments)); } }
/// <summary> /// Adds a command format to the parser /// </summary> /// <param name="part">The format to add</param> public void AddPart(CommandFormat part) { parts.Add(part); }