import multex.Failure;
/**
- * a class to do some basic, regexp based parsing of character data
- *
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
+ * Simple parser. Can be used to parse a <code>String</code> based using
+ * regular epxression.
*/
-
public class SimpleParser {
private String data;
private int position;
/**
- *
- * @param aData
+ * Initialize a new <code>SimpleParser</code>, with <code>aData</code>
+ * as the text to parse.
*/
public SimpleParser(String aData) {
}
/**
- *
- * @param aRegularExpression
- * @return
- * @throws SimpleParserExc
+ * Parses a regular expression. Uses suppled <code>aMessage</code> parameter
+ * in case of an error.
*/
-
- public boolean parses(RE aRegularExpression) throws SimpleParserExc {
- REMatch match = aRegularExpression.getMatch(data, position);
-
- return (match!=null && match.getStartIndex()==position) ;
- }
-
- /**
- *
- * @param aRegularExpression
- * @param aMessage
- * @return
- * @throws SimpleParserExc
- */
-
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();
}
/**
+ * Attempts to match the data right after the parsing position to
+ * a regular expression
*
- * @param aRegularExpression
- * @return
- * @throws SimpleParserExc
+ * @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
*
- * @param aRegularExpression
- * @throws SimpleParserExc
+ * @throws SimpleParserExc if no match could be found for the given regular
+ * expression.
*/
public void skip(RE aRegularExpression) throws SimpleParserExc {
}
/**
- *
- * @param anExpression
- * @return
- * @throws SimpleParserExc
+ * 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));
}
/**
- *
- * @param anExpression
- * @return
- * @throws SimpleParserExc
- * @throws SimpleParserFailure
+ * 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));
}
}
+
/**
- *
- * @param anExpression
- * @param aMessage
- * @return
- * @throws SimpleParserExc
- * @throws SimpleParserFailure
+ * 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);
}
/**
- *
- * @param anExpression
- * @throws SimpleParserExc
- * @throws SimpleParserFailure
+ * Skips (i.e. discards) text matching the supplied regular expression
*/
public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
}
/**
- *
- * @return true if the parser is at the end of the data
+ * returns <code>true</code> if the parser is at the end of the data
*/
-
public boolean isAtEnd() {
return position>=data.length();
}
/**
- *
- * @return
+ * Returns the "remaining" data: the data after the current position
*/
public String remainingData() {
return data.substring(position);
}
/**
- *
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
+ * Unchecked exception thrown from <code>SimpleParser</code>
*/
public static class SimpleParserFailure extends Failure {
}
}
+ /**
+ * Checked exception thrown from <code>SimpleParser</code>
+ */
public static class SimpleParserExc extends Exc {
public SimpleParserExc(String aMessage) {
super(aMessage);