line_pieces Subroutine

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


Called by

proc~~line_pieces~~CalledByGraph proc~line_pieces line_pieces proc~emit_body emit_body proc~emit_body->proc~line_pieces proc~first_piece_width first_piece_width proc~first_piece_width->proc~line_pieces proc~line_width line_width proc~line_width->proc~line_pieces proc~emit_continuation emit_continuation proc~emit_continuation->proc~emit_body proc~fitting_column fitting_column proc~emit_continuation->proc~fitting_column proc~emit_statement emit_statement proc~emit_statement->proc~emit_body proc~emit_statement->proc~emit_continuation proc~emit_statement->proc~fitting_column proc~comment_column comment_column proc~emit_statement->proc~comment_column proc~fitting_column->proc~first_piece_width proc~fitting_column->proc~line_width proc~comment_column->proc~fitting_column proc~layout layout proc~layout->proc~emit_statement proc~emit_comment_line emit_comment_line proc~layout->proc~emit_comment_line proc~emit_comment_line->proc~comment_column proc~format_pass format_pass proc~format_pass->proc~layout proc~format_text format_text proc~format_text->proc~format_pass proc~relayout relayout proc~format_text->proc~relayout proc~relayout->proc~format_pass proc~process_text process_text proc~process_text->proc~format_text proc~process_file process_file proc~process_file->proc~process_text proc~process_stdin process_stdin proc~process_stdin->proc~process_text proc~run run proc~run->proc~process_file proc~run->proc~process_stdin

Source Code

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.
    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), allocatable, intent(out) :: pieces(:)   !! the pieces, in order
    integer :: t, n, g, last

    allocate (pieces(2*(lend - first + 1) + 1))
    n = 0
    do t = first, lend
        if (tokens%tok(t)%kind == TK_SPACE) then
            if (t == lend) cycle
            if (tokens%tok(t + 1)%kind == TK_NEWLINE) cycle
            g = -1
            ! blanks between two code tokens may be adjusted by the rule for the second one, and
            ! blanks before a trailing comment by the comment spacing
            if (t > first .and. t < lend) then
                if (is_code_kind(tokens%tok(t - 1)%kind) .and. is_code_kind(tokens%tok(t + 1)%kind)) then
                    g = w%gap(t + 1)
                else if (tokens%tok(t + 1)%kind == TK_COMMENT) then
                    g = w%gap(t + 1)
                end if
                if (g == 0) cycle
            end if
            call add(0, tokens%tok(t)%first, tokens%tok(t)%last, max(g, 0))
            cycle
        end if
        if (t > first) then
            if (is_code_kind(tokens%tok(t)%kind) .and. is_code_kind(tokens%tok(t - 1)%kind)) then
                g = w%gap(t)                                   ! no blank in the input
                if (g == GAP_MIN1) then
                    call add(0, 1, 0, 1)
                else if (g > 0) then
                    call add(0, 1, 0, g)
                end if
            else if (tokens%tok(t)%kind == TK_COMMENT .and. tokens%tok(t - 1)%kind /= TK_SPACE) then
                if (w%gap(t) > 0) call add(0, 1, 0, w%gap(t))  ! a trailing comment right after the code
            end if
        end if
        last = tokens%tok(t)%last
        if (tokens%tok(t)%kind == TK_COMMENT) then       ! without trailing blanks and tabs
            do while (last >= tokens%tok(t)%first)
                if (src(last:last) /= ' ' .and. src(last:last) /= achar(9)) exit
                last = last - 1
            end do
        end if
        call add(t, tokens%tok(t)%first, last, 0)
    end do
    pieces = pieces(1:n)

contains

    subroutine add(tok, from, to, blanks)
        !! Append a piece with these components (see [[piece_t]]).
        integer, intent(in) :: tok, from, to, blanks   !! its `tok`, `first`, `last` and `blanks`
        n = n + 1
        pieces(n) = piece_t(tok, from, to, blanks)
    end subroutine add

end subroutine line_pieces