only_items Function

public function only_items(v) result(starts)

Positions in v%code where the items of a use statement's only: list start. Each item ends before the comma in front of the next one, the last at the end of the statement. Empty if there is no only: list, the list is empty, or it has an empty item.

Arguments

Type IntentOptional Attributes Name
type(statement_view_t), intent(in) :: v

the use statement

Return Value integer, allocatable, (:)


Called by

proc~~only_items~~CalledByGraph proc~only_items only_items proc~code_signature code_signature proc~code_signature->proc~only_items proc~sort_only_list sort_only_list proc~sort_only_list->proc~only_items proc~same_code same_code proc~same_code->proc~code_signature proc~sort_use sort_use proc~sort_use->proc~sort_only_list proc~format_text format_text proc~format_text->proc~sort_use 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

function only_items(v) result(starts)
    !! Positions in `v%code` where the items of a `use` statement's `only:` list start. Each item
    !! ends before the comma in front of the next one, the last at the end of the statement. Empty
    !! if there is no `only:` list, the list is empty, or it has an empty item.
    type(statement_view_t), intent(in) :: v   !! the `use` statement
    integer, allocatable :: starts(:)
    integer :: p, q, n, depth

    allocate (starts(0))
    n = v%n
    do p = 3, n - 1
        if (is_op(p - 1, ',') .and. is_op(p + 1, ':') .and. v%is(p, 'only')) exit
    end do
    if (p >= n .or. p + 2 > n) return
    starts = [p + 2]
    depth = 0
    do q = p + 2, n
        if (v%kinds(q) /= TK_OP) cycle
        if (v%is(q, '(') .or. v%is(q, '[')) then
            depth = depth + 1
        else if (v%is(q, ')') .or. v%is(q, ']')) then
            depth = depth - 1
        else if (v%is(q, ',') .and. depth == 0) then
            if (q == n .or. q == starts(size(starts))) then   ! an empty item
                deallocate (starts)
                allocate (starts(0))
                return
            end if
            starts = [starts, q + 1]
        end if
    end do

contains

    logical function is_op(j, text)
        !! True if code token `j` is the operator `text`.
        integer, intent(in) :: j                 !! index into `v%code(:)`
        character(len=*), intent(in) :: text     !! the operator, e.g. `,`
        is_op = v%kinds(j) == TK_OP .and. v%is(j, text)
    end function is_op

end function only_items