packages feed

ddc-code-0.4.3.1: tetra/base/Data/Text/Operator.ds

-- | Useful operators on text objects.
module Data.Text.Operator
export
{       -- * Desugaring
        textLit; paste; pastes;

        -- * Optimizations
        flattenText;

        -- * Pretty Printing
        parens;
}
import Data.Text.Base
where


-- Desuguaring -----------------------------------------------------------------
-- Names used by the Source Tetra desugarer to implement string literals.
textLit (x : TextLit#) : Text
 = TextLit (makeTextLit x)

paste  (x y : Text) : Text
 = TextApp x y

pastes (x y : Text) : Text
 = x % " " % y


-- Optimizations --------------------------------------------------------------
-- | If this text is not already in flat form then flatten it.
--
--   This allocates a new contiguous vector for the text object and
--   allows the program to release space for intermediate append nodes.
--
flattenText (tt: Text): Text
 = case tt of
        -- Single text literals are already flat.
        TextLit lit     -> tt

        -- Single text vectors are already flat.
        TextVec vec     -> tt

        -- Text has an outer append-node, 
        -- so flatten the whole thing.
        TextApp _ _     -> textOfVector (run vectorOfText [RegionText] tt)


-- Pretty Printing ------------------------------------------------------------
-- | Wrap a some text in parenthesis.
parens (tx: Text): Text
 = "(" % tx % ")"