# Path: hq/hquery/object_type.py
# def debug_dump_anything(obj):
#     if is_any_node(obj):
#         result = debug_dump_node(obj)
#     elif is_boolean(obj) or is_number(obj) or is_hash(obj) or is_array(obj):
#         result = repr(obj)
#     elif is_string(obj):
#         result = u'string("{0}")'.format(obj)
#     elif is_node_set(obj):
#         result = u'node-set({0})'.format(', '.join(truncate_string(debug_dump_node(node), 20) for node in obj))
#     elif is_sequence(obj):
#         result = u'sequence({0})'.format(', '.join(truncate_string(debug_dump_anything(item), 20) for item in obj))
#     else:
#         raise RuntimeError("debug_dump_anything doesn't know how to handle {0}".format(obj.__class__.__name__))
#     return debug_dump_long_string(result)
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# def sequence_concat(first, second):
#     first = make_sequence(first)
#     first.extend(make_sequence(second))
#     return first
# 
# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: hq/hquery/variables.py
# def push_variable(name, value):
#     global variable_stack
#     verbose_print(lambda: u'Pushing variable onto stack: let ${0} := {1}'.format(name, debug_dump_anything(value)))
#     variable_stack.append((name, value))
# 
# class variable_scope:
#     def __enter__(self):
#         self.mark = len(variable_stack)
# 
#     def __exit__(self, *args):
#         del variable_stack[self.mark:]
# 
# Path: hq/soup_util.py
# def debug_dump_long_string(s, length=50, one_line=True, suffix='...'):
#     return truncate_string(s, length, one_line, suffix)
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


from hq.hquery.object_type import debug_dump_anything
from hq.hquery.sequences import make_sequence, sequence_concat
from hq.hquery.syntax_error import HquerySyntaxError
from hq.hquery.variables import push_variable, variable_scope
from hq.soup_util import debug_dump_long_string
from hq.verbosity import verbose_print
        verbose_print(lambda: 'FLWOR evaluation completed; returning {0}'.format(debug_dump_anything(result)),
                      outdent_before=True)
        return result


    def set_iteration_expression(self, variable_name, expression_fn):
        if self.sequence_expression is not None:
            raise HquerySyntaxError('More than one "for" clause found in FLWOR "{0}"'.format(self.debug_dump()))
        self.sequence_variable = variable_name
        self.sequence_expression = expression_fn


    def set_return_expression(self, expression_fn):
        if self.return_expression is not None:
            raise HquerySyntaxError('More than one return clause found for FLWOR {0}'.format(self.debug_dump()))
        self.return_expression = expression_fn


    def _evaluate_iteration(self):
        with variable_scope():
            self._push_global_variables()

            sequence = make_sequence(self.sequence_expression())
            verbose_print('Iterating over sequence containing {0} items'.format(len(sequence)))
            result = []

            for item in sequence:
                verbose_print(lambda: u'Visiting item {0}'.format(debug_dump_anything(item)), indent_after=True)

                with variable_scope():

# Path: hq/hquery/object_type.py
# def debug_dump_anything(obj):
#     if is_any_node(obj):
#         result = debug_dump_node(obj)
#     elif is_boolean(obj) or is_number(obj) or is_hash(obj) or is_array(obj):
#         result = repr(obj)
#     elif is_string(obj):
#         result = u'string("{0}")'.format(obj)
#     elif is_node_set(obj):
#         result = u'node-set({0})'.format(', '.join(truncate_string(debug_dump_node(node), 20) for node in obj))
#     elif is_sequence(obj):
#         result = u'sequence({0})'.format(', '.join(truncate_string(debug_dump_anything(item), 20) for item in obj))
#     else:
#         raise RuntimeError("debug_dump_anything doesn't know how to handle {0}".format(obj.__class__.__name__))
#     return debug_dump_long_string(result)
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# def sequence_concat(first, second):
#     first = make_sequence(first)
#     first.extend(make_sequence(second))
#     return first
# 
# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: hq/hquery/variables.py
# def push_variable(name, value):
#     global variable_stack
#     verbose_print(lambda: u'Pushing variable onto stack: let ${0} := {1}'.format(name, debug_dump_anything(value)))
#     variable_stack.append((name, value))
# 
# class variable_scope:
#     def __enter__(self):
#         self.mark = len(variable_stack)
# 
#     def __exit__(self, *args):
#         del variable_stack[self.mark:]
# 
# Path: hq/soup_util.py
# def debug_dump_long_string(s, length=50, one_line=True, suffix='...'):
#     return truncate_string(s, length, one_line, suffix)
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


# Path: hq/hquery/flwor.py
from hq.hquery.object_type import debug_dump_anything
from hq.hquery.sequences import make_sequence, sequence_concat
from hq.hquery.syntax_error import HquerySyntaxError
from hq.hquery.variables import push_variable, variable_scope
from hq.soup_util import debug_dump_long_string
from hq.verbosity import verbose_print

        verbose_print(lambda: 'FLWOR evaluation completed; returning {0}'.format(debug_dump_anything(result)),
                      outdent_before=True)
        return result


    def set_iteration_expression(self, variable_name, expression_fn):
        if self.sequence_expression is not None:
            raise HquerySyntaxError('More than one "for" clause found in FLWOR "{0}"'.format(self.debug_dump()))
        self.sequence_variable = variable_name
        self.sequence_expression = expression_fn


    def set_return_expression(self, expression_fn):
        if self.return_expression is not None:
            raise HquerySyntaxError('More than one return clause found for FLWOR {0}'.format(self.debug_dump()))
        self.return_expression = expression_fn


    def _evaluate_iteration(self):
        with variable_scope():
            self._push_global_variables()

            sequence = make_sequence(self.sequence_expression())
            verbose_print('Iterating over sequence containing {0} items'.format(len(sequence)))
            result = []

            for item in sequence:
                verbose_print(lambda: u'Visiting item {0}'.format(debug_dump_anything(item)), indent_after=True)

                with variable_scope():

                    push_variable(self.sequence_variable, make_sequence(item))