示例#1
0
def build_def_single_markup(builder: Builder, lineno: int, token: str,
                            value: typing.Any) -> bool:
    assert token == "def "
    stmt, nodes = value
    if len(nodes) > 2:
        return False
    ln, token, nodes = nodes[0]
    if token != "out" or len(nodes) > 1:
        return False
    ln, token, value = nodes[0]
    if token != "markup":
        return False
    def_name = stmt[4:stmt.index("(", 5)]
    builder.add(lineno, stmt)
    builder.start_block()
    builder.add(ln, "return " + value)
    builder.end_block()
    builder.add(
        ln + 1,
        def_name.join((
            "super_defs['",
            "'] = ",
            "; ",
            " = local_defs.setdefault('",
            "', ",
            ")",
        )),
    )
    return True
示例#2
0
def build_def_syntax_check(builder: Builder, lineno: int, token: str,
                           value: typing.Any) -> bool:
    assert token == "def "
    stmt, nodes = value
    lineno, token, value = nodes[0]
    if token in compound_tokens:
        builder.add(lineno, stmt)
        builder.start_block()
        token = token.rstrip()
        error = """\
The compound statement '%s' is not allowed here. \
Add a line before it with @#ignore or leave it empty.

%s
    @#ignore
    @%s ...""" % (
            token,
            stmt,
            token,
        )
        builder.add(lineno, "raise SyntaxError(%s)" % repr(error))
        builder.end_block()
        return True
    elif len(nodes) > 1:
        # token before @end
        lineno, token, value = nodes[-2]
        if token == "#":
            del nodes[-2]
    return False
示例#3
0
def build_compound(builder: Builder, lineno: int, token: str,
                   value: typing.Any) -> bool:
    assert token in compound_tokens
    stmt, nodes = value
    builder.add(lineno, stmt)
    builder.start_block()
    builder.build_block(nodes)
    builder.end_block()
    return True
示例#4
0
def build_def_empty(builder: Builder, lineno: int, token: str,
                    value: typing.Any) -> bool:
    assert token == "def "
    stmt, nodes = value
    if len(nodes) > 1:
        return False
    def_name = stmt[4:stmt.index("(", 5)]
    builder.add(lineno, stmt)
    builder.start_block()
    builder.add(lineno, "return ''")
    builder.end_block()
    builder.add(
        lineno + 1,
        def_name.join((
            "super_defs['",
            "'] = ",
            "; ",
            " = local_defs.setdefault('",
            "', ",
            ")",
        )),
    )
    return True
示例#5
0
def build_def(builder: Builder, lineno: int, token: str,
              value: typing.Any) -> bool:
    assert token == "def "
    stmt, nodes = value
    def_name = stmt[4:stmt.index("(", 5)]
    builder.add(lineno, stmt)
    builder.start_block()
    builder.add(lineno + 1, WRITER_DECLARE)
    builder.build_block(nodes)
    lineno = builder.lineno
    builder.add(lineno, WRITER_RETURN)
    builder.end_block()
    builder.add(
        lineno + 1,
        def_name.join((
            "super_defs['",
            "'] = ",
            "; ",
            " = local_defs.setdefault('",
            "', ",
            ")",
        )),
    )
    return True