public override bool TryParse(ArgumentInput input, out KeyValuePair <TKey, TValue> result)
        {
            // This parser will parse a single key/value pair.
            // Valid syntaxes:
            //	<KEYTYPE>[WHITESPACE]<COLON>[WHITESPACE]<VALUETYPE>

            result = default;

            var keyParser   = Parser.GetParser <TKey>();
            var valueParser = Parser.GetParser <TValue>();

            if (!keyParser.TryParse(input, out TKey key))
            {
                return(false);                                                      // Failed to parse key
            }
            input.TrimWhitespace();

            if (input.Length == 0 || input[0] != ':')
            {
                return(false);         // No colon found
            }
            input.Claim();             // Claim colon

            input.TrimWhitespace();
            if (!valueParser.TryParse(input, out TValue value))
            {
                return(false);                                                            // Failed to parse value
            }
            result = new KeyValuePair <TKey, TValue>(key, value);

            return(true);
        }
Ejemplo n.º 2
0
 public override bool TryParse(ArgumentInput input, out T?result)
 {
     result = null;
     if (!Parser.GetParser <T>().TryParse(input, out T value))
     {
         return(false);
     }
     result = value;
     return(true);
 }
Ejemplo n.º 3
0
        public override bool TryParse(ArgumentInput input, out T result)
        {
            result = default;
            mBuilder.Clear();
            if (input.Length == 0 || char.IsWhiteSpace(input[0]))
            {
                return(false);                                                              // Unexpected EOL or whitespace
            }
            // Check for negative or positive sign and claim it
            if (input[0] == '-' || input[0] == '+')
            {
                mBuilder.Append(input[0]);
                input.Claim(1);
            }

            bool decimalFound = false;

            while (input.Length > 0)
            {
                if (input[0] == '.')
                {
                    if (decimalFound)
                    {
                        return(false);
                    }
                    decimalFound = true;
                }
                else if (char.IsWhiteSpace(input[0]))
                {
                    break;
                }
                else if (Parser.IsSpecial(input[0]))
                {
                    break;                                                  // Special character, must stop
                }
                else if (!char.IsDigit(input[0]))
                {
                    return(false);                                              // Character not a digit
                }
                mBuilder.Append(input[0]);
                input.Claim();
            }

            try
            {
                result = mParser(mBuilder.ToString());
            }
            catch (FormatException) { return(false); }
            catch (OverflowException) { return(false); }

            return(true);
        }
Ejemplo n.º 4
0
        public override bool TryParse(ArgumentInput input, out char result)
        {
            // This parser should match ONE and ONLY ONE character in the input.
            // If multiple characters appear together without whitespace/special char between them,
            // this is a faulty input.
            //
            // Syntax:
            //	[CHAR]{WHITESPACE|SPECIALCHAR}

            result = '\0';
            if (input.Length == 0 || char.IsWhiteSpace(input[0]))
            {
                return(false);                                                              // Unexpected EOL or whitespace
            }
            if (input.Length > 1 && !char.IsWhiteSpace(input[1]) && !Parser.IsSpecial(input[1]))
            {
                return(false);                                                                                             // Second character was not whitespace, malformed input
            }
            result = input[0];
            input.Claim();             // Claim the character
            return(true);
        }
Ejemplo n.º 5
0
        public override bool TryParse(ArgumentInput input, out bool result)
        {
            // Parses a boolean. This can accept a variety of inputs:
            //
            //	Full word:
            //		True => true (case-insensitive)
            //		False => false (case-insensitive)
            //
            //	Abbreviated word:
            //		t => true (case-insensitive)
            //		f => false (case-insensitive)
            //
            //	Numeric:
            //		1 => true
            //		0 => false

            mBuilder.Clear();
            result = false;

            if (input.Length == 0 || char.IsWhiteSpace(input[0]))
            {
                return(false);                                                              // Unexpected EOL or whitespace
            }
            while (input.Length > 0 && !char.IsWhiteSpace(input[0]) && !Parser.IsSpecial(input[0]))
            {
                mBuilder.Append(input[0]);
                input.Claim();
            }

            switch (mBuilder.ToString().ToLowerInvariant())
            {
            case "true":
                result = true;
                break;

            case "false":
                result = false;
                break;

            case "t":
                result = true;
                break;

            case "f":
                result = false;
                break;

            case "1":
                result = true;
                break;

            case "0":
                result = true;
                break;

            default:
                return(false);                        // Doesn't match specified inputs
            }

            return(true);
        }
Ejemplo n.º 6
0
        public override bool TryParse(ArgumentInput input, out string result)
        {
            // This parser will look for a string of characters that are either surrounded by single or double quotes
            //
            // If the string is surrounded by quotes, it will include everything between the first quote character found
            // and the last unescaped quote character. Quote characters may be escaped using \"
            //
            // If the string is not surrounded by quotes, it will start with the first character and stop at a special
            // character or whitespace.

            mBuilder.Clear();

            result = null;
            if (input.Length == 0 || char.IsWhiteSpace(input[0]))
            {
                return(false);                                                              // Unexpected EOL or whitespace
            }
            char?quoteChar = null;

            if (input[0] == '\'')
            {
                quoteChar = '\'';
            }
            else if (input[0] == '"')
            {
                quoteChar = '"';
            }

            if (quoteChar != null)
            {
                input.Claim();                                // Claim the quote character, if it was found
            }
            // Begin scanning for the string
            while (input.Length > 0)
            {
                if (quoteChar != null)
                {
                    if (input.Match("\\" + quoteChar))
                    {
                        // Escaped quotechar
                        mBuilder.Append(quoteChar);
                        input.Claim(2);
                        continue;
                    }
                    else if (input[0] == quoteChar)
                    {
                        input.Claim();                         // Claim end-quote
                        break;
                    }
                }
                else
                {
                    if (char.IsWhiteSpace(input[0]))
                    {
                        break;                                                  // Found whitespace not inside quotes
                    }
                    else if (Parser.IsSpecial(input[0]))
                    {
                        break;                                                      // Special character not inside quotes, must stop
                    }
                }

                // Append and claim the current character
                mBuilder.Append(input[0]);
                input.Claim();
            }

            result = mBuilder.ToString();
            return(true);
        }