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

Public Member Functions

def tostr (self)
 
def tostr_a (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 (keyword, cls, string, colons=False, require_cls=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

Base class to support situations where there is a keyword which is
optionally followed by further text, potentially separated by
'::'.

For example 'program fred', or 'import :: a,b'

WORD-cls is WORD [ [ :: ] cls ]

Definition at line 1596 of file utils.py.

Member Function Documentation

◆ match()

def fparser.two.utils.WORDClsBase.match (   keyword,
  cls,
  string,
  colons = False,
  require_cls = False 
)
static
Checks whether the content in string matches the expected
WORDClsBase format with 'keyword' providing the keyword, 'cls'
providing the following text, 'colons' specifying whether an
optional '::' is allowed as a separator between the keyword
and cls and 'require_cls' specifying whether cls must have
content or not.

Note, if the optional '::' is allowed and exists in the string
then 1) cls must also have content i.e. it implies
`require_cls=True` and 2) white space is not required between
the keyword and the '::' and the '::' and cls.

The simplest form of keyword pattern is a string. However this
method can also match more complex patterns as specified by
the Pattern class in pattern_tools.py. As patterns can be
built from combinations of other patterns (again see
pattern_tool.py) this method also supports a hierarchy of
lists and/or tuples of patterns.

:param keyword: the pattern of the WORD to match. This can be \
    a Pattern, string, list or tuple, with a list or tuple \
    containing one or more Pattern, string, list or tuple.
:type keyword: :py:class:`fparser.two.pattern_tools.Pattern`, \
    str, tuple of str/Pattern/tuple/list or list of \
    str/Pattern/tuple/list
:param cls: the class to match.
:type cls: a subclass of :py:class:`fparser.two.utils.Base`
:param str string: Text that we are trying to match.
:param bool colons: whether '::' is allowed as an optional \
    separator between between WORD and cls.
:param bool require_cls: whether content for cls is required \
    or not.

:returns: None if there is no match or, if there is a match, a \
    2-tuple containing a string matching the 'WORD' and an \
    instance of 'cls' (or None if an instance of cls is not \
    required and not provided).
:rtype: (str, cls or NoneType) or NoneType

Definition at line 1608 of file utils.py.

1608  def match(keyword, cls, string, colons=False, require_cls=False):
1609  """Checks whether the content in string matches the expected
1610  WORDClsBase format with 'keyword' providing the keyword, 'cls'
1611  providing the following text, 'colons' specifying whether an
1612  optional '::' is allowed as a separator between the keyword
1613  and cls and 'require_cls' specifying whether cls must have
1614  content or not.
1615 
1616  Note, if the optional '::' is allowed and exists in the string
1617  then 1) cls must also have content i.e. it implies
1618  `require_cls=True` and 2) white space is not required between
1619  the keyword and the '::' and the '::' and cls.
1620 
1621  The simplest form of keyword pattern is a string. However this
1622  method can also match more complex patterns as specified by
1623  the Pattern class in pattern_tools.py. As patterns can be
1624  built from combinations of other patterns (again see
1625  pattern_tool.py) this method also supports a hierarchy of
1626  lists and/or tuples of patterns.
1627 
1628  :param keyword: the pattern of the WORD to match. This can be \
1629  a Pattern, string, list or tuple, with a list or tuple \
1630  containing one or more Pattern, string, list or tuple.
1631  :type keyword: :py:class:`fparser.two.pattern_tools.Pattern`, \
1632  str, tuple of str/Pattern/tuple/list or list of \
1633  str/Pattern/tuple/list
1634  :param cls: the class to match.
1635  :type cls: a subclass of :py:class:`fparser.two.utils.Base`
1636  :param str string: Text that we are trying to match.
1637  :param bool colons: whether '::' is allowed as an optional \
1638  separator between between WORD and cls.
1639  :param bool require_cls: whether content for cls is required \
1640  or not.
1641 
1642  :returns: None if there is no match or, if there is a match, a \
1643  2-tuple containing a string matching the 'WORD' and an \
1644  instance of 'cls' (or None if an instance of cls is not \
1645  required and not provided).
1646  :rtype: (str, cls or NoneType) or NoneType
1647 
1648  """
1649  if isinstance(keyword, (tuple, list)):
1650  for child in keyword:
1651  try:
1652  obj = WORDClsBase.match(
1653  child, cls, string, colons=colons, require_cls=require_cls
1654  )
1655  except NoMatchError:
1656  obj = None
1657  if obj is not None:
1658  return obj
1659  return None
1660 
1661  if isinstance(keyword, str):
1662  line = string.lstrip()
1663  if line[: len(keyword)].upper() != keyword.upper():
1664  return None
1665  line = line[len(keyword) :]
1666  pattern_value = keyword
1667  else:
1668  my_match = keyword.match(string)
1669  if my_match is None:
1670  return None
1671  line = string[len(my_match.group()) :]
1672  pattern_value = keyword.value
1673 
1674  if not line:
1675  if require_cls:
1676  # no text found but it is required
1677  return None
1678  return pattern_value, None
1679  if isalnum(line[0]):
1680  return None
1681  line = line.lstrip()
1682  has_colons = False
1683  if colons and line.startswith("::"):
1684  has_colons = True
1685  line = line[2:].lstrip()
1686  if not line:
1687  if has_colons or require_cls:
1688  # colons without following content is not allowed.
1689  return None
1690  return pattern_value, None
1691  if cls is None:
1692  return None
1693  return pattern_value, cls(line)
1694 
Here is the caller graph for this function:

◆ tostr()

def fparser.two.utils.WORDClsBase.tostr (   self)
Convert the class into Fortran.

:return: String representation of this class without any \
 optional '::'.
:rtype: str

Definition at line 1695 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.

1695  def tostr(self):
1696  """Convert the class into Fortran.
1697 
1698  :return: String representation of this class without any \
1699  optional '::'.
1700  :rtype: str
1701 
1702  """
1703  if self.items[1] is None:
1704  return str(self.items[0])
1705  s = str(self.items[1])
1706  if s and s[0] in "(*":
1707  return "%s%s" % (self.items[0], s)
1708  return "%s %s" % (self.items[0], s)
1709 
Here is the caller graph for this function:

◆ tostr_a()

def fparser.two.utils.WORDClsBase.tostr_a (   self)
Convert the class into Fortran, adding in "::".

:return: String representation of this class including an \
 optional '::'.
:rtype: str

Definition at line 1710 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.

1710  def tostr_a(self):
1711  """Convert the class into Fortran, adding in "::".
1712 
1713  :return: String representation of this class including an \
1714  optional '::'.
1715  :rtype: str
1716 
1717  """
1718  if self.items[1] is None:
1719  return str(self.items[0])
1720  return "%s :: %s" % (self.items[0], self.items[1])
1721 
1722 

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