]> gitweb.fperrin.net Git - DictionaryPC.git/blobdiff - src/com/hughes/android/dictionary/parser/WikiTokenizer.java
Clearer error message if newline could not be found.
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / WikiTokenizer.java
index 9f3444f8f06d5ee27cbb8fa5d1fd7741add3e653..721231905326bfc4d70a388782e57415a72fd36c 100644 (file)
@@ -471,7 +471,7 @@ public final class WikiTokenizer {
         return token;
     }
 
-    final static String[] patterns = { "\n", "{{", "}}", "[[", "]]", "|", "=", "<!--" };
+    final static String[] patterns = { "\n", "{{", "}}", "[[", "]]", "[", "]", "|", "=", "<!--" };
     private int escapedFindEnd(final int start, final String toFind) {
         assert tokenStack.isEmpty();
 
@@ -479,17 +479,21 @@ public final class WikiTokenizer {
 
         int end = start;
         int firstNewline = -1;
-        int[] nextMatch = new int[8];
-        for (int i = 0; i < 8; ++i) {
-            nextMatch[i] = wikiText.indexOf(patterns[i], start);
-            if (nextMatch[i] == -1) nextMatch[i] = i > 0 ? 0x7fffffff : wikiText.length();
+        int[] nextMatch = new int[patterns.length];
+        for (int i = 0; i < nextMatch.length; ++i) {
+            nextMatch[i] = -2;
         }
+        int singleBrackets = 0;
         while (end < wikiText.length()) {
             // Manual replacement for matcher.find(end),
             // because Java regexp is a ridiculously slow implementation.
             // Initialize to always match the end.
             int matchIdx = 0;
-            for (int i = 1; i < 8; ++i) {
+            for (int i = 0; i < nextMatch.length; ++i) {
+                if (nextMatch[i] <= end) {
+                    nextMatch[i] = wikiText.indexOf(patterns[i], end);
+                    if (nextMatch[i] == -1) nextMatch[i] = i > 0 ? 0x7fffffff : wikiText.length();
+                }
                 if (nextMatch[i] < nextMatch[matchIdx]) {
                     matchIdx = i;
                 }
@@ -498,8 +502,6 @@ public final class WikiTokenizer {
             int matchStart = nextMatch[matchIdx];
             String matchText = patterns[matchIdx];
             int matchEnd = matchStart + matchText.length();
-            nextMatch[matchIdx] = wikiText.indexOf(patterns[matchIdx], matchEnd);
-            if (nextMatch[matchIdx] == -1) nextMatch[matchIdx] = matchIdx > 0 ? 0x7fffffff : wikiText.length();
             if (matchIdx == 0) {
                 matchText = "";
                 matchEnd = matchStart;
@@ -521,14 +523,23 @@ public final class WikiTokenizer {
                     addFunctionArg(insideFunction, matchStart);
                 }
                 return matchEnd;
+            } else if (matchText.equals("[")) {
+                singleBrackets++;
+            } else if (matchText.equals("]")) {
+                if (singleBrackets > 0) singleBrackets--;
             } else if (matchText.equals("[[") || matchText.equals("{{")) {
                 tokenStack.add(matchText);
             } else if (matchText.equals("]]") || matchText.equals("}}")) {
                 if (tokenStack.size() > 0) {
                     final String removed = tokenStack.remove(tokenStack.size() - 1);
                     if (removed.equals("{{") && !matchText.equals("}}")) {
-                        errors.add("Unmatched {{ error: " + wikiText.substring(start, matchEnd));
-                        return safeIndexOf(wikiText, start, "\n", "\n");
+                        if (singleBrackets >= 2) { // assume this is really two closing single ]
+                            singleBrackets -= 2;
+                            tokenStack.add(removed);
+                        } else {
+                            errors.add("Unmatched {{ error: " + wikiText.substring(start, matchEnd));
+                            return safeIndexOf(wikiText, start, "\n", "\n");
+                        }
                     } else if (removed.equals("[[") && !matchText.equals("]]")) {
                         errors.add("Unmatched [[ error: " + wikiText.substring(start, matchEnd));
                         return safeIndexOf(wikiText, start, "\n", "\n");
@@ -568,7 +579,7 @@ public final class WikiTokenizer {
             // We were looking for the end, we got it.
             return end;
         }
-        errors.add("Couldn't find: " + toFind + ", "+ wikiText.substring(start));
+        errors.add("Couldn't find: " + (toFind.equals("\n") ? "newline" : toFind) + ", "+ wikiText.substring(start));
         if (firstNewline != -1) {
             return firstNewline;
         }