001/*
002 *  gnu/regexp/RETokenOneOf.java
003 *  Copyright (C) 1998-2001 Wes Biggs
004 *
005 *  This library is free software; you can redistribute it and/or modify
006 *  it under the terms of the GNU Lesser General Public License as published
007 *  by the Free Software Foundation; either version 2.1 of the License, or
008 *  (at your option) any later version.
009 *
010 *  This library is distributed in the hope that it will be useful,
011 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 *  GNU Lesser General Public License for more details.
014 *
015 *  You should have received a copy of the GNU Lesser General Public License
016 *  along with this program; if not, write to the Free Software
017 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018 */
019package gnu.regexp;
020
021/**
022 * @since gnu.regexp 1.1.3
023 * @author Shashank Bapat
024 */
025final class RETokenLookAhead extends REToken
026{
027  REToken re;
028  boolean negative;
029
030  RETokenLookAhead(REToken re, boolean negative) throws REException {
031    super(0);
032    this.re = re;
033    this.negative = negative;
034  }
035
036  boolean match(CharIndexed input, REMatch mymatch)
037  {
038    REMatch trymatch = (REMatch)mymatch.clone();
039    REMatch trymatch1 = (REMatch)mymatch.clone();
040    REMatch newMatch = null;
041    if (re.match(input, trymatch)) {
042      if (negative) return false;
043      if (next(input, trymatch1))
044        newMatch = trymatch1;
045    }
046
047    if (newMatch != null) {
048      if (negative) return false;
049      //else
050      mymatch.assignFrom(newMatch);
051      return true;
052    }
053    else { // no match
054      if (negative)
055        return next(input, mymatch);
056      //else
057      return false;
058    }
059  }
060
061    void dump(StringBuffer os) {
062        os.append("(?");
063        os.append(negative ? '!' : '=');
064        re.dumpAll(os);
065        os.append(')');
066    }
067}
068