jfun.parsec.pattern

Class Pattern

public abstract class Pattern extends Object implements 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

Field Summary
static intMISMATCH
returned by match() method when match fails.
Method Summary
Patternifelse(Pattern yes, Pattern no)
If this pattern matches, match the remaining input against Pattern object yes.
Patternmany()
Matches this pattern for 0 or more times.
Patternmany(int min)
Matches this pattern for at least min times.
Patternmany1()
Matches this pattern for 1 or more times.
abstract intmatch(CharSequence src, int len, int from)
The actual length of the pattern string is len - from.
Patternnot()
If this pattern matches, return mismatch. return match length 0 otherwise.
Patternoptional()
Match with 0 length even if this pattern mismatches.
Patternpeek()
Matches with match length 0 if this Pattern object matches.
Patternrepeat(int n)
Matches the input against this pattern for n times.
Patternseq(Pattern p2)
First matches this pattern.
Patternsome(int max)
Matches this pattern for up to max times.
Patternsome(int min, int max)
Matches this pattern for at least min times and at most max times.

Field Detail

MISMATCH

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

Method Detail

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.

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.

match

public abstract int match(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.

not

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

Returns: the new Pattern object.

optional

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

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.

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.

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.

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.