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 __init__(self, first_axis, first_node_test, first_predicates, absolute=False, root_expression=None): self.absolute = absolute self.root_expression = root_expression self.steps = [] self.append_step(first_axis, first_node_test, first_predicates) if self.absolute and self.root_expression is not None: raise HquerySyntaxError('internal error forming location path; it looks both rooted and absolute')
def _evaluate_unary_operand(self, operand_generator, constructor=lambda v: v, type_name='xpath object'): try: self._gab('evaluating operand.', indent_after=True) operand_value = constructor(operand_generator()) self._gab('operand evaluation complete', outdent_before=True) return operand_value except TypeError: raise HquerySyntaxError('evaluated against a non-{0} operand'.format(type_name))
def __init__(self, op): if op == '>': self.base_op = gt elif op == '>=': self.base_op = ge elif op == '<': self.base_op = lt elif op == '<=': self.base_op = le else: raise HquerySyntaxError('unexpected relational operator "{0}"'.format(op))
def nud(self): if self.value != '-': raise HquerySyntaxError('unexpected {0} at beginning of an expression') right = self.parse_interface.expression(LBP.prefix_op) def evaluate(): right_value = self._evaluate_unary_operand(right, constructor=number, type_name='number') result = -right_value self._gab('returning {0}'.format(result)) return result return evaluate
def set_filters(self, source): while len(source) > 0: match = None for regex, constructor in _filter_map.items(): match = re.match(regex, source) if match: filter_fn = constructor( *_skip_over_embedded_groups_from_list_matches( match.groups())) self.filters.append(filter_fn) source = source[match.span()[1]:] break if match is None: raise HquerySyntaxError( 'Malformed filter "{0}" in computed JSON hash constructor filter clause' .format(source))
def _evaluate_binary_operands(self, left_generator, right_generator, constructor=lambda v: v, type_name='xpath object'): try: self._gab('operator evaluation...', indent_after=True) self._gab('evaluating left-hand side.', indent_after=True) left_value = constructor(left_generator()) self._gab('evaluating right-hand side.', outdent_before=True, indent_after=True) right_value = constructor(right_generator()) self._gab('operand evaluation complete', outdent_before=True) self._gab('evaluating expression {0} {1} {2}'.format(left_value, self, right_value), outdent_before=True) return left_value, right_value except TypeError: raise HquerySyntaxError('evaluated against a non-{0} operand'.format(type_name))
def _regex_replace_filter_link(arguments): if arguments is None or len(arguments) < 2: msg = 'interpolated string regex replace filter expects three arguments; got {0}' raise HquerySyntaxError(msg.format(arguments)) if len(arguments) == 3: flags = _xpath_flags_to_re_flags(arguments[2]) else: flags = 0 def construct(eval_fn): def evaluate(): value = eval_fn() if is_sequence(value): return [re.sub(arguments[0], arguments[1], string_value(item), flags=flags) for item in value] else: return re.sub(arguments[0], arguments[1], string_value(value), flags=flags) return evaluate return construct
def append_step(self, axis, node_test, predicates): if axis == Axis.css_class and not node_test.is_name_test: raise HquerySyntaxError('CSS class axis must be followed by a name test, not a node test') self.steps.append(LocationPathStep(axis, node_test, predicates))
def set_content(self, expression_fn): if self.contents is not None: raise HquerySyntaxError( 'Computed attribute constructor already has contents') self.contents = expression_fn
def set_contents(self, expression_fn): if self.contents is not None: raise HquerySyntaxError( 'computed JSON array constructor already has contents') self.contents = 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