Skip to content

Commit

Permalink
[update] added functionality to remember the previous pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinobuAmasaki committed Jan 28, 2024
1 parent 991b1ac commit 162f307
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/automaton_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ subroutine deallocate_automaton(self)
end if
end do

deallocate(self%nfa)
if (associated(self%nfa)) deallocate(self%nfa)

deallocate(self%dfa)
if (associated(self%dfa)) deallocate(self%dfa)


end subroutine deallocate_automaton
Expand Down
104 changes: 57 additions & 47 deletions src/forgex.f90
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module forgex
end interface

type(automaton_t) :: cache
character(:), allocatable :: pattern_cache

contains

Expand All @@ -47,24 +48,32 @@ function in__matching (pattern, str) result(res)
type(tree_t), pointer :: root
type(tape_t) :: tape


from = 0
to = 0

buff = pattern
if (pattern /= pattern_cache) then

root => build_syntax_tree(tape, buff)
! initialize
call cache%free()
call cache%init()

buff = pattern
root => build_syntax_tree(tape, buff)

! call print_tree(root)

call cache%init()
call cache%build_nfa(root)
! call print_tree(root)

call cache%build_nfa(root)

! call cache%print_nfa()

call cache%convert_NFA_to_DFA()
! call cache%print_nfa()
call cache%convert_NFA_to_DFA()

! call cache%print_dfa()
! call cache%print_dfa()
pattern_cache = pattern
call deallocate_tree()

end if

call cache%matching(char(10)//str//char(10), from, to)

Expand All @@ -89,9 +98,6 @@ function in__matching (pattern, str) result(res)
res = .false.
end if

call deallocate_tree()
call cache%free()

end function in__matching


Expand All @@ -108,35 +114,40 @@ function match__matching(pattern, str) result(res)
from = 0
to = 0

if (is_there_caret_at_the_top(pattern)) then
buff = pattern(2:len(pattern))
else
buff = pattern(1:len(pattern))
end if
if (pattern /= pattern_cache) then

if (is_there_dollar_at_the_end(pattern)) then
buff = buff(1:len_trim(pattern)-1)
end if
if (is_there_caret_at_the_top(pattern)) then
buff = pattern(2:len(pattern))
else
buff = pattern(1:len(pattern))
end if

root => build_syntax_tree(tape, buff)
if (is_there_dollar_at_the_end(pattern)) then
buff = buff(1:len_trim(pattern)-1)
end if

! call print_tree(root)
root => build_syntax_tree(tape, buff)

call cache%init()
call cache%build_nfa(root)
! call print_tree(root)

! call cache%print_nfa()
call cache%free()
call cache%init()
call cache%build_nfa(root)

call cache%convert_NFA_to_DFA()
! call cache%print_nfa()

! call cache%print_dfa()
call cache%convert_NFA_to_DFA()

! call cache%print_dfa()
pattern_cache = pattern
call deallocate_tree()

end if

res = cache%matching_exactly(str)

! write(stderr, *) from, to
call deallocate_tree()
call cache%free()


end function match__matching


Expand All @@ -156,25 +167,28 @@ function regex__matching (pattern, str, length, from, to) result(res)
from_l = 0
to_l = 0

buff = pattern
if (pattern /= pattern_cache) then
buff = pattern
root => build_syntax_tree(tape, buff)

root => build_syntax_tree(tape, buff)

! call print_tree(root)
call cache%free()
call cache%init()

! call print_tree(root)

call cache%init()
call cache%build_nfa(root)

call cache%build_nfa(root)
! call cache%print_nfa()

call cache%convert_NFA_to_DFA()

! call cache%print_nfa()

call cache%convert_NFA_to_DFA()
! call cache%print_dfa()
pattern_cache = pattern
call deallocate_tree()

! call cache%print_dfa()

call cache%matching(char(10)//str//char(10), from_l, to_l)
end if

call cache%matching(char(10)//str//char(10), from_l, to_l)

if (is_there_caret_at_the_top(pattern)) then
from_l = from_l
Expand All @@ -200,10 +214,6 @@ function regex__matching (pattern, str, length, from, to) result(res)
if (present(to)) to = 0
end if

call deallocate_tree()

call cache%free()

end function regex__matching

!---------------------------------------------------------------------!
Expand Down

0 comments on commit 162f307

Please sign in to comment.