//
// This software is now distributed according to
// the Lesser Gnu Public License. Please see
// http://www.gnu.org/copyleft/lesser.txt for
// the details.
// -- Happy Computing!
//
package com.stevesoft.pat;
import java.util.*;
/**
Shareware: package pat
Copyright 2001, Steven R. Brandt
*/ /**
Class Pattern is the base class on which all the other pattern
elements are built. */
public abstract class Pattern {
/** The ESC character, the user can provide his own value
for the escape character through regex.esc */
public final static char ESC = '\\';
final static String PROTECT_THESE = "[]{}(),$,-\"^.";
/** The interal match function, it must be provided by any
class which wishes to extend Pattern. */
public abstract int matchInternal(int i,Pthings p);
public abstract String toString();
// Class Pattern is a singly linked list
// chained together by member next. The member
// parent is used so that sub patterns can access
// the chain they are branching from.
Pattern next=null,parent=null;
/** This gets the next element of a Pattern that
we wish to match. If we are at the end of a
subchain of patterns, it will return us to the
parent chain. */
public Pattern getNext() {
return next != null ? next :
(parent == null ? null : parent.getNext());
}
/** Call this method if you have a pattern element that
takes a sub pattern (such as Or), and
after you have added a sub pattern to the current
pattern element. */
public void setParent(Pattern p) {
if(next != null) next.setParent(p);
else parent = p;
}
/** This determines if the remainder of a Pattern
matches. Type "return nextMatch" from within
matchInternal if the current
Pattern matches. Otherwise, return a -1.*/
public int nextMatch(int i,Pthings pt) {
Pattern p = getNext();
/*if(p == null) return i;
return p.matchInternal(i,pt);*/
return p==null ? i : p.matchInternal(i,pt);
}
/** This is a toString() for the remainder
of the Pattern elements after this one.
use this when overriding toString(). Called from
within toString(). */
public String nextString() {
if(next == null) return "";
return next.toString();
}
/** a method to detect whether char c is in String s */
final static boolean inString(char c,String s) {
int i;
for(i=0;i