jfun.parsec.pattern
Class Patterns

java.lang.Object
  extended by jfun.parsec.pattern.Patterns

public final class Patterns
extends java.lang.Object

This class provides all the basic Pattern implementations and all Pattern combinators.

Author:
Ben Yu Dec 16, 2004

Constructor Summary
Patterns()
           
 
Method Summary
static Pattern always()
          A Pattern object that always matches with 0 length.
static Pattern among(char[] cs)
          Succeed with match length 1 if the current character in the input is among the given characters.
static Pattern and(Pattern... pps)
          Find the match length that matches all of the patterns in the given Pattern object array.
static Pattern chars_eq(int l)
          Deprecated. Use hasExact(int) instead.
static Pattern chars_ge(int l)
          Deprecated. Use hasAtLeast(int) instead.
static Pattern eof()
          Ensures the input has no character left.
static Pattern hasAtLeast(int l)
          Ensures the input has at least l characters left.
static Pattern hasExact(int l)
          Ensures the input has exactly l characters left.
static Pattern ifelse(Pattern cond, Pattern yes, Pattern no)
          If the condiction Pattern object cond matches, match the remaining input against Pattern object yes.
static Pattern isChar(char c)
          Succeed with match length 1 if the current character in the input is same as character c.
static Pattern isChar(CharPredicate cp)
          Succeed with match length 1 if the current character in the input satisfies the given predicate.
static Pattern isDecimal()
          Recognizes a decimal number that can start with a decimal point.
static Pattern isDecimalL()
          a decimal number that has at least one number before the decimal point.
static Pattern isDecimalR()
          Recognizes a decimal point and 1 or more digits after it.
static Pattern isDecInteger()
          pattern for a decimal integer.
static Pattern isEscaped()
          Succeed with match length 2 if there are at least 2 characters in the input and the first character is '\' Mismatch otherwise.
static Pattern isExponential()
          Recognizes a the exponent part of a scientific number notation.
static Pattern isHexInteger()
          pattern for a hex integer.
static Pattern isInteger()
          pattern for an integer.
static Pattern isLineComment(java.lang.String open)
          Matches a line comment that starts with a string and end with EOF or Line Feed character.
static Pattern isOctInteger()
          pattern for a octal integer that starts with a 0 and followed by 0 or more [0-7] characters.
static Pattern isString(java.lang.String str)
          Matches a string.
static Pattern isStringCI(java.lang.String str)
          Matches a string case insensitively.
static Pattern isWord()
          a pattern for a standard english word.
static Pattern longer(Pattern p1, Pattern p2)
          Try two pattern objects, pick the one with the longer match length.
static Pattern longest(Pattern... pps)
          Try an array of pattern objects, pick the one with the longest match length.
static Pattern many(CharPredicate cp)
          Matches 0 or more characters that all satisfy the given predicate.
static Pattern many(int min, CharPredicate cp)
          Matches if the input starts with min or more characters that all satisfy the given predicate, mismatch otherwise.
static Pattern many(int min, Pattern pp)
          Matches if the input starts with min or more occurrences of patterns recognized by Pattern object pp, mismatch otherwise.
static Pattern many(Pattern pp)
          Matches 0 or more occurrences of patterns recognized by Pattern object pp.
static Pattern many1(CharPredicate cp)
          Matches characters that satisfies the given predicate for 1 or more times.
static Pattern never()
          A Pattern object that always returns MISMATCH.
static Pattern not(Pattern pp)
          Matches with match length 0 if the Pattern object pp mismatch.
static Pattern notAmong(char[] cs)
          Succeed with match length 1 if the current character in the input is not among the given characters.
static Pattern notChar(char c)
          Succeed with match length 1 if the current character in the input is not the same as character c.
static Pattern notRange(char c1, char c2)
          Succeed with match length 1 if the current character in the input is not between character c1 and c2.
static Pattern notString(java.lang.String str)
          Matches a character if the input has at least 1 character and does not match the given string.
static Pattern notStringCI(java.lang.String str)
          Matches a character if the input has at least 1 character and does not match the given string case insensitively.
static Pattern optional(Pattern pp)
          Match with 0 length even if Pattern object pp mismatches.
static Pattern or(Pattern... pps)
          try an array of Pattern objects subsequently until one matches.
static Pattern or(Pattern pp1, Pattern pp2)
          if the first Pattern object pp1 mismatches, try the second Pattern object pp2.
static Pattern peek(Pattern pp)
          Matches with match length 0 if the Pattern object pp matches.
