using System; using System.Collections.Generic; using Pidgin; using static Pidgin.Parser; using static Pidgin.Parser; namespace Dalamud.Bootstrap.SqexArg { public static class ArgumentParser { private static readonly Parser KeyMarkerNoEscape = String(" "); private static readonly Parser KeyMarkerEscape = String(" /"); private static readonly Parser KeyMarker = Try(KeyMarkerEscape).Or(KeyMarkerNoEscape); private static readonly Parser ValueMarker = String(" ="); private static readonly Parser EscapedSpace = String(" ").ThenReturn(' '); //private static readonly Parser String = Try(EscapedSpace).Or(AnyCharExcept(' ')).ManyString(); private static readonly Parser String = Try(EscapedSpace).Or(AnyCharExcept(' ')).ManyString(); private static readonly Parser> KeyValue = Map ( (_, key, _, value) => new KeyValuePair(key, value), KeyMarker, String, ValueMarker, String ); private static readonly Parser>> Parser = KeyValue.Many(); /// /// Parses the argument /// /// /// /// Thrown when failed to parse the input. public static IEnumerable> Parse(string input) { var test = KeyMarker.Parse(input); var result = Parser.Parse(input); if (!result.Success) { throw new SqexArgException($"Failed to parse the argument.\n{result.Error}"); } return result.Value; } } }