Debian Codegraph @

Tools/clinic/libclinic/app.py

raw
1 from __future__ import annotations
2 import os
3
4 from collections.abc import Callable, Sequence
5 from typing import Any, TYPE_CHECKING
6
7
8 import libclinic
9 from libclinic import fail, warn
10 from libclinic.function import Class
11 from libclinic.block_parser import Block, BlockParser
12 from libclinic.codegen import BlockPrinter, Destination, CodeGen
13 from libclinic.parser import Parser, PythonParser
14 from libclinic.dsl_parser import DSLParser
15 if TYPE_CHECKING:
16 from libclinic.clanguage import CLanguage
17 from libclinic.function import (
18 Module, Function, ClassDict, ModuleDict)
19 from libclinic.codegen import DestinationDict
20
21
22 # maps strings to callables.
23 # the callable should return an object
24 # that implements the clinic parser
25 # interface (__init__ and parse).
26 #
27 # example parsers:
28 # "clinic", handles the Clinic DSL
29 # "python", handles running Python code
30 #
31 parsers: dict[str, Callable[[Clinic], Parser]] = {
32 'clinic': DSLParser,
33 'python': PythonParser,
34 }
35
36
37 class Clinic:
38
39 presets_text = """
40 preset block
41 everything block
42 methoddef_ifndef buffer 1
43 docstring_prototype suppress
44 parser_prototype suppress
45 cpp_if suppress
46 cpp_endif suppress
47
48 preset original
49 everything block
50 methoddef_ifndef buffer 1
51 docstring_prototype suppress
52 parser_prototype suppress
53 cpp_if suppress
54 cpp_endif suppress
55
56 preset file
57 everything file
58 methoddef_ifndef file 1
59 docstring_prototype suppress
60 parser_prototype suppress
61 impl_definition block
62
63 preset buffer
64 everything buffer
65 methoddef_ifndef buffer 1
66 impl_definition block
67 docstring_prototype suppress
68 impl_prototype suppress
69 parser_prototype suppress
70
71 preset partial-buffer
72 everything buffer
73 methoddef_ifndef buffer 1
74 docstring_prototype block
75 impl_prototype suppress
76 methoddef_define block
77 parser_prototype block
78 impl_definition block
79
80 """
81
82 def __init__(
83 self,
84 language: CLanguage,
85 printer: BlockPrinter | None = None,
86 *,
87 filename: str,
88 limited_capi: bool,
89 verify: bool = True,
90 writer: libclinic.FileWriter | None = None,
91 ) -> None:
92 # maps strings to Parser objects.
93 # (instantiated from the "parsers" global.)
94 self.parsers: dict[str, Parser] = {}
95 self.language: CLanguage = language
96 if printer:
97 fail("Custom printers are broken right now")
98 self.printer = printer or BlockPrinter(language)
99 self.writer = writer or libclinic.FileWriter()
100 self.verify = verify
101 self.limited_capi = limited_capi
102 self.filename = filename
103 self.modules: ModuleDict = {}
104 self.classes: ClassDict = {}
105 self.functions: list[Function] = []
106 self.codegen = CodeGen(self.limited_capi)
107
108 self.line_prefix = self.line_suffix = ''
109
110 self.destinations: DestinationDict = {}
111 self.add_destination("block", "buffer")
112 self.add_destination("suppress", "suppress")
113 self.add_destination("buffer", "buffer")
114 if filename:
115 self.add_destination("file", "file", "{dirname}/clinic/{basename}.h")
116
117 d = self.get_destination_buffer
118 self.destination_buffers = {
119 'cpp_if': d('file'),
120 'docstring_prototype': d('suppress'),
121 'docstring_definition': d('file'),
122 'methoddef_define': d('file'),
123 'impl_prototype': d('file'),
124 'parser_prototype': d('suppress'),
125 'parser_definition': d('file'),
126 'cpp_endif': d('file'),
127 'methoddef_ifndef': d('file', 1),
128 'impl_definition': d('block'),
129 }
130
131 DestBufferType = dict[str, list[str]]
132 DestBufferList = list[DestBufferType]
133
134 self.destination_buffers_stack: DestBufferList = []
135
136 self.presets: dict[str, dict[Any, Any]] = {}
137 preset = None
138 for line in self.presets_text.strip().split('\n'):
139 line = line.strip()
140 if not line:
141 continue
142 name, value, *options = line.split()
143 if name == 'preset':
144 self.presets[value] = preset = {}
145 continue
146
147 if len(options):
148 index = int(options[0])
149 else:
150 index = 0
151 buffer = self.get_destination_buffer(value, index)
152
153 if name == 'everything':
154 for name in self.destination_buffers:
155 preset[name] = buffer
156 continue
157
158 assert name in self.destination_buffers
159 preset[name] = buffer
160
161 def add_destination(
162 self,
163 name: str,
164 type: str,
165 *args: str
166 ) -> None:
167 if name in self.destinations:
168 fail(f"Destination already exists: {name!r}")
169 self.destinations[name] = Destination(name, type, self, args)
170
171 def get_destination(self, name: str) -> Destination:
172 d = self.destinations.get(name)
173 if not d:
174 fail(f"Destination does not exist: {name!r}")
175 return d
176
177 def get_destination_buffer(
178 self,
179 name: str,
180 item: int = 0
181 ) -> list[str]:
182 d = self.get_destination(name)
183 return d.buffers[item]
184
185 def parse(self, input: str) -> str:
186 printer = self.printer
187 self.block_parser = BlockParser(input, self.language, verify=self.verify)
188 for block in self.block_parser:
189 dsl_name = block.dsl_name
190 if dsl_name:
191 if dsl_name not in self.parsers:
192 assert dsl_name in parsers, f"No parser to handle {dsl_name!r} block."
193 self.parsers[dsl_name] = parsers[dsl_name](self)
194 parser = self.parsers[dsl_name]
195 parser.parse(block)
196 printer.print_block(block)
197
198 # these are destinations not buffers
199 for name, destination in self.destinations.items():
200 if destination.type == 'suppress':
201 continue
202 output = destination.dump()
203
204 if output:
205 block = Block("", dsl_name="clinic", output=output)
206
207 if destination.type == 'buffer':
208 block.input = "dump " + name + "\n"
209 warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.")
210 printer.write("\n")
211 printer.print_block(block)
212 continue
213
214 if destination.type == 'file':
215 try:
216 dirname = os.path.dirname(destination.filename)
217 try:
218 self.writer.makedirs(dirname)
219 except FileExistsError:
220 if not os.path.isdir(dirname):
221 fail(f"Can't write to destination "
222 f"{destination.filename!r}; "
223 f"can't make directory {dirname!r}!")
224 if self.verify:
225 with open(destination.filename) as f:
226 parser_2 = BlockParser(f.read(), language=self.language)
227 blocks = list(parser_2)
228 if (len(blocks) != 1) or (blocks[0].input != 'preserve\n'):
229 fail(f"Modified destination file "
230 f"{destination.filename!r}; not overwriting!")
231 except FileNotFoundError:
232 pass
233
234 block.input = 'preserve\n'
235 includes = self.codegen.get_includes()
236
237 printer_2 = BlockPrinter(self.language)
238 printer_2.print_block(block, header_includes=includes)
239 self.writer.write(destination.filename,
240 printer_2.f.getvalue())
241 continue
242
243 return printer.f.getvalue()
244
245 def _module_and_class(
246 self, fields: Sequence[str]
247 ) -> tuple[Module | Clinic, Class | None]:
248 """
249 fields should be an iterable of field names.
250 returns a tuple of (module, class).
251 the module object could actually be self (a clinic object).
252 this function is only ever used to find the parent of where
253 a new class/module should go.
254 """
255 parent: Clinic | Module | Class = self
256 module: Clinic | Module = self
257 cls: Class | None = None
258
259 for idx, field in enumerate(fields):
260 if not isinstance(parent, Class):
261 if field in parent.modules:
262 parent = module = parent.modules[field]
263 continue
264 if field in parent.classes:
265 parent = cls = parent.classes[field]
266 else:
267 fullname = ".".join(fields[idx:])
268 fail(f"Parent class or module {fullname!r} does not exist.")
269
270 return module, cls
271
272 def __repr__(self) -> str:
273 return "<clinic.Clinic object>"