tree_graph__primary Subroutine

private pure recursive subroutine tree_graph__primary(self)

Type Bound

tree_t

Arguments

Type IntentOptional Attributes Name
class(tree_t), intent(inout) :: self

Source Code

   pure recursive subroutine tree_graph__primary(self)
      use :: forgex_utf8_m, only: ichar_utf8
      implicit none
      class(tree_t), intent(inout) :: self

      type(tree_node_t) :: node
      type(segment_t) :: seg
      character(:), allocatable :: chara

      select case (self%tape%current_token)
      case (tk_char)
         chara = self%tape%token_char
         seg = segment_t(ichar_utf8(chara), ichar_utf8(chara))
         node = make_atom(seg)
         call self%register_connector(node, terminal, terminal)
         call self%tape%get_token() 
      
      case (tk_lpar)
         
         if (self%tape%current_token == tk_lpar) then
            self%paren_balance = self%paren_balance +1
         end if

         call self%tape%get_token()
         call self%regex()
      
         ! If regex fails, return immediately.
         if (.not. self%is_valid_pattern) return

         ! If not a right parenthesis, throw an error.
         if (self%tape%current_token /= tk_rpar) then
            self%code = SYNTAX_ERR_PARENTHESIS_MISSING
            self%is_valid_pattern = .false.
            return
         end if
         call self%tape%get_token()

      case (tk_lsbracket)
         call self%char_class()
         if (self%tape%current_token /= tk_rsbracket) then
            self%code = SYNTAX_ERR_BRACKET_MISSING
            self%is_valid_pattern = .false.
            return
         end if
         call self%tape%get_token()

      case (tk_backslash)
         call self%shorthand()
         call self%tape%get_token()
      
      case (tk_dot)
         node = make_atom(SEG_ANY)
         call self%register_connector(node, terminal, terminal)
         call self%tape%get_token()

      case (tk_caret)
         call self%caret_dollar()
         call self%tape%get_token()
         
      case (tk_dollar)
         call self%caret_dollar()
         call self%tape%get_token()
      
      case (tk_rsbracket)
         self%code = SYNTAX_ERR_BRACKET_UNEXPECTED
         self%is_valid_pattern = .false.
         return

      case (tk_rpar)
         self%code = SYNTAX_ERR_PARENTHESIS_UNEXPECTED
         self%is_valid_pattern = .false.
         return
   
      case default
         self%code = SYNTAX_ERR
         self%is_valid_pattern = .false.
         return
      end select

   end subroutine tree_graph__primary