Skip to content
This repository
Browse code

fixed bug which was making Options non-reusable

  • Loading branch information...
commit f7b4f13a776ded2479d7c717471333fedb5d0b13 1 parent 2593a6a
authored May 16, 2012
2  TODO
@@ -13,7 +13,7 @@ section at the end of the document.
13 13
 . bail out of recursion via exception rather than the current laborious and expensive reeling in of failed tries
14 14
 * put tokenization handling into dfh.grammar
15 15
 * document new tokenization handling
16  
-. make Options reusable
  16
+* make Options reusable
17 17
 . make convenience methods Match.closest(String) and Match.closest(Pattern)
18 18
 . added unit tests for Match.hasLabel(String), which appears to have a latent bug having to do with alternations
19 19
 . preserve original rule names as labels where necessary during grammar composition
3  src/dfh/grammar/GlobalState.java
@@ -30,6 +30,7 @@
30 30
 	public final CharSequence cs;
31 31
 	public final ReversedCharSequence rcs;
32 32
 	public final boolean isReversed;
  33
+	public final int length;
33 34
 
34 35
 	/**
35 36
 	 * Constructor called in {@link Grammar} only.
@@ -55,6 +56,7 @@ private GlobalState(GlobalState gs) {
55 56
 		this.debug = gs.debug;
56 57
 		this.rcsEnd = gs.rcsEnd;
57 58
 		this.study = gs.study;
  59
+		this.length = this.end - this.start;
58 60
 	}
59 61
 
60 62
 	/**
@@ -80,6 +82,7 @@ private GlobalState(CharSequence cs, ReversedCharSequence rcs,
80 82
 		this.debug = trace != null;
81 83
 		this.rcsEnd = rcs.translate(start) + 1;
82 84
 		this.study = study;
  85
+		this.length = end - start;
83 86
 	}
84 87
 
85 88
 	/**
20  src/dfh/grammar/Grammar.java
@@ -798,7 +798,7 @@ public Matcher lookingAt(final CharSequence cs, Options opt)
798 798
 		checkComplete();
799 799
 		final GlobalState co = verifyOptions(cs, opt);
800 800
 		final boolean ltm = containsAlternation && opt.longestMatch();
801  
-		final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
  801
+		final Map<Integer, CachedMatch>[] cache = offsetCache(opt, co.length);
802 802
 		final Set<Integer> startOffsets = startOffsets(cs, co, cache);
803 803
 		final Matcher m = root.matcher(co.start, cache, new DummyMatcher(co));
804 804
 		final LTMMatcher ltmm = ltm ? new LTMMatcher(m) : null;
@@ -913,7 +913,8 @@ public Matcher find(final CharSequence s, Options opt)
913 913
 		checkComplete();
914 914
 		final GlobalState options = verifyOptions(s, opt);
915 915
 		final boolean ltm = containsAlternation && opt.longestMatch();
916  
-		final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
  916
+		final Map<Integer, CachedMatch>[] cache = offsetCache(opt,
  917
+				options.length);
917 918
 		List<Integer> list = new ArrayList<Integer>(startOffsets(s, options,
918 919
 				cache));
919 920
 		Collections.sort(list);
@@ -927,11 +928,13 @@ public Matcher find(final CharSequence s, Options opt)
927 928
 	 * take place.
928 929
 	 * 
929 930
 	 * @param options
  931
+	 * @param length
  932
+	 *            length of character sequence
930 933
 	 * 
931 934
 	 * @return map from labels to sets of offsets where the associated rules are
932 935
 	 *         known not to match
933 936
 	 */
