net.percederberg.grammatica.parser.re
Class Matcher

java.lang.Object
  extended by net.percederberg.grammatica.parser.re.Matcher

public class Matcher
extends java.lang.Object

A regular expression string matcher. This class handles the matching of a specific string with a specific regular expression. It contains state information about the matching process, as for example the position of the latest match, and a number of flags that were set. This class is not thread-safe.


Method Summary
 int end()
          Returns the end position of the latest match.
 boolean hasReadEndOfString()
          Checks if the end of the string was encountered during the last match attempt.
 boolean isCaseInsensitive()
          Checks if this matcher compares in case-insensitive mode.
 int length()
          Returns the length of the latest match.
 boolean matchFrom(int pos)
          Attempts to find a match starting at the specified position in the string.
 boolean matchFromBeginning()
          Attempts to find a match starting at the beginning of the string.
 void reset()
          Resets the information about the last match.
 void reset(ReaderBuffer buffer)
          Resets the matcher for use with a new look-ahead character input buffer.
 void reset(java.lang.String str)
          Resets the matcher for use with a new input string.
 int start()
          Returns the start position of the latest match.
 java.lang.String toString()
          Returns the latest matched string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

isCaseInsensitive

public boolean isCaseInsensitive()
Checks if this matcher compares in case-insensitive mode.

Returns:
true if the matching is case-insensitive, or false otherwise
Since:
1.5

reset

public void reset()
Resets the information about the last match. This will clear all flags and set the match length to a negative value. This method is automatically called before starting a new match.


reset

public void reset(java.lang.String str)
Resets the matcher for use with a new input string. This will clear all flags and set the match length to a negative value.

Parameters:
str - the new string to work with
Since:
1.5

reset

public void reset(ReaderBuffer buffer)
Resets the matcher for use with a new look-ahead character input buffer. This will clear all flags and set the match length to a negative value.

Parameters:
buffer - the character input stream
Since:
1.5

start

public int start()
Returns the start position of the latest match. If no match has been encountered, this method returns zero (0).

Returns:
the start position of the latest match

end

public int end()
Returns the end position of the latest match. This is one character after the match end, i.e. the first character after the match. If no match has been encountered, this method returns the same value as start().

Returns:
the end position of the latest match

length

public int length()
Returns the length of the latest match.

Returns:
the length of the latest match, or -1 if no match was found

hasReadEndOfString

public boolean hasReadEndOfString()
Checks if the end of the string was encountered during the last match attempt. This flag signals that more input may be needed in order to get a match (or a longer match).

Returns:
true if the end of string was encountered, or false otherwise

matchFromBeginning

public boolean matchFromBeginning()
                           throws java.io.IOException
Attempts to find a match starting at the beginning of the string.

Returns:
true if a match was found, or false otherwise
Throws:
java.io.IOException - if an I/O error occurred while reading an input stream

matchFrom

public boolean matchFrom(int pos)
                  throws java.io.IOException
Attempts to find a match starting at the specified position in the string.

Parameters:
pos - the starting position of the match
Returns:
true if a match was found, or false otherwise
Throws:
java.io.IOException - if an I/O error occurred while reading an input stream

toString

public java.lang.String toString()
Returns the latest matched string. If no string has been matched, an empty string will be returned.

Overrides:
toString in class java.lang.Object
Returns:
the latest matched string