fdoc

subroutineparse_fstringrecursiveprivate

recursive subroutine parse_fstring(c, text, id)

An f-string, whose quotes and escapes are already gone.

Arguments

NameTypeIntentAttributesDescription
ctype(fypp_t)inout

the state

textcharacter(len=*)in

the string

idintegerout

the result

Calls

Source

Show the code
recursive subroutine parse_fstring(c, text, id)
    type(fypp_t), intent(inout) :: c        !! the state
    character(len=*), intent(in) :: text    !! the string
    integer, intent(out) :: id              !! the result
    type(buffer_t) :: b
    character(len=:), allocatable :: piece
    integer :: i, close_brace, item

    id = new_expr(c, E_FSTRING)
    allocate (c%exprs(id)%args(0))
    i = 1
    do while (i <= len(text))
        if (text(i:i) /= '{') then
            call add(b, text(i:i))
            i = i + 1
            cycle
        end if
        if (i < len(text)) then
            if (text(i + 1:i + 1) == '{') then
                call add(b, '{')
                i = i + 2
                cycle
            end if
        end if
        call take(b, piece)
        if (len(piece) > 0) then
            item = new_expr(c, E_CONST)
            c%exprs(item)%value%kind = V_STR
            call move_alloc(piece, c%exprs(item)%value%text)
            call push_id(c%exprs(id)%args, item)
        end if
        close_brace = index(text(i:), '}')
        if (close_brace == 0) then
            call fail(c, 'an f-string is not closed: '//text)
            return
        end if
        call compile(c, text(i + 1:i + close_brace - 2), item)
        call push_id(c%exprs(id)%args, item)
        i = i + close_brace
    end do
    call take(b, piece)
    if (len(piece) > 0) then
        item = new_expr(c, E_CONST)
        c%exprs(item)%value%kind = V_STR
        call move_alloc(piece, c%exprs(item)%value%text)
        call push_id(c%exprs(id)%args, item)
    end if
end subroutine parse_fstring

Graphs

Calls

Called by