static Pattern range(char c1, char c2)
          Succeed with match length 1 if the current character in the input is between character c1 and c2.
static Pattern regex_modifiers()
          Get the pattern that matches regular expression modifiers.
static Pattern regex_pattern()
          Get the Pattern object that matches any regular expression pattern string in the form of /some pattern here/.
static Pattern regex(java.util.regex.Pattern p)
          Adapt a regular expression pattern to a jfun.parsec.pattern.Pattern;
static Pattern regex(java.lang.String s)
          Adapt a regular expression pattern string to a jfun.parsec.pattern.Pattern;
static Pattern repeat(int n, CharPredicate cp)
          Matches if the input has at least n characters and the first n characters all satisfy the given predicate.
static Pattern repeat(int n, Pattern pp)
          Matches if the input n occurrences of Pattern pp.
static Pattern seq(Pattern... pps)
          Runs an array of Pattern objects subsequently until one mismatches.
static Pattern seq(Pattern pp1, Pattern pp2)
          First matches Pattern object pp1.
static Pattern shorter(Pattern p1, Pattern p2)
          Try two pattern objects, pick the one with the shorter match length.
static Pattern shortest(Pattern... pps)
          Try an array of pattern objects, pick the one with the shortest match length.
static Pattern some(int max, CharPredicate cp)
          Matches at most max number of characters that satisfies the given predicate.
static Pattern some(int min, int max, CharPredicate cp)
          Matches at least min and at most max number of characters that satisfies the given predicate, mismatch otherwise.
static Pattern some(int min, int max, Pattern pp)
          Matches at least min and at most max number of occurrences of pattern recognized by Pattern object pp, mismatch otherwise.
static Pattern some(int max, Pattern pp)
          Matches at most max number of occurrences of pattern recognized by Pattern object pp.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Patterns

public Patterns()
Method Detail

chars_ge

public static Pattern chars_ge(int l)
Deprecated. Use hasAtLeast(int) instead.


chars_eq

public static Pattern chars_eq(int l)
Deprecated. Use hasExact(int) instead.


hasAtLeast

public static Pattern hasAtLeast(int l)
Ensures the input has at least l characters left. match length is l if succeed.

Parameters:
l - the number of characters.
Returns:
the Pattern object.

hasExact

public static Pattern hasExact(int l)
Ensures the input has exactly l characters left. match length is l if succeed.

Parameters:
l - the number of characters.
Returns:
the Pattern object.

eof

public static Pattern eof()
Ensures the input has no character left. match length is 0 if succeed.

Returns:
the Pattern object.

isChar

public static Pattern isChar(char c)
Succeed with match length 1 if the current character in the input is same as character c. Mismatch otherwise.

Parameters:
c - the character to compare with.
Returns:
the Pattern object.

range

public static Pattern range(char c1,
                            char c2)
Succeed with match length 1 if the current character in the input is between character c1 and c2.

Parameters:
c1 - the first character.
c2 - the second character.
Returns:
the Pattern object.

notRange

public static Pattern notRange(char c1,
                               char c2)
Succeed with match length 1 if the current character in the input is not between character c1 and c2.

Parameters:
c1 - the first character.
c2 - the second character.
Returns:
the Pattern object.

among

public static Pattern among(char[] cs)
Succeed with match length 1 if the current character in the input is among the given characters.

Parameters:
cs - the characters to compare with.
Returns:
the Pattern object.

notAmong

public static Pattern notAmong(char[] cs)
Succeed with match length 1 if the current character in the input is not among the given characters.

Parameters:
cs - the characters to compare with.
Returns:
the Pattern object.

notChar

public static Pattern notChar(char c)
Succeed with match length 1 if the current character in the input is not the same as character c. Mismatch otherwise.

Parameters:
c - the character to compare with.
Returns:
the Pattern object.

isChar

public static Pattern isChar(CharPredicate cp)
Succeed with match length 1 if the current character in the input satisfies the given predicate. Mismatch otherwise.

Parameters:
cp - the predicate object.
Returns:
the Pattern object.

isEscaped

public static Pattern isEscaped()
Succeed with match length 2 if there are at least 2 characters in the input and the first character is '\' Mismatch otherwise.

Returns:
the Pattern object.

isLineComment

public static Pattern isLineComment(java.lang.String open)
Matches a line comment that starts with a string and end with EOF or Line Feed character.

Parameters:
open - the line comment starting string.
Returns:
the Pattern object.

