Permalink
Browse files

fixed bug which was making Options non-reusable

  • Loading branch information...
1 parent 2593a6a commit f7b4f13a776ded2479d7c717471333fedb5d0b13 David Houghton committed May 16, 2012
Showing with 43 additions and 9 deletions.
  1. +1 −1 TODO
  2. +3 −0 src/dfh/grammar/GlobalState.java
  3. +13 −7 src/dfh/grammar/Grammar.java
  4. +26 −1 src/dfh/grammar/Options.java
View
2 TODO
@@ -13,7 +13,7 @@ section at the end of the document.
. bail out of recursion via exception rather than the current laborious and expensive reeling in of failed tries
* put tokenization handling into dfh.grammar
* document new tokenization handling
-. make Options reusable
+* make Options reusable
. make convenience methods Match.closest(String) and Match.closest(Pattern)
. added unit tests for Match.hasLabel(String), which appears to have a latent bug having to do with alternations
. preserve original rule names as labels where necessary during grammar composition
View
3 src/dfh/grammar/GlobalState.java
@@ -30,6 +30,7 @@
public final CharSequence cs;
public final ReversedCharSequence rcs;
public final boolean isReversed;
+ public final int length;
/**
* Constructor called in {@link Grammar} only.
@@ -55,6 +56,7 @@ private GlobalState(GlobalState gs) {
this.debug = gs.debug;
this.rcsEnd = gs.rcsEnd;
this.study = gs.study;
+ this.length = this.end - this.start;
}
/**
@@ -80,6 +82,7 @@ private GlobalState(CharSequence cs, ReversedCharSequence rcs,
this.debug = trace != null;
this.rcsEnd = rcs.translate(start) + 1;
this.study = study;
+ this.length = end - start;
}
/**
View
20 src/dfh/grammar/Grammar.java
@@ -798,7 +798,7 @@ public Matcher lookingAt(final CharSequence cs, Options opt)
checkComplete();
final GlobalState co = verifyOptions(cs, opt);
final boolean ltm = containsAlternation && opt.longestMatch();
- final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
+ final Map<Integer, CachedMatch>[] cache = offsetCache(opt, co.length);
final Set<Integer> startOffsets = startOffsets(cs, co, cache);
final Matcher m = root.matcher(co.start, cache, new DummyMatcher(co));
final LTMMatcher ltmm = ltm ? new LTMMatcher(m) : null;
@@ -913,7 +913,8 @@ public Matcher find(final CharSequence s, Options opt)
checkComplete();
final GlobalState options = verifyOptions(s, opt);
final boolean ltm = containsAlternation && opt.longestMatch();
- final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
+ final Map<Integer, CachedMatch>[] cache = offsetCache(opt,
+ options.length);
List<Integer> list = new ArrayList<Integer>(startOffsets(s, options,
cache));
Collections.sort(list);
@@ -927,11 +928,13 @@ public Matcher find(final CharSequence s, Options opt)
* take place.
*
* @param options
+ * @param length
+ * length of character sequence
*
* @return map from labels to sets of offsets where the associated rules are
* known not to match
*/
- private Map<Integer, CachedMatch>[] offsetCache(Options options) {
+ private Map<Integer, CachedMatch>[] offsetCache(Options options, int length) {
if (initialRules == null) {
synchronized (this) {
// look for reversals
@@ -951,13 +954,13 @@ public Matcher find(final CharSequence s, Options opt)
Map<Integer, CachedMatch>[] offsetCache = new Map[max + 1];
boolean lean = options.leanMemory, fat = options.fatMemory;
if (!(lean || fat))
- fat = options.end - options.start < options.longStringLength;
+ fat = length < options.longStringLength;
for (int i = 0; i < offsetCache.length; i++) {
Map<Integer, CachedMatch> m;
if (lean)
m = new TreeMap<Integer, CachedMatch>();
else if (fat)
- m = new MatchCache(options.end - options.start);
+ m = new MatchCache(length);
else
m = new HashMap<Integer, CachedMatch>();
offsetCache[i] = m;
@@ -1137,7 +1140,8 @@ public Matcher matches(final CharSequence s, Options opt)
throws GrammarException {
checkComplete();
final GlobalState options = verifyOptions(s, opt);
- final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
+ final Map<Integer, CachedMatch>[] cache = offsetCache(opt,
+ options.length);
final Set<Integer> startOffsets = startOffsets(s, options, cache);
final Matcher m = root.matcher(options.start, cache, new DummyMatcher(
options));
@@ -1335,8 +1339,10 @@ private GlobalState verifyOptions(CharSequence s, Options opt) {
if (opt.start() > 0 && opt.start() >= s.length())
throw new GrammarException(
"start offset specified beyond end of string");
- if (opt.end == -1)
+ if (opt.end == -1) {
+ opt = new Options(opt);
opt.end(s.length());
+ }
return new GlobalState(s, opt);
}
View
27 src/dfh/grammar/Options.java
@@ -20,7 +20,7 @@
* @author David Houghton
*
*/
-public class Options {
+public class Options implements Cloneable {
/**
* Whether matches may overlap.
@@ -164,6 +164,26 @@ public Options(GlobalState options) {
}
/**
+ * Creates a clone of the input parameter.
+ *
+ * @param o
+ * clone of parameter
+ */
+ public Options(Options o) {
+ this.allowOverlap = o.allowOverlap;
+ this.end = o.end;
+ this.fatMemory = o.fatMemory;
+ this.keepRightmost = o.keepRightmost;
+ this.leanMemory = o.leanMemory;
+ this.longestMatch = o.longestMatch;
+ this.longStringLength = o.longStringLength;
+ this.maxRecursionDepth = o.maxRecursionDepth;
+ this.start = o.start;
+ this.study = o.study;
+ this.trace = o.trace;
+ }
+
+ /**
* @return whether matches iterated over may overlap
*/
public boolean allowOverlap() {
@@ -393,4 +413,9 @@ public Options longStringLength(int longStringLength) {
this.longStringLength = longStringLength;
return this;
}
+
+ @Override
+ public Object clone() {
+ return new Options(this);
+ }
}

0 comments on commit f7b4f13

Please sign in to comment.