示例#1
0
    public Value match(Value str) {
      PatternMatcher matcher = getMatcher();

      // Do the matching
      PatternMatcherInput jStr = new PatternMatcherInput(string(str));
      Pair result = null;
      Pair prev = null;
      boolean found = false;

      while (matcher.contains(jStr, pattern)) {
        found = true;

        MatchResult matchResult = matcher.getMatch();

        for (int i = 0, length = matchResult.groups(); i < length; i++) {
          Pair m = new Pair(new SchemeString(matchResult.group(i)), EMPTYLIST);
          if (result == null) result = prev = m;
          else {
            prev.setCdr(m);
            prev = m;
          }
        }
      }

      if (!found) return FALSE;
      else return result;
    }
示例#2
0
    public Value matchPositions(Value str) {
      PatternMatcher matcher = getMatcher();

      // Do the matching
      PatternMatcherInput jStr = new PatternMatcherInput(string(str));
      Pair result = null;
      Pair prev = null;
      boolean found = false;

      while (matcher.contains(jStr, pattern)) {
        found = true;

        MatchResult matchResult = matcher.getMatch();

        for (int i = 0, length = matchResult.groups(); i < length; i++) {
          Pair m =
              new Pair(
                  Quantity.valueOf(matchResult.beginOffset(i)),
                  Quantity.valueOf(matchResult.endOffset(i)));
          Pair elem = new Pair(m, EMPTYLIST);
          if (result == null) result = prev = elem;
          else {
            prev.setCdr(elem);
            prev = elem;
          }
        }
      }

      if (!found) return FALSE;
      else return result;
    }
示例#3
0
  public static final int optionsFromScheme(Value value, Value type) {
    if (value instanceof Symbol) {
      if (value == CASE_INSENSITIVE) {
        if (type == REGEX_PERL5) return Perl5Compiler.CASE_INSENSITIVE_MASK;
        else if (type == REGEX_GLOB) return GlobCompiler.CASE_INSENSITIVE_MASK;
        else if (type == REGEX_AWK) return AwkCompiler.CASE_INSENSITIVE_MASK;
        else throw new RuntimeException("Unknown compiler " + type);
      } else if (value == EXTENDED) {
        if (type == REGEX_PERL5) return Perl5Compiler.EXTENDED_MASK;
        else throw new RuntimeException("The extended mask is supported only by Perl5 regexps");
      } else if (value == SINGLELINE) {
        if (type == REGEX_PERL5) return Perl5Compiler.SINGLELINE_MASK;
        else throw new RuntimeException("The singleline mask is supported only by Perl5 regexps");
      } else if (value == MULTILINE) {
        if (type == REGEX_PERL5) return Perl5Compiler.MULTILINE_MASK;
        else if (type == REGEX_GLOB)
          throw new RuntimeException("Glob compiler doesn't support this option: " + value);
        else if (type == REGEX_AWK) return AwkCompiler.MULTILINE_MASK;
      } else throw new RuntimeException("Unsupported regexp option " + value);
    } else if (value instanceof Pair) {
      int options = 0;
      Pair pv = (Pair) value;

      while (pv != EMPTYLIST) {
        options |= optionsFromScheme(pv.car(), type);
        pv = (Pair) pv.cdr();
      }

      return options;
    } else throw new RuntimeException("Invalid format for options " + value);

    // Not reached, but keeps the Java compiler happy
    return 0;
  }