ffmt_layout Module

Writes the formatted text of a tokenized file: statements indented by their nesting depth, continuation lines aligned or indented by a fixed amount, comment lines indented like the code around them, and within each line the blanks and letter case that compute_gaps and compute_case ask for. Trailing blanks are removed.

Items protected by formatter directives (see ffmt_directives) are written as they are.


Uses

  • module~~ffmt_layout~~UsesGraph module~ffmt_layout ffmt_layout module~ffmt_case ffmt_case module~ffmt_layout->module~ffmt_case module~ffmt_options ffmt_options module~ffmt_layout->module~ffmt_options module~ffmt_source ffmt_source module~ffmt_layout->module~ffmt_source module~ffmt_statements ffmt_statements module~ffmt_layout->module~ffmt_statements module~ffmt_strings ffmt_strings module~ffmt_layout->module~ffmt_strings module~ffmt_structure ffmt_structure module~ffmt_layout->module~ffmt_structure module~ffmt_tokens ffmt_tokens module~ffmt_layout->module~ffmt_tokens module~ffmt_whitespace ffmt_whitespace module~ffmt_layout->module~ffmt_whitespace module~ffmt_case->module~ffmt_options module~ffmt_case->module~ffmt_source module~ffmt_case->module~ffmt_statements module~ffmt_case->module~ffmt_tokens module~ffmt_source->module~ffmt_statements module~ffmt_source->module~ffmt_tokens module~ffmt_directives ffmt_directives module~ffmt_source->module~ffmt_directives module~ffmt_lexer ffmt_lexer module~ffmt_source->module~ffmt_lexer module~ffmt_statements->module~ffmt_strings module~ffmt_statements->module~ffmt_tokens module~ffmt_structure->module~ffmt_source module~ffmt_structure->module~ffmt_statements module~ffmt_structure->module~ffmt_strings module~ffmt_structure->module~ffmt_tokens module~ffmt_constructs ffmt_constructs module~ffmt_structure->module~ffmt_constructs module~ffmt_tokens->module~ffmt_strings module~ffmt_whitespace->module~ffmt_options module~ffmt_whitespace->module~ffmt_source module~ffmt_whitespace->module~ffmt_statements module~ffmt_whitespace->module~ffmt_strings module~ffmt_whitespace->module~ffmt_tokens module~ffmt_constructs->module~ffmt_statements module~ffmt_constructs->module~ffmt_strings module~ffmt_constructs->module~ffmt_tokens module~ffmt_directives->module~ffmt_statements module~ffmt_directives->module~ffmt_tokens module~ffmt_lexer->module~ffmt_strings module~ffmt_lexer->module~ffmt_tokens

Used by

  • module~~ffmt_layout~~UsedByGraph module~ffmt_layout ffmt_layout module~ffmt_format ffmt_format module~ffmt_format->module~ffmt_layout module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_format

Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: ANCHOR_ASSIGN = 1
integer, private, parameter :: ANCHOR_DECL = 3
integer, private, parameter :: ANCHOR_ONLY = 2

Derived Types

type, private ::  piece_t

