code: private String appendErrorContext(String reason) {
          // Note: This relies on the calling code maintaining the ruleIndex
          // at a position that is useful for debugging.
          // For example, at the beginning of a reset or relation etc.
          StringBuilder msg = new StringBuilder(reason);
          msg.append(" at index ").append(ruleIndex);
          // We are not counting line numbers.
  
          msg.append(" near \"");
          // before ruleIndex
          int start = ruleIndex - (U_PARSE_CONTEXT_LEN - 1);
          if(start < 0) {
              start = 0;
          } else if(start > 0 && Character.isLowSurrogate(rules.charAt(start))) {
              ++start;
          }
          msg.append(rules, start, ruleIndex);
  
          msg.append('!');
          // starting from ruleIndex
          int length = rules.length() - ruleIndex;
          if(length >= U_PARSE_CONTEXT_LEN) {
              length = U_PARSE_CONTEXT_LEN - 1;
              if(Character.isHighSurrogate(rules.charAt(ruleIndex + length - 1))) {
                  --length;
              }
          }
          msg.append(rules, ruleIndex, ruleIndex + length);
          return msg.append('\"').toString();
      }
docs: C++ setErrorContext()