Tokens first:lend of a line (see line_pieces), while tracking open brackets and the
anchors for aligning continuation lines.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(writer_t), | intent(inout) | :: | 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 to write |
||
| integer, | intent(in) | :: | lend |
last token of the line, its line break if it has one |
subroutine emit_body(w, src, tokens, first, lend) !! Tokens `first:lend` of a line (see [[line_pieces]]), while tracking open brackets and the !! anchors for aligning continuation lines. type(writer_t), intent(inout) :: 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 to write integer, intent(in) :: lend !! last token of the line, its line break if it has one type(piece_t), allocatable :: pieces(:) integer :: k, t, ka, kb character(len=:), allocatable :: text call line_pieces(w, src, tokens, first, lend, pieces) do k = 1, size(pieces) t = pieces(k)%tok if (pieces(k)%blanks > 0) then call put(w, repeat(' ', pieces(k)%blanks)) cycle end if if (t == 0) then call put(w, src(pieces(k)%first:pieces(k)%last)) cycle end if if (w%cases%whole(t) == CASE_KEEP .and. w%cases%part(t) == CASE_KEEP) then call write_token(t, src(pieces(k)%first:pieces(k)%last)) ! without a copy cycle end if text = src(pieces(k)%first:pieces(k)%last) if (w%cases%whole(t) /= CASE_KEEP) text = to_case(text, w%cases%whole(t) == CASE_UPPER) if (w%cases%part(t) /= CASE_KEEP) then call kind_part(text, tokens%tok(t)%kind, ka, kb) if (kb >= ka) text(ka:kb) = to_case(text(ka:kb), w%cases%part(t) == CASE_UPPER) end if call write_token(t, text) end do contains subroutine write_token(t, text) !! Write token `t` as `text` (its text with the letter case applied), tracking brackets, !! anchors and trailing comments. integer, intent(in) :: t !! index into the token list character(len=*), intent(in) :: text !! the text to write if (tokens%tok(t)%kind == TK_COMMENT) then if (.not. w%line_comment) then ! a trailing comment: later lines may line up with it w%comment_in = input_column(src, tokens%tok(t)) w%comment_out = w%col w%line_comment = .true. end if end if if (.not. is_code_kind(tokens%tok(t)%kind)) then call put(w, text) return end if if (w%pending_bracket) w%bracket_first(w%nbrackets) = w%col if (w%pending_anchor > 0) w%anchor_col(w%pending_anchor) = w%col w%pending_bracket = .false. w%pending_anchor = 0 call put(w, text) if (tokens%tok(t)%kind == TK_OP) then select case (text) case ('(', '[') if (.not. allocated(w%bracket_first)) allocate (w%bracket_first(16)) if (w%nbrackets == size(w%bracket_first)) w%bracket_first = [w%bracket_first, w%bracket_first] w%nbrackets = w%nbrackets + 1 w%bracket_first(w%nbrackets) = -1 w%pending_bracket = .true. case (')', ']') if (w%nbrackets > 0) w%nbrackets = w%nbrackets - 1 case default if (w%nbrackets == 0) call top_level_operator(text) end select end if w%prev = t end subroutine write_token subroutine top_level_operator(op) !! Record alignment anchors at a top-level operator. character(len=*), intent(in) :: op !! text of the operator select case (op) case ('=', '=>') ! an assignment, unless a `,` or `:` came first (`use m, only: a => b`, `integer :: n = 1`), ! or the bindings of `generic :: name => a, b` if (.not. w%separated) then call anchor(ANCHOR_ASSIGN) else if (op == '=>' .and. w%generic .and. w%declaration) then call anchor(ANCHOR_ASSIGN) end if case ('::') call anchor(ANCHOR_DECL) w%separated = .true. w%declaration = .true. case (':') if (w%prev > 0) then if (token_lower(src, tokens%tok(w%prev)) == 'only') call anchor(ANCHOR_ONLY) end if w%separated = .true. case (',') w%separated = .true. end select end subroutine top_level_operator subroutine anchor(a) !! Make the next code token the first item after anchor `a`, unless that anchor is already set. integer, intent(in) :: a !! `ANCHOR_ASSIGN`, `ANCHOR_ONLY` or `ANCHOR_DECL` if (w%anchor_col(a) < 0) w%pending_anchor = a end subroutine anchor end subroutine emit_body