emit_body Subroutine

private subroutine emit_body(w, src, tokens, first, lend)

Tokens first:lend of a line (see line_pieces), while tracking open brackets and the anchors for aligning continuation lines.

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

integer, intent(in) :: first

first token to write

integer, intent(in) :: lend

last token of the line, its line break if it has one


Calls

proc~~emit_body~~CallsGraph proc~emit_body emit_body proc~input_column input_column proc~emit_body->proc~input_column proc~kind_part kind_part proc~emit_body->proc~kind_part proc~line_pieces line_pieces proc~emit_body->proc~line_pieces proc~put put proc~emit_body->proc~put proc~to_case to_case proc~emit_body->proc~to_case proc~token_lower token_lower proc~emit_body->proc~token_lower 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

Called by

proc~~emit_body~~CalledByGraph proc~emit_body emit_body proc~emit_continuation emit_continuation proc~emit_continuation->proc~emit_body proc~emit_statement emit_statement proc~emit_statement->proc~emit_body proc~emit_statement->proc~emit_continuation 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_body(w, src, tokens, first, lend)
    !! Tokens `first:lend` of a line (see [[line_pieces]]), while tracking open brackets and the
    !! anchors for aligning continuation lines.
    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
    integer, intent(in) :: first               !! first token to write
    integer, intent(in) :: lend                !! last token of the line, its line break if it has one
    type(piece_t), allocatable :: pieces(:)
    integer :: k, t, ka, kb
    character(len=:), allocatable :: text

    call line_pieces(w, src, tokens, first, lend, pieces)
    do k = 1, size(pieces)
        t = pieces(k)%tok
        if (pieces(k)%blanks > 0) then
            call put(w, repeat(' ', pieces(k)%blanks))
            cycle
        end if
        if (t == 0) then
            call put(w, src(pieces(k)%first:pieces(k)%last))
            cycle
        end if
        if (w%cases%whole(t) == CASE_KEEP .and. w%cases%part(t) == CASE_KEEP) then
            call write_token(t, src(pieces(k)%first:pieces(k)%last))     ! without a copy
            cycle
        end if
        text = src(pieces(k)%first:pieces(k)%last)
        if (w%cases%whole(t) /= CASE_KEEP) text = to_case(text, w%cases%whole(t) == CASE_UPPER)
        if (w%cases%part(t) /= CASE_KEEP) then
            call kind_part(text, tokens%tok(t)%kind, ka, kb)
            if (kb >= ka) text(ka:kb) = to_case(text(ka:kb), w%cases%part(t) == CASE_UPPER)
        end if
        call write_token(t, text)
    end do

contains

    subroutine write_token(t, text)
        !! Write token `t` as `text` (its text with the letter case applied), tracking brackets,
        !! anchors and trailing comments.
        integer, intent(in) :: t                 !! index into the token list
        character(len=*), intent(in) :: text     !! the text to write
        if (tokens%tok(t)%kind == TK_COMMENT) then
            if (.not. w%line_comment) then           ! a trailing comment: later lines may line up with it
                w%comment_in = input_column(src, tokens%tok(t))
                w%comment_out = w%col
                w%line_comment = .true.
            end if
        end if
        if (.not. is_code_kind(tokens%tok(t)%kind)) then
            call put(w, text)
            return
        end if
        if (w%pending_bracket) w%bracket_first(w%nbrackets) = w%col
        if (w%pending_anchor > 0) w%anchor_col(w%pending_anchor) = w%col
        w%pending_bracket = .false.
        w%pending_anchor = 0
        call put(w, text)
        if (tokens%tok(t)%kind == TK_OP) then
            select case (text)
            case ('(', '[')
                if (.not. allocated(w%bracket_first)) allocate (w%bracket_first(16))
                if (w%nbrackets == size(w%bracket_first)) w%bracket_first = [w%bracket_first, w%bracket_first]
                w%nbrackets = w%nbrackets + 1
                w%bracket_first(w%nbrackets) = -1
                w%pending_bracket = .true.
            case (')', ']')
                if (w%nbrackets > 0) w%nbrackets = w%nbrackets - 1
            case default
                if (w%nbrackets == 0) call top_level_operator(text)
            end select
        end if
        w%prev = t
    end subroutine write_token

    subroutine top_level_operator(op)
        !! Record alignment anchors at a top-level operator.
        character(len=*), intent(in) :: op   !! text of the operator
        select case (op)
        case ('=', '=>')
            ! an assignment, unless a `,` or `:` came first (`use m, only: a => b`, `integer :: n = 1`),
            ! or the bindings of `generic :: name => a, b`
            if (.not. w%separated) then
                call anchor(ANCHOR_ASSIGN)
            else if (op == '=>' .and. w%generic .and. w%declaration) then
                call anchor(ANCHOR_ASSIGN)
            end if
        case ('::')
            call anchor(ANCHOR_DECL)
            w%separated = .true.
            w%declaration = .true.
        case (':')
            if (w%prev > 0) then
                if (token_lower(src, tokens%tok(w%prev)) == 'only') call anchor(ANCHOR_ONLY)
            end if
            w%separated = .true.
        case (',')
            w%separated = .true.
        end select
    end subroutine top_level_operator

    subroutine anchor(a)
        !! Make the next code token the first item after anchor `a`, unless that anchor is already set.
        integer, intent(in) :: a   !! `ANCHOR_ASSIGN`, `ANCHOR_ONLY` or `ANCHOR_DECL`
        if (w%anchor_col(a) < 0) w%pending_anchor = a
    end subroutine anchor
end subroutine emit_body