tree_graph__build_syntax_tree Subroutine

private pure subroutine tree_graph__build_syntax_tree(self, pattern)

Type Bound

tree_t

Arguments

Type IntentOptional Attributes Name
class(tree_t), intent(inout) :: self
character(len=*), intent(in) :: pattern

Source Code

   pure subroutine tree_graph__build_syntax_tree(self, pattern)
      implicit none
      class(tree_t), intent(inout) :: self
      character(*), intent(in) :: pattern

      integer :: i, status

      ! if (allocated(self%nodes)) deallocate(self%nodes)
      allocate(self%nodes(TREE_NODE_BASE:TREE_NODE_UNIT), stat=status)
      
      self%nodes(TREE_NODE_BASE:TREE_NODE_UNIT)%own_i = [(i, i=TREE_NODE_BASE, TREE_NODE_UNIT)]
      self%num_alloc = 1
      self%tape%idx = 1
      self%tape%str = pattern
      self%top = 0
      self%paren_balance = 0
      call self%tape%get_token()

      ! Generate AST from a given pattern.
      call self%regex()

      ! Check the pattern is valid.
      if (.not. self%is_valid_pattern) return

      ! Determine if parentheses are balanced.
      if (self%paren_balance > 0) then
         self%is_valid_pattern = .false.
         self%code = SYNTAX_ERR_PARENTHESIS_MISSING
      else if (self%paren_balance < 0) then
         self%is_valid_pattern = .false.
         self%code = SYNTAX_ERR_PARENTHESIS_UNEXPECTED
      end if
      
      self%nodes(self%top)%parent_i = TERMINAL_INDEX
   end subroutine tree_graph__build_syntax_tree