jfun.parsec.pattern

Class Patterns

public final class Patterns extends Object

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

Author: Ben Yu Dec 16, 2004

Method Summary
static Patternalways()
A Pattern object that always matches with 0 length.
static Patternamong(char[] cs)
Succeed with match length 1 if the current character in the input is among the given characters.
static Patternand(Pattern... pps)
Find the match length that matches all of the patterns in the given Pattern object array.
static Patternchars_eq(int l)
static Patternchars_ge(int l)
static Patterneof()
Ensures the input has no character left. match length is 0 if succeed.
static PatternhasAtLeast(int l)
Ensures the input has at least l characters left. match length is l if succeed.
static PatternhasExact(int l)
Ensures the input has exactly l characters left. match length is l if succeed.
static Patternifelse(Pattern cond, Pattern yes, Pattern no)
If the condiction Pattern object cond matches, match the remaining input against Pattern object yes.
static PatternisChar(char c)
Succeed with match length 1 if the current character in the input is same as character c.
static PatternisChar(CharPredicate cp)
Succeed with match length 1 if the current character in the input satisfies the given predicate.
static PatternisDecimal()
Recognizes a decimal number that can start with a decimal point.
static PatternisDecimalL()
a decimal number that has at least one number before the decimal point. the decimal point and the numbers to the right are optional.
static PatternisDecimalR()
Recognizes a decimal point and 1 or more digits after it.
static PatternisDecInteger()
pattern for a decimal integer.
static PatternisEscaped()
Succeed with match length 2 if there are at least 2 characters in the input and the first character is '\' Mismatch otherwise.
static PatternisExponential()
Recognizes a the exponent part of a scientific number notation.
static PatternisHexInteger()
pattern for a hex integer.
static PatternisInteger()
pattern for an integer.
static PatternisLineComment(String open)
Matches a line comment that starts with a string and end with EOF or Line Feed character.
static PatternisOctInteger()
pattern for a octal integer that starts with a 0 and followed by 0 or more [0-7] characters.
static PatternisString(String str)
Matches a string.
static PatternisStringCI(String str)
Matches a string case insensitively.
static PatternisWord()
a pattern for a standard english word. it starts with an underscore or an alphametic character, followed by 0 or more alphanumeric characters.
static Patternlonger(Pattern p1, Pattern p2)
Try two pattern objects, pick the one with the longer match length.
static Patternlongest(Pattern... pps)
Try an array of pattern objects, pick the one with the longest match length.
static Patternmany(int min, CharPredicate cp)
Matches if the input starts with min or more characters that all satisfy the given predicate, mismatch otherwise.
static Patternmany(CharPredicate cp)
Matches 0 or more characters that all satisfy the given predicate.
static Patternmany(int min, Pattern pp)
Matches if the input starts with min or more occurrences of patterns recognized by Pattern object pp, mismatch otherwise.
static Patternmany(Pattern pp)
Matches 0 or more occurrences of patterns recognized by Pattern object pp.
static Patternmany1(CharPredicate cp)
Matches characters that satisfies the given predicate for 1 or more times.
static Patternnever()
A Pattern object that always returns MISMATCH.
static Patternnot(Pattern pp)
Matches with match length 0 if the Pattern object pp mismatch.
static PatternnotAmong(char[] cs)
Succeed with match length 1 if the current character in the input is not among the given characters.
static PatternnotChar(char c)
Succeed with match length 1 if the current character in the input is not the same as character c.
static PatternnotRange(char c1, char c2)
Succeed with match length 1 if the current character in the input is not between character c1 and c2.
static PatternnotString(String str)
Matches a character if the input has at least 1 character and does not match the given string.
static PatternnotStringCI(String str)
Matches a character if the input has at least 1 character and does not match the given string case insensitively.
static Patternoptional(Pattern pp)
Match with 0 length even if Pattern object pp mismatches.
static Patternor(Pattern pp1, Pattern pp2)
if the first Pattern object pp1 mismatches, try the second Pattern object pp2.
static Patternor(Pattern... pps)
try an array of Pattern objects subsequently until one matches.
static Patternpeek(Pattern pp)
Matches with match length 0 if the Pattern object pp matches.
static Patternrange(char c1, char c2)
Succeed with match length 1 if the current character in the input is between character c1 and c2.
static Patternregex(Pattern p)
Adapt a regular expression pattern to a jfun.parsec.pattern.Pattern;
static Patternregex(String s)
Adapt a regular expression pattern string to a jfun.parsec.pattern.Pattern;
static Patternregex_modifiers()
Get the pattern that matches regular expression modifiers.
static Patternregex_pattern()
Get the Pattern object that matches any regular expression pattern string in the form of /some pattern here/.
static Patternrepeat(int n, CharPredicate cp)
Matches if the input has at least n characters and the first n characters all satisfy the given predicate.
static Patternrepeat(int n, Pattern pp)
Matches if the input n occurrences of Pattern pp.
static Patternseq(Pattern pp1, Pattern pp2)
First matches Pattern object pp1.
static Patternseq(Pattern... pps)
Runs an array of Pattern objects subsequently until one mismatches.
static Patternshorter(Pattern p1, Pattern p2)
Try two pattern objects, pick the one with the shorter match length.
static Patternshortest(Pattern... pps)
Try an array of pattern objects, pick the one with the shortest match length.
static Patternsome(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 Patternsome(int max, CharPredicate cp)
Matches at most max number of characters that satisfies the given predicate.
static Patternsome(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 Patternsome(int max, Pattern pp)
Matches at most max number of occurrences of pattern recognized by Pattern object pp.

Method Detail

always

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

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.

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.

chars_eq

public static Pattern chars_eq(int l)

Deprecated: Use Patterns instead.

chars_ge

public static Pattern chars_ge(int l)

Deprecated: Use Patterns instead.

eof

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

Returns: the Pattern object.

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.

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.

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.

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.

isDecimal

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

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.

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.

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.

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.

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.

isInteger

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

Returns: the Pattern object.

isLineComment

public static Pattern isLineComment(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.

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.

isString

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

Returns: the Pattern object.

isStringCI

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

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.

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.

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.

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.

never

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

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.

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.

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.

notString

public static Pattern notString(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(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.

optional

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

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.

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.

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.

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.

regex

public static Pattern 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(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_modifiers

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

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.

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.

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.

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.

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.

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.