same_code Function

public function same_code(a, b, reordered_use, a_tokens)

True if a and b have the same code tokens, in the same order, with the same text and the same joins across line breaks (blanks, comments and line layout may differ). Names and dot operators are compared ignoring case, as Fortran does, and the two spellings of a relational operator (< and .lt.) are the same.

With reordered_use, use statements may also be in a different order within a run of them (not across other statements or preprocessor lines), and so may the items of an only: list.

Arguments

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

first source text

character(len=*), intent(in) :: b

second source text

logical, intent(in), optional :: reordered_use

allow reordered use statements; default false

type(token_list_t), intent(in), optional :: a_tokens

the tokens of a, if already known

Return Value logical


Calls

proc~~same_code~~CallsGraph proc~same_code same_code proc~code_signature code_signature proc~same_code->proc~code_signature proc~same_token same_token proc~same_code->proc~same_token proc~tokenize tokenize proc~same_code->proc~tokenize proc~builder_append string_builder_t%builder_append proc~code_signature->proc~builder_append proc~builder_get string_builder_t%builder_get proc~code_signature->proc~builder_get proc~canonical_relational canonical_relational proc~code_signature->proc~canonical_relational proc~folded_length folded_length proc~code_signature->proc~folded_length proc~is_use_statement is_use_statement proc~code_signature->proc~is_use_statement proc~lowercase lowercase proc~code_signature->proc~lowercase proc~only_items only_items proc~code_signature->proc~only_items proc~parse_source parse_source proc~code_signature->proc~parse_source proc~set_view set_view proc~code_signature->proc~set_view proc~sorted_order sorted_order proc~code_signature->proc~sorted_order proc~to_s to_s proc~code_signature->proc~to_s proc~token_lower token_lower proc~code_signature->proc~token_lower proc~token_text token_text proc~code_signature->proc~token_text proc~same_token->proc~canonical_relational proc~same_token->proc~folded_length proc~lower_char lower_char proc~same_token->proc~lower_char proc~same_token->proc~token_lower 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~lowercase proc~comment_kind->proc~peek proc~is_letter is_letter proc~comment_kind->proc~is_letter proc~dotop_at->proc~peek proc~dotop_at->proc~is_letter proc~dotop_end->proc~lowercase 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~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~lower_char 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~lowercase->proc~lower_char proc~name_end->proc~peek proc~name_end->proc~is_name_char proc~parse_source->proc~tokenize proc~protected_items protected_items proc~parse_source->proc~protected_items proc~split_statements split_statements proc~parse_source->proc~split_statements proc~preproc_end->proc~line_end proc~preproc_end->proc~peek proc~last_nonblank last_nonblank proc~preproc_end->proc~last_nonblank proc~make_lowercase make_lowercase proc~set_view->proc~make_lowercase 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~token_lower->proc~make_lowercase 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~only_blanks_to_eol->proc~line_end proc~only_blanks_to_eol->proc~last_nonblank 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~directive->proc~token_text

Source Code

logical function same_code(a, b, reordered_use, a_tokens)
    !! True if `a` and `b` have the same code tokens, in the same order, with the same text and the
    !! same joins across line breaks (blanks, comments and line layout may differ). Names and dot
    !! operators are compared ignoring case, as Fortran does, and the two spellings of a relational
    !! operator (`<` and `.lt.`) are the same.
    !!
    !! With `reordered_use`, `use` statements may also be in a different order within a run of them
    !! (not across other statements or preprocessor lines), and so may the items of an `only:` list.
    character(len=*), intent(in) :: a                     !! first source text
    character(len=*), intent(in) :: b                     !! second source text
    logical, intent(in), optional :: reordered_use        !! allow reordered `use` statements; default false
    type(token_list_t), intent(in), optional :: a_tokens    !! the tokens of `a`, if already known
    type(token_list_t) :: ta, tb

    if (present(reordered_use)) then
        if (reordered_use) then
            same_code = code_signature(a) == code_signature(b)
            return
        end if
    end if
    ! token by token, without building signatures
    call tokenize(b, tb)
    if (present(a_tokens)) then
        same_code = same_lists(a_tokens)          ! without a copy of the token list
    else
        call tokenize(a, ta)
        same_code = same_lists(ta)
    end if

contains

    logical function same_lists(ta)
        !! True if the code tokens of `a` (`ta`) match those of `b` (`tb`).
        type(token_list_t), intent(in) :: ta   !! the tokens of `a`
        integer :: i, j
        same_lists = .false.
        i = 0
        j = 0
        do
            i = next_code(ta, i)
            j = next_code(tb, j)
            if (i > ta%n .or. j > tb%n) exit
            if (.not. same_token(a, ta%tok(i), b, tb%tok(j))) return
        end do
        same_lists = i > ta%n .and. j > tb%n
    end function same_lists

    pure integer function next_code(tokens, k) result(next)
        !! The index of the first code token after token `k`, or `tokens%n + 1`.
        type(token_list_t), intent(in) :: tokens   !! the token list
        integer, intent(in) :: k                   !! index into `tokens`, or 0 to start at the beginning
        next = k + 1
        do while (next <= tokens%n)
            if (is_code_kind(tokens%tok(next)%kind)) return
            next = next + 1
        end do
    end function next_code

end function same_code