subroutinecount_source
subroutine count_source(text, stats)
Count the lines of text. A last line with no line ending counts as a line.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| text | character(len=*) | in | the source | |
| stats | type(file_stats_t) | inout | where the counts go |
Calls
- end_line
Source
Show the code
subroutine count_source(text, stats)
character(len=*), intent(in) :: text !! the source
type(file_stats_t), intent(inout) :: stats !! where the counts go
type(token_list_t) :: tokens
logical :: code, comment, open
integer :: k
stats%bytes = len(text, int64)
call tokenize(text, tokens)
code = .false.
comment = .false.
open = .false.
do k = 1, tokens%n
select case (tokens%tok(k)%kind)
case (TK_NEWLINE)
call end_line()
cycle
case (TK_COMMENT)
comment = .true.
case (TK_SPACE, TK_BOM)
continue
case default
code = .true.
end select
open = .true.
end do
if (open) call end_line()
contains
!> Count the line that ends here.
subroutine end_line()
stats%lines = stats%lines + 1
if (code) then
stats%code = stats%code + 1
else if (comment) then
stats%comments = stats%comments + 1
else
stats%blank = stats%blank + 1
end if
code = .false.
comment = .false.
open = .false.
end subroutine end_line
end subroutine count_source