pretty 0.1.0 copy "pretty: ^0.1.0" to clipboard
pretty: ^0.1.0 copied to clipboard

outdatedDart 1 only

Pretty-printing combinators

Pretty-Printing Combinators #

Build Status

A Dart port of Christian Lindig's strict version of Wadler's pretty-printing combinators. The present version deforests away the second intermediate tree of Lindig's implementation, which should make it slightly more efficient.

This is what this library allows for:

class Tree {
  final String name;
  final List<Tree> children;
  Tree(this.name, this.children);

  Document get pretty => // see below
}

final someTree = new Tree("aaa",
    [new Tree("bbbbb",
        [new Tree("ccc", []),
         new Tree("dd", [])]),
    new Tree("eee", []),
    new Tree("ffff",
        [new Tree("gg", []),
         new Tree("hhh", []),
         new Tree("ii", [])])]);

void main() {
  for (int width in [100, 50, 20, 10]) {
    print(someTree.pretty.render(width));
    print("");
  }
}

Output:

aaa { bbbbb { ccc, dd }, eee, ffff { gg, hhh, ii } }

aaa {
  bbbbb { ccc, dd },
  eee,
  ffff { gg, hhh, ii }
}

aaa {
  bbbbb { ccc, dd },
  eee,
  ffff {
    gg,
    hhh,
    ii
  }
}

aaa {
  bbbbb {
    ccc,
    dd
  },
  eee,
  ffff {
    gg,
    hhh,
    ii
  }
}

The set of combinators exposed by the library helps defining Tree's pretty method:

Document get pretty => (text(name) + _brackets).group;

Document get _brackets {
  return children.isEmpty
      ? empty
      : text(" {") + (line + _prettyChildren).nest(2) + line + text("}");
}

Document get _prettyChildren {
  return (text(",") + line).join(children.map((c) => c.pretty));
}

More documentation is underway.

0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Pretty-printing combinators

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on pretty