一句话:提出“代码样式表”,将 CSS 式选择器推广到 AST,按语义信息对不同粒度的代码显示进行样式化。
程序文本的排版样式很有限:编辑器通常只有字体和语法高亮,且不区分程序与任务。受 CSS 启发,本文提出代码样式表,用规则从抽象语法树(AST)选择元素,再样式化其可视化表示。选择器语言将 CSS 核心构造推广到含代数数据类型的编程语言环境;针对程序高度嵌套的特点,设计比 HTML 盒式布局更紧凑的多行嵌套块布局算法。作者为 Haskell 子集实现该系统,并通过多种代码呈现与可视化任务表明:代码样式表能依据 AST 结构、静态类型、运行时值及作者/读者偏好,细粒度且统一地渲染程序,可用于未来文本与结构编辑器。
Program text is rendered using impoverished typographic styles. Beyond choice of fonts and syntax-highlighting colors, code editors and related tools utilize very few text decorations. These limited styles are, furthermore, applied in monolithic fashion, regardless of the programs and tasks at hand. We present the notion of code style sheets for styling program text. Motivated by analogy to cascading style sheets (CSS) for styling HTML documents, code style sheets provide mechanisms for defining rules to select elements from an abstract syntax tree (AST) in order to style their corresponding visual representation. Technically, our selector language generalizes essential constructs from CSS to a programming-language setting with algebraic data types (such as ASTs). Practically, code style sheets allow ASTs to be styled granularly, based on semantic information—such as the structure of abstract syntax, static type information, and corresponding run-time values—as well as design choices on the part of authors and readers of a program. Because programs are heavily nested in structure, a key aspect of our design is a layout algorithm that renders nested, multiline text blocks more compactly than in existing box-based layout systems such as HTML. In this paper, we design and implement a code style sheets system for a subset of Haskell, using it to illustrate several code presentation and visualization tasks. These examples demonstrate that code style sheets provide a uniform framework for rendering programs in multivarious ways, which could be employed in future designs for text-based as well as structure editors.