next_token Subroutine

public subroutine next_token(string, pos, token)

Advance pointer to next text token

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: string

String to check

integer, intent(inout) :: pos

Current position in string

type(token_type), intent(out) :: token

Token found


Source Code

subroutine next_token(string, pos, token)

   !> String to check
   character(len=*), intent(in) :: string

   !> Current position in string
   integer, intent(inout) :: pos

   !> Token found
   type(token_type), intent(out) :: token

   integer :: start

   if (pos >= len(string)) then
      token = token_type(len(string)+1, len(string)+1)
      return
   end if

   do while(pos < len(string))
      pos = pos + 1
      select case(string(pos:pos))
      case(" ", achar(9), achar(10), achar(13))
         continue
      case default
         exit
      end select
   end do

   start = pos

   do while(pos < len(string))
      pos = pos + 1
      select case(string(pos:pos))
      case(" ", achar(9), achar(10), achar(13))
         pos = pos - 1
         exit
      case default
         continue
      end select
   end do

   token = token_type(start, pos)
end subroutine next_token