parse_backslash_and_hyphen_in_char_array Subroutine

public pure subroutine parse_backslash_and_hyphen_in_char_array(array, ierr)

This subroutine processes a character array, and outputs the corresponding flagged array. It removes backslash and hyphen characters, and then flags the current element in character_array_t type array.

Arguments

Type IntentOptional Attributes Name
type(character_array_t), intent(inout), allocatable :: array(:)
integer, intent(inout) :: ierr

Source Code

   pure subroutine parse_backslash_and_hyphen_in_char_array(array, ierr)
      use :: forgex_parameters_m
      use :: forgex_error_m
      implicit none
      type(character_array_t), intent(inout), allocatable :: array(:)
      type(character_array_t), allocatable :: temp(:)
      integer, intent(inout) :: ierr

      integer :: i, k, siz
      logical :: is_already_subtraction_zone

      if (.not. allocated(array)) return
      if (size(array, dim=1) < 1) return 

      allocate(temp(size(array, dim=1)))

      k = 1 ! actual size counter to output.
      is_already_subtraction_zone = .false.

      ! Main loop
      do i = 1, size(array, dim=1) ! i is array's index
         if (1 < i .and. i < size(array,dim=1)) then
           
            ! Handling subtract expression
            if (.not. is_already_subtraction_zone) then
               if (array(i)%c == SYMBOL_HYPN .and. array(i+1)%c == SYMBOL_HYPN) then
                  temp(k:size(temp))%is_subtract = .true.
                  is_already_subtraction_zone = .true.
                  cycle
               end if
            else
               if (array(i)%c == SYMBOL_HYPN .and. array(i+1)%c == SYMBOL_HYPN) then
                  ierr = SYNTAX_ERR_MISPLACED_SUBTRACTION_OPERATOR
                  return
               end if
            end if

            if (array(i-1)%c == SYMBOL_HYPN .and. array(i)%c == SYMBOL_HYPN) then
               cycle
            end if
         end if

         if (array(i)%c == SYMBOL_BSLH .and. .not. temp(k)%is_escaped) then
            ! If the current character is backslash
            ! except the `is_escaped` of `temp(k)` is true.
            temp(k)%is_escaped = .true.
         
         else if (array(i)%c == SYMBOL_HYPN .and. .not. i == 1) then
            ! If the current character is hyphen,
            ! except for the first character. 
            temp(k-1)%is_hyphenated = .true.
         
         else
            ! For characters has no special meaning.
            temp(k)%c = array(i)%c
            k = k + 1
         end if
      end do

      ! Copy from local array to the arguemnt array.
      siz = k - 1
      if (allocated(array)) deallocate(array)
      allocate(array(siz))
      array(:) = temp(1:siz)
      
   end subroutine parse_backslash_and_hyphen_in_char_array