Skip to main content

module lang::pico::format::Formatting

rascal-Not specified

Demonstrates ((Tree2Box)), ((Box2Text)) and ((HiFiLayoutDiff)) for constructing a declarative and HiFi Pico formatting pipeline

Usage

import lang::pico::format::Formatting;

Dependencies

extend lang::box::util::Tree2Box;
import ParseTree;
import analysis::diff::edits::ExecuteTextEdits;
import analysis::diff::edits::HiFiLayoutDiff;
import lang::box::\syntax::Box;
import lang::box::util::Box2Text;
import lang::pico::\syntax::Main;

Description

Using four generic or generated, "language parametric", building blocks we construct a Pico formatting pipeline:

  • Parse Tree is used to generate a parser for Pico.
  • Tree2 Box provides the extensible/overridable and declarative To Box function which maps language constructs to Box expressions. The To Box function combines generic language-parametric rules, as well as bespoke language specific rules..
  • Box2 Text is a generic reusable algorithm for two-dimensional string layout.
  • Finally, Hi Fi Layout Diff generically extracts Text Edits from two trees which are equal modulo whitespace and comments.

Benefits

  • The formatting is style is programmed declaratively by mapping language patterns to Box expressions.
  • The pipeline never loses source code comments, and this requires no attention from the language engineer.

Pitfalls

  • Box2 Text must be extended for the open recursive calls of To Box to reach the extensions in the current module. If you import Box2 Text the extended To Box rules will only be found if they describe top-level tree nodes.

function formatPicoFile

In-place formatting of an entire Pico file

void formatPicoFile(loc file)

function formatPicoString

Format a string that contains an entire Pico program

str formatPicoString(str file)

function formatPicoTree

Pico Format function for reuse in file, str or IDE-based formatting contexts

list[TextEdit] formatPicoTree(start[Program] file)

function toBox

Format while

Box toBox((Statement) `while <Expression e> do <{Statement ";"}* block> od`, FO opts = fo())

function toBox

Format if-then-else

Box toBox((Statement) `if <Expression e> then <{Statement ";"}* thenPart> else <{Statement ";"}* elsePart> fi`, FO opts = fo())