emit_statement Subroutine

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


Calls

proc~~emit_statement~~CallsGraph proc~emit_statement emit_statement proc~comment_column comment_column proc~emit_statement->proc~comment_column proc~continuation_target continuation_target proc~emit_statement->proc~continuation_target proc~emit_body emit_body proc~emit_statement->proc~emit_body proc~emit_continuation emit_continuation proc~emit_statement->proc~emit_continuation proc~emit_range emit_range proc~emit_statement->proc~emit_range proc~first_non_blank first_non_blank proc~emit_statement->proc~first_non_blank proc~fitting_column fitting_column proc~emit_statement->proc~fitting_column proc~input_column input_column proc~emit_statement->proc~input_column proc~put put proc~emit_statement->proc~put proc~token_lower token_lower proc~emit_statement->proc~token_lower proc~token_text token_text proc~emit_statement->proc~token_text proc~comment_column->proc~fitting_column proc~comment_column->proc~input_column proc~emit_body->proc~input_column proc~emit_body->proc~put proc~emit_body->proc~token_lower proc~kind_part kind_part proc~emit_body->proc~kind_part proc~line_pieces line_pieces proc~emit_body->proc~line_pieces proc~to_case to_case proc~emit_body->proc~to_case proc~emit_continuation->proc~emit_body proc~emit_continuation->proc~first_non_blank proc~emit_continuation->proc~fitting_column proc~emit_continuation->proc~input_column proc~emit_continuation->proc~put proc~emit_range->proc~put proc~emit_range->proc~token_text proc~first_piece_width first_piece_width proc~fitting_column->proc~first_piece_width proc~line_width line_width proc~fitting_column->proc~line_width proc~utf8_width utf8_width proc~input_column->proc~utf8_width proc~builder_append string_builder_t%builder_append proc~put->proc~builder_append proc~put->proc~utf8_width proc~make_lowercase make_lowercase proc~token_lower->proc~make_lowercase proc~first_piece_width->proc~line_pieces proc~first_piece_width->proc~utf8_width proc~line_width->proc~line_pieces proc~line_width->proc~utf8_width

Called by

proc~~emit_statement~~CalledByGraph proc~emit_statement emit_statement proc~layout layout proc~layout->proc~emit_statement 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 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.
    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
    integer :: j, lend, f
    logical :: first_line, mid_line

    w%nbrackets = 0
    w%anchor_col = -1
    w%pending_anchor = 0
    w%pending_bracket = .false.
    w%separated = .false.
    w%declaration = .false.
    w%prev = 0
    ! a statement after `;` starts in the middle of a line
    mid_line = .false.
    if (s%first > 1) mid_line = tokens%tok(s%first - 1)%kind /= TK_NEWLINE .and. tokens%tok(s%first - 1)%kind /= TK_BOM
    first_line = .true.
    j = s%first
    do while (j <= s%last)
        lend = j
        do while (lend < s%last)
            if (tokens%tok(lend)%kind == TK_NEWLINE) exit
            lend = lend + 1
        end do
        if (tokens%tok(j)%kind == TK_BOM) then
            call put(w, token_text(src, tokens%tok(j)))
            j = j + 1
        end if
        f = first_non_blank(tokens, j, lend)
        w%line_comment = .false.

        if (f > lend) then
            continue                                            ! nothing left on the line
        else if (tokens%tok(f)%kind == TK_NEWLINE) then
            call put(w, token_text(src, tokens%tok(f)))         ! blank line
        else if (tokens%tok(f)%kind == TK_PREPROC) then
            call emit_range(w, src, tokens, j, lend)
        else if (tokens%tok(f)%kind == TK_COMMENT) then
            call put(w, repeat(' ', comment_column(w, src, tokens, f, ind + opts%continuation_indent)))
            call emit_body(w, src, tokens, f, lend)
            w%line_comment = .true.
        else if (first_line) then
            if (mid_line) then
                call emit_body(w, src, tokens, j, lend)
            else if (labeled .and. tokens%tok(f)%kind == TK_INT) then
                call put(w, token_text(src, tokens%tok(f)))
                call put(w, repeat(' ', max(1, ind - w%col)))
                call start_statement(first_non_blank(tokens, f + 1, lend))   ! the code after the label
                call emit_body(w, src, tokens, first_non_blank(tokens, f + 1, lend), lend)
            else
                call put(w, repeat(' ', fitting_column(w, src, tokens, f, lend, &
                                                       [ind, input_column(src, tokens%tok(f))])))
                call start_statement(f)
                call emit_body(w, src, tokens, f, lend)
            end if
            first_line = .false.
        else
            call emit_continuation(w, src, tokens, f, lend, continuation_target(w, ind, opts), &
                                   ind + opts%continuation_indent)
        end if
        if (.not. w%line_comment) w%comment_in = -1
        w%pending_anchor = 0
        w%pending_bracket = .false.
        j = lend + 1
    end do
contains

    subroutine start_statement(f)
        !! Record where the statement starting with token `f` begins, for comment lines after it.
        integer, intent(in) :: f   !! first code token of the statement, after any label
        w%stmt_in = -1
        if (f > s%last) return
        w%stmt_in = input_column(src, tokens%tok(f))
        w%stmt_out = w%col
        w%generic = token_lower(src, tokens%tok(f)) == 'generic'
    end subroutine start_statement

end subroutine emit_statement