Source

Opifex / src / Language / Common / Formatter.ml

The branch 'port-stringpainter' does not exist.
(*
 * Opifex
 *
 * Copyrights(C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *)

open AST
open Batteries

(*************************************************************************************************
 * Datatype
 ************************************************************************************************)

let indent_str = "    "

type document
    = Break
    | Word of string
    | SQWord of string
    | DQWord of string
    | Oper of string
    | Syntax of string
    | ABOper of arithmetic_binary_operator
    | AUOper of arithmetic_unary_operator
    | BBOper of boolean_binary_operator
    | BUOper of boolean_unary_operator
    | BABOper of boolean_arithmetic_binary_operator
    | Value of string
    | SpecialValue of string
    | Lb of label
    | Int of int
    | Bool of bool
    | Keyword of string
    | IndentBreak of document list
    | Nested of document list
    | Suffix of string
    | NestedIndent of document list
    | SyntaxSuffix of string
    | SyntaxPrefix of string
    | Var of variable
    | Id of identifier
    | ChangeSep of string
    | Special of string
    | Newline

(*************************************************************************************************
 * The New Engine
 ************************************************************************************************)
open StringPainter.Prioritized

module SP_Engine = struct

    let rec process = function
        | Break  ->
            psp_break

        | Newline ->
            psp_newline

        | ChangeSep sep'  ->
            psp_nested psp_max_priority []

        | Word str ->
            psp_word str

        | Syntax str ->
            psp_syntax str

        | SyntaxSuffix str ->
            psp_syntax str

        | SyntaxPrefix str ->
            psp_syntax str

        | Var variable ->
            psp_word (string_of_variable variable)

        | Id identifier ->
            psp_label (string_of_identifier identifier)

        | Suffix str ->
            psp_syntax str

        | Oper str ->
            psp_operator str

        | ABOper oper ->
            psp_operator (string_of_arithmetic_binary_operator oper)      

        | BBOper oper ->
            psp_operator (string_of_boolean_binary_operator oper)

        | BUOper oper ->
            psp_operator (string_of_boolean_unary_operator oper)

        | BABOper oper ->
            psp_operator (string_of_boolean_arithmetic_binary_operator oper)

        | AUOper oper ->
            psp_operator (string_of_arithmetic_unary_operator oper)

        | SQWord str ->
            psp_word str

        | DQWord str ->
            psp_word str

        | Keyword str ->
            psp_keyword str

        | Int i ->
            psp_value_int i

        | Bool b ->
            psp_value_bool b

        | Value b ->
            psp_value b

        | SpecialValue b ->
            psp_value_keyword b

        | Special b ->
            psp_special b

        | Lb l ->
            psp_label (string_of_label l)

        | Nested nested ->
            psp_nested psp_max_priority (List.map process nested)

        | NestedIndent nested ->
            psp_nested psp_max_priority (List.map process nested)

        | IndentBreak nested ->
            psp_indent psp_max_priority (List.map process nested)

end


(*************************************************************************************************
 * Helpers
 ************************************************************************************************)

let build_string = render_painter -| psp_nested 0 -| List.map SP_Engine.process

let print_document = print_endline -| build_string

let show_option cont = function
    | None   -> []
    | Some v -> cont v