reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / util / SimpleParser.java
index 975dd3d..4b0b148 100755 (executable)
@@ -37,23 +37,16 @@ import multex.Exc;
 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) {
@@ -62,31 +55,15 @@ public class SimpleParser {
   }
 
   /**
-   *
-   * @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();
 
@@ -94,20 +71,21 @@ public class SimpleParser {
   }
 
   /**
+   * 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 {
@@ -118,12 +96,9 @@ public class SimpleParser {
   }
 
   /**
-   *
-   * @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));
@@ -140,13 +115,22 @@ public class SimpleParser {
   }
 
   /**
-   *
-   * @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));
@@ -162,15 +146,11 @@ public class SimpleParser {
     }
   }
 
+
   /**
-   *
-   * @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);
@@ -187,10 +167,7 @@ public class SimpleParser {
   }
 
   /**
-   *
-   * @param anExpression
-   * @throws SimpleParserExc
-   * @throws SimpleParserFailure
+   * Skips (i.e. discards) text matching the supplied regular expression
    */
 
   public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
@@ -209,30 +186,21 @@ public class SimpleParser {
   }
 
   /**
-   *
-   * @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 {
@@ -245,6 +213,9 @@ public class SimpleParser {
     }
   }
 
+  /**
+   * Checked exception thrown from <code>SimpleParser</code>
+   */
   public static class SimpleParserExc extends Exc {
     public SimpleParserExc(String aMessage) {
       super(aMessage);