// Doc IR — Prettier's document intermediate representation // Ported from Prettier's src/document/builders.js // // A Doc is either a string, an array of Docs, or a command. // Since Zena doesn't have untagged unions as first-class values yet, // we use a class hierarchy with a DocKind discriminator. // ============================================================================ // Doc Kind Discriminator // ============================================================================ export enum DocKind { Text, Array, Group, Indent, Dedent, Align, IfBreak, Fill, Line, Softline, Hardline, LiteralLine, LineSuffix, LineSuffixBoundary, BreakParent, Trim, IndentIfBreak, Label } // ============================================================================ // Doc Base Class // ============================================================================ export abstract class Doc { kind: DocKind; new(kind: DocKind) : kind = kind; } // ============================================================================ // Doc Types // ============================================================================ export class TextDoc extends Doc { value: String; new(value: String) : value = value, super(DocKind.Text); } export class ArrayDoc extends Doc { parts: Array; new(parts: Array) : parts = parts, super(DocKind.Array); } export class GroupDoc extends Doc { contents: Doc; var shouldBreak: boolean; id: i32; new(contents: Doc, shouldBreak: boolean, id: i32) : contents = contents, shouldBreak = shouldBreak, id = id, super(DocKind.Group); } export class IndentDoc extends Doc { contents: Doc; new(contents: Doc) : contents = contents, super(DocKind.Indent); } export class DedentDoc extends Doc { contents: Doc; new(contents: Doc) : contents = contents, super(DocKind.Dedent); } export class AlignDoc extends Doc { n: i32; contents: Doc; new(n: i32, contents: Doc) : n = n, contents = contents, super(DocKind.Align); } export class IfBreakDoc extends Doc { breakContents: Doc; flatContents: Doc; groupId: i32; new(breakContents: Doc, flatContents: Doc, groupId: i32) : breakContents = breakContents, flatContents = flatContents, groupId = groupId, super(DocKind.IfBreak); } export class FillDoc extends Doc { parts: Array; new(parts: Array) : parts = parts, super(DocKind.Fill); } export class LineSuffixDoc extends Doc { contents: Doc; new(contents: Doc) : contents = contents, super(DocKind.LineSuffix); } export class LineSuffixBoundaryDoc extends Doc { new() : super(DocKind.LineSuffixBoundary); } export class LineDoc extends Doc { new() : super(DocKind.Line); } export class SoftlineDoc extends Doc { new() : super(DocKind.Softline); } export class HardlineDoc extends Doc { new() : super(DocKind.Hardline); } export class LiteralLineDoc extends Doc { new() : super(DocKind.LiteralLine); } export class BreakParentDoc extends Doc { new() : super(DocKind.BreakParent); } export class TrimDoc extends Doc { new() : super(DocKind.Trim); } export class IndentIfBreakDoc extends Doc { contents: Doc; groupId: i32; new(contents: Doc, groupId: i32) : contents = contents, groupId = groupId, super(DocKind.IndentIfBreak); } export class LabelDoc extends Doc { tag: String; contents: Doc; new(tag: String, contents: Doc) : tag = tag, contents = contents, super(DocKind.Label); } // ============================================================================ // Doc Builder Functions // ============================================================================ export function text(value: String): Doc { return new TextDoc(value); } export function group(contents: Doc): Doc { return new GroupDoc(contents, false, 0); } export function groupBreak(contents: Doc): Doc { return new GroupDoc(contents, true, 0); } export function groupWithBreak(contents: Doc, shouldBreak: boolean): Doc { return new GroupDoc(contents, shouldBreak, 0); } export function groupWithId(contents: Doc, id: i32): Doc { return new GroupDoc(contents, false, id); } export function indent(contents: Doc): Doc { return new IndentDoc(contents); } export function dedent(contents: Doc): Doc { return new DedentDoc(contents); } export function align(n: i32, contents: Doc): Doc { return new AlignDoc(n, contents); } export function ifBreak(breakContents: Doc, flatContents: Doc): Doc { return new IfBreakDoc(breakContents, flatContents, 0); } export function ifBreakWithId(breakContents: Doc, flatContents: Doc, groupId: i32): Doc { return new IfBreakDoc(breakContents, flatContents, groupId); } export function fill(parts: Array): Doc { return new FillDoc(parts); } export function lineSuffix(contents: Doc): Doc { return new LineSuffixDoc(contents); } export function indentIfBreak(contents: Doc, groupId: i32): Doc { return new IndentIfBreakDoc(contents, groupId); } export function label(tag: String, contents: Doc): Doc { return new LabelDoc(tag, contents); } // Concat an array of docs export function concat(parts: Array): Doc { return new ArrayDoc(parts); } // Join docs with a separator export function join(sep: Doc, docs: Array): Doc { let parts = new Array(); var i = 0; while (i < docs.length) { if (i > 0) { parts.push(sep); } parts.push(docs[i]); i += 1; } return new ArrayDoc(parts); } export let line: Doc = new LineDoc(); export let softline: Doc = new SoftlineDoc(); export let hardline: Doc = new HardlineDoc(); export let literalline: Doc = new LiteralLineDoc(); export let lineSuffixBoundary: Doc = new LineSuffixBoundaryDoc(); export let breakParent: Doc = new BreakParentDoc(); export let trim: Doc = new TrimDoc();