**************** The PPML library **************** .. current-library:: ppml .. current-module:: ppml Program notes need to be stored and later displayed in browsers. This presents us with two problems. At the point of creation we have no way of knowing the column width that will be used when the note is displayed. There may even be more than one width if we want to be smart when a browser window is resized. A second problem arises if we store a program note in the form of a condition string + arguments and the arguments are context sensitive. We could just save everything as a string, but then the logical structure of the message is lost. An alternative is to store the text in a more structured form. This is the purpose of the :class:`` class and its derivatives. The interface is based on Oppen's 1980 TOPLAS paper. A PPML document, represented as :class:`` is a tree of PPML tokens. The tokens available and their associated constructor functions are: * :class:`` (:func:`ppml-block`) * :class:`` (:func:`ppml-break`) * :class:`` (:func:`ppml-browser-aware-object`) * :class:`` (:func:`ppml-separator-block`) * :class:`` (:func:`ppml-string`) * :class:`` (:func:`ppml-suspension`) Structure is provided through instances of :class:`` and :class:`` as they can contain subnodes of PPML. Constructing a PPML Document **************************** Constructing a PPML document is typically done by using the various constructor functions: .. code-block:: dylan define compiler-sideways method as (class == , o :: <&generic-function>) => (ppml :: ) let sig = model-signature(o); if (sig) ppml-block(vector(ppml-string(o.^function-name), ppml-break(), as(, sig))) else ppml-string(o.^function-name) end; end method; Printing a PPML Document ************************ Given a PPML document, the best way to print it is via :gf:`ppml-print`: .. code-block:: dylan ppml-print(ppml, make(, margin: 100)); The PPML module *************** PPML Tokens and Constructors ============================ .. class:: :abstract: :superclasses: :drm:`` :description: The abstract base class for all PPML tokens. .. class:: :superclasses: :class:`` :keyword break-type: An instance of :type:``. :keyword constituents: An instance of :type:``. :keyword offset: An instance of :type:``. :description: To add structure to the output, we can package up a sequence of tokens into a block. There are a couple of attributes associated with a block. The *offset* indicates how much to indent subsequent lines of the block if a break is necessary. When a block is longer than a line then we have a number of options. We can display as much on each line as possible, only breaking when really necessary. Alternatively, we can break the block at each break point, e.g.:: aaa aaa bbb bbb ccc ddd ccc ddd The choice of layout depends on whether the *break-type* attribute of the block is ``#"consistent"`` or ``#"inconsistent"``. The third alternative is ``#"fit"``. This suppresses all breaks and truncates the output if it won't fit on a line. The size of the block is cached in the block for efficiency. .. function:: ppml-block :signature: ppml-block (constituents #key offset type) => (ppml) :parameter constituents: An instance of :type:``. :parameter #key offset: An instance of :type:``. :parameter #key type: An instance of :type:``. :value ppml: An instance of :class:``. :description: Construct a :class:``. .. class:: :superclasses: :class:`` :keyword blank-space: An instance of :type:``. :keyword offset: An instance of :type:``. :description: A ```` indicates a position in the output where it is permissible to break the output if it won't fit on a single line. If we don't need to break the line then we output blank-space spaces. If we do need to break then we indent offset spaces relative to the current line indent. .. function:: ppml-break :signature: ppml-break (#key space offset) => (ppml) :parameter #key space: An instance of :type:``. :parameter #key offset: An instance of :type:``. :value ppml: An instance of :class:``. :description: Construct a :class:``. .. class:: :superclasses: :class:`` :keyword object: An instance of :drm:``. :description: The browser "knows" about some of the objects manipulated by the compiler, e.g. the various kinds of definition, and so we store these directly. Furthermore we recompute the ppml representation of the object every time token-size is called as the representation may depend on browser settings. .. function:: ppml-browser-aware-object :signature: ppml-browser-aware-object (o) => (ppml) :parameter o: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Construct a :class:``. .. class:: :superclasses: :class:`` :keyword separator: An instance of :type:``. :description: When constructing blocks representing collections it is wasteful to explicitly store the separators between elements. The :class:`` class captures this common case. The default value for the *separator* is ``vector(ppml-string(","), ppml-break(space: 1))``. .. function:: ppml-separator-block :signature: ppml-separator-block (constituents #key separator offset type left-bracket right-bracket) => (ppml) :parameter constituents: An instance of :type:``. :parameter #key separator: An instance of :type:``. :parameter #key offset: An instance of :type:``. :parameter #key type: An instance of :type:``. The default value is ``#"inconsistent"``. :parameter #key left-bracket: An instance of ``false-or()``. :parameter #key right-bracket: An instance of ``false-or()``. :value ppml: An instance of :class:``. :description: Construct a :class:``. .. class:: :superclasses: :class:`` :keyword string: An instance of :drm:``. :description: The simplest ppml token is just a string. .. function:: ppml-string :signature: ppml-string (str) => (ppml) :parameter str: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Construct a :class:``. .. class:: :superclasses: :class:`` :keyword cache-token?: An instance of :drm:``. The default value is ``#t``. :keyword pair: Either an instance of :class:`` or a :drm:`` of :drm:`` and its arguments. :description: Sometimes it is more space efficient to delay the construction of the :class:`` equivalent of an object until we need to print it. The :class:`` class supports this. It contains either a :class:`` token, or a pair of a function and its arguments. When we need a token and encounter the pair we apply the function to its arguments. This should return an instance of :class:``. Optionally we can overwrite the pair by the result. .. function:: ppml-suspension :signature: ppml-suspension (fun #rest args) => (ppml) :parameter fun: An instance of :drm:``. :parameter #rest args: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Construct a :class:``. Conversion to PPML ================== .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns the result of calling ``print-object`` on the object as PPML. (It does this by using ``"%="`` along with ``format-to-string``. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns the quoted string value as PPML. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns the string value of the symbol as PPML. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns PPML representing the collection as a comma-separated list surrounded by ``#(...)``. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns PPML representing the collection. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns PPML representing the vector as a comma-separated list surrounded by ``#[...]``. .. method:: as :specializer: class == , :parameter class: The class :class:``. :parameter object: An instance of :drm:``. :value ppml: An instance of :class:``. :description: Returns PPML representing the list as a comma-separated list surrounded by ``#(...)``. Printing / Formatting ===================== .. generic-function:: format-to-ppml :signature: format-to-ppml (string #rest args) => (ppml) :parameter string: An instance of :drm:``. :parameter #rest args: An instance of :drm:``. :value ppml: An instance of :class:``. :description: We insert breaks at the places where arguments are inserted in the format string. This will hopefully give us reasonable output, but not always as good as we could do by hand. We separate out the processing of the format string so that we can share the constant components of the resulting ppml-block if the same format expression is used multiple times. .. generic-function:: ppml-format-string :signature: ppml-format-string (string) => (f) :parameter string: An instance of :drm:``. :value f: An instance of :drm:``. :description: Used by :gf:`format-to-ppml`. .. generic-function:: ppml-print :signature: ppml-print (t pp) => () :parameter t: An instance of :class:``. :parameter pp: An instance of :class:``. :description: This is the best way to display PPML. .. generic-function:: ppml-print-one-line :signature: ppml-print-one-line (t pp) => () :parameter t: An instance of :class:``. :parameter pp: An instance of :class:``. :description: This prints in the same way as :gf:`ppml-print`, but will limit the output to a single line. It does this by internally using a line-break-type of ``#"fit"``. .. class:: :superclasses: :drm:`` :keyword margin: An instance of :type:``. :keyword newline-function: An instance of :drm:``, taking no arguments. The default value writes a newline to ``*standard-output*``. :keyword output-function: An instance of :drm:``, taking a single :drm:`` argument. The default value writes to ``*standard-output*``. :keyword terse-depth: An instance of :drm:``. The default value is ``100``. :description: When outputting ppml we need to keep track of the space left on the current line and the current margin. We store these values in a :class:`` object, along with the functions used to display text and line breaks. The *terse-depth* is used to limit recursion amongst :class:`` instances. Once printing has recursed through *terse-depth* blocks, it will change the *break-type* to ``#"fit"`` to abbreviate things. Type Aliases and Constants ========================== .. type:: :equivalent: ``limited(, min: 0)`` .. type:: :equivalent: ``one-of(#"consistent", #"inconsistent", #"fit")`` .. type:: :equivalent: :drm:`` :description: .. note:: This should be ``limited(, of: )``. .. constant:: $line-break :value: ``ppml-break(space: 999)`` :description: A way to force a line break by making a break with a space larger than the column width.