Skip to content

Commit

Permalink
Merge pull request #274 from ecmwf-ifs/nabr-regex-fixes
Browse files Browse the repository at this point in the history
REGEX frontend: white space and nesting bugfix
  • Loading branch information
reuterbal authored Apr 11, 2024
2 parents e5eae78 + 92c0c5c commit 5c356ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
8 changes: 4 additions & 4 deletions loki/frontend/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ def __init__(self):
r'^module[ \t]+(?P<name>\w+)\b.*?$'
r'(?P<spec>.*?)'
r'(?P<contains>^contains\n(?:'
r'(?:[ \t\w()=]*?subroutine.*?^end[ \t]*subroutine\b(?:[ \t]\w+)?\n)|'
r'(?:[ \t\w()=]*?function.*?^end[ \t]*function\b(?:[ \t]\w+)?\n)|'
r'(?:[ \t\w()=]*?subroutine.*?^end[ \t]*subroutine\b(?:[ \t]*\w+)?\n)|'
r'(?:[ \t\w()=]*?function.*?^end[ \t]*function\b(?:[ \t]*\w+)?\n)|'
r'(?:^#\w+.*?\n)'
r')*)?'
r')*?)?'
r'^end[ \t]*module\b(?:[ \t](?P=name))?',
re.IGNORECASE | re.DOTALL | re.MULTILINE
)
Expand Down Expand Up @@ -473,7 +473,7 @@ def __init__(self):
r'(?:[ \t\w()=]*?subroutine.*?^end[ \t]*subroutine\b(?:[ \t]\w+)?\n)|'
r'(?:[ \t\w()=]*?function.*?^end[ \t]*function\b(?:[ \t]\w+)?\n)|'
r'(?:^#\w+.*?\n)'
r')*)?'
r')*?)?'
r'^end[ \t]*(?P=keyword)\b(?:[ \t](?P=name))?',
re.IGNORECASE | re.DOTALL | re.MULTILINE
)
Expand Down
44 changes: 35 additions & 9 deletions tests/test_frontends.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,13 @@ def test_regex_sourcefile_from_source():
m = 2
call routine_b(m, 6)
end subroutine module_routine
end subroutine module_routine
function module_function(n)
integer n
n = 3
end function module_function
integer module_function
module_function = n + 3
end function module_function
end module some_module
module other_module
Expand Down Expand Up @@ -565,28 +566,53 @@ def test_regex_sourcefile_from_source():
integer c
c = 8
end subroutine !add"£^£$
end subroutine routine_b
endsubroutine routine_b
function function_d(d)
integer d
d = 6
end function function_d
module last_module
implicit none
contains
subroutine last_routine1
call contained()
contains
subroutine contained
integer n
n = 1
end subroutine contained
end subroutine last_routine1
subroutine last_routine2
call contained2()
contains
subroutine contained2
integer m
m = 1
end subroutine contained2
end subroutine last_routine2
end module last_module
""".strip()

sourcefile = Sourcefile.from_source(fcode, frontend=REGEX)
assert [m.name for m in sourcefile.modules] == ['some_module', 'other_module']
assert [m.name for m in sourcefile.modules] == ['some_module', 'other_module', 'last_module']
assert [r.name for r in sourcefile.routines] == [
'routine_a', 'routine_b', 'function_d'
]
assert [r.name for r in sourcefile.all_subroutines] == [
'routine_a', 'routine_b', 'function_d', 'module_routine', 'module_function'
'routine_a', 'routine_b', 'function_d', 'module_routine', 'module_function',
'last_routine1', 'last_routine2'
]

assert len(r := sourcefile['last_module']['last_routine1'].routines) == 1 and r[0].name == 'contained'
assert len(r := sourcefile['last_module']['last_routine2'].routines) == 1 and r[0].name == 'contained2'

code = sourcefile.to_fortran()
assert code.count('SUBROUTINE') == 10
assert code.count('SUBROUTINE') == 18
assert code.count('FUNCTION') == 6
assert code.count('CONTAINS') == 2
assert code.count('MODULE') == 4
assert code.count('CONTAINS') == 5
assert code.count('MODULE') == 6


def test_regex_sourcefile_from_file(here):
Expand Down

0 comments on commit 5c356ca

Please sign in to comment.