A piece of an output line: a token, or the blanks before one. Its text is src(first:last) (with the token's letter case applied), or blanks blanks if that is positive; it is not copied, so that measuring a line allocates nothing.

Components

Type Visibility Attributes Name Initial
integer, public :: blanks = 0

if positive, the piece is this many blanks instead

integer, public :: first = 1

first byte of the text in the source

integer, public :: last = 0

last byte of the text in the source

integer, public :: tok = 0

token index, or 0 for blanks

type, private ::  writer_t

Output being built, and the alignment state of the current statement.

Components

Type Visibility Attributes Name Initial
integer, public :: anchor_col(3) = -1

column of the first item after an assignment = / =>, a use statement's only: and a declaration's ::, or -1

integer, public, allocatable :: bracket_first(:)

column of the first item after each open bracket, or -1; only bracket_first(1:nbrackets) is in use

type(string_builder_t), public :: buf

the output text

type(case_marks_t), public :: cases

letter case wanted for each token (see compute_case)

integer, public :: col = 0

output column: characters since the last line break

integer, public :: comment_in = -1

input column of the comment on the previous line, or -1

integer, public :: comment_out = -1

output column of that comment

logical, public :: declaration = .false.

a top-level :: has been seen in the current statement

integer, public, allocatable :: gap(:)

blanks wanted before each token (see compute_gaps)

logical, public :: generic = .false.

the current statement is a generic binding

logical, public :: keep_original = .true.

fall back to a code line's original indentation when the new one would make it too long (when lines are not split)

logical, public :: line_comment = .false.

a comment has been written on the current line

integer, public :: line_length = 0

maximum line length, or 0

integer, public, allocatable :: long_lines(:)

input lines that kept their indentation to stay within the line length

integer, public :: nbrackets = 0

open brackets in the current statement

integer, public :: pending_anchor = 0

the next code token is the first item after this anchor

logical, public :: pending_bracket = .false.

the next code token is the first item after a bracket

integer, public :: prev = 0

the previous code token of the statement (index into the token list), or 0

logical, public :: separated = .false.

a top-level , or : has been seen in the statement

integer, public :: stmt_in = -1

input column of the last statement, while comment lines directly follow it, or -1

integer, public :: stmt_out = -1

output column of that statement


Functions

private function comment_column(w, src, tokens, j, default) result(col)

Output column for the comment-only line with comment token j: lined up with the comment on the line above if it was lined up with it in the input, otherwise default. Records the comment for the next line.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: j

the comment token

integer, intent(in) :: default

column to use if it is not lined up with the comment above

Return Value integer

private pure function continuation_target(w, ind, opts) result(target)

Column for the code of a continuation line: the first item after the innermost open bracket; outside brackets, the first item after an assignment, a use statement's only: or a declaration's ::; otherwise a fixed indent.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(in) :: w

the output and the alignment state

integer, intent(in) :: ind

indentation of the statement, in blanks

type(format_options_t), intent(in) :: opts

the formatting options

Return Value integer

private pure function first_non_blank(tokens, first, last) result(f)

First token in first:last that is not a blank, or last + 1.

Arguments

Type IntentOptional Attributes Name
type(token_list_t), intent(in) :: tokens

the token list

integer, intent(in) :: first

the token range to search

integer, intent(in) :: last

the token range to search

Return Value integer

private function first_piece_width(w, src, tokens, first, lend) result(width)

Width of tokens first:lend up to the end of the first code token that has another code token after it on the line (the shortest first part of a split), or a huge value if there is none.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(in) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: first

first token of the part of the line to measure

integer, intent(in) :: lend

last token of the line, its line break if it has one

Return Value integer

private function fitting_column(w, src, tokens, f, lend, candidates, from, prefix, always_original) result(col)

The first of the candidates columns at which the output line fits within the line length, or the smallest if none fits. The last candidate is the line's original column: if only that one fits, the line is recorded in long_lines. When lines are split (w%keep_original false), the original column is not a candidate for code, since splitting will make the line fit; it is still used if always_original is true (for comments, which are not split). Also when lines are split, a candidate other than the last is accepted if the line can be split to fit there: if its first piece up to a possible break fits.

Read more…

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: f

first non-blank token of the input line

integer, intent(in) :: lend

last token of the line, its line break if it has one

integer, intent(in) :: candidates(:)

columns to try, best first; negative ones are skipped

integer, intent(in), optional :: from

first token written after the prefix; requires prefix

integer, intent(in), optional :: prefix

width of the text written before token from

logical, intent(in), optional :: always_original

try the original column even when lines are split

Return Value integer

private pure function input_column(src, t) result(col)

Column of token t in the input (characters before it on its line).

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

the source text

type(token_t), intent(in) :: t

a token of src

Return Value integer

public function layout(source, st, gap, cases, opts, long_lines) result(out)

The formatted text of source, from its structure (analyze_structure), the gaps (compute_gaps) and the letter case (compute_case) wanted.

Arguments

Type IntentOptional Attributes Name
type(source_t), intent(in) :: source

the source

type(structure_t), intent(in) :: st

its block structure

integer, intent(in) :: gap(:)

blanks wanted before each token

type(case_marks_t), intent(in) :: cases

letter case wanted for each token

type(format_options_t), intent(in) :: opts

the formatting options

integer, intent(out), optional, allocatable :: long_lines(:)

input lines that kept their indentation because the new one would make them longer than opts%line_length

Return Value character(len=:), allocatable

private function line_width(w, src, tokens, first, lend) result(width)

Width of tokens first:lend as they will be written on one line, without the line break and without a trailing comment (compilers only limit the length of the code). A comment on a line of its own counts.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(in) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: first

first token of the part of the line to measure

integer, intent(in) :: lend

last token of the line, its line break if it has one

Return Value integer

private pure function to_case(text, upper) result(t)

text in ASCII upper or lower case.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

the text to convert

logical, intent(in) :: upper

true for upper case, false for lower case

Return Value character(len=len)

private pure function trim_blanks(text) result(t)

text without trailing blanks and tabs.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

the text to trim

Return Value character(len=:), allocatable


Subroutines

private subroutine emit_blank(w, src, tokens, s)

A blank line: only its line break (and a byte order mark) is kept.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

type(statement_t), intent(in) :: s

the blank line item

private subroutine emit_body(w, src, tokens, first, lend)

Tokens first:lend of a line (see line_pieces), while tracking open brackets and the anchors for aligning continuation lines.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: first

first token to write

integer, intent(in) :: lend

last token of the line, its line break if it has one

private subroutine emit_comment_line(w, src, tokens, s, ind, opts)

A comment line, re-indented to ind. A comment that lines up with the comment on the line above (a continued trailing comment) stays lined up with it. Comment lines directly after a statement that are indented more than it keep their extra indentation. With keep_column1_comments, a comment that starts in column 1 stays there.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

type(statement_t), intent(in) :: s

the comment line item

integer, intent(in) :: ind

indentation of the code around it, in blanks

type(format_options_t), intent(in) :: opts

the formatting options

private subroutine emit_continuation(w, src, tokens, f, lend, aligned, indented)

A continuation line whose first non-blank token is f, with its code starting at the column aligned, or at indented or its original column if that would make the line too long.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: f

first non-blank token of the line

integer, intent(in) :: lend

last token of the line, its line break if it has one

integer, intent(in) :: aligned

preferred column for the code (see continuation_target)

integer, intent(in) :: indented

column for the code with a fixed continuation indent

private subroutine emit_fypp_line(w, src, tokens, s, ind)

A preprocessor line: a fypp directive (#:, #!, $:, @:) is indented by ind; a cpp line is written as it is. Continuation lines of the directive are not changed.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

type(statement_t), intent(in) :: s

the line, a preprocessor item

integer, intent(in) :: ind

indentation, in blanks

private subroutine emit_range(w, src, tokens, first, last)

Append tokens unchanged.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: first

the tokens first:last to append

integer, intent(in) :: last

the tokens first:last to append

private subroutine emit_statement(w, src, tokens, s, labeled, ind, opts)

A code statement, with its continuation lines and the comment, blank and preprocessor lines between them. The first line is indented to ind; a statement label stays at the start of the line.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

type(statement_t), intent(in) :: s

the code statement

logical, intent(in) :: labeled

the statement has a statement label

integer, intent(in) :: ind

indentation of the statement, in blanks

type(format_options_t), intent(in) :: opts

the formatting options

private subroutine line_pieces(w, src, tokens, first, lend, pieces)

The pieces of output for tokens first:lend of a line: the tokens, with the blanks between them adjusted by the whitespace rules and trailing blanks removed.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(in) :: w

the output and the alignment state

character(len=*), intent(in) :: src

the source text

type(token_list_t), intent(in) :: tokens

its tokens

integer, intent(in) :: first

first token of the part of the line

integer, intent(in) :: lend

last token of the line, its line break if it has one

type(piece_t), intent(out), allocatable :: pieces(:)

the pieces, in order

private subroutine put(w, text)

Append text and track the output column.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(inout) :: w

the output and the alignment state

character(len=*), intent(in) :: text

text to append; it may contain line breaks