ghc-lib-parser-9.12.2.20250421: The GHC API, decoupled from GHC versions

Copyright | (c) The University of Glasgow 2001
License | BSD-style (see the file LICENSE)
Maintainer | Jeffrey Young <jeffrey.young@iohk.io> Luite Stegeman <luite.stegeman@iohk.io> Sylvain Henry <sylvain.henry@iohk.io> Josh Meredith <josh.meredith@iohk.io>
Stability | experimental
Safe Haskell | Ignore
Language | GHC2021

GHC.JS.Ppr

Contents

- Orphan instances

Description

- Domain and Purpose

  GHC.JS.Ppr defines the code generation facilities for the JavaScript backend. That is, this module exports a function from the JS backend IR to JavaScript compliant concrete syntax that can readily be executed by nodejs or called in a browser.

- Design

  This module follows the architecture and style of the other backends in GHC: it instances Outputable for the relevant types, creates a class that describes a morphism from the IR domain to JavaScript concrete Syntax and then generates that syntax on a case by case basis.

- How to use

  The key functions are `renderJS`, `jsToDoc`, and the `RenderJS` record. Use the `RenderJS` record and `jsToDoc` to define a custom renderers for specific parts of the backend, for example in `Opt` a custom renderer ensures all `Ident` generated by the linker optimization pass are prefixed differently than the default. Use `renderJS` to generate JavaScript concrete syntax in the general case, suitable for human consumption.

Synopsis

- renderJs :: JsToDoc a => a -> SDoc
- renderPrefixJs :: JsToDoc a => a -> SDoc
- renderPrefixJs' :: (JsToDoc a, JsRender doc) => RenderJs doc -> a -> doc
- class JsToDoc a where
  - jsToDocR :: JsRender doc => RenderJs doc -> a -> doc
- defaultRenderJs :: RenderJs doc
- data RenderJs doc = RenderJs {
  - renderJsS :: !(JsRender doc => RenderJs doc -> JStat -> doc)
  - renderJsE :: !(JsRender doc => RenderJs doc -> JExpr -> doc)
  - renderJsV :: !(JsRender doc => RenderJs doc -> JVal -> doc)
  - renderJsI :: !(JsRender doc => RenderJs doc -> Ident -> doc)
  }
- class IsLine doc => JsRender doc where
  - (<+?>) :: doc -> doc -> doc
  - ($$$) :: doc -> doc -> doc
  - jcat :: [doc] -> doc
  - jnest :: doc -> doc
  - addSemi :: doc -> doc
- jsToDoc :: JsToDoc a => a -> SDoc
- pprStringLit :: IsLine doc => FastString -> doc
- interSemi :: JsRender doc => [doc] -> doc
- braceNest :: JsRender doc => doc -> doc
- hangBrace :: JsRender doc => doc -> doc -> doc

# Documentation

renderJs :: JsToDoc a => a -> SDoc Source #

Render a syntax tree as a pretty-printable document (simply showing the resultant doc produces a nice, well formatted String).

renderPrefixJs :: JsToDoc a => a -> SDoc Source #

Render a syntax tree as a pretty-printable document, using a given prefix to all generated names. Use this with distinct prefixes to ensure distinct generated names between independent calls to render(Prefix)Js.

renderPrefixJs' :: (JsToDoc a, JsRender doc) => RenderJs doc -> a -> doc Source #

class JsToDoc a where Source #

Methods

jsToDocR :: JsRender doc => RenderJs doc -> a -> doc Source #

#### Instances

Instances details

JsToDoc Ident Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> Ident -> doc Source #
JsToDoc JExpr Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> JExpr -> doc Source #
JsToDoc JStat Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> JStat -> doc Source #
JsToDoc JVal Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> JVal -> doc Source #
JsToDoc [JExpr] Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> [JExpr] -> doc Source #
JsToDoc [JStat] Source # |
Instance details Defined in GHC.JS.Ppr Methods jsToDocR :: JsRender doc => RenderJs doc -> [JStat] -> doc Source #

defaultRenderJs :: RenderJs doc Source #

data RenderJs doc Source #

Constructors

RenderJs |

Fields

- renderJsS :: !(JsRender doc => RenderJs doc -> JStat -> doc)
- renderJsE :: !(JsRender doc => RenderJs doc -> JExpr -> doc)
- renderJsV :: !(JsRender doc => RenderJs doc -> JVal -> doc)
- renderJsI :: !(JsRender doc => RenderJs doc -> Ident -> doc)

class IsLine doc => JsRender doc where Source #

JsRender controls the differences in whitespace between HLine and SDoc. Generally, this involves the indentation and newlines in the human-readable SDoc implementation being replaced in the HLine version by the minimal whitespace required for valid JavaScript syntax.

Methods

(<+?>) :: doc -> doc -> doc Source #

Concatenate with an optional single space

($$$) :: doc -> doc -> doc Source #

Concatenate with an optional newline

jcat :: [doc] -> doc Source #

Concatenate these `doc`s, either vertically (SDoc) or horizontally (HLine)

jnest :: doc -> doc Source #

Optionally indent the following

addSemi :: doc -> doc Source #

Append semi-colon (and line-break in HLine mode)

#### Instances

Instances details

JsRender HLine Source # |
Instance details Defined in GHC.JS.Ppr Methods (<+?>) :: HLine -> HLine -> HLine Source # ($$$) :: HLine -> HLine -> HLine Source # jcat :: [HLine] -> HLine Source # jnest :: HLine -> HLine Source # addSemi :: HLine -> HLine Source #
JsRender SDoc Source # |
Instance details Defined in GHC.JS.Ppr Methods (<+?>) :: SDoc -> SDoc -> SDoc Source # ($$$) :: SDoc -> SDoc -> SDoc Source # jcat :: [SDoc] -> SDoc Source # jnest :: SDoc -> SDoc Source # addSemi :: SDoc -> SDoc Source #

jsToDoc :: JsToDoc a => a -> SDoc Source #

pprStringLit :: IsLine doc => FastString -> doc Source #

interSemi :: JsRender doc => [doc] -> doc Source #

braceNest :: JsRender doc => doc -> doc Source #

The structure `{body}`, optionally indented over multiple lines

hangBrace :: JsRender doc => doc -> doc -> doc Source #

The structure `hdr {body}`, optionally indented over multiple lines

# Orphan instances

Outputable JExpr Source # |
Instance details Methods ppr :: JExpr -> SDoc Source #
Outputable JVal Source # |
Instance details Methods ppr :: JVal -> SDoc Source #