Example #1
0
    def jsemit_ForNode(self, node, indent):
        tok = PythonTokenizer(node.stmt)
        tok.consume_till('in')
        a = node.stmt[:tok.index].strip()  # for i in
        a = a[len("for"):-len("in")].strip()  # strip `for` and `in`

        b = node.stmt[tok.index:-1].strip()  # rest of for stmt excluding :
        b = web.re_compile(r"loop.setup\((.*)\)").match(b).group(1)

        text = ""
        text += indent + f"foreach({py2js(b)}, loop, function(loop, {a}) {{\n"
        text += self.jsemit(node.suite, indent + INDENT)
        text += indent + "});\n"
        return text
Example #2
0
def tokenize(code):
    """Tokenize python code.::

    >>> list(tokenize("x + y"))
    ['x', ' ', '+', ' ', 'y']
    """
    end = 0
    tok = PythonTokenizer(code)
    try:
        while True:
            x = next(tok)
            begin = x.begin[1]
            if begin > end:
                yield ' ' * (begin - end)
            if x.value:
                yield x.value
            end = x.end[1]
    except StopIteration:
        pass