The pieces of output for tokens first:lend of a line: the tokens, with the blanks between them
adjusted by the whitespace rules and trailing blanks removed.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(writer_t), | intent(in) | :: | w |
the output and the alignment state |
||
| character(len=*), | intent(in) | :: | src |
the source text |
||
| type(token_list_t), | intent(in) | :: | tokens |
its tokens |
||
| integer, | intent(in) | :: | first |
first token of the part of the line |
||
| integer, | intent(in) | :: | lend |
last token of the line, its line break if it has one |
||
| type(piece_t), | intent(out), | allocatable | :: | pieces(:) |
the pieces, in order |
subroutine line_pieces(w, src, tokens, first, lend, pieces) !! The pieces of output for tokens `first:lend` of a line: the tokens, with the blanks between them !! adjusted by the whitespace rules and trailing blanks removed. type(writer_t), intent(in) :: w !! the output and the alignment state character(len=*), intent(in) :: src !! the source text type(token_list_t), intent(in) :: tokens !! its tokens integer, intent(in) :: first !! first token of the part of the line integer, intent(in) :: lend !! last token of the line, its line break if it has one type(piece_t), allocatable, intent(out) :: pieces(:) !! the pieces, in order integer :: t, n, g, last allocate (pieces(2*(lend - first + 1) + 1)) n = 0 do t = first, lend if (tokens%tok(t)%kind == TK_SPACE) then if (t == lend) cycle if (tokens%tok(t + 1)%kind == TK_NEWLINE) cycle g = -1 ! blanks between two code tokens may be adjusted by the rule for the second one, and ! blanks before a trailing comment by the comment spacing if (t > first .and. t < lend) then if (is_code_kind(tokens%tok(t - 1)%kind) .and. is_code_kind(tokens%tok(t + 1)%kind)) then g = w%gap(t + 1) else if (tokens%tok(t + 1)%kind == TK_COMMENT) then g = w%gap(t + 1) end if if (g == 0) cycle end if call add(0, tokens%tok(t)%first, tokens%tok(t)%last, max(g, 0)) cycle end if if (t > first) then if (is_code_kind(tokens%tok(t)%kind) .and. is_code_kind(tokens%tok(t - 1)%kind)) then g = w%gap(t) ! no blank in the input if (g == GAP_MIN1) then call add(0, 1, 0, 1) else if (g > 0) then call add(0, 1, 0, g) end if else if (tokens%tok(t)%kind == TK_COMMENT .and. tokens%tok(t - 1)%kind /= TK_SPACE) then if (w%gap(t) > 0) call add(0, 1, 0, w%gap(t)) ! a trailing comment right after the code end if end if last = tokens%tok(t)%last if (tokens%tok(t)%kind == TK_COMMENT) then ! without trailing blanks and tabs do while (last >= tokens%tok(t)%first) if (src(last:last) /= ' ' .and. src(last:last) /= achar(9)) exit last = last - 1 end do end if call add(t, tokens%tok(t)%first, last, 0) end do pieces = pieces(1:n) contains subroutine add(tok, from, to, blanks) !! Append a piece with these components (see [[piece_t]]). integer, intent(in) :: tok, from, to, blanks !! its `tok`, `first`, `last` and `blanks` n = n + 1 pieces(n) = piece_t(tok, from, to, blanks) end subroutine add end subroutine line_pieces