Debian Codegraph @

blueprintcompiler/language/imports.py

raw
1 # imports.py
2 #
3 # Copyright 2022 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
21 from .. import gir
22 from .common import *
23
24
25 class GtkDirective(AstNode):
26 grammar = Statement(
27 Match("using").err(
28 'File must start with a "using Gtk" directive (e.g. `using Gtk 4.0;`)'
29 ),
30 Match("Gtk").err(
31 'File must start with a "using Gtk" directive (e.g. `using Gtk 4.0;`)'
32 ),
33 UseNumberText("version").expected("a version number for GTK"),
34 )
35
36 @validate("version")
37 def gtk_version(self):
38 version = self.tokens["version"]
39 if version not in ["4.0"]:
40 err = CompileError("Only GTK 4 is supported")
41 if version and version.startswith("4"):
42 err.hint(
43 "Expected the GIR version, not an exact version number. Use 'using Gtk 4.0;'."
44 )
45 else:
46 err.hint("Expected 'using Gtk 4.0;'")
47 raise err
48
49 try:
50 gir.get_namespace("Gtk", version)
51 except CompileError as e:
52 raise CompileError(
53 "Could not find GTK 4 introspection files. Is gobject-introspection installed?",
54 fatal=True,
55 # preserve the hints from the original error, because it contains
56 # useful debugging information
57 hints=e.hints,
58 )
59
60 @property
61 def gir_namespace(self):
62 # For better error handling, just assume it's 4.0
63 return gir.get_namespace("Gtk", "4.0")
64
65 @docs()
66 def ref_docs(self):
67 return get_docs_section("Syntax GtkDecl")
68
69
70 class Import(AstNode):
71 grammar = Statement(
72 "using",
73 UseIdent("namespace").expected("a GIR namespace"),
74 UseNumberText("version").expected("a version number"),
75 )
76
77 @property
78 def namespace(self):
79 return self.tokens["namespace"]
80
81 @property
82 def version(self):
83 return self.tokens["version"]
84
85 @validate("namespace", "version")
86 def namespace_exists(self):
87 if self.namespace is None or self.version is None:
88 return
89
90 gir.get_namespace(self.namespace, self.version)
91
92 @validate()
93 def unused(self):
94 if self.namespace not in self.root.used_imports:
95 raise UnusedWarning(
96 f"Unused import: {self.namespace}",
97 self.range,
98 actions=[
99 CodeAction("Remove import", "", self.range.with_trailing_newline)
100 ],
101 )
102
103 @property
104 def gir_namespace(self):
105 if self.namespace is None or self.version is None:
106 return None
107
108 try:
109 return gir.get_namespace(self.namespace, self.version)
110 except CompileError:
111 return None
112
113 @docs()
114 def ref_docs(self):
115 return get_docs_section("Syntax Using")