/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
*
* This file is part of Mir.
*
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two. You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries. If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so. If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
*/
package mir.util;
-import java.util.*;
-import gnu.regexp.*;
+import gnu.regexp.RE;
+import gnu.regexp.REException;
+import gnu.regexp.REMatch;
import multex.Exc;
import multex.Failure;
+/**
+ * Simple parser. Can be used to parse a <code>String</code> based using
+ * regular epxression.
+ */
public class SimpleParser {
private String data;
private int position;
+ /**
+ * Initialize a new <code>SimpleParser</code>, with <code>aData</code>
+ * as the text to parse.
+ */
+
public SimpleParser(String aData) {
data=aData;
position=0;
}
- public boolean parses(RE aRegularExpression) throws SimpleParserExc {
- REMatch match = aRegularExpression.getMatch(data, position);
-
- return (match!=null && match.getStartIndex()==position) ;
- }
-
+ /**
+ * Parses a regular expression. Uses suppled <code>aMessage</code> parameter
+ * in case of an error.
+ */
public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
REMatch match = aRegularExpression.getMatch(data, position);
- if (match==null || match.getStartIndex()!=position)
+ if (match==null || match.getStartIndex()!=position) {
throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
+ }
position=match.getEndIndex();
return match.toString();
}
+ /**
+ * Attempts to match the data right after the parsing position to
+ * a regular expression
+ *
+ * @throws SimpleParserExc if no match could be found for the given regular
+ * expression.
+ */
public String parse(RE aRegularExpression) throws SimpleParserExc {
return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");
}
+ /**
+ * Attempts to skip data at the parsing position matching the supplied regular expression
+ *
+ * @throws SimpleParserExc if no match could be found for the given regular
+ * expression.
+ */
+
public void skip(RE aRegularExpression) throws SimpleParserExc {
REMatch match = aRegularExpression.getMatch(data, position);
position=match.getEndIndex();
}
+ /**
+ * Returns <code>true</code> if the data at the parsing position matches the given
+ * regular expression.
+ */
public boolean parses(String anExpression) throws SimpleParserExc {
try {
return parses(new RE(anExpression));
}
}
+ /**
+ * Returns <code>true</code> if the data at the parsing position matches the given
+ * regular expression.
+ */
+ public boolean parses(RE aRegularExpression) throws SimpleParserExc {
+ REMatch match = aRegularExpression.getMatch(data, position);
+
+ return (match!=null && match.getStartIndex()==position) ;
+ }
+
+ /**
+ * Attempts to match the data right after the parsing position to
+ * a regular expression
+ *
+ * @throws SimpleParserExc if no match could be found for the given regular
+ * expression.
+ */
public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
try {
return parse(new RE(anExpression));
}
}
+
+ /**
+ * Parses text at the current parsing position matching the given <code>anExpression</code>.
+ * Will apply the supplied message to an exception if no match could be found.
+ */
public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {
try {
return parse(new RE(anExpression), aMessage);
}
}
+ /**
+ * Skips (i.e. discards) text matching the supplied regular expression
+ */
+
public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
try {
skip(new RE(anExpression));
throw new SimpleParserFailure( t );
}
}
+
+ /**
+ * returns <code>true</code> if the parser is at the end of the data
+ */
public boolean isAtEnd() {
return position>=data.length();
}
+ /**
+ * Returns the "remaining" data: the data after the current position
+ */
+ public String remainingData() {
+ return data.substring(position);
+ }
+
+ /**
+ * Unchecked exception thrown from <code>SimpleParser</code>
+ */
+
public static class SimpleParserFailure extends Failure {
public SimpleParserFailure(Throwable aThrowable) {
super(aThrowable.getMessage(), aThrowable);
}
}
+ /**
+ * Checked exception thrown from <code>SimpleParser</code>
+ */
public static class SimpleParserExc extends Exc {
public SimpleParserExc(String aMessage) {
super(aMessage);