subroutinestatement_textprivate
subroutine statement_text(src, item, text)
The code of statement item: its tokens as written, without comments and continuation marks.
Continuation lines are joined as FORD joins them.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| src | type(source_t) | in | the source | |
| item | integer | in | the statement | |
| text | character(len=:) | out | allocatable | its code |
Source
Show the code
subroutine statement_text(src, item, text)
type(source_t), intent(in) :: src !! the source
integer, intent(in) :: item !! the statement
character(len=:), allocatable, intent(out) :: text !! its code
character(len=:), allocatable :: buffer
logical :: after_break, pending_blank
integer :: t, first, last, n, width
first = src%stmts%s(item)%first
last = src%stmts%s(item)%last
allocate (character(len=src%tokens%tok(last)%last - src%tokens%tok(first)%first + 2) :: buffer)
n = 0
after_break = .false.
pending_blank = .false.
do t = first, last
associate (tok => src%tokens%tok(t))
select case (tok%kind)
case (TK_NEWLINE)
after_break = .true.
case (TK_COMMENT, TK_PREPROC)
cycle
case (TK_CONT)
if (tok%sub == CT_LEADING) then
after_break = .false.
pending_blank = .false.
end if
case (TK_SPACE)
if (.not. after_break) pending_blank = .true.
case default
if (after_break) pending_blank = .true.
after_break = .false.
if (tok%kind == TK_OP .and. src%text(tok%first:tok%last) == ';') cycle
if (pending_blank .and. n > 0) then
n = n + 1
buffer(n:n) = ' '
end if
pending_blank = .false.
width = tok%last - tok%first + 1
buffer(n + 1:n + width) = src%text(tok%first:tok%last)
n = n + width
end select
end associate
end do
text = buffer(1:n)
end subroutine statement_text