001/*
002 *  gnu/regexp/REToken.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 */
019
020package gnu.regexp;
021import java.io.Serializable;
022
023abstract class REToken implements Serializable {
024
025  protected REToken next = null;
026  protected REToken uncle = null;
027  protected int subIndex;
028
029  protected REToken(int subIndex) {
030      this.subIndex = subIndex;
031  }
032
033  int getMinimumLength() {
034    return 0;
035  }
036
037  void setUncle(REToken anUncle) {
038    uncle = anUncle;
039  }
040
041    /** Returns true if the match succeeded, false if it failed. */
042    abstract boolean match(CharIndexed input, REMatch mymatch);
043  
044    /** Returns true if the rest of the tokens match, false if they fail. */
045    protected boolean next(CharIndexed input, REMatch mymatch) {
046        if (next == null) {
047            if (uncle == null) {
048                return true;
049            } else {
050                return uncle.match(input, mymatch);
051            }
052        } else {
053            return next.match(input, mymatch);
054        }
055    }
056  
057  boolean chain(REToken token) {
058      next = token;
059      return true; // Token was accepted
060  }
061
062    abstract void dump(StringBuffer os);
063
064  void dumpAll(StringBuffer os) {
065    dump(os);
066    if (next != null) next.dumpAll(os);
067  }
068}