isString

public static Pattern isString(java.lang.String str)
Matches a string.

Returns:
the Pattern object.

isStringCI

public static Pattern isStringCI(java.lang.String str)
Matches a string case insensitively.

Returns:
the Pattern object.

notString

public static Pattern notString(java.lang.String str)
Matches a character if the input has at least 1 character and does not match the given string.

Returns:
the Pattern object.

notStringCI

public static Pattern notStringCI(java.lang.String str)
Matches a character if the input has at least 1 character and does not match the given string case insensitively.

Returns:
the Pattern object.

not

public static Pattern not(Pattern pp)
Matches with match length 0 if the Pattern object pp mismatch. Mismatch otherwise.

Parameters:
pp - the Pattern object.
Returns:
the new Pattern object.

peek

public static Pattern peek(Pattern pp)
Matches with match length 0 if the Pattern object pp matches. Mismatch otherwise.

Parameters:
pp - the Pattern object.
Returns:
the new Pattern object.

or

public static Pattern or(Pattern pp1,
                         Pattern pp2)
if the first Pattern object pp1 mismatches, try the second Pattern object pp2.

Parameters:
pp1 - the 1st Pattern object.
pp2 - the 2nd Pattern object.
Returns:
the new Pattern object.

and

public static Pattern and(Pattern... pps)
Find the match length that matches all of the patterns in the given Pattern object array. Mismatch if any one mismatches.

Parameters:
pps - the Pattern array.
Returns:
the new Pattern object.

seq

public static Pattern seq(Pattern pp1,
                          Pattern pp2)
First matches Pattern object pp1. If succeed, match the remaining input against Pattern pp2. Fails if either pp1 or pp2 fails. Succeed with the entire match length, which is the sum of the match length of pp1 and pp2.

Parameters:
pp1 - the 1st Pattern object to match.
pp2 - the 2nd Pattern object to match.
Returns:
the new Pattern object.

or

public static Pattern or(Pattern... pps)
try an array of Pattern objects subsequently until one matches. Mismatch if the array is empty.

Parameters:
pps - the Pattern object array.
Returns:
the new Pattern object.

seq

public static Pattern seq(Pattern... pps)
Runs an array of Pattern objects subsequently until one mismatches. Return the total match length if all succeed.

Parameters:
pps - the Pattern object array.
Returns:
the new Pattern object.

repeat

public static Pattern repeat(int n,
                             CharPredicate cp)
Matches if the input has at least n characters and the first n characters all satisfy the given predicate.

Parameters:
n - the number of characters to test.
cp - the predicate object.
Returns:
the Pattern object.

repeat

public static Pattern repeat(int n,
                             Pattern pp)
Matches if the input n occurrences of Pattern pp.

Parameters:
n - the number of occurrences.
pp - the Pattern object.
Returns:
the new Pattern object.

many

public static Pattern many(int min,
                           CharPredicate cp)
Matches if the input starts with min or more characters that all satisfy the given predicate, mismatch otherwise.

Parameters:
min - the minimal number of characters to match.
cp - the predicate.
Returns:
the Pattern object.

many

public static Pattern many(CharPredicate cp)
Matches 0 or more characters that all satisfy the given predicate.

Parameters:
cp - the predicate.
Returns:
the Pattern object.

many

public static Pattern many(int min,
                           Pattern pp)
Matches if the input starts with min or more occurrences of patterns recognized by Pattern object pp, mismatch otherwise.

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

many

public static Pattern many(Pattern pp)
Matches 0 or more occurrences of patterns recognized by Pattern object pp.

Parameters:
pp - the Pattern object.
Returns:
the new Pattern object.

some

public static Pattern some(int min,
                           int max,
                           CharPredicate cp)
Matches at least min and at most max number of characters that satisfies the given predicate, mismatch otherwise.

Parameters:
min - the minimal number of characters.
max - the maximal number of characters.
cp - the predicate.
Returns:
the Pattern object.

some

public static Pattern some(int max,
                           CharPredicate cp)
Matches at most max number of characters that satisfies the given predicate.

Parameters:
max - the maximal number of characters.
cp - the predicate.
Returns:
the Pattern object.

some

public static Pattern some(int min,
                           int max,
                           Pattern pp)
Matches at least min and at most max number of occurrences of pattern recognized by Pattern object pp, mismatch otherwise.

Parameters:
min - the minimal number of occurrences of pattern.
max - the maximal number of occurrences of pattern.
pp - the Pattern object.
Returns:
the new Pattern object.

