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

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 (brackets, cls, string, require_cls=True)
 

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

bracket-base is left-bracket something right-bracket.

This class is able to cope with nested brackets as long as they
are correctly nested. Brackets in strings are ignored.

The 'something' can be specified as being optional.

Definition at line 1208 of file utils.py.

Member Function Documentation

◆ match()

def fparser.two.utils.BracketBase.match (   brackets,
  cls,
  string,
  require_cls = True 
)
static
A generic match method for all types of bracketed
expressions.

:param str brackets: the format of the left and right brackets \
provided as a string, for example '()'
:param cls: the class to match the content within the brackets \
:type cls: subclass of :py:class:`fparser.two.utils.Base`
:param str string: the content to match
:param bool require_cls: whether the class and associated \
content is mandatory (True) or optional (False). The default \
is True.
:return: None if there is no match, otherwise a tuple with the \
first and third entries being strings containing the left and \
right brackets respectively and the second entry being either \
None or an instance of the class provided as the second \
argument (cls).
:rtype: 'NoneType', ( `str`, `NoneType`, `str`) or ( `str`, \
`cls`, `str` )

Definition at line 1220 of file utils.py.

1220  def match(brackets, cls, string, require_cls=True):
1221  """A generic match method for all types of bracketed
1222  expressions.
1223 
1224  :param str brackets: the format of the left and right brackets \
1225  provided as a string, for example '()'
1226  :param cls: the class to match the content within the brackets \
1227  :type cls: subclass of :py:class:`fparser.two.utils.Base`
1228  :param str string: the content to match
1229  :param bool require_cls: whether the class and associated \
1230  content is mandatory (True) or optional (False). The default \
1231  is True.
1232  :return: None if there is no match, otherwise a tuple with the \
1233  first and third entries being strings containing the left and \
1234  right brackets respectively and the second entry being either \
1235  None or an instance of the class provided as the second \
1236  argument (cls).
1237  :rtype: 'NoneType', ( `str`, `NoneType`, `str`) or ( `str`, \
1238  `cls`, `str` )
1239 
1240  """
1241  if not cls and require_cls:
1242  return None
1243  if not string:
1244  return None
1245  string_strip = string.strip()
1246  if not brackets:
1247  return None
1248  brackets_nospc = brackets.replace(" ", "")
1249  if not brackets_nospc:
1250  return None
1251  if len(brackets_nospc) % 2 == 1:
1252  # LHS and RHS bracketing must be the same size
1253  return None
1254  bracket_len = len(brackets_nospc) // 2
1255  left = brackets_nospc[:bracket_len]
1256  right = brackets_nospc[-bracket_len:]
1257  if len(string_strip) < bracket_len * 2:
1258  return None
1259  if not (string_strip.startswith(left) and string_strip.endswith(right)):
1260  return None
1261  # Check whether or not there's anything between the open
1262  # and close brackets
1263  line = string_strip[bracket_len:-bracket_len].lstrip()
1264  if (not line and cls and require_cls) or (line and not cls):
1265  return None
1266  if not line and (not cls or not require_cls):
1267  return left, None, right
1268  return left, cls(line), right
1269 
Here is the caller graph for this function:

◆ tostr()

def fparser.two.utils.BracketBase.tostr (   self)
:raises InternalError: if the internal items list variable is \
not the expected size.
:raises InternalError: if the first element of the internal \
items list is None or is an empty string.

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

1270  def tostr(self):
1271  """
1272  :raises InternalError: if the internal items list variable is \
1273  not the expected size.
1274  :raises InternalError: if the first element of the internal \
1275  items list is None or is an empty string.
1276  """
1277 
1278  if len(self.items) != 3:
1279  raise InternalError(
1280  "Class BracketBase method tostr() has '{0}' items, "
1281  "but expecting 3.".format(len(self.items))
1282  )
1283  if not self.items[0]:
1284  raise InternalError(
1285  "Class BracketBase method tostr(). 'Items' entry 0 "
1286  "should be a string containing the left hand bracket "
1287  "but it is empty or None"
1288  )
1289  if not self.items[2]:
1290  raise InternalError(
1291  "Class BracketBase method tostr(). 'Items' entry 2 "
1292  "should be a string containing the right hand bracket "
1293  "but it is empty or None"
1294  )
1295  if self.items[1] is None:
1296  return "{0}{1}".format(self.items[0], self.items[2])
1297  return "{0}{1}{2}".format(self.items[0], self.items[1], self.items[2])
1298 
1299 
Here is the caller graph for this function:

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