same_token Function

private pure function same_token(sa, s, sb, t)

True if code token s of sa matches code token t of sb, as their keys in code_signature would: same kind (operators all alike), same glue, and the same text, ignoring case where Fortran does (see folded_length), with relational operators in their symbol spelling.

Arguments

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

source text of s

type(token_t), intent(in) :: s

a code token of sa

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

source text of t

type(token_t), intent(in) :: t

a code token of sb

Return Value logical


Calls

proc~~same_token~~CallsGraph proc~same_token same_token proc~canonical_relational canonical_relational proc~same_token->proc~canonical_relational proc~folded_length folded_length proc~same_token->proc~folded_length proc~lower_char lower_char proc~same_token->proc~lower_char proc~token_lower token_lower proc~same_token->proc~token_lower proc~make_lowercase make_lowercase proc~token_lower->proc~make_lowercase

Called by

proc~~same_token~~CalledByGraph proc~same_token same_token proc~same_code same_code proc~same_code->proc~same_token

Source Code

pure logical function same_token(sa, s, sb, t)
    !! True if code token `s` of `sa` matches code token `t` of `sb`, as their keys in
    !! [[code_signature]] would: same kind (operators all alike), same glue, and the same text,
    !! ignoring case where Fortran does (see [[folded_length]]), with relational operators in their
    !! symbol spelling.
    character(len=*), intent(in) :: sa   !! source text of `s`
    character(len=*), intent(in) :: sb   !! source text of `t`
    type(token_t), intent(in) :: s       !! a code token of `sa`
    type(token_t), intent(in) :: t       !! a code token of `sb`
    integer :: ks, kt, k, nfold
    character(len=:), allocatable :: ca, cb

    ks = s%kind
    kt = t%kind
    if (ks == TK_DOTOP) ks = TK_OP
    if (kt == TK_DOTOP) kt = TK_OP
    same_token = .false.
    if (ks /= kt .or. (s%glued .neqv. t%glued)) return
    if (s%last - s%first == t%last - t%first) then
        nfold = folded_length(sa(s%first:s%last), s%kind)
        same_token = .true.
        do k = 0, s%last - s%first
            if (k < nfold) then
                if (lower_char(sa(s%first + k:s%first + k)) == lower_char(sb(t%first + k:t%first + k))) cycle
            else
                if (sa(s%first + k:s%first + k) == sb(t%first + k:t%first + k)) cycle
            end if
            same_token = .false.
            exit
        end do
        if (same_token) return
    end if
    ! `.lt.` and `<`: rare, so the allocating comparison is fine here
    if (ks == TK_OP) then
        ca = canonical_relational(token_lower(sa, s))
        cb = canonical_relational(token_lower(sb, t))
        same_token = len(ca) == len(cb) .and. ca == cb
    end if
end function same_token