ffmt_format Module

Formatting a whole source text: the main library entry point.


Uses

  • module~~ffmt_format~~UsesGraph module~ffmt_format ffmt_format module~ffmt_case ffmt_case module~ffmt_format->module~ffmt_case module~ffmt_layout ffmt_layout module~ffmt_format->module~ffmt_layout module~ffmt_lexer ffmt_lexer module~ffmt_format->module~ffmt_lexer module~ffmt_linebreak ffmt_linebreak module~ffmt_format->module~ffmt_linebreak module~ffmt_options ffmt_options module~ffmt_format->module~ffmt_options module~ffmt_relational ffmt_relational module~ffmt_format->module~ffmt_relational module~ffmt_source ffmt_source module~ffmt_format->module~ffmt_source module~ffmt_statements ffmt_statements module~ffmt_format->module~ffmt_statements module~ffmt_strings ffmt_strings module~ffmt_format->module~ffmt_strings module~ffmt_structure ffmt_structure module~ffmt_format->module~ffmt_structure module~ffmt_tokens ffmt_tokens module~ffmt_format->module~ffmt_tokens module~ffmt_use_sort ffmt_use_sort module~ffmt_format->module~ffmt_use_sort module~ffmt_whitespace ffmt_whitespace module~ffmt_format->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_layout->module~ffmt_case module~ffmt_layout->module~ffmt_options module~ffmt_layout->module~ffmt_source module~ffmt_layout->module~ffmt_statements module~ffmt_layout->module~ffmt_strings module~ffmt_layout->module~ffmt_structure module~ffmt_layout->module~ffmt_tokens module~ffmt_layout->module~ffmt_whitespace module~ffmt_lexer->module~ffmt_strings module~ffmt_lexer->module~ffmt_tokens module~ffmt_linebreak->module~ffmt_source module~ffmt_linebreak->module~ffmt_statements module~ffmt_linebreak->module~ffmt_strings module~ffmt_linebreak->module~ffmt_tokens module~ffmt_relational->module~ffmt_options module~ffmt_relational->module~ffmt_source module~ffmt_relational->module~ffmt_statements module~ffmt_relational->module~ffmt_strings module~ffmt_relational->module~ffmt_tokens module~ffmt_source->module~ffmt_lexer 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_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_use_sort->module~ffmt_source module~ffmt_use_sort->module~ffmt_statements module~ffmt_use_sort->module~ffmt_strings module~ffmt_use_sort->module~ffmt_tokens 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

Used by

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

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: FMT_ERR_INTERNAL = 1

formatting would have changed the code; the text is returned unchanged

integer, public, parameter :: FMT_OK = 0

formatted


Functions

private function code_signature(src) result(sig)

The code tokens of src as one string that same_code compares when use statements may be reordered: for each token its kind (operators all alike), whether it is glued to the previous token, and its text as key gives it. The signatures of use statements in a run are sorted, and so are those of the items of each only: list.

Arguments

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

source text

Return Value character(len=:), allocatable

private pure function folded_length(text, kind) result(n)

How much of a token's text, from its start, Fortran reads ignoring case: all of a name, an operator or a number (exponent letters and kinds), the kind prefix of a string, and none of anything else.

Arguments

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

the token's text

integer, intent(in) :: kind

its kind (TK_*)

Return Value integer

private function limit_diagnostics(src) result(diags)

Warnings for input beyond the limits of the Fortran 2023 standard (6.3.2.1): lines longer than 10,000 characters, and statements of more than 1,000,000 characters (counted without comments and line breaks).

Arguments

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

source text

Return Value type(diagnostic_t), allocatable, (:)

public pure function longest_code(text) result(width)

An upper bound of the width of the longest code line of text: the characters of each line before a comment, without the blanks before it, as long_code_lines counts them. Preprocessor lines do not count. Strings are followed across continuation lines, so that a ! inside one does not end the code, but the scan does not tokenize, so it may count more than a token-based count would; never less.

Arguments

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

the formatted text

Return Value integer

private pure function longest_line(text) result(width)

Width of the longest line of text, in characters (as utf8_width counts them).

Arguments

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

text whose lines end with LF

Return Value integer

private function relayout(text, changed, opts) result(out)

text with the statements at changed (byte ranges, in order) laid out again, each on its own at the column where it starts. text is formatted apart from those statements, and they do not depend on the lines around them (see stands_alone), so this gives what formatting the whole text again would, in a fraction of the time.

Arguments

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

the text, after lines were split

integer, intent(in) :: changed(:,:)

byte ranges of the statements to lay out

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

the formatting options

Return Value character(len=:), allocatable

public function same_code(a, b, reordered_use, a_tokens)

True if a and b have the same code tokens, in the same order, with the same text and the same joins across line breaks (blanks, comments and line layout may differ). Names and dot operators are compared ignoring case, as Fortran does, and the two spellings of a relational operator (< and .lt.) are the same.

Read more…

Arguments

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

first source text

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

second source text

logical, intent(in), optional :: reordered_use

allow reordered use statements; default false

type(token_list_t), intent(in), optional :: a_tokens

the tokens of a, if already known

Return Value logical

private pure function same_token(sa, s, sb, t)

True if code token s of sa matches code token t of sb, as their keys in code_signature would: same kind (operators all alike), same glue, and the same text, ignoring case where Fortran does (see folded_length), with relational operators in their symbol spelling.

Arguments

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

source text of s

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

a code token of sa

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

source text of t

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

a code token of sb

Return Value logical

private pure function too_long(text, limit)

True if a code line of text may be longer than limit characters (see longest_code). A line has no more characters than bytes, so the scan is only needed when a line has more bytes than that, which finding the line breaks shows quickly.

Arguments

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

the formatted text

integer, intent(in) :: limit

the line length

Return Value logical


Subroutines

private subroutine format_pass(src, opts, out, diags, src_tokens)

One pass of the formatter: structure, whitespace, case and layout.

Arguments

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

source text

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

formatting options

character(len=:), intent(out), allocatable :: out

the formatted text

type(diagnostic_t), intent(out), allocatable :: diags(:)

problems found in this pass

type(token_list_t), intent(out), optional :: src_tokens

the tokens of src

public subroutine format_text(src, opts, out, stat, errmsg, diags)

Format src. On success out is the formatted text and stat is FMT_OK.

Read more…

Arguments

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

source text to format

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

formatting options

character(len=:), intent(out), allocatable :: out

the formatted text

integer, intent(out) :: stat

FMT_OK or FMT_ERR_INTERNAL

character(len=:), intent(out), allocatable :: errmsg

error message; unallocated if stat is FMT_OK

type(diagnostic_t), intent(out), optional, allocatable :: diags(:)

problems found while formatting