Ejemplo n.º 1
0
        def macro(self):
            if self.element.py_match is not None:
                if self.match_symbol not in itertools.chain(
                        *self.element.stream.scope):
                    raise NameError(self.match_symbol)
                symbols = self.element.stream.symbols
                args = symbols.out, symbols.write, "select"
                if self.element.py_once.lower() in config.TRUEVALS:
                    once = 1
                else:
                    once = repr(None)
                decorator = '%s("""%s""", %s)' % (self.match_symbol,
                                                  self.element.py_match, once)
                return types.method("match", args, decorators=(decorator, ))

            return self.element.py_def
Ejemplo n.º 2
0
        def macro(self):
            if self.element.py_match is not None:
                if self.match_symbol not in itertools.chain(
                    *self.element.stream.scope):
                    raise NameError(self.match_symbol)
                symbols = self.element.stream.symbols
                args = symbols.out, symbols.write, "select"
                if self.element.py_once.lower() in config.TRUEVALS:
                    once = 1
                else:
                    once = repr(None)
                decorator = '%s("""%s""", %s)' % (
                    self.match_symbol, self.element.py_match, once)
                return types.method(
                    "match", args, decorators=(decorator,))

            return self.element.py_def
Ejemplo n.º 3
0
    def method(self, string):
        """Parse a method definition.

        >>> method = ExpressionTranslator().method

        >>> method('name')
        name()

        >>> method('name(a, b, c)')
        name(a, b, c)

        """

        m = self.re_method.match(string)
        if m is None:
            raise ValueError("Not a valid method definition (%s)." % string)

        name = m.group('name')
        args = [arg.strip() for arg in (m.group('args') or "").split(',') if arg]

        return types.method(name, args)