def _parse_op(self, op_name): self._get_token() # '(' op = self._transformations.get(op_name, None) if op is None: raise errors.IndexDefinitionParseError("Unknown operation: %s" % op_name) args = [] while True: args.append(self._parse_term()) sep = self._get_token() if sep == ')': break if sep != ',': raise errors.IndexDefinitionParseError( "Unexpected token '%s' in parentheses." % (sep, )) parsed = [] for i, arg in enumerate(args): arg_type = op.args[i % len(op.args)] if arg_type == 'expression': inner = self._to_getter(arg) else: try: inner = arg_type(arg) except ValueError as e: raise errors.IndexDefinitionParseError( "Invalid value %r for argument type %r " "(%r)." % (arg, arg_type, e)) parsed.append(inner) return op(*parsed)
def _parse_term(self): term = self._get_token() if term is None: raise errors.IndexDefinitionParseError( "Unexpected end of index definition.") if term in (',', ')', '('): raise errors.IndexDefinitionParseError( "Unexpected token '%s' at start of expression." % (term, )) next_token = self._peek_token() if next_token == '(': return self._parse_op(term) return term
def parse(self, expression): self._set_expression(expression) term = self._to_getter(self._parse_term()) if self._peek_token(): raise errors.IndexDefinitionParseError( "Unexpected token '%s' after end of expression." % (self._peek_token(), )) return term
def check_fieldname(fieldname): if fieldname.endswith('.'): raise errors.IndexDefinitionParseError( "Fieldname cannot end in '.':%s^" % (fieldname, ))