futhark-0.25.27: An optimising compiler for a functional, array-oriented language.

Safe Haskell | Safe-Inferred
Language | GHC2021

Futhark.Fmt.Monad

Contents

- Formatting styles

Synopsis

- type Fmt = FmtM (Doc AnsiStyle)
- nil :: Fmt
- nest :: Int -> Fmt -> Fmt
- stdNest :: Fmt -> Fmt
- text :: AnsiStyle -> Text -> Fmt
- space :: Fmt
- hardline :: Fmt
- line :: Fmt
- sep :: Fmt -> [Fmt] -> Fmt
- brackets :: Fmt -> Fmt
- braces :: Fmt -> Fmt
- parens :: Fmt -> Fmt
- (<|>) :: Fmt -> Fmt -> Fmt
- (<+>) :: Fmt -> Fmt -> Fmt
- (</>) :: Fmt -> Fmt -> Fmt
- (<:/>) :: Fmt -> Fmt -> Fmt
- hardIndent :: Int -> Fmt -> Fmt
- indent :: Int -> Fmt -> Fmt
- hardStdIndent :: Fmt -> Fmt
- stdIndent :: Fmt -> Fmt
- type FmtM a = ReaderT Layout (State FmtState) a
- popComments :: Fmt
- runFormat :: FmtM a -> [Comment] -> Text -> a
- align :: Fmt -> Fmt
- fmtCopyLoc :: Located a => AnsiStyle -> a -> Fmt
- comment :: Text -> Fmt
- sepArgs :: Located a => (a -> Fmt) -> NonEmpty a -> Fmt
- localLayout :: Located a => a -> FmtM b -> FmtM b
- localLayoutList :: Located a => [a] -> FmtM b -> FmtM b
- sepDecs :: Located a => (a -> Fmt) -> [a] -> Fmt
- fmtByLayout :: Located a => a -> Fmt -> Fmt -> Fmt
- addComments :: Located a => a -> Fmt -> Fmt
- sepComments :: (a -> Loc) -> (a -> Fmt) -> Fmt -> [a] -> Fmt
- sepLineComments :: (a -> Loc) -> (a -> Fmt) -> Fmt -> [a] -> Fmt
- sepLine :: Fmt -> [Fmt] -> Fmt
- commentStyle :: AnsiStyle
- constantStyle :: AnsiStyle
- keywordStyle :: AnsiStyle
- bindingStyle :: AnsiStyle
- infixStyle :: AnsiStyle

# Documentation

type Fmt = FmtM (Doc AnsiStyle) Source #

nil :: Fmt Source #

An empty input.

nest :: Int -> Fmt -> Fmt Source #

Indents everything after a line occurs if in multiline and if in singleline then indent.

stdNest :: Fmt -> Fmt Source #

Nest but with the standard value of two spaces.

text :: AnsiStyle -> Text -> Fmt Source #

Creates a piece of text, it should not contain any new lines.

space :: Fmt Source #

A space.

hardline :: Fmt Source #

Forces a line to be used regardless of layout, this should ideally not be used.

line :: Fmt Source #

A line or a space depending on layout.

sep :: Fmt -> [Fmt] -> Fmt Source #

brackets :: Fmt -> Fmt Source #

Adds brackets.

braces :: Fmt -> Fmt Source #

Adds braces.

parens :: Fmt -> Fmt Source #

Add parenthesis.

(<|>) :: Fmt -> Fmt -> Fmt infixr 4 Source #

If in a singleline layout then choose `a`, if in a multiline layout choose `b`.

(<+>) :: Fmt -> Fmt -> Fmt infixr 6 Source #

Concatenate with a space between.

(</>) :: Fmt -> Fmt -> Fmt infixr 6 Source #

Concatenate with a space if in singleline layout and concatenate by a line in multiline.

(<:/>) :: Fmt -> Fmt -> Fmt infixr 6 Source #

If in a singleline layout then concatenate with `nil` and in multiline concatenate by a line.

hardIndent :: Int -> Fmt -> Fmt Source #

Indents everything by `i`, should never be used.

indent :: Int -> Fmt -> Fmt Source #

Indents if in multiline by `i` if in singleline it does not indent.

hardStdIndent :: Fmt -> Fmt Source #

Hard indents with the standard size of two.

stdIndent :: Fmt -> Fmt Source #

Idents with the standard size of two.

type FmtM a = ReaderT Layout (State FmtState) a Source #

The format monad used to keep track of comments and layout. It is a a combincation of a reader and state monad. The comments and reading from the input file are the state monads job to deal with. While the reader monad deals with the propagating the current layout.

popComments :: Fmt Source #

Retrieves the last comments from the monad and concatenates them together.

runFormat :: FmtM a -> [Comment] -> Text -> a Source #

Given a formatter `FmtM a`, a sequence of comments ordered in increasing order by location, and the original text files content. Run the formatter and create `a`.

align :: Fmt -> Fmt Source #

Aligns line by line.

fmtCopyLoc :: Located a => AnsiStyle -> a -> Fmt Source #

Using the location of `a` get the segment of text in the original file to create a `Fmt`.

comment :: Text -> Fmt Source #

A comment.

sepArgs :: Located a => (a -> Fmt) -> NonEmpty a -> Fmt Source #

This is used for function arguments. It seperates multiline arguments by lines and singleline arguments by spaces. We specially handle the case where all the arguments are on a single line except for the last one, which may continue to the next line.

localLayout :: Located a => a -> FmtM b -> FmtM b Source #

This function determines the Layout of `a` and updates the monads environment to format in the appropriate style. It determines this by checking if the location of `a` spans over two or more lines.

localLayoutList :: Located a => [a] -> FmtM b -> FmtM b Source #

This function determines the Layout of `[a]` and if it is singleline then it updates the monads enviroment to format singleline style otherwise format using multiline style. It determines this by checking if the locations of `[a]` start and end at any different line number.

sepDecs :: Located a => (a -> Fmt) -> [a] -> Fmt Source #

If in singleline layout seperate by spaces. In a multiline layout seperate by a single line if two neighbouring elements are singleline. Otherwise sepereate by two lines.

fmtByLayout :: Located a => a -> Fmt -> Fmt -> Fmt Source #

This function allows to inspect the layout of an expression `a` and if it is singleline line then use format `s` and if it is multiline format `m`.

addComments :: Located a => a -> Fmt -> Fmt Source #

This function uses the location of `a` and prepends comments if the comments location is less than the location of `a`. It format `b` in accordance with if `a` is singleline or multiline using `localLayout`. It currently does not handle trailing comment perfectly. See testsfmttraillingComments*.fut.

sepComments :: (a -> Loc) -> (a -> Fmt) -> Fmt -> [a] -> Fmt Source #

sepLineComments :: (a -> Loc) -> (a -> Fmt) -> Fmt -> [a] -> Fmt Source #

sepLine :: Fmt -> [Fmt] -> Fmt Source #

Seperates element by a `s` followed by a space in singleline layout and seperates by a line followed by a `s` in multine layout.

# Formatting styles

commentStyle :: AnsiStyle Source #

constantStyle :: AnsiStyle Source #

keywordStyle :: AnsiStyle Source #

bindingStyle :: AnsiStyle Source #

infixStyle :: AnsiStyle Source #