Classify a code statement. in_interface is true directly inside an interface block,
where module procedure lists procedures instead of starting one.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | src |
source text |
||
| type(token_list_t), | intent(in) | :: | tokens |
tokens of |
||
| type(statement_view_t), | intent(in) | :: | v |
the statement |
||
| logical, | intent(in) | :: | in_interface |
the innermost open construct is an interface block |
function classify_statement(src, tokens, v, in_interface) result(info) !! Classify a code statement. `in_interface` is true directly inside an interface block, !! where `module procedure` lists procedures instead of starting one. character(len=*), intent(in) :: src !! source text type(token_list_t), intent(in) :: tokens !! tokens of `src` type(statement_view_t), intent(in) :: v !! the statement logical, intent(in) :: in_interface !! the innermost open construct is an interface block type(stmt_info_t) :: info type(words_t) :: wd integer :: k, j if (v%n == 0) return k = v%first if (k == 2) call read_label(v%low(v%lo(1):v%hi(1)), info%label) ! construct name, possibly partly generated by fypp: `outer: do`, `get_${x}$: if (...) then` j = k do while (j < v%n) if (.not. is_name(j)) exit j = j + 1 end do if (j > k .and. j < v%n .and. v%is(j, ':')) then info%name = src(tokens%tok(v%code(k))%first:tokens%tok(v%code(j - 1))%last) k = j + 1 end if if (.not. is_name(k)) return ! an assignment to a variable named like a keyword, or to an element of one (`rank (1) = n`) j = k + 1 if (v%is(j, '(')) then if (v%closing(j) > 0) j = v%closing(j) + 1 end if if (v%is(j, '=') .or. v%is(j, '=>') .or. v%is(j, '%')) return call split_words() call classify_words() contains logical function is_name(j) !! True if code token `j` is a name, or inline fypp that generates one (`${name}$`). integer, intent(in) :: j !! index into `v%code(:)` is_name = v%kind_of(j) == TK_NAME .or. v%kind_of(j) == TK_FYPP end function is_name function word(j) result(t) !! Lowercase word `j`, or blank past the end. Words are only compared with keywords, all !! shorter than `WORD_LEN`, so one that is cut short cannot match by mistake. The length is !! fixed because classification must be thread-safe: the length of a deferred-length !! function result is kept in static storage by gfortran. integer, intent(in) :: j !! index into the words `wd` character(len=WORD_LEN) :: t if (j > wd%n) then t = '' else if (j <= size(wd%w)) then t = wd%w(j)%s else t = v%low(v%lo(wd%tok(j)):v%hi(wd%tok(j))) end if end function word integer function after_parens(j) result(next) !! Index of the code token after the parenthesis that opens at word `j`, or 0 if word `j` !! is not `(` or the parenthesis is not closed. integer, intent(in) :: j !! index into the words `wd` next = 0 if (j > wd%n) return next = v%closing(wd%tok(j)) if (next > 0) next = next + 1 end function after_parens logical function is_name_word(j) !! True if word `j` is a name, or inline fypp that generates one (`${name}$`). integer, intent(in) :: j !! index into the words `wd` is_name_word = .false. if (j > wd%n) return is_name_word = is_name(wd%tok(j)) end function is_name_word logical function has_attribute(attr) !! True if `attr` appears among the words before `::`. character(len=*), intent(in) :: attr !! the attribute, in lowercase integer :: j has_attribute = .false. do j = 2, wd%n if (word(j) == '::') return if (word(j) == attr) then has_attribute = .true. return end if end do end function has_attribute integer function word_at(c) result(j) !! Index of the first word at or after code token `c`, or `wd%n + 1` if there is none. integer, intent(in) :: c !! index into `v%code(:)` do j = 1, wd%n if (wd%tok(j) >= c) return end do j = wd%n + 1 end function word_at subroutine split_words() !! Fill `wd` with the words of the statement from code token `k` on, splitting the first !! one with `expand_keyword`. type(string_t), allocatable :: parts(:) integer :: j, np ! only `end...`, `else...`, `select...`, `block...`, `change...` and `double...` can be ! combined keywords; other first words are read from the view, like the rest (see `word`) associate (first => v%low(v%lo(k):v%hi(k))) if (index(first, 'end') == 1 .or. index(first, 'else') == 1 .or. index(first, 'select') == 1 .or. & index(first, 'block') == 1 .or. index(first, 'change') == 1 .or. index(first, 'double') == 1) then parts = expand_keyword(first) if (size(parts) > 1) call move_alloc(parts, wd%w) end if end associate if (.not. allocated(wd%w)) allocate (wd%w(0)) np = max(1, size(wd%w)) allocate (wd%tok(np + v%n - k)) wd%tok(1:np) = k wd%n = np do j = k + 1, v%n wd%n = wd%n + 1 wd%tok(wd%n) = j end do end subroutine split_words subroutine classify_words() !! Set `info` from the words in `wd`, by the first word. integer :: c character(len=:), allocatable :: label_text select case (word(1)) case ('end') c = closed_construct(word(2), word(3)) if (c /= C_NONE) call set(info, ROLE_CLOSE, c) case ('else') select case (word(2)) case ('', 'if') call set(info, ROLE_MIDDLE, C_IF) case ('where') call set(info, ROLE_MIDDLE, C_WHERE) case default if (wd%n == 2) call set(info, ROLE_MIDDLE, C_IF) ! else construct-name end select case ('if') c = after_parens(2) if (c > 0 .and. c == v%n) then if (v%is(c, 'then')) call set(info, ROLE_OPEN, C_IF) end if case ('do') call set(info, ROLE_OPEN, C_DO) if (wd%n >= 2) then if (v%kind_of(wd%tok(2)) == TK_INT) then label_text = word(2) call read_label(label_text, info%do_label) end if end if case ('select') select case (word(2)) case ('case', 'type', 'rank') call set(info, ROLE_OPEN, C_SELECT) end select case ('case', 'rank') if (word(2) == '(' .or. word(2) == 'default') call set(info, ROLE_MIDDLE, C_SELECT) case ('type') if (word(2) == 'is' .and. word(3) == '(') then call set(info, ROLE_MIDDLE, C_SELECT) else if (word(2) == '(') then call check_procedure(1) else if (wd%n >= 2 .and. .not. has_attribute('deferred')) then ! `type, deferred :: T` declares a type parameter of a template (F202Y) call set(info, ROLE_OPEN, C_TYPE) end if case ('class') if ((word(2) == 'is' .and. word(3) == '(') .or. (word(2) == 'default' .and. wd%n == 2)) then call set(info, ROLE_MIDDLE, C_SELECT) else call check_procedure(1) end if case ('module') if (word(2) == 'procedure') then if (.not. in_interface) call set(info, ROLE_OPEN, C_PROCEDURE) else call check_procedure(1) if (info%role == ROLE_NONE .and. is_name_word(2)) call set(info, ROLE_OPEN, C_MODULE) end if case ('submodule') if (word(2) == '(') call set(info, ROLE_OPEN, C_SUBMODULE) case ('program') call set(info, ROLE_OPEN, C_PROGRAM) case ('block') if (wd%n == 1) then call set(info, ROLE_OPEN, C_BLOCK) else if (word(2) == 'data') then call set(info, ROLE_OPEN, C_BLOCK_DATA) end if case ('interface') call set(info, ROLE_OPEN, C_INTERFACE) case ('abstract') if (word(2) == 'interface') call set(info, ROLE_OPEN, C_INTERFACE) case ('enum') call set(info, ROLE_OPEN, C_ENUM) case ('enumeration') if (word(2) == 'type') call set(info, ROLE_OPEN, C_ENUMERATION) case ('associate') if (word(2) == '(') call set(info, ROLE_OPEN, C_ASSOCIATE) case ('critical') call set(info, ROLE_OPEN, C_CRITICAL) case ('change') if (word(2) == 'team') call set(info, ROLE_OPEN, C_TEAM) case ('where') if (after_parens(2) == v%n + 1) call set(info, ROLE_OPEN, C_WHERE) case ('forall') if (after_parens(2) == v%n + 1) call set(info, ROLE_OPEN, C_FORALL) case ('contains') if (wd%n == 1) call set(info, ROLE_MIDDLE, C_CONTAINS) case default call check_procedure(1) end select end subroutine classify_words subroutine check_procedure(j0) !! `[prefixes] [type-spec] function name` or `[prefixes] subroutine name`, starting at word `j0`: !! if the words match, set `info` to open a function or subroutine. integer, intent(in) :: j0 !! index of the first word to look at integer :: j, c j = j0 do while (j <= wd%n) if (v%kind_of(wd%tok(j)) == TK_FYPP) then j = j + 1 ! a type or prefix generated by fypp, e.g. `${t}$ function f()` cycle end if select case (word(j)) case ('pure', 'impure', 'elemental', 'recursive', 'non_recursive', 'module', 'simple') j = j + 1 case ('integer', 'real', 'complex', 'logical', 'character', 'doubleprecision', 'doublecomplex') j = j + 1 call skip_selector(j) case ('double') j = j + 1 if (word(j) == 'precision' .or. word(j) == 'complex') j = j + 1 case ('type', 'class') c = after_parens(j + 1) if (c == 0) return j = word_at(c) case ('function', 'subroutine') if (j + 1 > wd%n) return if (.not. is_name_word(j + 1)) return call set(info, ROLE_OPEN, merge(C_FUNCTION, C_SUBROUTINE, word(j) == 'function')) return case default ! a prefix made by a cpp macro: `PURE_IF_NO_DEBUG FUNCTION f(x)` if (.not. is_name_word(j)) return if (word(j + 1) /= 'function' .and. word(j + 1) /= 'subroutine') return j = j + 1 end select end do end subroutine check_procedure subroutine skip_selector(j) !! Skip a kind or length selector after an intrinsic type: `(kind=8)`, `(len=*)`, `*8`, `*(*)`. integer, intent(inout) :: j !! word index: the word after the type name, then the word after the selector integer :: c if (word(j) == '(') then c = after_parens(j) if (c > 0) j = word_at(c) else if (word(j) == '*') then j = j + 1 if (word(j) == '(') then c = after_parens(j) if (c > 0) j = word_at(c) else j = j + 1 end if end if end subroutine skip_selector end function classify_statement