ffmt_directives.f90 Source File


This file depends on

sourcefile~~ffmt_directives.f90~~EfferentGraph sourcefile~ffmt_directives.f90 ffmt_directives.f90 sourcefile~ffmt_statements.f90 ffmt_statements.f90 sourcefile~ffmt_directives.f90->sourcefile~ffmt_statements.f90 sourcefile~ffmt_tokens.f90 ffmt_tokens.f90 sourcefile~ffmt_directives.f90->sourcefile~ffmt_tokens.f90 sourcefile~ffmt_statements.f90->sourcefile~ffmt_tokens.f90 sourcefile~ffmt_strings.f90 ffmt_strings.f90 sourcefile~ffmt_statements.f90->sourcefile~ffmt_strings.f90 sourcefile~ffmt_tokens.f90->sourcefile~ffmt_strings.f90

Files dependent on this one

sourcefile~~ffmt_directives.f90~~AfferentGraph sourcefile~ffmt_directives.f90 ffmt_directives.f90 sourcefile~ffmt_source.f90 ffmt_source.f90 sourcefile~ffmt_source.f90->sourcefile~ffmt_directives.f90 sourcefile~ffmt_case.f90 ffmt_case.f90 sourcefile~ffmt_case.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_format.f90 ffmt_format.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_case.f90 sourcefile~ffmt_layout.f90 ffmt_layout.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_layout.f90 sourcefile~ffmt_linebreak.f90 ffmt_linebreak.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_linebreak.f90 sourcefile~ffmt_relational.f90 ffmt_relational.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_relational.f90 sourcefile~ffmt_structure.f90 ffmt_structure.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_structure.f90 sourcefile~ffmt_use_sort.f90 ffmt_use_sort.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_use_sort.f90 sourcefile~ffmt_whitespace.f90 ffmt_whitespace.f90 sourcefile~ffmt_format.f90->sourcefile~ffmt_whitespace.f90 sourcefile~ffmt_layout.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_layout.f90->sourcefile~ffmt_case.f90 sourcefile~ffmt_layout.f90->sourcefile~ffmt_structure.f90 sourcefile~ffmt_layout.f90->sourcefile~ffmt_whitespace.f90 sourcefile~ffmt_linebreak.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_relational.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_structure.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_use_sort.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_whitespace.f90->sourcefile~ffmt_source.f90 sourcefile~ffmt_cli.f90 ffmt_cli.f90 sourcefile~ffmt_cli.f90->sourcefile~ffmt_format.f90

Source Code

module ffmt_directives
!! Formatter directives in comments, which protect code from formatting:
!!
!! * `!&` at the end of a statement keeps the statement exactly as it is.
!! * `!&<` on a line of its own starts a region that is kept exactly as it is, and `!&>` ends it.
use ffmt_tokens
use ffmt_statements
implicit none
private

public :: protected_items

integer, parameter :: DIR_NONE = 0, DIR_KEEP = 1, DIR_OFF = 2, DIR_ON = 3   !! none, `!&`, `!&<`, `!&>`

contains

function protected_items(src, tokens, stmts) result(keep)
    !! True for each item that must be written exactly as it is: statements with a `!&` comment, and
    !! everything from a `!&<` line to the next `!&>` line, both included.
    character(len=*), intent(in) :: src              !! source text
    type(token_list_t), intent(in) :: tokens         !! tokens of `src`
    type(statement_list_t), intent(in) :: stmts      !! items of `tokens`
    logical, allocatable :: keep(:)
    logical :: off
    integer :: k, j, d

    allocate (keep(stmts%n))
    off = .false.
    do k = 1, stmts%n
        keep(k) = off
        do j = stmts%s(k)%first, stmts%s(k)%last
            if (tokens%tok(j)%kind /= TK_COMMENT) cycle
            d = directive(src, tokens%tok(j))
            if (stmts%s(k)%kind == SK_COMMENT .and. d == DIR_OFF) then
                off = .true.
                keep(k) = .true.
            else if (stmts%s(k)%kind == SK_COMMENT .and. d == DIR_ON) then
                off = .false.
                keep(k) = .true.
            else if (stmts%s(k)%kind == SK_CODE .and. d == DIR_KEEP) then
                keep(k) = .true.
            end if
        end do
    end do
end function protected_items

integer function directive(src, t)
    !! Formatter directive (one of the DIR_* constants) of a comment token.
    character(len=*), intent(in) :: src   !! source text
    type(token_t), intent(in) :: t        !! a comment token of `src`
    character(len=:), allocatable :: text
    integer :: n
    directive = DIR_NONE
    if (t%sub /= CM_FORMATTER) return
    text = token_text(src, t)
    n = len(text)
    do while (n > 0)                        ! without trailing blanks and tabs
        if (text(n:n) /= ' ' .and. text(n:n) /= achar(9)) exit
        n = n - 1
    end do
    text = text(1:n)
    if (text == '!&<') then
        directive = DIR_OFF
    else if (text == '!&>') then
        directive = DIR_ON
    else if (len(text) == 2) then
        directive = DIR_KEEP
    else if (text(3:3) == ' ') then
        directive = DIR_KEEP
    end if
end function directive

end module ffmt_directives