jfun.parsec.pattern
Class Pattern

java.lang.Object
  extended by jfun.parsec.pattern.Pattern
All Implemented Interfaces:
java.io.Serializable

public abstract class Pattern
extends java.lang.Object
implements java.io.Serializable

A Pattern object encapsulates an algorithm to recognize certain string pattern. When fed with a character range, a Pattern object either fails to match, or matches with the match length returned.

Pattern object is used for terminal level parsers. A Pattern differs from a Parser in that it does not return object, and it simply reports mismatch whenenver fails. While Parser cannot be implemented directly, Pattern can be implemented directly by user code.

Author:
Ben Yu Dec 16, 2004
See Also:
Serialized Form

Field Summary
static int MISMATCH
          returned by match() method when match fails.
 
Constructor Summary
Pattern()
           
 
Method Summary
 Pattern ifelse(Pattern yes, Pattern no)
          If this pattern matches, match the remaining input against Pattern object yes.
 Pattern many()
          Matches this pattern for 0 or more times.
 Pattern many(int min)
          Matches this pattern for at least min times.
 Pattern many1()
          Matches this pattern for 1 or more times.
abstract  int match(java.lang.CharSequence src, int len, int from)
          The actual length of the pattern string is len - from.
 Pattern not()
          If this pattern matches, return mismatch.
 Pattern optional()
          Match with 0 length even if this pattern mismatches.
 Pattern peek()
          Matches with match length 0 if this Pattern object matches.
 Pattern repeat(int n)
          Matches the input against this pattern for n times.
 Pattern seq(Pattern p2)
          First matches this pattern.
 Pattern some(int max)
          Matches this pattern for up to max times.
 Pattern some(int min, int max)
          Matches this pattern for at least min times and at most max times.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MISMATCH

public static final int MISMATCH
returned by match() method when match fails.

See Also:
Constant Field Values
Constructor Detail

Pattern

public Pattern()
Method Detail

match

public abstract int match(java.lang.CharSequence src,
                          int len,
                          int from)
The actual length of the pattern string is len - from.

Parameters:
src - the source string.
len - the length of the sequence. NOTE: the range is [from, len], not [from,from+len].
from - the starting index in the sequence.
Returns:
the number of characters matched. MISMATCH otherwise.

seq

public final Pattern seq(Pattern p2)
First matches this pattern. If succeed, match the remaining input against Pattern p2. Fails if either this or p2 fails. Succeed with the entire match length, which is the sum of the match length of this and p2.

Parameters:
p2 - the next Pattern object to match.
Returns:
the new Pattern object.

optional

public final Pattern optional()
Match with 0 length even if this pattern mismatches.

Returns:
the new Pattern object.

many

public final Pattern many()
Matches this pattern for 0 or more times. Return the total match length.

Returns:
the new Pattern object.

many

public final Pattern many(int min)
Matches this pattern for at least min times. Return the total match length.

Parameters:
min - the minimal number of times to match.
Returns:
the new Pattern object.

many1

public final Pattern many1()
Matches this pattern for 1 or more times. Return the total match length.

Returns:
the new Pattern object.

some

public final Pattern some(int max)
Matches this pattern for up to max times. Return the total match length.

Parameters:
max - the maximal number of times to match.
Returns:
the new Pattern object.

some

public final Pattern some(int min,
                          int max)
Matches this pattern for at least min times and at most max times. Return the total match length.

Parameters:
min - the minimal number of times to match.
max - the maximal number of times to match.
Returns:
the new Pattern object.

not

public final Pattern not()
If this pattern matches, return mismatch. return match length 0 otherwise.

Returns:
the new Pattern object.

peek

public final Pattern peek()
Matches with match length 0 if this Pattern object matches. Mismatch otherwise.

Returns:
the new Pattern object.

ifelse

public final Pattern ifelse(Pattern yes,
                            Pattern no)
If this pattern matches, match the remaining input against Pattern object yes. Otherwise, match the input against Pattern object no.

Parameters:
yes - the true Pattern.
no - the false Pattern.
Returns:
the new Pattern object.

repeat

public final Pattern repeat(int n)
Matches the input against this pattern for n times.

Parameters:
n - the number of times to match.
Returns:
the new Pattern object.