ffmt_structure Module

Block structure of a file: the nesting depth of every statement, from a stack of open constructs, plus diagnostics for statements that do not fit (an end with nothing to close, an else outside an if, a construct still open at the end of the file, ...).


Uses

  • module~~ffmt_structure~~UsesGraph module~ffmt_structure ffmt_structure module~ffmt_constructs ffmt_constructs module~ffmt_structure->module~ffmt_constructs module~ffmt_source ffmt_source module~ffmt_structure->module~ffmt_source module~ffmt_statements ffmt_statements module~ffmt_structure->module~ffmt_statements module~ffmt_strings ffmt_strings module~ffmt_structure->module~ffmt_strings module~ffmt_tokens ffmt_tokens module~ffmt_structure->module~ffmt_tokens module~ffmt_constructs->module~ffmt_statements module~ffmt_constructs->module~ffmt_strings module~ffmt_constructs->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_tokens->module~ffmt_strings 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_structure~~UsedByGraph module~ffmt_structure ffmt_structure module~ffmt_format ffmt_format module~ffmt_format->module~ffmt_structure module~ffmt_layout ffmt_layout module~ffmt_format->module~ffmt_layout module~ffmt_layout->module~ffmt_structure module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_format

Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: FY_CLOSE = 3

#:endif, #:endfor, ...

integer, private, parameter :: FY_MIDDLE = 2

#:elif, #:else, #:nextarg

integer, private, parameter :: FY_OPEN = 1

#:if, #:for, #:def, #:block, #:call, #:mute

integer, private, parameter :: FY_OTHER = 0

not a block directive (#:set, $:, cpp, ...)

integer, private, parameter :: PP_ELSE = 2
integer, private, parameter :: PP_ENDIF = 3
integer, private, parameter :: PP_IF = 1
integer, private, parameter :: PP_OTHER = 0

Derived Types

type, public ::  diagnostic_t

A problem with the block structure, such as an end that matches no open construct.

Components

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

source line of the statement concerned

character(len=:), public, allocatable :: message

what is wrong, e.g. 'if is not closed'

type, private ::  saved_stack_t

Scope stacks for one preprocessor conditional: as it was at the #if (restored at each #elif / #else) and as it was at the end of the first branch (used after #endif).

Components

Type Visibility Attributes Name Initial
type(scope_t), public, allocatable :: after_first(:)

the stack at the end of the first branch; unallocated until then

type(scope_t), public, allocatable :: at_if(:)

the stack at the #if

type, private ::  scope_stack_t

The open constructs, outermost first.

Components

Type Visibility Attributes Name Initial
type(scope_t), public, allocatable :: s(:)

storage; only s(1:top) is in use

integer, public :: top = 0

number of open constructs

Type-Bound Procedures

procedure, public :: push => stack_push

open a construct

procedure, public :: restore => stack_restore

replace the open constructs by saved ones

procedure, public :: snapshot => stack_snapshot

the open constructs, to save

type, private ::  scope_t

An open construct on the scope stack.

Components

Type Visibility Attributes Name Initial
integer, public :: construct = C_NONE

one of the C_* constants

integer, public :: do_label = 0

for do 10 ...: the label of the statement that ends the loop, else 0

integer, public :: line = 0

source line of the opening statement

type, public ::  structure_t

The block structure of a source: for each item (see ffmt_statements) what it is and how deeply it is nested.

Components

Type Visibility Attributes Name Initial
integer, public, allocatable :: depth(:)

nesting depth of each item: the constructs around it

integer, public, allocatable :: fypp(:)

fypp blocks (#:if, #:for, #:def, ...) around each item; a directive line is outside its own block

type(stmt_info_t), public, allocatable :: info(:)

classification of each item (ROLE_NONE for non-code)

integer, public, allocatable :: procedures(:)

how many are procedure definitions (subroutine, function or separate module procedure, but not interface bodies)

integer, public, allocatable :: units(:)

how many of those are program units (module, submodule or main program)


Functions

private pure function closes(closing, open)

True if an end of kind closing ends the open construct open.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: closing

construct named by the end (C_ANY for a bare end)

integer, intent(in) :: open

construct that is open

Return Value logical

private pure function conditional_kind(text) result(kind)

Kind of a preprocessor line (lowercase text): PP_IF, PP_ELSE (#elif, #else), PP_ENDIF or PP_OTHER.

Arguments

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

text of the preprocessor token, in lowercase

Return Value integer

private function fypp_depths(source) result(fypp)

The number of fypp blocks around each item of source. The directives are counted wherever they are, also inside a continued statement; a directive line itself is at the level outside its block, like else and end statements.

Arguments

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

the source

Return Value integer, allocatable, (:)

private pure function fypp_kind(text) result(kind)

Kind of a preprocessor line (lowercase text) as a fypp block directive: one of the FY_* constants.

Arguments

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

text of the preprocessor token, in lowercase

Return Value integer

private pure function middle_fits(middle, open)

True if a middle statement of construct middle (else of C_IF, contains, ...) may appear directly inside the open construct open.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: middle

construct of the middle statement

integer, intent(in) :: open

construct that is open

Return Value logical

private pure function stack_snapshot(self) result(scopes)

The open constructs, outermost first.

Arguments

Type IntentOptional Attributes Name
class(scope_stack_t), intent(in) :: self

the stack

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


Subroutines

public subroutine analyze_structure(source, views, st, diags)

Classify every statement and compute its depth: the number of constructs enclosing it, and how many of those are program units and procedures, so callers can choose not to indent their bodies. An opening statement is at the depth outside its construct, the statements inside are one deeper, and middle (else, contains, ...) and closing statements are at the opening depth. Non-code items get the depth of the code around them.

Read more…

Arguments

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

the source

type(statement_view_t), intent(in) :: views(:)

the view of each item (see statement_views)

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

the structure

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

structure problems found, in the order found

private pure subroutine stack_push(self, sc)

Open construct sc, growing the storage as needed.

Arguments

Type IntentOptional Attributes Name
class(scope_stack_t), intent(inout) :: self

the stack

type(scope_t), intent(in) :: sc

the construct to open

private pure subroutine stack_restore(self, scopes)

Replace the open constructs by scopes (outermost first).

Arguments

Type IntentOptional Attributes Name
class(scope_stack_t), intent(inout) :: self

the stack

type(scope_t), intent(in) :: scopes(:)

a saved stack (see stack_snapshot)