1# coding=utf-8 2# 3# Copyright (c) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import sys 17from dataclasses import dataclass 18from typing import TYPE_CHECKING 19 20from taihe.driver.backend import Backend, BackendConfig 21from taihe.utils.outputs import DEFAULT_INDENT 22 23if TYPE_CHECKING: 24 from taihe.driver.contexts import CompilerInstance 25 26 27@dataclass 28class PrettyPrintBackendConfig(BackendConfig): 29 NAME = "pretty-print" 30 31 show_resolved = True 32 colorize = True 33 34 def construct(self, instance: "CompilerInstance") -> Backend: 35 from taihe.semantics.format import PrettyPrinter 36 from taihe.utils.outputs import BaseWriter 37 38 class PrettyPrintBackendImpl(Backend): 39 def __init__(self, ci: "CompilerInstance", config: BackendConfig): 40 super().__init__(ci) 41 pass 42 self._ci = ci 43 self._config = config 44 45 def generate(self): 46 PrettyPrinter( 47 BaseWriter( 48 sys.stdout, 49 default_indent=DEFAULT_INDENT, 50 comment_prefix="// ", 51 ), 52 self._config.show_resolved, 53 self._config.colorize, 54 ).handle_decl(self._ci.package_group) 55 56 return PrettyPrintBackendImpl(instance, self)