Пример #1
0
def build_module(builder: Builder, lineno: int, token: str,
                 nodes: typing.List[typing.Any]) -> bool:
    assert token == "module"
    for lineno, token, value in nodes:
        if token == "def ":
            builder.build_token(lineno, token, value)
    return True
Пример #2
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
Пример #3
0
def build_code(
    builder: Builder, lineno: int, token: str, lines: typing.List[str]
) -> bool:
    for line in lines:
        builder.add(lineno, line)
        lineno += 1
    return True
Пример #4
0
def build_require(builder: Builder, lineno: int, token: str,
                  variables: typing.List[str]) -> bool:
    assert token == "require"
    builder.add(
        lineno,
        "; ".join([name + " = ctx['" + name + "']" for name in variables]),
    )
    return True
Пример #5
0
def build_var(builder: Builder, lineno: int, token: str,
              value: typing.Any) -> bool:
    assert token == "var"
    var, var_filters = value
    if var_filters:
        for f in var_filters:
            var = known_var_filters.get(f, f) + "(" + var + ")"
    builder.add(lineno, "w(" + var + ")")
    return True
Пример #6
0
def build_from(
    builder: Builder,
    lineno: int,
    token: str,
    value: typing.Tuple[str, str, str],
) -> bool:
    assert token == "from "
    name, var, alias = value
    builder.add(lineno,
                alias + " = _i(" + name + ").local_defs['" + var + "']")
    return True
Пример #7
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
Пример #8
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
Пример #9
0
def build_extends(builder: Builder, lineno: int, token: str,
                  nodes: typing.List[typing.Any]) -> bool:
    assert token == "render"
    n = len(nodes)
    if not n:
        return False
    lineno, token, value = nodes[-1]
    if token != "extends":
        return False
    extends, nested = value
    if n > 1:
        nested = nodes[:-1] + nested
    for lineno, token, value in nested:
        if token in extends_tokens:
            builder.build_token(lineno, token, value)
    builder.add(
        builder.lineno + 1,
        "return _r(" + extends + ", ctx, local_defs, super_defs)",
    )
    return True
Пример #10
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
Пример #11
0
def build_render(builder: Builder, lineno: int, token: str,
                 nodes: typing.Iterable[Token]) -> bool:
    assert lineno <= 0
    assert token == "render"
    builder.add(lineno, WRITER_DECLARE)
    builder.build_block(nodes)
    lineno = builder.lineno
    builder.add(lineno + 1, WRITER_RETURN)
    return True
Пример #12
0
def build_render_single_markup(builder: Builder, lineno: int, token: str,
                               nodes: typing.List[typing.Any]) -> bool:
    assert lineno <= 0
    assert token == "render"
    if len(nodes) > 1:
        return False
    if len(nodes) == 0:
        builder.add(lineno, "return ''")
        return True
    ln, token, nodes = nodes[0]
    if token != "out" or len(nodes) > 1:
        return False
    ln, token, value = nodes[0]
    if token != "markup":
        return False
    if value:
        builder.add(ln, "return " + value)
    else:
        builder.add(ln, "return ''")
    return True
Пример #13
0
def build_end(builder: Builder, lineno: int, token: str,
              value: typing.Any) -> bool:
    if builder.lineno != lineno:
        builder.add(lineno - 1, "")
    return True
Пример #14
0
def build_comment(builder: Builder, lineno: int, token: str,
                  comment: str) -> bool:
    assert token == "#"
    builder.add(lineno, comment)
    return True
Пример #15
0
def build_markup(builder: Builder, lineno: int, token: str,
                 value: str) -> bool:
    assert token == "markup"
    if value:
        builder.add(lineno, "w(" + value + ")")
    return True
Пример #16
0
def build_include(builder: Builder, lineno: int, token: str,
                  value: str) -> bool:
    assert token == "include"
    builder.add(lineno, "w(_r(" + value + ", ctx, local_defs, super_defs))")
    return True
Пример #17
0
def build_out(builder: Builder, lineno: int, token: str,
              nodes: typing.Iterable[Token]) -> bool:
    assert token == "out"
    builder.build_block(nodes)
    return True
Пример #18
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
Пример #19
0
def build_import(builder: Builder, lineno: int, token: str,
                 value: typing.Tuple[str, str]) -> bool:
    assert token == "import "
    name, var = value
    builder.add(lineno, var + " = _i(" + name + ")")
    return True