long_code_lines Function

public function long_code_lines(src, line_length) result(lines)

Line numbers of code lines whose code (without a trailing comment) is longer than line_length, outside protected code.

Arguments

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

the source text

integer, intent(in) :: line_length

maximum line width, in characters; 0 or less for no limit

Return Value integer, allocatable, (:)


Calls

proc~~long_code_lines~~CallsGraph proc~long_code_lines long_code_lines proc~parse_source parse_source proc~long_code_lines->proc~parse_source proc~utf8_width utf8_width proc~long_code_lines->proc~utf8_width proc~protected_items protected_items proc~parse_source->proc~protected_items proc~split_statements split_statements proc~parse_source->proc~split_statements proc~tokenize tokenize proc~parse_source->proc~tokenize proc~directive directive proc~protected_items->proc~directive proc~ends_with_continuation ends_with_continuation proc~split_statements->proc~ends_with_continuation proc~find_semicolon find_semicolon proc~split_statements->proc~find_semicolon proc~line_end_token line_end_token proc~split_statements->proc~line_end_token proc~line_kind line_kind proc~split_statements->proc~line_kind proc~push statement_list_t%push proc~split_statements->proc~push proc~add add proc~tokenize->proc~add proc~comment_kind comment_kind proc~tokenize->proc~comment_kind proc~dotop_at dotop_at proc~tokenize->proc~dotop_at proc~dotop_end dotop_end proc~tokenize->proc~dotop_end proc~fypp_inline_end fypp_inline_end proc~tokenize->proc~fypp_inline_end proc~is_blank is_blank proc~tokenize->proc~is_blank proc~is_digit is_digit proc~tokenize->proc~is_digit proc~is_preproc_start is_preproc_start proc~tokenize->proc~is_preproc_start proc~is_quote is_quote proc~tokenize->proc~is_quote proc~lex_number lex_number proc~tokenize->proc~lex_number proc~line_end line_end proc~tokenize->proc~line_end proc~name_end name_end proc~tokenize->proc~name_end proc~operator_end operator_end proc~tokenize->proc~operator_end proc~peek peek proc~tokenize->proc~peek proc~preproc_end preproc_end proc~tokenize->proc~preproc_end proc~string_end string_end proc~tokenize->proc~string_end proc~add->proc~is_blank proc~add->proc~peek proc~is_code_char is_code_char proc~add->proc~is_code_char proc~push~2 token_list_t%push proc~add->proc~push~2 proc~comment_kind->proc~peek proc~is_letter is_letter proc~comment_kind->proc~is_letter proc~lowercase lowercase proc~comment_kind->proc~lowercase proc~token_text token_text proc~directive->proc~token_text proc~dotop_at->proc~peek proc~dotop_at->proc~is_letter proc~dotop_end->proc~name_end proc~dotop_end->proc~peek proc~is_name_char is_name_char proc~dotop_end->proc~is_name_char proc~dotop_end->proc~lowercase proc~fypp_inline_end->proc~peek proc~is_preproc_start->proc~peek proc~lex_number->proc~dotop_at proc~lex_number->proc~is_digit proc~lex_number->proc~name_end proc~lex_number->proc~peek proc~digits_end digits_end proc~lex_number->proc~digits_end proc~lex_number->proc~is_name_char proc~lower_char lower_char proc~lex_number->proc~lower_char proc~name_end->proc~peek proc~name_end->proc~is_name_char proc~preproc_end->proc~line_end proc~preproc_end->proc~peek proc~last_nonblank last_nonblank proc~preproc_end->proc~last_nonblank proc~string_end->proc~is_blank proc~string_end->proc~line_end proc~string_end->proc~peek proc~only_blanks_to_eol only_blanks_to_eol proc~string_end->proc~only_blanks_to_eol proc~digits_end->proc~is_digit proc~digits_end->proc~peek proc~is_code_char->proc~is_blank proc~is_name_char->proc~is_digit proc~is_name_char->proc~is_letter proc~last_nonblank->proc~is_blank proc~lowercase->proc~lower_char proc~only_blanks_to_eol->proc~line_end proc~only_blanks_to_eol->proc~last_nonblank

Called by

proc~~long_code_lines~~CalledByGraph proc~long_code_lines long_code_lines proc~format_text format_text proc~format_text->proc~long_code_lines 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 long_code_lines(src, line_length) result(lines)
    !! Line numbers of code lines whose code (without a trailing comment) is longer than `line_length`,
    !! outside protected code.
    character(len=*), intent(in) :: src           !! the source text
    integer, intent(in) :: line_length            !! maximum line width, in characters; 0 or less for no limit
    integer, allocatable :: lines(:)
    type(source_t) :: source
    integer :: k, t, col, last_line, line, p, q

    allocate (lines(0))
    if (line_length <= 0) return
    call parse_source(src, source)
    last_line = 0
    do k = 1, source%stmts%n
        if (source%keep(k) .or. source%stmts%s(k)%kind /= SK_CODE) cycle
        col = 0
        do t = source%stmts%s(k)%first, source%stmts%s(k)%last
            associate (tk => source%tokens%tok(t))
                if (tk%kind == TK_NEWLINE) then
                    col = 0
                    cycle
                end if
                if (t == source%stmts%s(k)%first) col = start_column(t)
                if (tk%kind == TK_COMMENT .or. tk%kind == TK_PREPROC) cycle
                ! a token continued on later lines (a string) is measured line by line
                line = tk%line
                p = tk%first
                do
                    q = index(src(p:tk%last), achar(10))
                    if (q == 0) exit
                    q = p + q - 1                     ! the line feed
                    if (q > p) then
                        if (src(q - 1:q - 1) == achar(13)) q = q - 1
                    end if
                    call check_width(tk, col + utf8_width(src(p:q - 1)), line)
                    col = 0
                    line = line + 1
                    p = p + index(src(p:tk%last), achar(10))
                end do
                col = col + utf8_width(src(p:tk%last))
                call check_width(tk, col, line)
            end associate
        end do
    end do

contains

    subroutine check_width(tk, width, line)
        !! Record `line` if it holds code token `tk` and its code is `width` characters wide, too long.
        type(token_t), intent(in) :: tk   !! the token
        integer, intent(in) :: width      !! width of the line up to the end of `tk` on it
        integer, intent(in) :: line       !! line number
        if (is_code_kind(tk%kind) .and. width > line_length .and. line /= last_line) then
            lines = [lines, line]
            last_line = line
        end if
    end subroutine check_width

    integer function start_column(t)
        !! Column of token `t`: the width of the text before it on its line.
        integer, intent(in) :: t   !! index into the token list
        integer :: i
        i = scan(src(1:source%tokens%tok(t)%first - 1), achar(10)//achar(13), back=.true.)
        start_column = utf8_width(src(i + 1:source%tokens%tok(t)%first - 1))
    end function start_column

end function long_code_lines