some

public static Pattern some(int max,
                           Pattern pp)
Matches at most max number of occurrences of pattern recognized by Pattern object pp.

Parameters:
max - the maximal number of occurrences of pattern.
pp - the Pattern object.
Returns:
the new Pattern object.

longer

public static Pattern longer(Pattern p1,
                             Pattern p2)
Try two pattern objects, pick the one with the longer match length. If two pattern objects have the same length, the first one is favored.

Parameters:
p1 - the 1st pattern object.
p2 - the 2nd pattern object.
Returns:
the new Pattern object.

longest

public static Pattern longest(Pattern... pps)
Try an array of pattern objects, pick the one with the longest match length. If two pattern objects have the same length, the first one is favored.

Parameters:
pps - the array of Pattern objects.
Returns:
the new Pattern object.

shorter

public static Pattern shorter(Pattern p1,
                              Pattern p2)
Try two pattern objects, pick the one with the shorter match length. If two pattern objects have the same length, the first one is favored.

Parameters:
p1 - the 1st pattern object.
p2 - the 2nd pattern object.
Returns:
the new Pattern object.

shortest

public static Pattern shortest(Pattern... pps)
Try an array of pattern objects, pick the one with the shortest match length. If two pattern objects have the same length, the first one is favored.

Parameters:
pps - the array of Pattern objects.
Returns:
the new Pattern object.

ifelse

public static Pattern ifelse(Pattern cond,
                             Pattern yes,
                             Pattern no)
If the condiction Pattern object cond matches, match the remaining input against Pattern object yes. Otherwise, match the input against Pattern object no.

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

many1

public static Pattern many1(CharPredicate cp)
Matches characters that satisfies the given predicate for 1 or more times. Return the total match length.

Returns:
the new Pattern object.

optional

public static Pattern optional(Pattern pp)
Match with 0 length even if Pattern object pp mismatches.

Returns:
the new Pattern object.

never

public static Pattern never()
A Pattern object that always returns MISMATCH.

Returns:
the Pattern object.

always

public static Pattern always()
A Pattern object that always matches with 0 length.

Returns:
the Pattern object.

isDecimalL

public static Pattern isDecimalL()
a decimal number that has at least one number before the decimal point. the decimal point and the numbers to the right are optional. 0, 11., 2.3 are all good candidates. While .1, . are not.

Returns:
the Pattern object.

isDecimalR

public static Pattern isDecimalR()
Recognizes a decimal point and 1 or more digits after it.

Returns:
the Pattern object.

isDecimal

public static Pattern isDecimal()
Recognizes a decimal number that can start with a decimal point.

Returns:
the Pattern object.

isWord

public static Pattern isWord()
a pattern for a standard english word. it starts with an underscore or an alphametic character, followed by 0 or more alphanumeric characters.

Returns:
the Pattern object.

isInteger

public static Pattern isInteger()
pattern for an integer. ([0-9]+)

Returns:
the Pattern object.

isOctInteger

public static Pattern isOctInteger()
pattern for a octal integer that starts with a 0 and followed by 0 or more [0-7] characters.

Returns:
the Pattern object.

isDecInteger

public static Pattern isDecInteger()
pattern for a decimal integer. It starts with a non-zero digit and followed by 0 or more digits.

Returns:
the Pattern object.

isHexInteger

public static Pattern isHexInteger()
pattern for a hex integer. It starts with a 0x or 0X, followed by 1 or more hex digits.

Returns:
the Pattern object.

isExponential

public static Pattern isExponential()
Recognizes a the exponent part of a scientific number notation. It can be e12, E-1, etc.

Returns:
the Pattern object.

regex

public static Pattern regex(java.util.regex.Pattern p)
Adapt a regular expression pattern to a jfun.parsec.pattern.Pattern;

Parameters:
p - the regular expression pattern.
Returns:
the jfun.parsec.pattern.Pattern object.

regex

public static Pattern regex(java.lang.String s)
Adapt a regular expression pattern string to a jfun.parsec.pattern.Pattern;

Parameters:
s - the regular expression pattern string.
Returns:
the jfun.parsec.pattern.Pattern object.

regex_pattern

public static Pattern regex_pattern()
Get the Pattern object that matches any regular expression pattern string in the form of /some pattern here/. '\' is used as escape character.


regex_modifiers

public static Pattern regex_modifiers()
Get the pattern that matches regular expression modifiers. Basically this is a list of alpha characters.