protected static void updateTreeLabels(
      Tree root, Tree tree, MutableInteger offset, MutableInteger leafIndex) {
    if (tree.isLeaf()) {
      leafIndex.value++;
      return;
    }
    String labelValue = tree.label().value().toUpperCase();
    int begin = root.leftCharEdge(tree);
    int end = root.rightCharEdge(tree);
    // System.out.println(labelValue+"("+begin+","+end+")");
    int length = end - begin;

    // apply offset to begin extent
    begin += offset.value;

    // calculate offset delta based on label
    if (double_quote_lable_pattern.matcher(labelValue).matches() && length > 1) {
      offset.value--;
      log.debug("Quotes label pattern fired: " + offset);
    } else if (bracket_label_pattern.matcher(labelValue).matches()) {
      offset.value -= 4;
      log.debug("Bracket label pattern fired: " + offset);
    } else if (tree.isPreTerminal()) {
      Tree leaf = tree.firstChild();
      String text = leaf.label().value();
      Matcher matcher = escaped_char_pattern.matcher(text);
      while (matcher.find()) {
        offset.value--;
      }
    }

    for (Tree child : tree.children()) updateTreeLabels(root, child, offset, leafIndex);

    // apply offset to end extent
    end += offset.value;

    // set begin and end offsets on node
    MapLabel label = new MapLabel(tree.label());
    label.put(BEGIN_KEY, begin);
    label.put(END_KEY, end);
    label.put(MapLabel.INDEX_KEY, leafIndex.value);
    tree.setLabel(label);
  }
  /**
   * Maps Tree node offsets using provided mapping.
   *
   * @param tree the Tree whose begin and end extents should be mapped.
   * @param mapping the list of RangeMap objects which defines the mapping.
   */
  protected static void mapOffsets(Tree tree, List<RangeMap> mapping) {
    // if mapping is empty, then assume 1-to-1 mapping.
    if (mapping == null || mapping.size() == 0) return;

    int begin_map_index = 0;
    RangeMap begin_rmap = mapping.get(begin_map_index);
    TREE:
    for (Tree t : tree) {
      if (t.isLeaf()) continue;
      MapLabel label = (MapLabel) t.label();
      int begin = (Integer) label.get(BEGIN_KEY);
      // "end" must be index of last char in range
      int end = (Integer) label.get(END_KEY) - 1;

      // find the first rangemap whose end is greater than the
      // beginning of current annotation.
      // log.debug("Finding RangeMap whose extents include
      // annotation.begin");
      while (begin_rmap.end <= begin) {
        begin_map_index++;
        if (begin_map_index >= mapping.size()) break TREE;
        begin_rmap = mapping.get(begin_map_index);
      }

      // if beginning of current rangemap is greater than end of
      // current annotation, then skip this annotation (default
      // mapping is 1-to-1).
      if (begin_rmap.begin > end) {
        // log.debug("Skipping annotation (assuming 1-to-1 offset
        // mapping)");
        continue;
      }

      // if beginning of current annotation falls within current range
      // map, then map it back to source space.
      int new_begin = begin;
      if (begin_rmap.begin <= new_begin) {
        // log.debug("Applying RangeMap to begin offset");
        new_begin = begin_rmap.map(new_begin);
      }

      // find the first rangemap whose end is greater than the end of
      // current annotation.
      // log.debug("Finding RangeMap whose extents include
      // annotation.end");
      int end_map_index = begin_map_index;
      RangeMap end_rmap = begin_rmap;
      END_OFFSET:
      while (end_rmap.end <= end) {
        end_map_index++;
        if (end_map_index >= mapping.size()) break END_OFFSET;
        end_rmap = mapping.get(end_map_index);
      }

      // if end of current annotation falls within "end" range map,
      // then map it back to source space.
      int new_end = end;
      if (end_rmap.begin <= end) {
        // log.debug("Applying RangeMap to end offset");
        new_end = end_rmap.map(end);
      }

      label.put(BEGIN_KEY, new_begin);
      label.put(END_KEY, new_end + 1);
    }
  }