def SimplifyNode(node, restriction=None):
  if node.getType() == QueryLexer.VALUE:
    return node
  elif node.getType() == QueryParser.SEQUENCE and node.getChildCount() == 1:
    return SimplifyNode(node.children[0], restriction)
  elif node.getType() == QueryParser.CONJUNCTION and node.getChildCount() == 1:
    return SimplifyNode(node.children[0], restriction)
  elif node.getType() == QueryParser.DISJUNCTION and node.getChildCount() == 1:
    return SimplifyNode(node.children[0], restriction)
  elif node.getType() == QueryLexer.HAS or node.getType() == QueryLexer.EQ:
    lhs = node.getChild(0);
    if lhs.getType() == QueryLexer.VALUE:
      myField = lhs.getChild(1).getText()
      if restriction is None:
        restriction = lhs
      else:
        otherField = restriction.getChild(1).getText();
        if myField != otherField:
          raise QueryTreeException(
              "Restriction on %s and %s" % (otherField, myField),
              lhs.getChild(1).getCharPositionInLine());
    rhs = node.getChild(1);
    flattened = SimplifyNode(rhs, restriction);
    if (flattened.getType() == QueryLexer.HAS or
        flattened.getType() == QueryLexer.EQ or
        flattened.getType() == QueryLexer.CONJUNCTION or
        flattened.getType() == QueryLexer.DISJUNCTION or
        flattened.getType() == QueryLexer.SEQUENCE):
      return flattened;
    if flattened != rhs:
      node.setChild(1, flattened);
    if restriction != lhs:
      node.setChild(0, restriction);
    return node;
  for i in range(node.getChildCount()):
    original = node.getChild(i);
    flattened = SimplifyNode(node.getChild(i), restriction);
    if original != flattened:
      node.setChild(i, flattened)
  return node;
dataset/ETHPy150Open GoogleCloudPlatform/python-compat-runtime/appengine-compat/exported_appengine_sdk/google/appengine/api/search/query_parser.py/SimplifyNode/original