Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET

Very Powerful, Generic, Irony Based, Database Searcher

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
26 Aug 2010Ms-PL8 min read 30.3K   546   29  
Usage of Irony to produce a Google-like search tool on any column in a database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Irony.Parsing;
using System.Globalization;

namespace searchComponent.grammar
{
    /// <summary>
    /// This class represents the string Grammar, this is used only for strings.
    /// Some examples are: vladimir, calderon*, vladimir* *calderon, vladimir and calderon or yaksic
    /// </summary>
    public class StringGrammar : Grammar
    {
        /// <summary>
        /// This is where the grammar is defined.
        /// </summary>
        public StringGrammar() : base(false)
        {
            this.GrammarComments = @"
Simple grammar to operate on numbers. It allows numbers and number comparisons like <78
or !54, also it allows and or operators and the : operator which means between. Floats and integers
are allowed, the dot . is the decimal separator.";

            // 1. Terminals
            var Word = CreateTerm("Term");
            Word.Priority = Terminal.LowestPriority;
            var Phrase = new StringLiteral("Phrase", "\"");
            var ImpliedAnd = new ImpliedSymbolTerminal("ImpliedAnd"); 

            // 2. Non Terminals
            var Term = new NonTerminal("Term");
            var UnaryTerm = new NonTerminal("UnaryTerm");
            var UnaryOp = new NonTerminal("UnaryOp");
            var AndExpression = new NonTerminal("AndExpression");
            var AndTerm = new NonTerminal("AndTerm");
            var AndOp = new NonTerminal("AndOp");
            var OrOp = new NonTerminal("OrOp");
            var OrExpression = new NonTerminal("OrExpression");
            var Expression = new NonTerminal("Expression");

            // 3. BNF Rules
            Expression.Rule = OrExpression | AndExpression | UnaryTerm;
            OrExpression.Rule = Expression + OrOp + AndTerm;
            AndExpression.Rule = AndTerm + AndOp + UnaryTerm;
            AndTerm.Rule = AndExpression | UnaryTerm;

            OrOp.Rule = ToTerm("or");
            AndOp.Rule = ToTerm("and") | ImpliedAnd;

            UnaryTerm.Rule =
                (UnaryOp + Word + UnaryOp) | 
                (UnaryOp + Word) | 
                (Word + UnaryOp) | 
                Term;

            UnaryOp.Rule = ToTerm("*");
            Term.Rule = 
                Word | 
                Phrase;

            this.Root = Expression;

            // 4. Operators precedence
            RegisterOperators(10, AndOp);
            RegisterOperators(20, OrOp);
        }

        /// <summary>
        /// Creates extended identifier terminal that allows international characters
        /// Following the pattern used for c# identifier terminal in TerminalFactory.CreateCSharpIdentifier method;
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private IdentifierTerminal CreateTerm(string name)
        {
            IdentifierTerminal term = new IdentifierTerminal(name);
            term.CharCategories.AddRange(new UnicodeCategory[] {
             UnicodeCategory.UppercaseLetter, //Ul
             UnicodeCategory.LowercaseLetter, //Ll
             UnicodeCategory.TitlecaseLetter, //Lt
             //UnicodeCategory.ModifierLetter,  //Lm
             //UnicodeCategory.OtherLetter,     //Lo
             UnicodeCategory.LetterNumber,     //Nl
             UnicodeCategory.DecimalDigitNumber, //Nd
             UnicodeCategory.ConnectorPunctuation //Pc
             //UnicodeCategory.NonSpacingMark,       //Mn
          });
            //StartCharCategories are the same
            term.StartCharCategories.AddRange(term.CharCategories);
            return term;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Chief Technology Officer Artexacta
Bolivia Bolivia
just a coder. Love Java, but working on asp.net

Comments and Discussions