Debian Codegraph @

blueprintcompiler/language/adw_breakpoint.py

raw
1 # adw_breakpoint.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 .common import *
21 from .contexts import ScopeCtx, ValueTypeCtx
22 from .gobject_object import Object, validate_parent_type
23 from .values import Value
24
25
26 class AdwBreakpointCondition(AstNode):
27 grammar = [
28 UseExact("kw", "condition"),
29 "(",
30 UseQuoted("condition"),
31 Match(")").expected(),
32 ]
33
34 @property
35 def condition(self) -> str:
36 return self.tokens["condition"]
37
38 @property
39 def document_symbol(self) -> DocumentSymbol:
40 return DocumentSymbol(
41 "condition",
42 SymbolKind.Property,
43 self.range,
44 self.group.tokens["kw"].range,
45 self.condition,
46 )
47
48 @docs("kw")
49 def keyword_docs(self):
50 klass = self.root.gir.get_type("Breakpoint", "Adw")
51 if klass is None:
52 return None
53 prop = klass.properties.get("condition")
54 assert isinstance(prop, gir.Property)
55 return prop.doc
56
57 @validate()
58 def unique(self):
59 self.validate_unique_in_parent("Duplicate condition statement")
60
61
62 class AdwBreakpointSetter(AstNode):
63 grammar = Statement(
64 UseIdent("object"),
65 Match(".").expected(),
66 UseIdent("property"),
67 Match(":").expected(),
68 Value,
69 )
70
71 @property
72 def object_id(self) -> str:
73 return self.tokens["object"]
74
75 @property
76 def object(self) -> T.Optional[Object]:
77 return self.context[ScopeCtx].objects.get(self.object_id)
78
79 @property
80 def property_name(self) -> T.Optional[str]:
81 return self.tokens["property"]
82
83 @property
84 def value(self) -> T.Optional[Value]:
85 return self.children[Value][0] if len(self.children[Value]) > 0 else None
86
87 @property
88 def gir_class(self) -> T.Optional[GirType]:
89 if self.object is not None:
90 return self.object.gir_class
91 else:
92 return None
93
94 @property
95 def gir_property(self) -> T.Optional[gir.Property]:
96 if (
97 self.gir_class is not None
98 and not isinstance(self.gir_class, ExternType)
99 and self.property_name is not None
100 ):
101 assert isinstance(self.gir_class, gir.Class) or isinstance(
102 self.gir_class, gir.TemplateType
103 )
104 return self.gir_class.properties.get(self.property_name)
105 else:
106 return None
107
108 @property
109 def document_symbol(self) -> T.Optional[DocumentSymbol]:
110 if self.value is None:
111 return None
112
113 return DocumentSymbol(
114 f"{self.object_id}.{self.property_name}",
115 SymbolKind.Property,
116 self.range,
117 self.group.tokens["object"].range,
118 self.value.range.text,
119 )
120
121 def get_reference(self, idx: int) -> T.Optional[LocationLink]:
122 if idx in self.group.tokens["object"].range:
123 if self.object is not None:
124 return LocationLink(
125 self.group.tokens["object"].range,
126 self.object.range,
127 self.object.ranges["id"],
128 )
129
130 return None
131
132 @context(ValueTypeCtx)
133 def value_type(self) -> ValueTypeCtx:
134 if self.gir_property is not None:
135 type = self.gir_property.type
136 else:
137 type = None
138
139 return ValueTypeCtx(type, allow_null=True)
140
141 @docs("object")
142 def object_docs(self):
143 if self.object is not None:
144 return f"```\n{self.object.signature}\n```"
145 else:
146 return None
147
148 @docs("property")
149 def property_docs(self):
150 if self.gir_property is not None:
151 return self.gir_property.doc
152 else:
153 return None
154
155 @validate("object")
156 def object_exists(self):
157 if self.object is None:
158 raise CompileError(
159 f"Could not find object with ID {self.object_id}",
160 did_you_mean=(self.object_id, self.context[ScopeCtx].objects.keys()),
161 )
162
163 @validate("property")
164 def property_exists(self):
165 if self.gir_class is None or self.gir_class.incomplete:
166 # Objects that we have no gir data on should not be validated
167 # This happens for classes defined by the app itself
168 return
169
170 if self.gir_property is None and self.property_name is not None:
171 raise CompileError(
172 f"Class {self.gir_class.full_name} does not have a property called {self.property_name}",
173 did_you_mean=(self.property_name, self.gir_class.properties.keys()),
174 )
175
176 @validate()
177 def unique(self):
178 self.validate_unique_in_parent(
179 f"Duplicate setter for {self.object_id}.{self.property_name}",
180 lambda x: x.object_id == self.object_id
181 and x.property_name == self.property_name,
182 )
183
184
185 class AdwBreakpointSetters(AstNode):
186 grammar = [
187 Keyword("setters"),
188 Match("{").expected(),
189 Until(AdwBreakpointSetter, "}"),
190 ]
191
192 @property
193 def setters(self) -> T.List[AdwBreakpointSetter]:
194 return self.children[AdwBreakpointSetter]
195
196 @property
197 def document_symbol(self) -> DocumentSymbol:
198 return DocumentSymbol(
199 "setters",
200 SymbolKind.Struct,
201 self.range,
202 self.group.tokens["setters"].range,
203 )
204
205 @validate()
206 def container_is_breakpoint(self):
207 validate_parent_type(self, "Adw", "Breakpoint", "breakpoint setters")
208
209 @validate()
210 def unique(self):
211 self.validate_unique_in_parent("Duplicate setters block")
212
213 @docs("setters")
214 def ref_docs(self):
215 return get_docs_section("Syntax ExtAdwBreakpoint")
216
217
218 @decompiler("condition", cdata=True)
219 def decompile_condition(ctx: DecompileCtx, gir, cdata):
220 ctx.print(f"condition({escape_quote(cdata)})")
221
222
223 @decompiler("setter", element=True)
224 def decompile_setter(ctx: DecompileCtx, gir, element):
225 assert ctx.parent_node is not None
226 # only run for the first setter
227 for child in ctx.parent_node.children:
228 if child.tag == "setter":
229 if child != element:
230 # already decompiled
231 return
232 else:
233 break
234
235 ctx.print("setters {")
236 for child in ctx.parent_node.children:
237 if child.tag == "setter":
238 object_id = child["object"]
239 property_name = child["property"]
240 obj = ctx.find_object(object_id)
241 if obj is not None:
242 gir_class = ctx.type_by_cname(obj["class"])
243 else:
244 gir_class = None
245
246 if object_id == ctx.template_class:
247 object_id = "template"
248
249 comments, string = ctx.decompile_value(
250 child.cdata,
251 gir_class,
252 (child["translatable"], child["context"], child["comments"]),
253 )
254 ctx.print(f"{comments} {object_id}.{property_name}: {string};")