def METHOD_NAME(self):
    """finalize"""
    root = current = None
    stack = []
    for i, action in enumerate(self.actions):
        if isinstance(action, self.SetParentField):
            if action.node_value is None:
                new_node = {"_type": action.node_type}
            else:
                new_node = action.node_value
            if action.parent_field_name is None:
                # Initial node in tree.
                assert root is None
                root = current = new_node
                stack.append(root)
                continue
            existing_list = current.get(action.parent_field_name)
            if existing_list is None:
                current[action.parent_field_name] = new_node
            else:
                assert isinstance(existing_list, list)
                current[action.parent_field_name].append(new_node)
            if action.node_value is None:
                stack.append(current)
                current = new_node
        elif isinstance(action, self.CreateParentFieldList):
            current[action.parent_field_name] = []
        elif isinstance(action, self.AppendTerminalToken):
            tokens = current.get(action.parent_field_name)
            if tokens is None:
                tokens = current[action.parent_field_name] = []
            tokens.append('"' + action.value + '"')
        elif isinstance(action, self.FinalizeTerminal):
            terminal = "".join(current.get(action.parent_field_name, []))
            constructor = self.SIMPLE_TERMINAL_TYPES.get(action.terminal_type)
            if constructor:
                try:
                    value = constructor(terminal)
                except ValueError:
                    value = self.SIMPLE_TERMINAL_TYPES_DEFAULT[action.terminal_type]
            elif action.terminal_type == "bytes":
                value = terminal.decode("latin1")
            elif action.terminal_type == "NoneType":
                value = None
            else:
                raise ValueError(f"Unknown terminal type: {action.terminal_type}")
            current[action.parent_field_name] = value
        elif isinstance(action, self.NodeFinished):
            current = stack.pop()
        else:
            raise ValueError(action)
    assert not stack
    return root, self.model.preproc.grammar.unparse(root, self.db, self.value_list)