Removes any parenthesis around nodes that can't be resolved further. Input must be a top level expression. Returns a node.
357124
function removeUnnecessaryParens(node, rootNode=false) {
  // Parens that wrap everything are redundant.
  // NOTE: removeUnnecessaryParensSearch recursively removes parens that aren't
  // needed, while this step only applies to the very top level expression.
  // e.g. (2 + 3) * 4 can't become 2 + 3 * 4, but if (2 + 3) as a top level
  // expression can become 2 + 3
  if (rootNode) {
    while (Node.Type.isParenthesis(node)) {
      node = node.content;
    }
  }
  return removeUnnecessaryParensSearch(node);
}
344711
function (node)
    {
        if (typeof node !== "string") { return node; }
        return node.replace(/^\s+|\s+$/g, '')
            .replace(/\\/g,  "\\5c")
            .replace(/ /g,   "\\20")
            .replace(/\"/g,  "\\22")
            .replace(/\&/g,  "\\26")
            .replace(/\'/g,  "\\27")
            .replace(/\//g,  "\\2f")
            .replace(/:/g,   "\\3a")
            .replace(/</g,   "\\3c")
            .replace(/>/g,   "\\3e")
            .replace(/@/g,   "\\40");
    }
532329
private static ExpressionTree stripParensAndCasts(ExpressionTree expr) {
    boolean someChange = true;
    while (someChange) {
      someChange = false;
      if (expr.getKind().equals(PARENTHESIZED)) {
        expr = ((ParenthesizedTree) expr).getExpression();
        someChange = true;
      }
      if (expr.getKind().equals(TYPE_CAST)) {
        expr = ((TypeCastTree) expr).getExpression();
        someChange = true;
      }
    }
    return expr;
  }
329875
def trim_tree_before(element, include_element=True, keep_head=True):
    
    el = element
    for parent_el in element.iterancestors():
        parent_el.text = None
        if el != element or include_element:
            el = el.getprevious()
        else:
            parent_el.text = el.tail
        while el is not None:
            remove_el = el
            el = el.getprevious()
            tag = remove_el.tag
            is_head = isinstance(tag, string_class) and tag.lower() == 'head'
            if not keep_head or not is_head:
                parent_el.remove(remove_el)
        el = parent_el
321177
def remove_elements(parent_to_parse, element_paths, clear_empty=False):
    

    element = get_element(parent_to_parse)
    removed = []

    if element is None or not element_paths:
        return removed

    if isinstance(element_paths, string_types):
        rem = remove_element(element, element_paths, clear_empty)
        removed.extend(rem if isinstance(rem, list) else [rem])
    else:
        for xpath in element_paths:
            rem = remove_element(element, xpath, clear_empty)
            removed.extend(rem if isinstance(rem, list) else [rem])

    return removed
127049
function() {
    var node
      , segs = []
      , star;

    star = this.accept('*');
    if (star) segs.push(new nodes.Literal('*'));

    while (true) {
      if (this.accept('{')) {
        this.state.push('interpolation');
        segs.push(this.expression());
        this.expect('}');
        this.state.pop();
      } else if (node = this.accept('-')){
        segs.push(new nodes.Literal('-'));
      } else if (node = this.accept('ident')){
        segs.push(node.val);
      } else {
        break;
      }
    }
    if (!segs.length) this.expect('ident');
    return segs;
  }
1367675
function nodeText (node) {
  return squashWhitespace(node.textContent)

  function squashWhitespace (string) {
    return string.replace(/\s{2,}/g, ' ').trim()
  }
}
575670
@Override
  public void endVisit(ParenthesizedExpression node) {
    Expression expr = node.getExpression();
    if (expr instanceof ParenthesizedExpression) {
      Expression inner = ((ParenthesizedExpression) expr).getExpression();
      if (isEqualityExpression(inner)) {
        node.replaceWith(TreeUtil.remove(expr));
      }
    } else if (!(node.getParent() instanceof Expression) && isEqualityExpression(expr)) {
      node.replaceWith(TreeUtil.remove(expr));
    }
  }
1042776
function fixParagraphNode(node, fullText) {
    const firstNode = node.children[0];
    const lastNode = node.children[node.children.length - 1];
    node.range = [firstNode.range[0], lastNode.range[1]];
    node.raw = fullText.slice(node.range[0], node.range[1]);
    node.loc = {
        start: {
            line: firstNode.loc.start.line,
            column: firstNode.loc.start.column
        },
        end: {
            line: lastNode.loc.end.line,
            column: lastNode.loc.end.column
        }
    };
}
361413
function (node, match) {
      do {
        if (!node || node.nodeName.toLowerCase() === match) {
          break
        }
        node = node.parentNode
      } while (node)

      return node
    }
231732
function removeUnnecessaryParensInOperatorNode(node) {
  node.args.forEach((child, i) => {
    node.args[i] = removeUnnecessaryParensSearch(child);
  });

  // Sometimes, parens are around expressions that have been simplified
  // all they can be. If that expression is part of an addition or subtraction
  // operation, we can remove the parenthesis.
  // e.g. (x+4) + 12 -> x+4 + 12
  if (node.op === '+') {
    node.args.forEach((child, i) => {
      if (Node.Type.isParenthesis(child) &&
          !canCollectOrCombine(child.content)) {
        // remove the parens by replacing the child node (in its args list)
        // with its content
        node.args[i] = child.content;
      }
    });
  }
  // This is different from addition because when subtracting a group of terms
  //in parenthesis, we want to distribute the subtraction.
  // e.g. `(2 + x) - (1 + x)` => `2 + x - (1 + x)` not `2 + x - 1 + x`
  else if (node.op === '-') {
    if (Node.Type.isParenthesis(node.args[0]) &&
        !canCollectOrCombine(node.args[0].content)) {
      node.args[0] = node.args[0].content;
    }
  }

  return node;
}
528498
public static String getNodeText(Node<?> node, InputBuffer inputBuffer) {
        checkArgNotNull(node, "node");
        checkArgNotNull(inputBuffer, "inputBuffer");
        if (node.hasError()) {
            // if the node has a parse error we cannot simply cut a string out of the underlying input buffer, since we
            // would also include illegal characters, so we need to build it constructively
            StringBuilder sb = new StringBuilder();
            for (int i = node.getStartIndex(); i < node.getEndIndex(); i++) {
                char c = inputBuffer.charAt(i);
                switch (c) {
                    case Chars.DEL_ERROR:
                        i++;
                        break;
                    case Chars.INS_ERROR:
                    case Chars.EOI:
                        break;
                    case Chars.RESYNC_START:
                        i++;
                        while (inputBuffer.charAt(i) != Chars.RESYNC_END) i++;
                        break;
                    case Chars.RESYNC_END:
                    case Chars.RESYNC_EOI:
                    case Chars.RESYNC:
                        // we should only see proper RESYNC_START / RESYNC_END blocks
                        throw new IllegalStateException();
                    default:
                        sb.append(c);
                }
            }
            return sb.toString();
        }        
        return inputBuffer.extract(node.getStartIndex(), node.getEndIndex());
    }
1047844
def suppress_deg_one_node(self, to_par_edge, nd_id, to_child_edge):
        """"""
        # circumvent the node with nd_id
        to_child_edge_id = to_child_edge['@id']
        par = to_par_edge['@source']
        self._edge_by_source[par][to_child_edge_id] = to_child_edge
        to_child_edge['@source'] = par
        # make it a tip...
        del self._edge_by_source[nd_id]
        # delete it
        self._del_tip(nd_id)
75528
function() {
    var node = this.expression();

    while (this.accept(',')) {
      if (node.isList) {
        list.push(this.expression());
      } else {
        var list = new nodes.Expression(true);
        list.push(node);
        list.push(this.expression());
        node = list;
      }
    }
    return node;
  }
1097830
def clear_children(parent_to_parse, element_path=None):
    

    element = get_element(parent_to_parse, element_path)

    if element is None:
        return parent_to_parse
    else:
        elem_txt = element.text
        elem_atr = element.attrib

        element.clear()

        element.text = elem_txt
        element.attrib = elem_atr

    return element
13340
private Node getParentNode(Node node) {

		if (node == null || node == root)
			return null;

		Node newNode = node.getParentNode();
		if (newNode == null)
			return null;

		int accept = acceptNode(newNode);

		if (accept == NodeFilter.FILTER_ACCEPT)
			return newNode;
		else
			// if (accept == NodeFilter.SKIP_NODE)
			// and REJECT too.
			return getParentNode(newNode);

	}
317819
private static String cleanTranslationText(final String input, final boolean removeWhitespaceFromStart,
            final boolean removeWhitespaceFromEnd) {
        String retValue = XMLUtilities.cleanText(input);

        retValue = retValue.trim();

        /*
         * When presenting the contents of a childless XML node to the translator, there is no need for white space padding.
         * When building up a translatable string from a succession of text nodes, whitespace becomes important.
         */
        if (!removeWhitespaceFromStart) {
            if (PRECEEDING_WHITESPACE_SIMPLE_RE_PATTERN.matcher(input).matches()) {
                retValue = " " + retValue;
            }
        }

        if (!removeWhitespaceFromEnd) {
            if (TRAILING_WHITESPACE_SIMPLE_RE_PATTERN.matcher(input).matches()) {
                retValue += " ";
            }
        }

        return retValue;
    }