| 1 |
# binding.py |
| 2 |
# |
| 3 |
# Copyright 2023 James Westman <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as |
| 7 |
# published by the Free Software Foundation; either version 3 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This file is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 19 |
|
| 20 |
from dataclasses import dataclass |
| 21 |
|
| 22 |
from .common import * |
| 23 |
from .contexts import ValueTypeCtx |
| 24 |
from .expression import CastExpr, Expression, LiteralExpr, LookupOp |
| 25 |
|
| 26 |
|
| 27 |
class BindingFlag(AstNode): |
| 28 |
grammar = [ |
| 29 |
AnyOf( |
| 30 |
UseExact("flag", "inverted"), |
| 31 |
UseExact("flag", "bidirectional"), |
| 32 |
UseExact("flag", "no-sync-create"), |
| 33 |
UseExact("flag", "sync-create"), |
| 34 |
) |
| 35 |
] |
| 36 |
|
| 37 |
@property |
| 38 |
def flag(self) -> str: |
| 39 |
return self.tokens["flag"] |
| 40 |
|
| 41 |
@validate() |
| 42 |
def sync_create(self): |
| 43 |
if self.flag == "sync-create": |
| 44 |
raise UpgradeWarning( |
| 45 |
"'sync-create' is now the default. Use 'no-sync-create' if this is not wanted.", |
| 46 |
actions=[CodeAction("remove 'sync-create'", "")], |
| 47 |
) |
| 48 |
|
| 49 |
@validate() |
| 50 |
def unique(self): |
| 51 |
self.validate_unique_in_parent( |
| 52 |
f"Duplicate flag '{self.flag}'", lambda x: x.flag == self.flag |
| 53 |
) |
| 54 |
|
| 55 |
@validate() |
| 56 |
def flags_only_if_simple(self): |
| 57 |
if self.parent.simple_binding is None: |
| 58 |
raise CompileError( |
| 59 |
"Only bindings with a single lookup can have flags", |
| 60 |
) |
| 61 |
|
| 62 |
@docs() |
| 63 |
def ref_docs(self): |
| 64 |
return get_docs_section("Syntax Binding") |
| 65 |
|
| 66 |
|
| 67 |
class Binding(AstNode): |
| 68 |
grammar = [ |
| 69 |
AnyOf(Keyword("bind"), UseExact("bind", "bind-property")), |
| 70 |
Expression, |
| 71 |
ZeroOrMore(BindingFlag), |
| 72 |
] |
| 73 |
|
| 74 |
@context(ValueTypeCtx) |
| 75 |
def value_type(self): |
| 76 |
prev_ctx = self.parent.context[ValueTypeCtx] |
| 77 |
return ValueTypeCtx( |
| 78 |
prev_ctx.value_type, |
| 79 |
must_infer_type=prev_ctx.must_infer_type, |
| 80 |
allow_null=True, |
| 81 |
) |
| 82 |
|
| 83 |
@property |
| 84 |
def expression(self) -> Expression: |
| 85 |
return self.children[Expression][0] |
| 86 |
|
| 87 |
@property |
| 88 |
def flags(self) -> T.List[BindingFlag]: |
| 89 |
return self.children[BindingFlag] |
| 90 |
|
| 91 |
@property |
| 92 |
def simple_binding(self) -> T.Optional["SimpleBinding"]: |
| 93 |
expr = self.expression.last |
| 94 |
|
| 95 |
if isinstance(expr, CastExpr): |
| 96 |
expr = expr.lhs |
| 97 |
|
| 98 |
if isinstance(expr, LookupOp): |
| 99 |
if isinstance(expr.lhs, LiteralExpr): |
| 100 |
from .values import IdentLiteral |
| 101 |
|
| 102 |
if isinstance(expr.lhs.literal.value, IdentLiteral): |
| 103 |
flags = [x.flag for x in self.flags] |
| 104 |
return SimpleBinding( |
| 105 |
expr.lhs.literal.value.ident, |
| 106 |
expr.property_name, |
| 107 |
no_sync_create="no-sync-create" in flags, |
| 108 |
bidirectional="bidirectional" in flags, |
| 109 |
inverted="inverted" in flags, |
| 110 |
) |
| 111 |
return None |
| 112 |
|
| 113 |
@validate("bind") |
| 114 |
def bind_property(self): |
| 115 |
if self.tokens["bind"] == "bind-property": |
| 116 |
raise UpgradeWarning( |
| 117 |
"'bind-property' is no longer needed. Use 'bind' instead. (blueprint 0.8.2)", |
| 118 |
actions=[CodeAction("use 'bind'", "bind")], |
| 119 |
) |
| 120 |
|
| 121 |
@docs("bind") |
| 122 |
def ref_docs(self): |
| 123 |
return get_docs_section("Syntax Binding") |
| 124 |
|
| 125 |
|
| 126 |
@dataclass |
| 127 |
class SimpleBinding: |
| 128 |
source: str |
| 129 |
property_name: str |
| 130 |
no_sync_create: bool = False |
| 131 |
bidirectional: bool = False |
| 132 |
inverted: bool = False |
| 133 |
|
| 134 |
|
| 135 |
@decompiler("binding") |
| 136 |
def decompile_binding(ctx: DecompileCtx, gir: gir.GirContext, name: str): |
| 137 |
ctx.end_block_with(";") |
| 138 |
ctx.print(f"{name}: bind ") |