Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions scripts/fortran_tools/parse_fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,37 +671,36 @@ def parse_fortran_var_decl(line, source, run_env):
>>> _DUMMY_RUN_ENV = CCPPFrameworkEnv(None, ndict={'host_files':'', \
'scheme_files':'', \
'suites':''})
>>> parse_fortran_var_decl("integer :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('local_name')
>>> parse_fortran_var_decl("integer :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('local_name')
'foo'
>>> parse_fortran_var_decl("integer :: foo = 0", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('local_name')
>>> parse_fortran_var_decl("integer :: foo = 0", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('local_name')
'foo'
>>> parse_fortran_var_decl("integer :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('optional')
>>> parse_fortran_var_decl("integer :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('optional')
False
>>> parse_fortran_var_decl("integer, optional :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('optional')
>>> parse_fortran_var_decl("integer, optional :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('optional')
'True'
>>> parse_fortran_var_decl("integer, dimension(:) :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("integer, dimension(:) :: foo", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(:)'
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(bar)", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(bar)", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(bar)'
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(:,:), baz", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(:,:), baz", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(:,:)'
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(:,:), baz", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[1].get_prop_value('dimensions')
>>> parse_fortran_var_decl("integer, dimension(:) :: foo(:,:), baz", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][1].get_prop_value('dimensions')
'(:)'
>>> parse_fortran_var_decl("real (kind=kind_phys), pointer :: phii (:,:) => null() !< interface geopotential height", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("real (kind=kind_phys), pointer :: phii (:,:) => null() !< interface geopotential height", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(:,:)'
>>> parse_fortran_var_decl("real(kind=kind_phys), dimension(im, levs, ntrac), intent(in) :: qgrs", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("real(kind=kind_phys), dimension(im, levs, ntrac), intent(in) :: qgrs", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(im, levs, ntrac)'
>>> parse_fortran_var_decl("character(len=*), intent(out) :: errmsg", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('local_name')
>>> parse_fortran_var_decl("character(len=*), intent(out) :: errmsg", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('local_name')
'errmsg'
>>> parse_fortran_var_decl("character(len=512), intent(out) :: errmsg", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('kind')
>>> parse_fortran_var_decl("character(len=512), intent(out) :: errmsg", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('kind')
'len=512'
>>> parse_fortran_var_decl("real(kind_phys), intent(out) :: foo(8)", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
>>> parse_fortran_var_decl("real(kind_phys), intent(out) :: foo(8)", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_prop_value('dimensions')
'(8)'
>>> parse_fortran_var_decl("real(kind_phys), intent(out) :: foo(8)", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_dimensions()
>>> parse_fortran_var_decl("real(kind_phys), intent(out) :: foo(8)", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0][0].get_dimensions()
['8']
>>> parse_fortran_var_decl("character(len=*), intent(out) :: errmsg", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('local_name') #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
ParseSyntaxError: Invalid variable declaration, character(len=*), intent(out) :: errmsg, intent not allowed in module variable, in <standard input>
>>> parse_fortran_var_decl("character(len=*), intent(out) :: errmsg", ParseSource('foo.F90', 'module', ParseContext()), _DUMMY_RUN_ENV)[1][0]
'Syntax error: Invalid variable declaration, character(len=*), intent(out) :: errmsg, intent not allowed in module variable, in <standard input>'

## NB: Expressions (including function calls) not currently supported here
#>>> parse_fortran_var_decl("real(kind_phys), intent(out) :: foo(size(bar))", ParseSource('foo.F90', 'scheme', ParseContext()), _DUMMY_RUN_ENV)[0].get_prop_value('dimensions')
Expand All @@ -714,7 +713,9 @@ def parse_fortran_var_decl(line, source, run_env):
sline = sline[0:sline.index('!')].rstrip()
# end if
tobject = ftype_factory(sline, context)
newvars = list()
newvars = []
errors = []
errtyp = "Syntax error"
if tobject is not None:
varprops = sline[tobject.type_len:].strip()
def_dims = None # Default dimensions
Expand All @@ -728,17 +729,14 @@ def parse_fortran_var_decl(line, source, run_env):
if prop[0:6] == 'intent':
if source.ptype != 'scheme':
typ = source.ptype
errmsg = 'Invalid variable declaration, {}, intent'
errmsg = errmsg + ' not allowed in {} variable'
ctx = context_string(context)
emsg1 = f"Invalid variable declaration, {sline}, "
emsg2 = f"intent not allowed in {typ} variable"
errmsg = f"{errtyp}: {emsg1}{emsg2}{ctx}"
if run_env.logger is not None:
ctx = context_string(context)
errmsg = "WARNING: " + errmsg + "{}"
run_env.logger.warning(errmsg.format(sline,
typ, ctx))
else:
raise ParseSyntaxError(errmsg.format(sline, typ),
context=context)
run_env.logger.warning(errmsg)
# end if
errors.append(errmsg)
else:
intent = prop[6:].strip()[1:-1].strip()
# end if
Expand Down Expand Up @@ -771,14 +769,12 @@ def parse_fortran_var_decl(line, source, run_env):
varname = var[0:ploc].strip()
begin, end = check_balanced_paren(var)
if (begin < 0) or (end < 0):
ctx = context_string(context)
errmsg = f"{errtyp}: Invalid variable declaration, {var}{ctx}"
if run_env.logger is not None:
ctx = context_string(context)
errmsg = "WARNING: Invalid variable declaration, {}{}"
run_env.logger.warning(errmsg.format(var, ctx))
else:
raise ParseSyntaxError('variable declaration',
token=var, context=context)
run_env.logger.warning(errmsg)
# end if
errors.append(errmsg)
else:
dimspec = var[begin:end+1]
# end if
Expand Down Expand Up @@ -813,12 +809,16 @@ def parse_fortran_var_decl(line, source, run_env):
# XXgoldyXX: I am nervous about allowing invalid Var objects here
# Also, this tends to cause an exception that ends up back here
# which is not a good idea.
var = FortranVar(prop_dict, source, run_env)
newvars.append(var)
try:
var = FortranVar(prop_dict, source, run_env)
newvars.append(var)
except ParseSyntaxError as perr:
errors.append(perr)
# end try
# end for
# No else (not a variable declaration)
# end if
return newvars
return newvars, errors

########################################################################
# Future classes
Expand Down
Loading