| 101 | INCLUDE_COMMENT_COLUMN: Final[int] = 35 |
| 102 | |
| 103 | def print_block( |
| 104 | self, |
| 105 | block: Block, |
| 106 | *, |
| 107 | header_includes: list[Include] | None = None, |
| 108 | ) -> None: |
| 109 | input = block.input |
| 110 | output = block.output |
| 111 | dsl_name = block.dsl_name |
| 112 | write = self.f.write |
| 113 | |
| 114 | assert not ((dsl_name is None) ^ (output is None)), "you must specify dsl_name and output together, dsl_name " + repr(dsl_name) |
| 115 | |
| 116 | if not dsl_name: |
| 117 | write(input) |
| 118 | return |
| 119 | |
| 120 | write(self.language.start_line.format(dsl_name=dsl_name)) |
| 121 | write("\n") |
| 122 | |
| 123 | body_prefix = self.language.body_prefix.format(dsl_name=dsl_name) |
| 124 | if not body_prefix: |
| 125 | write(input) |
| 126 | else: |
| 127 | for line in input.split('\n'): |
| 128 | write(body_prefix) |
| 129 | write(line) |
| 130 | write("\n") |
| 131 | |
| 132 | write(self.language.stop_line.format(dsl_name=dsl_name)) |
| 133 | write("\n") |
| 134 | |
| 135 | output = '' |
| 136 | if header_includes: |
| 137 | # Emit optional "#include" directives for C headers |
| 138 | output += '\n' |
| 139 | |
| 140 | current_condition: str | None = None |
| 141 | for include in header_includes: |
| 142 | if include.condition != current_condition: |
| 143 | if current_condition: |
| 144 | output += '#endif\n' |
| 145 | current_condition = include.condition |
| 146 | if include.condition: |
| 147 | output += f'{include.condition}\n' |
| 148 | |
| 149 | if current_condition: |
| 150 | line = f'# include "{include.filename}"' |
| 151 | else: |
| 152 | line = f'#include "{include.filename}"' |
| 153 | if include.reason: |
| 154 | comment = f'// {include.reason}\n' |
| 155 | line = line.ljust(self.INCLUDE_COMMENT_COLUMN - 1) + comment |
| 156 | output += line |
| 157 | |
| 158 | if current_condition: |
| 159 | output += '#endif\n' |
| 160 | |