fparser.two.symbol_table

The fparser2 symbol-table module. Defines various classes as well as the single, global SYMBOL_TABLES instance. The latter is a container for all of the top-level scoping units encountered during parsing.

Module Contents

Classes

SymbolTables

Class encapsulating functionality for the global symbol-tables object.

SymbolTable

Class implementing a single symbol table.

Attributes

SYMBOL_TABLES

exception fparser.two.symbol_table.SymbolTableError[source]

Bases: Exception

Base class exception for symbol-table related errors.

class fparser.two.symbol_table.SymbolTables[source]

Class encapsulating functionality for the global symbol-tables object. This is a container for all symbol tables constructed while parsing code. All names are converted to lower case (since Fortran is not case sensitive).

property current_scope[source]
Returns:

the symbol table for the current scoping unit or None.

Return type:

fparser.two.symbol_table.SymbolTable or NoneType

__str__()[source]

Return str(self).

enable_checks(value)[source]

Sets whether or not to enable consistency checks in every symbol table that is created during a parse.

Parameters:

value (bool) – whether or not checks are enabled.

clear()[source]

Deletes any stored SymbolTables.

add(name, node=None)[source]

Add a new symbol table with the supplied name. The name will be converted to lower case if necessary.

Parameters:
  • name (str) – the name for the new table.

  • node (Optional[fparser.two.utils.Base]) – the node in the parse tree associated with this table.

Returns:

the new symbol table.

Return type:

fparser.two.symbol_table.SymbolTable

Raises:

SymbolTableError – if there is already an entry with the supplied name.

lookup(name)[source]

Find the named symbol table and return it.

Parameters:

name (str) – the name of the required symbol table (not case sensitive).

Returns:

the named symbol table.

Return type:

fparser.two.symbol_table.SymbolTable

enter_scope(name, node=None)[source]

Called when the parser enters a new scoping region (i.e. when it encounters one of the classes listed in _scoping_unit_classes). Sets the ‘current scope’ to be the symbol table with the supplied name. If we are not currently within a tree of scoping regions then a new entry is created in the internal dict of symbol tables. If there is an existing tree then a new table is created and added to the bottom.

Parameters:
  • name (str) – name of the scoping region.

  • node (Optional[fparser.two.utils.Base]) – the node of the parse tree associated with this region.

exit_scope()[source]

Marks the end of the processing of the current scoping unit. Since we are exiting the current scoping region, the new ‘current scoping region’ will be its parent.

Raises:

SymbolTableError – if there is no current scope from which to exit.

remove(name)[source]

Removes the named symbol table and any descendants it may have. When searching for the named table, the current scope takes priority followed by the list of top-level symbol tables.

Parameters:

name (str) – the name of the symbol table to remove (not case sensitive).

Raises:

SymbolTableError – if the named symbol table is not in the current scope or in the list of top-level symbol tables.

class fparser.two.symbol_table.SymbolTable(name, parent=None, checking_enabled=False, node=None)[source]

Class implementing a single symbol table.

Since this functionality is not yet fully mature, checks that new symbols don’t clash with existing symbols are disabled by default. Once #201 is complete it is planned to switch this so that the checks are instead enabled by default.

Parameters:
  • name (str) – the name of this scope. Will be the name of the associated module or routine.

  • parent (fparser.two.symbol_table.SymbolTable.Symbol) – the symbol table within which this one is nested (if any).

  • checking_enabled (bool) – whether or not validity checks are performed for symbols added to the table.

  • node (Optional[fparser.two.utils.Base]) – the node in the parse tree associated with this table.

Raises:

TypeError – if the supplied node is of the wrong type.

property name[source]
Returns:

the name of this symbol table (scoping region).

Return type:

str

property parent[source]
Returns:

the parent symbol table (scoping region) that contains this one (if any).

Return type:

fparser.two.symbol_table.SymbolTable or NoneType

property node[source]
Returns:

the scoping node (in the parse tree) asssociated with this SymbolTable.

Return type:

fparser.two.utils.Base

property children[source]
Returns:

the child (nested) symbol tables, if any.

Return type:

list of fparser.two.symbol_table.SymbolTable

property root[source]
Returns:

the top-level symbol table that contains the current scoping region (symbol table).

Return type:

fparser.two.symbol_table.SymbolTable

property wildcard_imports[source]
Returns:

names of all modules with wildcard imports into this scope or an empty list if there are none.

Return type:

List[Optional[str]]

property all_symbols_resolved[source]
Returns:

whether all symbols in this scope have been resolved. i.e. if there are any wildcard imports or this table is within a submodule then there could be symbols we don’t have definitions for.

Return type:

bool

Symbol[source]
__str__()[source]

Return str(self).

add_data_symbol(name, primitive_type)[source]

Creates a new Symbol with the specified properties and adds it to the symbol table. The supplied name is converted to lower case.

TODO #201 add support for other symbol properties (kind, shape and visibility).

Parameters:
  • name (str) – the name of the symbol.

  • primitive_type (str) – the primitive type of the symbol.

Raises:
  • TypeError – if any of the supplied parameters are of the wrong type.

  • SymbolTableError – if the symbol table already contains an entry with the supplied name.

add_use_symbols(name, only_list=None, rename_list=None)[source]

Creates an entry in the table for the USE of a module with the supplied name. If no only_list is supplied then this USE represents a wildcard import of all public symbols in the named module. If the USE statement has an ONLY clause but without any named symbols then only_list should be an empty list.

A USE can also have one or more rename entries without an only list.

Parameters:
  • name (str) – the name of the module being imported via a USE. Not case sensitive.

  • only_list (Optional[List[Tuple[str, str | NoneType]]]) – if there is an ‘only:’ clause on the USE statement then this contains a list of tuples, each holding the local name of the symbol and its name in the module from which it is imported. These names are case insensitive.

  • rename_list (Optional[List[Tuple[str, str]]]) – a list of symbols that are renamed from the scope being imported. Each entry is a tuple containing the name in the local scope and the corresponding name in the module from which it is imported. These names are case insensitive.

lookup(name)[source]

Lookup the symbol with the supplied name.

Parameters:

name (str) – the name of the symbol to lookup (not case sensitive).

Returns:

the named symbol.

Return type:

fparser.two.symbol_table.SymbolTable.Symbol

Raises:

KeyError – if the named symbol cannot be found in this or any parent scope.

add_child(child)[source]

Adds a child symbol table (scoping region nested within this one).

Parameters:

child (fparser.two.symbol_table.SymbolTable) – the nested symbol table.

Raises:

TypeError – if the supplied child is not a SymbolTable.

del_child(name)[source]

Removes the named symbol table.

Parameters:

name (str) – the name of the child symbol table to delete (not case sensitive).

Raises:

KeyError – if the named table is not a child of this one.

fparser.two.symbol_table.SYMBOL_TABLES[source]