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."); } } }
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); }
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 + "*"; }
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()); }
private bool Equals(ConditionOperator obj) { return(value == obj.value); }
private ConditionExpression(ConditionExpression parent, ConditionOperator op) : this(parent.options) { index = parent.subexpressions.Count; this.parent = parent; this.op = op; }
private void Reset() { ResetToken(); lastOp = ConditionOperator.And; }