Transform parsed tree for grammar with removed unit rules.
    The unit rules will be returned back to the tree.
    :param root: Root of the parsed tree.
    :return: Modified tree.
178715
def unit_rules_restore(root):
    # type: (Nonterminal) -> Nonterminal
    
    items = Traversing.post_order(root)
    items = filter(lambda x: isinstance(x, ReducedUnitRule), items)
    for rule in items:
        parent_nonterm = rule.from_symbols[0]  # type: Nonterminal
        # restore chain of unit rules
        for r in rule.by_rules:
            created_rule = r()  # type: Rule
            parent_nonterm._set_to_rule(created_rule)
            created_rule._from_symbols.append(parent_nonterm)
            created_nonterm = r.toSymbol()  # type: Nonterminal
            created_rule._to_symbols.append(created_nonterm)
            created_nonterm._set_from_rule(created_rule)
            parent_nonterm = created_nonterm
        # restore last rule
        last_rule = rule.end_rule()  # type: Rule
        last_rule._from_symbols.append(parent_nonterm)
        parent_nonterm._set_to_rule(last_rule)
        for ch in rule.to_symbols:  # type: Nonterminal
            ch._set_from_rule(last_rule)
            last_rule._to_symbols.append(ch)
    return root
738517
def root(self):
        
        ret = self
        while hasattr(ret, 'parent') and ret.parent:
            ret = ret.parent
        return ret if isinstance(ret, SchemaABC) else None
959104
public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) {
       if (shouldStop(CompileState.ATTR))
           return List.nil();
        return enterTrees(roots);
    }
859612
def _remove_unit_rule(g, rule):
    """"""
    new_rules = [x for x in g.rules if x != rule]
    refs = [x for x in g.rules if x.lhs == rule.rhs[0]]
    new_rules += [build_unit_skiprule(rule, ref) for ref in refs]
    return Grammar(new_rules)
471285
def root(self):
            
            str_treeRoot                = '/'
            self.l_cwd                  = [str_treeRoot]
            self.snode_current          = self.snode_root
            self.sbranch_current        = self.sbranch_root
107580
def translate(root_list, use_bag_semantics=False):
    
    translator = (Translator() if use_bag_semantics else SetTranslator())
    return [translator.translate(root).to_sql() for root in root_list]
656214
def parse(self, lines, root):
    

    lst = IgnoreList(root)
    lst.parse(lines)
    self.append(lst)
1289336
def pre_order(root):
        # type: (Nonterminal) -> Generator
        

        def traverse_rule(item, callback):
            yield item
            for el in item.to_symbols:
                yield callback(el)

        def traverse_nonterminal(item, callback):
            yield item
            yield callback(item.to_rule)

        def traverse_terminal(item, callback):
            yield item

        return Traversing.traverse_separated(root, traverse_rule, traverse_nonterminal, traverse_terminal)
669319
def convert_parens_in_rst_tree_str(rst_tree_str):
    '''
    
    '''
    def replace_brackets(matchobj):
        edu = matchobj.group(0)
        for (br, br_repl) in DIS_ESCAPES:
            edu = re.sub("\{}".format(br), br_repl, edu)
        return edu

    return re.sub('_!(.*?)_!', replace_brackets, rst_tree_str)
1230896
def fromtree(cls, tree):
        
        mets = cls()
        mets.tree = tree
        mets._parse_tree(tree)

        return mets
4059
def split_root(self):
        
        if not PY3 and hasattr(self._lib, 'splitunc'):
            root, rest = self._lib.splitunc(self.path)
            if root:
                if rest.startswith(self._sep):
                    root += self._sep
                    rest = rest[1:]
                return self.__class__(root), self.__class__(rest)
        root, rest = self._lib.splitdrive(self.path)
        if root:
            if rest.startswith(self._sep):
                root += self._sep
                rest = rest[1:]
            return self.__class__(root), self.__class__(rest)
        if self.path.startswith(self._sep):
            return self.__class__(self._sep), self.__class__(rest[1:])
        return self.__class__(''), self
