File: binding.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (123 lines) | stat: -rw-r--r-- 3,854 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# binding.py
#
# Copyright 2023 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from dataclasses import dataclass

from .common import *
from .expression import Expression, LiteralExpr, LookupOp


class BindingFlag(AstNode):
    grammar = [
        AnyOf(
            UseExact("flag", "inverted"),
            UseExact("flag", "bidirectional"),
            UseExact("flag", "no-sync-create"),
            UseExact("flag", "sync-create"),
        )
    ]

    @property
    def flag(self) -> str:
        return self.tokens["flag"]

    @validate()
    def sync_create(self):
        if self.flag == "sync-create":
            raise UpgradeWarning(
                "'sync-create' is now the default. Use 'no-sync-create' if this is not wanted.",
                actions=[CodeAction("remove 'sync-create'", "")],
            )

    @validate()
    def unique(self):
        self.validate_unique_in_parent(
            f"Duplicate flag '{self.flag}'", lambda x: x.flag == self.flag
        )

    @validate()
    def flags_only_if_simple(self):
        if self.parent.simple_binding is None:
            raise CompileError(
                "Only bindings with a single lookup can have flags",
            )

    @docs()
    def ref_docs(self):
        return get_docs_section("Syntax Binding")


class Binding(AstNode):
    grammar = [
        AnyOf(Keyword("bind"), UseExact("bind", "bind-property")),
        Expression,
        ZeroOrMore(BindingFlag),
    ]

    @property
    def expression(self) -> Expression:
        return self.children[Expression][0]

    @property
    def flags(self) -> T.List[BindingFlag]:
        return self.children[BindingFlag]

    @property
    def simple_binding(self) -> T.Optional["SimpleBinding"]:
        if isinstance(self.expression.last, LookupOp):
            if isinstance(self.expression.last.lhs, LiteralExpr):
                from .values import IdentLiteral

                if isinstance(self.expression.last.lhs.literal.value, IdentLiteral):
                    flags = [x.flag for x in self.flags]
                    return SimpleBinding(
                        self.expression.last.lhs.literal.value.ident,
                        self.expression.last.property_name,
                        no_sync_create="no-sync-create" in flags,
                        bidirectional="bidirectional" in flags,
                        inverted="inverted" in flags,
                    )
        return None

    @validate("bind")
    def bind_property(self):
        if self.tokens["bind"] == "bind-property":
            raise UpgradeWarning(
                "'bind-property' is no longer needed. Use 'bind' instead. (blueprint 0.8.2)",
                actions=[CodeAction("use 'bind'", "bind")],
            )

    @docs("bind")
    def ref_docs(self):
        return get_docs_section("Syntax Binding")


@dataclass
class SimpleBinding:
    source: str
    property_name: str
    no_sync_create: bool = False
    bidirectional: bool = False
    inverted: bool = False


@decompiler("binding")
def decompile_binding(ctx: DecompileCtx, gir: gir.GirContext, name: str):
    ctx.end_block_with(";")
    ctx.print(f"{name}: bind ")