934  
-	private Map<Integer, CachedMatch>[] offsetCache(Options options) {
  937
+	private Map<Integer, CachedMatch>[] offsetCache(Options options, int length) {
935 938
 		if (initialRules == null) {
936 939
 			synchronized (this) {
937 940
 				// look for reversals
@@ -951,13 +954,13 @@ public Matcher find(final CharSequence s, Options opt)
951 954
 		Map<Integer, CachedMatch>[] offsetCache = new Map[max + 1];
952 955
 		boolean lean = options.leanMemory, fat = options.fatMemory;
953 956
 		if (!(lean || fat))
954  
-			fat = options.end - options.start < options.longStringLength;
  957
+			fat = length < options.longStringLength;
955 958
 		for (int i = 0; i < offsetCache.length; i++) {
956 959
 			Map<Integer, CachedMatch> m;
957 960
 			if (lean)
958 961
 				m = new TreeMap<Integer, CachedMatch>();
959 962
 			else if (fat)
960  
-				m = new MatchCache(options.end - options.start);
  963
+				m = new MatchCache(length);
961 964
 			else
962 965
 				m = new HashMap<Integer, CachedMatch>();
963 966
 			offsetCache[i] = m;
@@ -1137,7 +1140,8 @@ public Matcher matches(final CharSequence s, Options opt)
1137 1140
 			throws GrammarException {
1138 1141
 		checkComplete();
1139 1142
 		final GlobalState options = verifyOptions(s, opt);
1140  
-		final Map<Integer, CachedMatch>[] cache = offsetCache(opt);
  1143
+		final Map<Integer, CachedMatch>[] cache = offsetCache(opt,
  1144
+				options.length);
1141 1145
 		final Set<Integer> startOffsets = startOffsets(s, options, cache);
1142 1146
 		final Matcher m = root.matcher(options.start, cache, new DummyMatcher(
1143 1147
 				options));
@@ -1335,8 +1339,10 @@ private GlobalState verifyOptions(CharSequence s, Options opt) {
1335 1339
 		if (opt.start() > 0 && opt.start() >= s.length())
1336 1340
 			throw new GrammarException(
1337 1341
 					"start offset specified beyond end of string");
1338  
-		if (opt.end == -1)
  1342
+		if (opt.end == -1) {
  1343
+			opt = new Options(opt);
1339 1344
 			opt.end(s.length());
  1345
+		}
1340 1346
 		return new GlobalState(s, opt);
1341 1347
 	}
1342 1348
 
27  src/dfh/grammar/Options.java
@@ -20,7 +20,7 @@
20 20
  * @author David Houghton
21 21
  * 
22 22
  */
23  
-public class Options {
  23
+public class Options implements Cloneable {
24 24
 
25 25
 	/**
26 26
 	 * Whether matches may overlap.
@@ -164,6 +164,26 @@ public Options(GlobalState options) {
164 164
 	}
165 165
 
166 166
 	/**
  167
+	 * Creates a clone of the input parameter.
  168
+	 * 
  169
+	 * @param o
  170
+	 *            clone of parameter
  171
+	 */
  172
+	public Options(Options o) {
  173
+		this.allowOverlap = o.allowOverlap;
  174
+		this.end = o.end;
  175
+		this.fatMemory = o.fatMemory;
  176
+		this.keepRightmost = o.keepRightmost;
  177
+		this.leanMemory = o.leanMemory;
  178
+		this.longestMatch = o.longestMatch;
  179
+		this.longStringLength = o.longStringLength;
  180
+		this.maxRecursionDepth = o.maxRecursionDepth;
  181
+		this.start = o.start;
  182
+		this.study = o.study;
  183
+		this.trace = o.trace;
  184
+	}
  185
+
  186
+	/**
167 187
 	 * @return whether matches iterated over may overlap
168 188
 	 */
169 189
 	public boolean allowOverlap() {
@@ -393,4 +413,9 @@ public Options longStringLength(int longStringLength) {
393 413
 		this.longStringLength = longStringLength;
394 414
 		return this;
395 415
 	}
  416
+
  417
+	@Override
  418
+	public Object clone() {
  419
+		return new Options(this);
  420
+	}
396 421
 }

0 notes on commit f7b4f13

Please sign in to comment.
Something went wrong with that request. Please try again.