fparser Reference Guide  0.0.14
fparser.two.utils.KeywordValueBase Class Reference
Inheritance diagram for fparser.two.utils.KeywordValueBase:
Collaboration diagram for fparser.two.utils.KeywordValueBase:

Public Member Functions

def tostr (self)
 
- Public Member Functions inherited from fparser.two.utils.Base
def __init__ (self, string, parent_cls=None)
 
def __new__ (cls, string, parent_cls=None)
 
def get_root (self)
 
def children (self)
 
def init (self, items)
 
def torepr (self)
 
def __str__ (self)
 
def __repr__ (self)
 
def tofortran (self, tab="", isfix=None)
 
def restore_reader (self, reader)
 
- Public Member Functions inherited from fparser.two.utils.ComparableMixin
def __lt__ (self, other)
 
def __le__ (self, other)
 
def __eq__ (self, other)
 
def __ge__ (self, other)
 
def __gt__ (self, other)
 
def __ne__ (self, other)
 

Static Public Member Functions

def match (lhs_cls, rhs_cls, string, require_lhs=True, upper_lhs=False)
 

Additional Inherited Members

- Public Attributes inherited from fparser.two.utils.Base
 parent
 
 items
 
- Static Public Attributes inherited from fparser.two.utils.Base
 subclasses
 

Detailed Description

keyword-value-base is [ lhs = ] rhs

where:

R215 keyword is name.

Definition at line 1121 of file utils.py.

Member Function Documentation

◆ match()

def fparser.two.utils.KeywordValueBase.match (   lhs_cls,
  rhs_cls,
  string,
  require_lhs = True,
  upper_lhs = False 
)
static
Attempts to match the supplied `string` with `lhs_cls` = `rhs_cls`.
If `lhs_cls` is a str then it is compared with the content to the
left of the first '=' character in `string`. If that content is a
valid Fortran name but does *not* match `lhs_cls` then the match
fails, irrespective of the setting of `require_lhs`.

:param lhs_cls: list, tuple or single value of classes to attempt to \
        match LHS against (in order), or string containing \
        keyword to match.
:type lhs_cls: names of classes deriving from `:py:class:Base` or str
:param rhs_cls: name of class to match RHS against.
:type rhs_cls: name of a class deriving from `:py:class:Base`
:param str string: text to be matched.
:param bool require_lhs: whether the expression to be matched must \
                 contain a LHS that is assigned to.
:param bool upper_lhs: whether or not to convert the LHS of the \
               matched expression to upper case.

:return: instances of the classes representing quantities on the LHS \
 and RHS (LHS is optional) or None if no match is found.
:rtype: 2-tuple of objects or NoneType

Definition at line 1133 of file utils.py.

References fparser.two.Fortran2003.Comment.items, fparser.one.statements.StatementWithNamelist.items, fparser.one.statements.Assign.items, fparser.one.statements.Call.items, fparser.one.statements.ComputedGoto.items, fparser.one.statements.AssignedGoto.items, fparser.two.utils.Base.items, fparser.one.statements.Print.items, fparser.one.statements.Read0.items, fparser.one.statements.Read1.items, fparser.one.typedecl_statements.Implicit.items, fparser.one.statements.Write.items, fparser.one.statements.Allocate.items, fparser.one.statements.Deallocate.items, fparser.one.statements.ModuleProcedure.items, fparser.one.statements.Access.items, fparser.one.statements.Save.items, fparser.one.statements.Nullify.items, fparser.one.statements.Use.items, fparser.one.statements.Parameter.items, fparser.one.statements.Equivalence.items, fparser.one.statements.Dimension.items, fparser.one.statements.Target.items, fparser.one.statements.Pointer.items, fparser.one.statements.Inquire.items, fparser.one.statements.Namelist.items, fparser.one.statements.Common.items, fparser.one.statements.Intent.items, fparser.one.statements.Entry.items, fparser.one.statements.GenericBinding.items, fparser.one.statements.Allocatable.items, fparser.one.statements.Bind.items, fparser.one.statements.Case.items, fparser.one.statements.TypeIs.items, fparser.one.statements.ClassIs.items, fparser.one.statements.Enumerator.items, and fparser.one.statements.Depend.items.

1133  def match(lhs_cls, rhs_cls, string, require_lhs=True, upper_lhs=False):
1134  """
1135  Attempts to match the supplied `string` with `lhs_cls` = `rhs_cls`.
1136  If `lhs_cls` is a str then it is compared with the content to the
1137  left of the first '=' character in `string`. If that content is a
1138  valid Fortran name but does *not* match `lhs_cls` then the match
1139  fails, irrespective of the setting of `require_lhs`.
1140 
1141  :param lhs_cls: list, tuple or single value of classes to attempt to \
1142  match LHS against (in order), or string containing \
1143  keyword to match.
1144  :type lhs_cls: names of classes deriving from `:py:class:Base` or str
1145  :param rhs_cls: name of class to match RHS against.
1146  :type rhs_cls: name of a class deriving from `:py:class:Base`
1147  :param str string: text to be matched.
1148  :param bool require_lhs: whether the expression to be matched must \
1149  contain a LHS that is assigned to.
1150  :param bool upper_lhs: whether or not to convert the LHS of the \
1151  matched expression to upper case.
1152 
1153  :return: instances of the classes representing quantities on the LHS \
1154  and RHS (LHS is optional) or None if no match is found.
1155  :rtype: 2-tuple of objects or NoneType
1156 
1157  """
1158  if require_lhs and "=" not in string:
1159  return None
1160  if isinstance(lhs_cls, (list, tuple)):
1161  for cls in lhs_cls:
1162  obj = KeywordValueBase.match(
1163  cls, rhs_cls, string, require_lhs=require_lhs, upper_lhs=upper_lhs
1164  )
1165  if obj:
1166  return obj
1167  return obj
1168  # We can't just blindly check whether 'string' contains an '='
1169  # character as it could itself hold a string constant containing
1170  # an '=', e.g. FMT='("Hello = False")'.
1171  # Therefore we only split on the left-most '=' character
1172  pieces = string.split("=", 1)
1173  lhs = None
1174  if len(pieces) == 2:
1175  # It does contain at least one '='. Proceed to attempt to match
1176  # the content on the LHS of it.
1177  lhs = pieces[0].strip()
1178  if isinstance(lhs_cls, str):
1179  # lhs_cls is a keyword
1180  if upper_lhs:
1181  lhs = lhs.upper()
1182  if lhs != lhs_cls:
1183  # The content to the left of the '=' does not match the
1184  # supplied keyword
1185  lhs = None
1186  else:
1187  lhs = lhs_cls(lhs)
1188  if not lhs:
1189  # We haven't matched the LHS and therefore proceed to treat the
1190  # whole string as a RHS if the LHS is not strictly required.
1191  if require_lhs:
1192  return None
1193  rhs = string.strip()
1194  else:
1195  rhs = pieces[-1].strip()
1196  if rhs:
1197  rhs = rhs_cls(rhs)
1198  if not rhs:
1199  return None
1200  return lhs, rhs
1201 
Here is the caller graph for this function:

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