示例#1
0
            public ConditionParser(String condition, FullTextSearchOptions options)
            {
                ConditionStream stream = new ConditionStream(condition, options);

                this.options = options;

                rootExpression    = new ConditionExpression(options);
                currentExpression = rootExpression;

                Reset();

                while (stream.Read())
                {
                    if (ConditionOperator.IsSymbol(stream.Current))
                    {
                        PutToken();
                        SetToken(stream.Current);
                        PutToken();
                        continue;
                    }
                    switch (stream.Current)
                    {
                    case ' ': PutToken(); continue;

                    case '(': PushExpression(); continue;

                    case ')': PopExpression(); continue;

                    case '"':
                        PutToken();
                        inQuotes = true;
                        SetToken(stream.ReadQuote());
                        PutToken();
                        inQuotes = false;
                        continue;
                    }
                    AddToken(stream.Current);
                }
                PutToken();

                if (!object.ReferenceEquals(rootExpression, currentExpression))
                {
                    if ((options & FullTextSearchOptions.ThrowOnUnbalancedParens) != 0)
                    {
                        throw new InvalidOperationException("Unbalanced parentheses.");
                    }
                }
            }
示例#2
0
            public ConditionExpression AddSubexpression(ConditionOperator op)
            {
                ConditionOperator newOp = op;

                if (op == ConditionOperator.Near)
                {
                    if ((options & FullTextSearchOptions.ThrowOnInvalidNearUse) != 0)
                    {
                        throw new InvalidOperationException("Invalid near operator before subexpression.");
                    }
                    newOp = ConditionOperator.And;
                }

                ConditionExpression exp = new ConditionExpression(this, newOp);

                subexpressions.Add(exp);

                return(exp);
            }
示例#3
0
            private ConditionExpression(ConditionExpression parent, ConditionOperator op, string term)
                : this(parent, op)
            {
                this.term = term;

                isTerm = true;

                isPhrase = (term.IndexOf(' ') != -1);
                int prefixIndex = term.IndexOf('*');

                isPrefix = (prefixIndex != -1);

                if (!isPrefix)
                {
                    return;
                }

                if (!isPhrase)
                {
                    if ((options & FullTextSearchOptions.TrimPrefixTerms) == 0)
                    {
                        return;
                    }
                    if (prefixIndex == (term.Length - 1))
                    {
                        return;
                    }
                    this.term = (prefixIndex == 0) ? "" : term.Remove(prefixIndex + 1);
                    return;
                }

                if ((options & FullTextSearchOptions.TrimPrefixPhrases) == 0)
                {
                    return;
                }
                term      = Regex.Replace(term, @"(\*[^ ]+)|(\*)", "");
                term      = Regex.Replace(term.Trim(), @"[ ]{2,}", " ");
                this.term = term + "*";
            }
示例#4
0
            public string ReadQuote()
            {
                StringBuilder sb = new StringBuilder();

                while (Read())
                {
                    if (Current.Equals('"'))
                    {
                        if ((index + 1) == condition.Length)
                        {
                            index = condition.Length;
                            return(sb.ToString());
                        }
                        char peek = condition[index + 1];
                        if ((peek == ' ') || (peek == ')') || (peek == '(') || (ConditionOperator.IsSymbol(peek)))
                        {
                            return(sb.ToString());
                        }
                        if (peek == '"')
                        {
                            index += 1;
                        }
                        else
                        {
                            if ((options & FullTextSearchOptions.ThrowOnUnbalancedQuotes) != 0)
                            {
                                return(sb.ToString());
                            }
                        }
                    }
                    sb.Append(Current);
                }
                if ((options & FullTextSearchOptions.ThrowOnUnbalancedQuotes) != 0)
                {
                    throw new InvalidOperationException("Unbalanced quotes.");
                }
                return(sb.ToString());
            }
示例#5
0
 private bool Equals(ConditionOperator obj)
 {
     return(value == obj.value);
 }
示例#6
0
 private ConditionExpression(ConditionExpression parent, ConditionOperator op) : this(parent.options)
 {
     index       = parent.subexpressions.Count;
     this.parent = parent;
     this.op     = op;
 }
示例#7
0
 private void Reset()
 {
     ResetToken();
     lastOp = ConditionOperator.And;
 }