971771
def transform_tree(tree, scope_stack)
      if Symbol === tree[0]
        case tree[0]
        when :assign, :massign
          lhs, rhs = tree[1..2]
          begin
            add_variables_from_node(lhs, scope_stack)
          rescue SyntaxError => err
            wrap_node_with_error(tree)
          else
            transform_tree(rhs, scope_stack)
          end
        when :for
          vars, iterated, body = tree[1..3]
          add_variables_from_node(vars, scope_stack)
          transform_tree(iterated, scope_stack)
          transform_tree(body, scope_stack)
        when :var_ref
          # When we reach a :var_ref, we should know everything we need to know
          # in order to tell if it should be transformed into a :vcall.
          if tree[1][0] == :@ident && !scope_stack.has_variable?(tree[1][1])
            tree[0] = :vcall
          end
        when :class
          name, superclass, body = tree[1..3]
          if name[1][0] == :class_name_error || scope_stack.in_method?
            wrap_node_with_error(tree)
          else
            transform_tree(superclass, scope_stack) if superclass  # superclass node
            scope_stack.with_closed_scope do
              transform_tree(body, scope_stack)
            end
          end
        when :module
          name, body = tree[1..2]
          if name[1][0] == :class_name_error || scope_stack.in_method?
            wrap_node_with_error(tree)
          else
            scope_stack.with_closed_scope do
              transform_tree(body, scope_stack)  # body
            end
          end
        when :sclass
          singleton, body = tree[1..2]
          transform_tree(singleton, scope_stack)
          scope_stack.with_closed_scope do
            transform_tree(body, scope_stack)
          end
        when :def
          scope_stack.with_closed_scope(true) do
            param_node = tree[2]
            body = tree[3]
            transform_params_then_body(tree, param_node, body, scope_stack)
          end
        when :defs
          transform_tree(tree[1], scope_stack)  # singleton could be a method call!
          scope_stack.with_closed_scope(true) do
            param_node = tree[4]
            body = tree[5]
            transform_params_then_body(tree, param_node, body, scope_stack)
          end
        when :lambda
          param_node, body = tree[1..2]
          scope_stack.with_open_scope do
            transform_params_then_body(tree, param_node, body, scope_stack)
          end
        when :rescue
          list, name, body = tree[1..3]
          transform_tree(list, scope_stack) if list
          # Don't forget the rescue argument!
          if name
            add_variables_from_node(name, scope_stack)
          end
          transform_tree(body, scope_stack)
        when :method_add_block
          call, block = tree[1..2]
          # first transform the call
          transform_tree(call, scope_stack)
          # then transform the block
          param_node, body = block[1..2]
          scope_stack.with_open_scope do
            begin
              if param_node
                transform_params(param_node[1], scope_stack)
                if param_node[2]
                  add_variable_list(param_node[2], scope_stack, false)
                end
              end
            rescue SyntaxError
              wrap_node_with_error(tree)
            else
              transform_tree(body, scope_stack)
            end
          end
        when :binary
          # must check for named groups in a literal match. wowzerz.
          lhs, op, rhs = tree[1..3]
          if op == :=~
            if lhs[0] == :regexp_literal
              add_locals_from_regexp(lhs, scope_stack)
              transform_tree(rhs, scope_stack)
            elsif lhs[0] == :paren && !lhs[1].empty? && lhs[1] != [[:void_stmt]] && lhs[1].last[0] == :regexp_literal
              lhs[1][0..-2].each { |node| transform_tree(node, scope_stack) }
              add_locals_from_regexp(lhs[1].last, scope_stack)
              transform_tree(rhs, scope_stack)
            else
              transform_in_order(tree, scope_stack)
            end
          else
            transform_in_order(tree, scope_stack)
          end
        when :if_mod, :unless_mod, :while_mod, :until_mod, :rescue_mod
          # The AST is the reverse of the parse order for these nodes.
          transform_tree(tree[2], scope_stack)
          transform_tree(tree[1], scope_stack)
        when :alias_error, :assign_error  # error already top-level! wrap it again.
          wrap_node_with_error(tree)
        else
          transform_in_order(tree, scope_stack)
        end
      else
        transform_in_order(tree, scope_stack)
      end
    end
221142
def normalize(self):
        
        for i, rnode in enumerate(self._nodes):
            rule = Rule(self, i - 1, descendant=False)
            rule.normalize()
853667
def dump_selectors(root)
      root.children.each do |child|
        next dump_selectors(child) if child.is_a?(Tree::DirectiveNode)
        next unless child.is_a?(Tree::RuleNode)
        child.rule = [child.parsed_rules.to_s]
        dump_selectors(child)
      end
    end
1132277
def transform_from_chomsky_normal_form(root):
    # type: (Nonterminal) -> Nonterminal
    
    # Transforms leaves
    items = Traversing.post_order(root)
    items = filter(lambda x: isinstance(x, (ChomskyTermRule, ChomskyTerminalReplaceRule)), items)
    de = deque(items)
    while de:
        rule = de.popleft()
        if isinstance(rule, ChomskyTermRule):
            upper_nonterm = rule.from_symbols[0]  # type: Nonterminal
            term = rule.to_symbols[0]
            Manipulations.replaceNode(upper_nonterm, term)
        elif isinstance(rule, ChomskyTerminalReplaceRule):
            created_rule = rule.from_rule()  # type: Rule
            Manipulations.replaceRule(rule, created_rule)
            de.append(created_rule)

    # Transform inner nodes
    items = Traversing.post_order(root)
    items = filter(lambda x: isinstance(x, ChomskySplitRule), items)
    de = deque(items)
    while de:
        rule = de.popleft()
        if isinstance(rule, ChomskySplitRule):
            created_rule = rule.from_rule()  # type: Rule
            # parent nonterminals
            for p in rule.from_symbols:  # type: Nonterminal
                p._set_to_rule(created_rule)
                created_rule._from_symbols.append(p)
            # left child
            left_child = rule.to_symbols[0]  # type: Nonterminal
            left_child._set_from_rule(created_rule)
            created_rule._to_symbols.append(left_child)
            # right childs
            for ch in rule.to_symbols[1].to_rule.to_symbols:  # type: Nonterminal
                ch._set_from_rule(created_rule)
                created_rule.to_symbols.append(ch)
            # add back if the rules is ChomskySplitRule again
            de.appendleft(created_rule)
    return root
11872
def fixup_parse_tree(cls_node):
    
    for node in cls_node.children:
        if node.type == syms.suite:
            # already in the preferred format, do nothing
            return

    # !%@#! oneliners have no suite node, we have to fake one up
    for i, node in enumerate(cls_node.children):
        if node.type == token.COLON:
            break
    else:
        raise ValueError("No class suite and no ':'!")

    # move everything into a suite node
    suite = Node(syms.suite, [])
    while cls_node.children[i+1:]:
        move_node = cls_node.children[i+1]
        suite.append_child(move_node.clone())
        move_node.remove()
    cls_node.append_child(suite)
    node = suite
319706
def to_qtree(self, instring, schema):
        
        root_list = self.to_syntax_tree(instring, schema)
        return qtree_translator.translate(root_list)