Example #1
0
        /// <summary>
        /// Given the start (offset) of the next token, traverse through
        /// the string to find the next token, stripping correctly
        /// enclosed quotes.
        /// </summary>
        /// <param name="input">
        /// Input string
        /// </param>
        /// <param name="_offset">
        /// Current offset position for the string parser.
        /// </param>
        /// <returns>
        /// The next token on the input string
        /// </returns>
        private Token GetNextToken(string input, ref int _offset)
        {
            StringBuilder sb = new StringBuilder();
            //bool singleQuoted = false;
            //bool doubleQuoted = false;
            bool readingIdentifier = false;

            while (_offset < input.Length)
            {
                char cc = input[_offset++];
                if ((cc == ',') || (cc == '+') || (cc == '!'))
                {
                    if (!readingIdentifier)
                    {
                        sb.Append(cc);
                    }
                    else
                    {
                        _offset--;
                    }
                    break;
                }
                else
                {
                    sb.Append(cc);
                    readingIdentifier = true;
                }
            }

            string result = sb.ToString().Trim();

            // If resulting identifier is enclosed in paired quotes,
            // remove the only the first pair of quotes from the string
            if (result.Length >= 2 &&
                ((result[0] == '\'' && result[result.Length - 1] == '\'') ||
                 (result[0] == '\"' && result[result.Length - 1] == '\"')))
            {
                result = result.Substring(1, result.Length - 2);
            }

            result = result.Trim();

            // possible empty token because white spaces are enclosed in quotation marks.
            if (String.IsNullOrWhiteSpace(result))
            {
                throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException),
                                                               null, "EmptyTokenString", EnumExpressionEvaluatorStrings.EmptyTokenString,
                                                               EnumMinimumDisambiguation.EnumAllValues(typeof(T)));
            }
            else if (result[0] == '(')
            {
                int matchIndex = input.IndexOf(')', _offset);
                if (result[result.Length - 1] == ')' || matchIndex >= 0)
                {
                    throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException),
                                                                   null, "NoIdentifierGroupingAllowed", EnumExpressionEvaluatorStrings.NoIdentifierGroupingAllowed);
                }
            }
            if (result.Equals(","))
            {
                return(new Token(TokenKind.Or));
            }
            else if (result.Equals("+"))
            {
                return(new Token(TokenKind.And));
            }
            else if (result.Equals("!"))
            {
                return(new Token(TokenKind.Not));
            }
            else
            {
                return(new Token(result));
            }
        }
Example #2
0
        private Token GetNextToken(string input, ref int _offset)
        {
            string        str;
            StringBuilder builder = new StringBuilder();

            for (bool flag = false; _offset < input.Length; flag = true)
            {
                char ch = input[_offset++];
                switch (ch)
                {
                case ',':
                case '+':
                case '!':
                    if (!flag)
                    {
                        builder.Append(ch);
                    }
                    else
                    {
                        _offset--;
                    }
                    goto Label_0054;
                }
                builder.Append(ch);
            }
Label_0054:
            str = builder.ToString().Trim();
            if ((str.Length >= 2) && (((str[0] == '\'') && (str[str.Length - 1] == '\'')) || ((str[0] == '"') && (str[str.Length - 1] == '"'))))
            {
                str = str.Substring(1, str.Length - 2);
            }
            str = str.Trim();
            if (string.IsNullOrWhiteSpace(str))
            {
                throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "EmptyTokenString", EnumExpressionEvaluatorStrings.EmptyTokenString, new object[] { EnumMinimumDisambiguation.EnumAllValues(typeof(T)) });
            }
            if (str[0] == '(')
            {
                int index = input.IndexOf(')', _offset);
                if ((str[str.Length - 1] == ')') || (index >= 0))
                {
                    throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "NoIdentifierGroupingAllowed", EnumExpressionEvaluatorStrings.NoIdentifierGroupingAllowed, new object[0]);
                }
            }
            if (str.Equals(","))
            {
                return(new Token(TokenKind.Or));
            }
            if (str.Equals("+"))
            {
                return(new Token(TokenKind.And));
            }
            if (str.Equals("!"))
            {
                return(new Token(TokenKind.Not));
            }
            return(new Token(str));
        }