fparser Reference Guide  0.0.14
fparser.two.parser.ParserFactory Class Reference

Public Member Functions

def create (self, std=None)
 

Detailed Description

Creates a parser suitable for the specified Fortran standard.

Definition at line 95 of file parser.py.

Member Function Documentation

◆ create()

def fparser.two.parser.ParserFactory.create (   self,
  std = None 
)
Creates a class hierarchy suitable for the specified Fortran
standard. Also sets-up the list of classes that define scoping
regions in the global SymbolTables object and clears any existing
symbol table information.

:param str std: the Fortran standard. Choices are 'f2003' or \
        'f2008'. 'f2003' is the default.
:return: a Program class (not object) for use with the Fortran reader
:rtype: :py:class:`fparser.two.Fortran2003.Program`

:raises ValueError: if the supplied value for the std parameter \
            is invalid

For example:

>>> from fparser.two.parser import ParserFactory
>>> f2003_parser = ParserFactory().create()
>>> f2003_parser = ParserFactory().create(std='f2003')
>>> f2008_parser = ParserFactory().create(std='f2008')
>>> # Assuming that a reader has already been created ...
>>> ast = f2008_parser(reader)
>>> print(ast)

Definition at line 98 of file parser.py.

References fparser.two.parser.ParserFactory._setup(), fparser.two.C99Preprocessor.Cpp_Pp_Tokens.subclass_names, fparser.two.Fortran2003.Comment.subclass_names, fparser.two.C99Preprocessor.Cpp_If_Stmt.subclass_names, fparser.two.Fortran2008.Executable_Construct_C201.subclass_names, fparser.two.C99Preprocessor.Cpp_Elif_Stmt.subclass_names, fparser.two.Fortran2008.Action_Stmt_C201.subclass_names, fparser.two.C99Preprocessor.Cpp_Else_Stmt.subclass_names, fparser.two.Fortran2008.Action_Stmt_C816.subclass_names, fparser.two.Fortran2008.Action_Stmt_C828.subclass_names, fparser.two.C99Preprocessor.Cpp_Endif_Stmt.subclass_names, fparser.two.C99Preprocessor.Cpp_Macro_Identifier_List.subclass_names, fparser.two.Fortran2003.Execution_Part_Construct_C201.subclass_names, fparser.two.C99Preprocessor.Cpp_Undef_Stmt.subclass_names, fparser.two.Fortran2003.Executable_Construct_C201.subclass_names, fparser.two.C99Preprocessor.Cpp_Warning_Stmt.subclass_names, fparser.two.Fortran2003.Action_Stmt_C201.subclass_names, fparser.two.Fortran2003.Action_Stmt_C802.subclass_names, fparser.two.Fortran2003.Action_Stmt_C824.subclass_names, fparser.two.Fortran2003.Digit_String.subclass_names, fparser.two.utils.Type_Declaration_StmtBase.subclass_names, fparser.two.Fortran2003.Dimension_Component_Attr_Spec.subclass_names, fparser.two.Fortran2003.Proc_Component_PASS_Arg_Name.subclass_names, fparser.two.Fortran2003.Binding_PASS_Arg_Name.subclass_names, fparser.two.Fortran2003.Generic_Binding.subclass_names, fparser.two.Fortran2003.Ac_Implied_Do.subclass_names, fparser.two.Fortran2003.Ac_Implied_Do_Control.subclass_names, fparser.two.Fortran2003.Ac_Do_Variable.subclass_names, fparser.two.Fortran2003.Object_Name_Deferred_Shape_Spec_List_Item.subclass_names, fparser.two.Fortran2003.Target_Entity_Decl.subclass_names, fparser.two.Fortran2003.Data_Ref.subclass_names, fparser.two.Fortran2003.Scalar_Char_Initialization_Expr.subclass_names, fparser.two.Fortran2003.Parenthesis.subclass_names, fparser.two.Fortran2003.Connect_Spec.subclass_names, fparser.two.Fortran2003.Io_Control_Spec_List.subclass_names, fparser.two.Fortran2003.Io_Control_Spec.subclass_names, fparser.two.Fortran2003.Data_Edit_Desc_C1002.subclass_names, fparser.two.Fortran2003.Main_Program0.subclass_names, fparser.two.Fortran2003.Function_Body.subclass_names, fparser.two.Fortran2003.Subroutine_Body.subclass_names, and fparser.two.Fortran2003.Prefix.subclass_names.

