set_view Subroutine

public subroutine set_view(src, tokens, stmt, v)

Make v the view of code statement stmt.

Arguments

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

source text

type(token_list_t), intent(in) :: tokens

tokens of src

type(statement_t), intent(in) :: stmt

the statement

type(statement_view_t), intent(out) :: v

the view


Calls

proc~~set_view~~CallsGraph proc~set_view set_view proc~make_lowercase make_lowercase proc~set_view->proc~make_lowercase

Called by

proc~~set_view~~CalledByGraph proc~set_view set_view proc~code_signature code_signature proc~code_signature->proc~set_view proc~set_views set_views proc~set_views->proc~set_view proc~sort_use sort_use proc~sort_use->proc~set_view proc~statement_view statement_view proc~statement_view->proc~set_view proc~format_pass format_pass proc~format_pass->proc~set_views proc~format_text format_text proc~format_text->proc~sort_use proc~format_text->proc~format_pass proc~relayout relayout proc~format_text->proc~relayout proc~same_code same_code proc~same_code->proc~code_signature proc~statement_views statement_views proc~statement_views->proc~set_views proc~process_text process_text proc~process_text->proc~format_text proc~relayout->proc~format_pass 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 set_view(src, tokens, stmt, v)
    !! Make `v` the view of code statement `stmt`.
    character(len=*), intent(in) :: src          !! source text
    type(token_list_t), intent(in) :: tokens     !! tokens of `src`
    type(statement_t), intent(in) :: stmt        !! the statement
    type(statement_view_t), intent(out) :: v     !! the view
    integer :: j, p, d, total

    ! the code tokens, as code_tokens finds them, counted first to allocate once
    v%n = 0
    do j = stmt%first, stmt%last
        if (is_statement_code(j)) v%n = v%n + 1
    end do
    allocate (v%code(v%n), v%kinds(v%n), v%depth(v%n), v%lo(v%n), v%hi(v%n))
    p = 0
    do j = stmt%first, stmt%last
        if (.not. is_statement_code(j)) cycle
        p = p + 1
        v%code(p) = j
    end do
    total = 0
    do j = 1, v%n
        total = total + tokens%tok(v%code(j))%last - tokens%tok(v%code(j))%first + 1
    end do
    allocate (character(len=total) :: v%low)
    p = 0
    d = 0
    do j = 1, v%n
        associate (t => tokens%tok(v%code(j)))
            v%kinds(j) = t%kind
            v%lo(j) = p + 1
            v%low(p + 1:p + t%last - t%first + 1) = src(t%first:t%last)
            call make_lowercase(v%low(p + 1:p + t%last - t%first + 1))
            p = p + t%last - t%first + 1
            v%hi(j) = p
        end associate
        v%depth(j) = d
        if (v%kinds(j) /= TK_OP) cycle
        select case (v%low(v%lo(j):v%hi(j)))
        case ('(', '[')
            d = d + 1
        case (')', ']')
            d = max(0, d - 1)
            v%depth(j) = d
        end select
    end do
    if (v%n > 1) then
        if (v%kinds(1) == TK_INT) v%first = 2
    end if

contains

    logical function is_statement_code(k)
        !! True if token `k` is one of the code tokens of the statement (not a `;` separator).
        integer, intent(in) :: k   !! index into the token list
        is_statement_code = is_code_kind(tokens%tok(k)%kind)
        if (is_statement_code .and. tokens%tok(k)%kind == TK_OP) &
            is_statement_code = src(tokens%tok(k)%first:tokens%tok(k)%last) /= ';'
    end function is_statement_code

end subroutine set_view