Exemple #1
0
        /// <summary>
        /// Get the value represented by a string containing an SDL literal.
        /// </summary>
        /// <param name="literal">An SDL literal</param>
        /// <returns>A value for an SDL literal</returns>
        /// <exception cref="FormatException">(or subclass SDLParseException) If
        /// the string cannot be interpreted as an SDL literal</exception>
        /// <exception cref="ArgumentNullException">If <c>literal</c> is null.
        /// </exception>
        public static Object Value(String literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException("literal argument to " +
                                                "SDL.Value(string) cannot be null");
            }

            if (literal.StartsWith("\"") || literal.StartsWith("`"))
            {
                return(Parser.ParseString(literal));
            }
            if (literal.StartsWith("'"))
            {
                return(Parser.ParseCharacter(literal));
            }
            if (literal.Equals("null"))
            {
                return(null);
            }
            if (literal.Equals("true") || literal.Equals("on"))
            {
                return(true);
            }
            if (literal.Equals("false") || literal.Equals("off"))
            {
                return(false);
            }
            if (literal.StartsWith("["))
            {
                return(Parser.ParseBinary(literal));
            }
            if (literal[0] != '/' && literal.IndexOf('/') != -1)
            {
                return(Parser.ParseDateTime(literal));
            }
            if (literal[0] != ':' && literal.IndexOf(':') != -1)
            {
                return(Parser.ParseTimeSpan(literal));
            }
            if ("01234567890-.".IndexOf(literal[0]) != -1)
            {
                return(Parser.ParseNumber(literal));
            }

            throw new FormatException("String " + literal + " does not " +
                                      "represent an SDL type.");
        }
Exemple #2
0
        internal Token(Parser parser, string text, int line, int position)
        {
            this.parser   = parser;
            this.text     = text;
            this.line     = line;
            this.position = position;
            this.size     = text.Length;
            this.obj      = (object)null;
            try
            {
                char ch = text[0];
                switch (ch)
                {
                case '"':
                case '`':
                    this.type = Type.STRING;
                    this.obj  = (object)Parser.ParseString(text);
                    break;

                case '\'':
                    this.type = Type.CHARACTER;
                    this.obj  = (object)text[1];
                    break;

                default:
                    if (text == "null")
                    {
                        this.type = Type.NULL;
                        break;
                    }
                    else if (text == "true" || text == "on")
                    {
                        this.type = Type.BOOLEAN;
                        this.obj  = (object)true;
                        break;
                    }
                    else if (text == "false" || text == "off")
                    {
                        this.type = Type.BOOLEAN;
                        this.obj  = (object)false;
                        break;
                    }
                    else if ((int)ch == 91)
                    {
                        this.type = Type.BINARY;
                        this.obj  = (object)Parser.ParseBinary(text);
                        break;
                    }
                    else if ((int)ch != 47 && text.IndexOf('/') != -1 && text.IndexOf(':') == -1)
                    {
                        this.type = Type.DATE;
                        this.obj  = (object)Parser.ParseDateTime(text);
                        break;
                    }
                    else if ((int)ch != 58 && text.IndexOf(':') != -1)
                    {
                        this.type = Type.TIME;
                        this.obj  = (object)Token.ParseTimeSpanWithZone(text, parser, line, position);
                        break;
                    }
                    else if ("01234567890-.".IndexOf(ch) != -1)
                    {
                        this.type = Type.NUMBER;
                        this.obj  = Parser.ParseNumber(text);
                        break;
                    }
                    else
                    {
                        this.type = (int)ch != 123 ? ((int)ch != 125 ? ((int)ch != 61 ? ((int)ch != 58 ? Type.IDENTIFIER : Type.COLON) : Type.EQUALS) : Type.END_BLOCK) : Type.START_BLOCK;
                        break;
                    }
                }
            }
            catch (FormatException ex)
            {
                throw new SDLParseException(ex.Message, line, position);
            }
            this.punctuation = this.type == Type.COLON || this.type == Type.EQUALS || this.type == Type.START_BLOCK || this.type == Type.END_BLOCK;
            this.literal     = this.type != Type.IDENTIFIER && !this.punctuation;
        }