reorganize friendlyparser, behavior unchanged
authorKshitij Bansal <kshitij@cs.nyu.edu>
Thu, 10 Jul 2014 17:05:45 +0000 (13:05 -0400)
committerKshitij Bansal <kshitij@cs.nyu.edu>
Thu, 10 Jul 2014 17:05:45 +0000 (13:05 -0400)
src/parser/antlr_input.cpp

index ca8f805b4e5868f96ca39afe440bfa616433aa1a..d72b311cb8266123630f09f5fbe2a140f4357f6f 100644 (file)
@@ -400,32 +400,38 @@ std::string parseErrorHelper(const char* lineStart, int charPositionInLine, cons
         }
       }
     } else {
-      // go to nearest alphanumeric string (before current position),
-      // see if that word can be found in original message. If so,
-      // point to that, else keep pointer where it was.
-      int nearestWordEn = caretPos - 1;
-      while(nearestWordEn > 0 && !isSimpleChar(slice[nearestWordEn])) {
-        --nearestWordEn;
-      }
-      if(isSimpleChar(slice[nearestWordEn])) {
-        int nearestWordSt = nearestWordEn;
-        while(nearestWordSt > 0 && isSimpleChar(slice[nearestWordSt - 1])) {
-          --nearestWordSt;
+      bool foundCaretPos = false;
+
+      for(int tries = 0; tries < 1 && caretPos > 0 && !foundCaretPos; ++tries) {
+        // go to nearest alphanumeric string (before current position),
+        // see if that word can be found in original message. If so,
+        // point to that, else keep pointer where it was.
+        int nearestWordEn = caretPos - 1;
+        while(nearestWordEn > 0 && !isSimpleChar(slice[nearestWordEn])) {
+          --nearestWordEn;
         }
-        string word = slice.substr(nearestWordSt, (nearestWordEn - nearestWordSt + 1));
-        size_t matchLoc = wholeWordMatch(message, word, isSimpleChar);
-        Debug("friendlyparser") << "[friendlyparser] nearest word = " << word << std::endl;
-        Debug("friendlyparser") << "[friendlyparser] matchLoc = " << matchLoc << endl;
-        if( matchLoc != string::npos ) {
-          Debug("friendlyparser") << "[friendlyparser] strong evidence that caret should be at "
-                                  << nearestWordSt << std::endl;
+        if(isSimpleChar(slice[nearestWordEn])) {
+          int nearestWordSt = nearestWordEn;
+          while(nearestWordSt > 0 && isSimpleChar(slice[nearestWordSt - 1])) {
+            --nearestWordSt;
+          }
+          string word = slice.substr(nearestWordSt, (nearestWordEn - nearestWordSt + 1));
+          size_t matchLoc = wholeWordMatch(message, word, isSimpleChar);
+          Debug("friendlyparser") << "[friendlyparser] nearest word = " << word << std::endl;
+          Debug("friendlyparser") << "[friendlyparser] matchLoc = " << matchLoc << endl;
+          if( matchLoc != string::npos ) {
+            Debug("friendlyparser") << "[friendlyparser] strong evidence that caret should be at "
+                                    << nearestWordSt << std::endl;
+            foundCaretPos = true;
+          }
           caretPos = nearestWordSt;
-        } else {
-          // this doesn't look good. caret generally getting printed
-          // at unhelpful positions. improve upstream?
-          return message;
         }
       }
+      if( !foundCaretPos) {
+        // this doesn't look good. caret generally getting printed
+        // at unhelpful positions. improve upstream?
+        return message;
+      }
     }
     caretPos += caretPosExtra;
   }// end of caret position computation/heuristics