98  def create(self, std=None):
99  """Creates a class hierarchy suitable for the specified Fortran
100  standard. Also sets-up the list of classes that define scoping
101  regions in the global SymbolTables object and clears any existing
102  symbol table information.
103 
104  :param str std: the Fortran standard. Choices are 'f2003' or \
105  'f2008'. 'f2003' is the default.
106  :return: a Program class (not object) for use with the Fortran reader
107  :rtype: :py:class:`fparser.two.Fortran2003.Program`
108 
109  :raises ValueError: if the supplied value for the std parameter \
110  is invalid
111 
112  For example:
113 
114  >>> from fparser.two.parser import ParserFactory
115  >>> f2003_parser = ParserFactory().create()
116  >>> f2003_parser = ParserFactory().create(std='f2003')
117  >>> f2008_parser = ParserFactory().create(std='f2008')
118  >>> # Assuming that a reader has already been created ...
119  >>> ast = f2008_parser(reader)
120  >>> print(ast)
121 
122  """
123  # Clear any existing symbol tables.
124  SYMBOL_TABLES.clear()
125 
126  # find all relevant classes in our Fortran2003 file as we
127  # always need these.
128  # pylint: disable=import-outside-toplevel
129  from fparser.two import Fortran2003
130 
131  f2003_cls_members = get_module_classes(Fortran2003)
132  if not std:
133  # default to f2003.
134  std = "f2003"
135 
136  if std == "f2003":
137  # we already have our required list of classes so call _setup
138  # to setup our class hierarchy.
139  self._setup(f2003_cls_members)
140  # We can now specify which classes are taken as defining new
141  # scoping regions. Programs without the optional program-stmt
142  # are handled separately in the Fortran2003.Main_Program0 class.
143  SYMBOL_TABLES.scoping_unit_classes = [
144  Fortran2003.Module_Stmt,
145  Fortran2003.Subroutine_Stmt,
146  Fortran2003.Program_Stmt,
147  Fortran2003.Function_Stmt,
148  ]
149  # the class hierarchy has been set up so return the top
150  # level class that we start from when parsing Fortran code.
151  return Fortran2003.Program
152  if std == "f2008":
153  # we need to find all relevent classes in our Fortran2003
154  # and Fortran2008 files and then ensure that where classes
155  # have the same name we return the Fortran2008 class
156  # i.e. where Fortran2008 extends Fortran2003 we return
157  # Fortran2008.
158  # First find all Fortran2008 classes.
159  from fparser.two import Fortran2008
160 
161  f2008_cls_members = get_module_classes(Fortran2008)
162  # next add in Fortran2003 classes if they do not already
163  # exist as a Fortran2008 class.
164  f2008_class_names = [i[0] for i in f2008_cls_members]
165  for local_cls in f2003_cls_members:
166  if local_cls[0] not in f2008_class_names:
167  f2008_cls_members.append(local_cls)
168  # we now have our required list of classes so call _setup
169  # to setup our class hierarchy.
170  self._setup(f2008_cls_members)
171  # We can now specify which classes are taken as defining new
172  # scoping regions. Programs without the optional program-stmt
173  # are handled separately in the Fortran2003.Main_Program0 class.
174  SYMBOL_TABLES.scoping_unit_classes = [
175  Fortran2003.Module_Stmt,
176  Fortran2003.Subroutine_Stmt,
177  Fortran2003.Program_Stmt,
178  Fortran2003.Function_Stmt,
179  Fortran2008.Submodule_Stmt,
180  ]
181  # the class hierarchy has been set up so return the top
182  # level class that we start from when parsing Fortran
183  # code. Fortran2008 does not extend the top level class so
184  # we return the Fortran2003 one.
185  return Fortran2003.Program
186 
187  raise ValueError(f"'{std}' is an invalid standard")
188 
Here is the call graph for this function:

The documentation for this class was generated from the following file: