parse_object Subroutine

private recursive subroutine parse_object(unit, parent)

Arguments

Type IntentOptional AttributesName
integer, intent(inout) :: unit
type(fson_value), pointer:: parent

Contents

Source Code


Source Code

  recursive subroutine parse_object(unit, parent)
  use fson_value_m, only: fson_value_create, fson_value_add
  implicit none

    integer, intent(inout) :: unit
    type(fson_value), pointer :: parent, pair

    logical :: eof
    character :: c

    ! pair name
    c = pop_char(unit, eof=eof, skip_ws=.true.)
    if (eof) then
       print *, "ERROR: Unexpected end of file while parsing start of object."
       call exit (1)
    else if ("}" == c) then
       ! end of an empty object
       return
    else if ('"' == c) then
       pair => fson_value_create()
       pair % name => parse_string(unit)
    else
       print *, "ERROR: Expecting string: '", c, "'"
       call exit (1)
    end if

    ! pair value
    c = pop_char(unit, eof=eof, skip_ws=.true.)
    if (eof) then
       print *, "ERROR: Unexpected end of file while parsing object member. 1"
       call exit (1)
    else if (":" == c) then
       ! parse the value
       call parse_value(unit, pair)
       call fson_value_add(parent, pair)
    else
       print *, "ERROR: Expecting : and then a value. ", c
       call exit (1)
    end if

    ! another possible pair
    c = pop_char(unit, eof=eof, skip_ws=.true.)
    if (eof) then
       return
    else if ("," == c) then
       ! read the next member
       call parse_object(unit=unit, parent=parent)
    else if ("}" == c) then
       return
    else
       print *, "ERROR: Expecting end of object.", c
       call exit (1)
    end if

  end subroutine parse_object