/*

    vregular.h

    VUCAKO regular expressions

    5.10.2001

    Copyright (C) 2001 by Josef Pelikan, MFF UK Prague
        http://cgg.ms.mff.cuni.cz/~pepca/

    VUCAKO Bench is Open Source software (see http://www.opensource.org/
    for description what Open Source is); you may distributed it and/or
    modify it under the terms of GNU General Public License as published
    by the Free Software Foundation; either version 2, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    A copy of the GNU General Public License is available as
    `/usr/doc/copyright/GPL' in the Debian GNU/Linux distribution or on
    the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You can
    also obtain it by writing to the Free Software Foundation, Inc., 59
    Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifndef _VREGULAR_H
#define _VREGULAR_H

//#define VREG_STAT
    // statistics (query complexity, cache, etc.)

//-----------------------------------------------------------
/*  regular expression syntax:

  <regexp> ::= <factor> [ "|" <factor> ]* |
               ""
  <factor> ::= <term> [ "&" <term> ]*
  <term> ::= <term> <term> |
             <atom> "*" |
             <atom> "?" |
             <atom> "+" |
             <atom> "{" [ <number> ] "," [ <number> ] "}" |
             [ "^" ] <atom>
  <number> ::= [ <digit> ]+
  <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  <atom> ::= "(" <regexp> ")" |
             <wildcard> | <escape> | <char>
  <wildcard> ::= "." |
                 "[" [ "^" ] <list> "]"
  <list> ::= [ "]" ] [ "-" ] <inner>
  <inner> ::= <innerchar> <inner> |
              <innerchar> "-" <innerchar> <inner> |
              ""
  <escape> ::= "\\" | "\|" | "\&" | "\(" | "\)" | "\*" | "\?" | "\+" |
               "\{" | "\." | "\[" | "\^"
  <char> ::= -- regular ASCII character excluding "\|&()*?+{.[^" --
  <innerchar> ::= -- regular ASCII character excluding "]" --

*/
//-----------------------------------------------------------
//  VRegularExpression object:
//      this version is suitable for short searched strings
//      only .. ~5k characters at most

#define VRegInfinity    12000

class VRegularItem;

class VRegularExpression {

public:

    VRegularExpression ( const char *expr, bool ic =false );

    virtual bool match ( const char *str );
        // true/false string matching

    virtual void wildCard ( int i, int &begin, int &end );
        // returns wild-card range from the last string match
        // !!! NOT YET !!!

#ifdef VREG_STAT

    unsigned calls;
        // complexity of the last "match()" call

    unsigned iterCalls;
        // number of iteration sub-calls for the last "match()" call

    unsigned inserts, tries, hits, positive;
        // cache statistics

    void sync ();
        // synchronizes cache statistics

#endif

    bool ignoreCase () { return iCase; }

    virtual ~VRegularExpression ();

    //  support for VRegularItem:

    const char *string;
        // searched string

    int searchLen;
        // searched string's length

    byte *cache;
        // global match cache

    int cacheAlloc;
        // actual size of allocated cache array

    int cacheUsed;
        // size of cache used for the given string length

    int tt1;
        // 2*searchLen + 1

protected:

    bool iCase;
        // ignore letter case?

    const char *ex;
        // stored expression, parsing pointer

    VRegularItem *it;
        // regular item of the expression

    //------------------------------------------
    //  regular expression parsing:

    VRegularItem *regexp ();

    VRegularItem *factor ();

    VRegularItem *term ();

    };

#endif
