psyclone.parse

This directory contains classes related to parsing Fortran.

Submodules

Classes

  • ModuleInfo: This class stores mostly cached information about modules: it stores

  • ModuleManager: This class implements a singleton that manages module

class psyclone.parse.ModuleInfo(name, filename)

This class stores mostly cached information about modules: it stores the original filename, if requested it will read the file and then caches the plain text file, and if required it will parse the file, and then cache the fparser AST.

Parameters:
  • name (str) – the module name.

  • filename (str) – the name of the source file that stores this module (including path).

Inheritance

Inheritance diagram of ModuleInfo
property filename
Returns:

the filename that contains the source code for this module.

Return type:

str

get_parse_tree()

Returns the fparser AST for this module. The first time, the file will be parsed by fparser using the Fortran 2008 standard. The AST is then cached for any future uses.

Returns:

the fparser AST for this module.

Return type:

fparser.two.Fortran2003.Program

get_source_code()

Returns the source code for the module. The first time, it will be read from the file, but the data is then cached.

Returns:

the source code.

Return type:

str

Raises:

ModuleInfoError – when the file cannot be read.

get_used_modules()

This function returns a set of all modules used in this module. Fortran intrinsic modules will be ignored. The information is based on the fparser parse tree of the module (since fparser can handle more files than PSyir, like LFRic’s constants_mod which has pre-processor directives).

Returns:

a set with all imported module names.

Return type:

Set[str]

get_used_symbols_from_modules()

This function returns information about which modules are used by this module, and also which symbols are imported. The return value is a dictionary with the used module name as key, and a set of all imported symbol names as value.

Returns:

a dictionary that gives for each module name the set of symbols imported from it.

Return type:

Dict[str, Set[str]]

class psyclone.parse.ModuleManager

This class implements a singleton that manages module dependencies.

Inheritance

Inheritance diagram of ModuleManager
add_search_path(directories, recursive=True)

If the directory is not already contained in the search path, add it. Directory can either be a string, in which case it is a single directory, or a list of directories, each one a string.

Parameters:
  • directories (Union[str, List[str]]) – the directory/directories to add.

  • recursive (bool) – whether recursively all subdirectories should be added to the search path.

static get()

Static function that if necessary creates and returns the singleton ModuleManager instance.

get_all_dependencies_recursively(all_mods)

This function collects recursively all module dependencies for any of the modules in the all_mods set. I.e. it will add all modules used by any module listed in all_mods, and any modules used by the just added modules etc. In the end, it will return a dictionary that for each module lists which modules it depends on. This dictionary will be complete, i.e. all modules that are required for the original set of modules (and that could be found) will be a key in the dictionary. It will include the original set of modules as well.

If a module cannot be found (e.g. its path was not given to the ModuleManager, or it might be a system module for which the sources are not available, a message will be printed, and this module will be ignored (i.e. not listed in any dependencies). # TODO 2120: allow a choice to abort or ignore.

Parameters:

all_mods (Set[str]) – the set of all modules for which to collect module dependencies.

Returns:

a dictionary with all modules that are required (directly or indirectly) for the modules in all_mods.

Return type:

Dict[str, Set[str]]

get_module_info(module_name)

This function returns the ModuleInformation for the specified module.

Parameters:

module_name (str) – name of the module.

Returns:

the filename that contains the module

Return type:

str

Raises:

FileNotFoundError – if the module_name is not found in either the cached data nor in the search path.

get_modules_in_file(filename)

This function returns the list of modules defined in the specified file. The base implementation assumes the use of the LFRic coding style: the file a_mod.f90 implements the module a_mod. This function can be implemented in a derived class to actually parse the source file if required.

Parameters:

filename (str) – the file name for which to find the list of modules it contains.

Returns:

the list of all modules contained in the specified file.

Return type:

List[str]

static sort_modules(module_dependencies)

This function sorts the given dependencies so that all dependencies of a module are before any module that needs it. Input is a dictionary that contains all modules to be sorted as keys, and the value for each module is the set of dependencies that the module depends on.

Parameters:

module_dependencies (Dict[str, Set[str]]) – the list of modules required as keys, with all their dependencies as value.

Returns:

the sorted list of modules.

Return type:

List[str]

Exceptions

  • ModuleInfoError: PSyclone-specific exception for use when an error with the module manager

exception psyclone.parse.ModuleInfoError(value)

PSyclone-specific exception for use when an error with the module manager happens - typically indicating that some module information cannot be found.

Parameters:

value (str) – the message associated with the error.

Inheritance

Inheritance diagram of ModuleInfoError