| 181 | return d.buffers[item] |
| 182 | |
| 183 | def parse(self, input: str) -> str: |
| 184 | printer = self.printer |
| 185 | self.block_parser = BlockParser(input, self.language, verify=self.verify) |
| 186 | for block in self.block_parser: |
| 187 | dsl_name = block.dsl_name |
| 188 | if dsl_name: |
| 189 | if dsl_name not in self.parsers: |
| 190 | assert dsl_name in parsers, f"No parser to handle {dsl_name!r} block." |
| 191 | self.parsers[dsl_name] = parsers[dsl_name](self) |
| 192 | parser = self.parsers[dsl_name] |
| 193 | parser.parse(block) |
| 194 | printer.print_block(block) |
| 195 | |
| 196 | # these are destinations not buffers |
| 197 | for name, destination in self.destinations.items(): |
| 198 | if destination.type == 'suppress': |
| 199 | continue |
| 200 | output = destination.dump() |
| 201 | |
| 202 | if output: |
| 203 | block = Block("", dsl_name="clinic", output=output) |
| 204 | |
| 205 | if destination.type == 'buffer': |
| 206 | block.input = "dump " + name + "\n" |
| 207 | warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.") |
| 208 | printer.write("\n") |
| 209 | printer.print_block(block) |
| 210 | continue |
| 211 | |
| 212 | if destination.type == 'file': |
| 213 | try: |
| 214 | dirname = os.path.dirname(destination.filename) |
| 215 | try: |
| 216 | os.makedirs(dirname) |
| 217 | except FileExistsError: |
| 218 | if not os.path.isdir(dirname): |
| 219 | fail(f"Can't write to destination " |
| 220 | f"{destination.filename!r}; " |
| 221 | f"can't make directory {dirname!r}!") |
| 222 | if self.verify: |
| 223 | with open(destination.filename) as f: |
| 224 | parser_2 = BlockParser(f.read(), language=self.language) |
| 225 | blocks = list(parser_2) |
| 226 | if (len(blocks) != 1) or (blocks[0].input != 'preserve\n'): |
| 227 | fail(f"Modified destination file " |
| 228 | f"{destination.filename!r}; not overwriting!") |
| 229 | except FileNotFoundError: |
| 230 | pass |
| 231 | |
| 232 | block.input = 'preserve\n' |
| 233 | includes = self.codegen.get_includes() |
| 234 | |
| 235 | printer_2 = BlockPrinter(self.language) |
| 236 | printer_2.print_block(block, header_includes=includes) |
| 237 | libclinic.write_file(destination.filename, |
| 238 | printer_2.f.getvalue()) |
| 239 | continue |
| 240 | |