support for CAPTCHAs
[mir.git] / source / mir / util / SimpleParser.java
index 4b0b148..92a806b 100755 (executable)
 
 package mir.util;
 
-import gnu.regexp.RE;
-import gnu.regexp.REException;
-import gnu.regexp.REMatch;
-import multex.Exc;
 import multex.Failure;
+import org.apache.oro.text.regex.*;
 
 /**
  * Simple parser. Can be used to parse a <code>String</code> based using
  * regular epxression.
  */
 public class SimpleParser {
-  private String data;
   private int position;
 
+  private Perl5Compiler compiler = new Perl5Compiler();
+  private Perl5Matcher matcher = new Perl5Matcher();
+  private PatternMatcherInput input;
+
+
   /**
-   * Initialize a new <code>SimpleParser</code>, with <code>aData</code>
+   * Initialize a new <code>SimpleParser</code>, with <code>someData</code>
    * as the text to parse.
+   *
+   * @param someData the text to parse
    */
 
-  public SimpleParser(String aData) {
-    data=aData;
-    position=0;
+  public SimpleParser(String someData) {
+    position = 0;
+    input = new PatternMatcherInput(someData);
   }
 
   /**
    * Parses a regular expression. Uses suppled <code>aMessage</code> parameter
    * in case of an error.
+   *
+   * @param aRegularExpression  the expression to find at the cursor position
+   * @param aMessage the error message to apply if the regular expression couldn't be found
+   *
+   * @return the text that matched the regular expression
+   *
+   * @throws mir.util.SimpleParser.UnExpectedTokenExc
+   *     if the token wasn't found. <code>aMessage</code> will be used in the
+   *     exception's message 
    */
-  public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
+  public String parse(Pattern aRegularExpression, String aMessage) throws UnExpectedTokenExc {
+    input.setCurrentOffset(position);
+    if ( matcher.contains(input, aRegularExpression) && input.getMatchBeginOffset() == position) {
+        position = input.getMatchEndOffset();
 
-    if (match==null || match.getStartIndex()!=position) {
-      throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
+        return input.match();
+    }
+    else {
+      throw new UnExpectedTokenExc(aMessage + " at position " +
+              position + " in '" + input.toString() + "'");
     }
-
-    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
+   * @throws UnExpectedTokenExc 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()+"'");
+  public String parse(Pattern aRegularExpression) throws UnExpectedTokenExc {
+    return parse( aRegularExpression, "No match found for '" + aRegularExpression.getPattern() + "'");
   }
 
   /**
-   * Attempts to skip data at the parsing position matching the supplied regular expression
+   * Attempts to skip data at the parsing position matching the supplied regular expression.
+   * If no match is found, the method will simply return 
    *
-   * @throws SimpleParserExc if no match could be found for the given regular
-   * expression.
+   * @param aRegularExpression the expression to find and skip at the parsing position.
    */
 
-  public void skip(RE aRegularExpression) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
-
-    if (match!=null && match.getStartIndex()==position)
-      position=match.getEndIndex();
+  public void skip(Pattern aRegularExpression) {
+      input.setCurrentOffset(position);
+    
+      if (matcher.contains(input, aRegularExpression) &&
+          input.getMatchBeginOffset() == position) {
+          position = input.getMatchEndOffset();
+      }
   }
 
   /**
    * Returns <code>true</code> if the data at the parsing position matches the given
    * regular expression.
    */
-  public boolean parses(String anExpression) throws SimpleParserExc {
+  public boolean parses(String anExpression) throws MalformedRegularExpressionExc {
     try {
-      return parses(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
-    }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
+      return parses(compiler.compile(anExpression));
     }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
   /**
-   * Returns <code>true</code> if the data at the parsing position matches the given
+   * Tests whether the data at the parsing position matches a given
    * regular expression.
+   *
+   * @param aRegularExpression the pattern to look for
+   * @return returns <code>true</code> if the regular expression is found at the
+   *    parsing position
    */
-  public boolean parses(RE aRegularExpression) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
+  public boolean parses(Pattern aRegularExpression) {
+     input.setCurrentOffset(position);
 
-    return (match!=null && match.getStartIndex()==position) ;
+     return matcher.contains(input, aRegularExpression) && input.getMatchBeginOffset() == position;
   }
 
   /**
@@ -131,18 +144,12 @@ public class SimpleParser {
    * @throws SimpleParserExc if no match could be found for the given regular
    * expression.
    */
-  public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
+  public String parse(String anExpression) throws MalformedRegularExpressionExc, UnExpectedTokenExc {
     try {
-      return parse(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
-    }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
+      return parse(compiler.compile(anExpression));
     }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
@@ -151,18 +158,12 @@ public class SimpleParser {
    * 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 {
+  public String parse(String anExpression, String aMessage) throws MalformedRegularExpressionExc, UnExpectedTokenExc {
     try {
-      return parse(new RE(anExpression), aMessage);
+      return parse(compiler.compile(anExpression), aMessage);
     }
-    catch (SimpleParserExc e) {
-      throw e;
-    }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
-    }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
@@ -170,18 +171,12 @@ public class SimpleParser {
    * Skips (i.e. discards) text matching the supplied regular expression
    */
 
-  public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
+  public void skip(String anExpression) throws MalformedRegularExpressionExc {
     try {
-      skip(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
+      skip(compiler.compile(anExpression));
     }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
-    }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
@@ -189,14 +184,14 @@ public class SimpleParser {
    * returns <code>true</code> if the parser is at the end of the data
    */
   public boolean isAtEnd() {
-    return position>=data.length();
+    return position >=input.getEndOffset();
   }
 
   /**
    * Returns the "remaining" data: the data after the current position
    */
   public String remainingData() {
-    return data.substring(position);
+    return input.toString().substring(input.getEndOffset());
   }
 
   /**
@@ -216,9 +211,34 @@ public class SimpleParser {
   /**
    * Checked exception thrown from <code>SimpleParser</code>
    */
-  public static class SimpleParserExc extends Exc {
+  public static class SimpleParserExc extends Exception {
+    protected SimpleParserExc(String aMessage, Throwable aCause) {
+      super(aMessage, aCause);
+    }
+
+    public SimpleParserExc(Throwable aCause) {
+      super(aCause);
+    }
+
     public SimpleParserExc(String aMessage) {
       super(aMessage);
     }
   }
+
+  public static class MalformedRegularExpressionExc extends SimpleParserExc {
+    public MalformedRegularExpressionExc(String aMessage, Throwable aCause) {
+      super(aMessage, aCause);
+    }
+
+    public MalformedRegularExpressionExc(Throwable aCause) {
+      super(aCause);
+    }
+  }
+
+  public static class UnExpectedTokenExc extends SimpleParserExc {
+    public UnExpectedTokenExc(String aMessage) {
+      super(aMessage);
+    }
+  }
+
 }
\ No newline at end of file