Headline

Unparsing in Language:Haskell with a pretty printing combinator library

Characteristics

This contribution demonstrates unparsing, i.e., rendering a term-based representation as according to a some specific text-based concrete syntax. The unparser is described at a relatively high level of abstraction by means of an appropriate library in Language:Haskell.

Illustration

Input of unparsing is a term-based representation like this; "..." indicates an elision:

sampleCompany = Company
  "Acme Corporation"
  [ Department "Research"
      (Employee "Craig" "Redmond" 123456)
      []
      [ Employee "Erik" "Utrecht" 12345,
        Employee "Ralf" "Koblenz" 1234
      ],
    Department "Development"
      ...
  ]

Output of unparsing is a test-based representation like this:

company "Acme Corporation" {
  department "Research" {
    manager "Craig" {
      address "Redmond"
      salary 123456.0
    }
    employee "Erik" {
      address "Utrecht"
      salary 12345.0
    }
    employee "Ralf" {
      address "Koblenz"
      salary 1234.0
    }
  }
  department "Development" {
    ...
  }
}

Here is the function for unparsing:

unparse :: Company -> Doc
unparse (Company n ds) =
  bracy "company" n (vcat (map unparseD ds))
  where
    bracy :: String -> String -> Doc -> Doc
    bracy k n d =
         text k <+> doubleQuotes (text n) <+> text "{"
      $$ nest 2 d
      $$ text "}"
    unparseD :: Department -> Doc
    unparseD (Department n m ds es) =
      bracy "department" n (vcat (   [unparseE "manager" m]
                                ++ map unparseD ds
                                ++ map (unparseE "employee") es))
      where
        unparseE :: String -> Employee -> Doc
        unparseE k (Employee n a s) = bracy k n (a' $$ s')
          where
            a' = text "address" <+> doubleQuotes (text a)
            s' = text "salary" <+> text (show s)

It uses various combinators of Technology:HughesPJ all over the place. For instance, it uses vcat to vertically compose departments; it uses nest to achieve indentation for constituents of companies, departments, and employees. Overall, all the subexpressions render the company terms to a library-specific type of Doc, which is essentially an abstraction over text.

Relationships


Ralf Lämmel edited this article at Sat, 20 Jun 2020 13:54:20 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.