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

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, op_pattern, rhs_cls, string, right=True, exclude_op_pattern=None)
 

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

binary-op-base is lhs op rhs

Splits the input text into text to the left of the matched
operator and text to the right of the matched operator and tries
to match the lhs text with the supplied lhs class rule and the rhs
text with the supplied rhs class rule.

Definition at line 965 of file utils.py.

Member Function Documentation

◆ match()

def fparser.two.utils.BinaryOpBase.match (   lhs_cls,
  op_pattern,
  rhs_cls,
  string,
  right = True,
  exclude_op_pattern = None 
)
static
Matches the binary-op-base rule.

If the operator defined by argument 'op_pattern' is found in
the string provided in argument 'string' then the text to the
left-hand-side of the operator is matched with the class rule
provided in the 'lhs_cls' argument and the text to the
right-hand-side of the operator is matched with the class rule
provided in the 'rhs_cls' argument.

If the optional 'right' argument is set to true (the default)
then, in the case where the pattern matches multiple times in
the input string, the right-most match will be chosen. If the
'right' argument is set to false then the left-most match will
be chosen.

if a pattern is provided to the optional 'exclude_op_pattern'
argument then there will be no match if the pattern matched by
the 'op_pattern' argument also matches this pattern. The
default (None) does nothing.

:param lhs_cls: an fparser2 object representing the rule that \
    should be matched to the lhs text.
:type lhs_cls: subclass of :py:class:`fparser.two.utils.Base`
:param op_pattern: the pattern to match.
:type op_pattern: `str` or \
    :py:class:`fparser.two.pattern_tools.Pattern`
:param rhs_cls: an fparser2 object representing the rule that \
    should be matched to the rhs text.
:type rhs_cls: subclass of :py:class:`fparser.two.utils.Base`
:param str string: the string to match with the pattern and \
    lhs and rhs rules.
:param bool right: in the case where there are multiple \
    matches to the pattern in the string this optional \
    argument specifies whether the righmost pattern match \
    should be chosen (True, the default) or whether the \
    leftmost pattern should be chosen (False).
:param exclude_op_pattern: optional argument which specifies a \
    particular subpattern to exclude from the match. Defaults \
    to None which means there is no subpattern.
:type exclude_op_pattern: :py:class:`fparser.two.pattern_tools.Pattern`

:returns: a tuple containing the matched lhs, the operator and \
    the matched rhs of the input string or None if there is \
    no match.
:rtype: (:py:class:`fparser.two.utils.Base`, str, \
    :py:class:`fparser.two.utils.Base`) or NoneType

Definition at line 978 of file utils.py.

978  ):
979  """Matches the binary-op-base rule.
980 
981  If the operator defined by argument 'op_pattern' is found in
982  the string provided in argument 'string' then the text to the
983  left-hand-side of the operator is matched with the class rule
984  provided in the 'lhs_cls' argument and the text to the
985  right-hand-side of the operator is matched with the class rule
986  provided in the 'rhs_cls' argument.
987 
988  If the optional 'right' argument is set to true (the default)
989  then, in the case where the pattern matches multiple times in
990  the input string, the right-most match will be chosen. If the
991  'right' argument is set to false then the left-most match will
992  be chosen.
993 
994  if a pattern is provided to the optional 'exclude_op_pattern'
995  argument then there will be no match if the pattern matched by
996  the 'op_pattern' argument also matches this pattern. The
997  default (None) does nothing.
998 
999  :param lhs_cls: an fparser2 object representing the rule that \
1000  should be matched to the lhs text.
1001  :type lhs_cls: subclass of :py:class:`fparser.two.utils.Base`
1002  :param op_pattern: the pattern to match.
1003  :type op_pattern: `str` or \
1004  :py:class:`fparser.two.pattern_tools.Pattern`
1005  :param rhs_cls: an fparser2 object representing the rule that \
1006  should be matched to the rhs text.
1007  :type rhs_cls: subclass of :py:class:`fparser.two.utils.Base`
1008  :param str string: the string to match with the pattern and \
1009  lhs and rhs rules.
1010  :param bool right: in the case where there are multiple \
1011  matches to the pattern in the string this optional \
1012  argument specifies whether the righmost pattern match \
1013  should be chosen (True, the default) or whether the \
1014  leftmost pattern should be chosen (False).
1015  :param exclude_op_pattern: optional argument which specifies a \
1016  particular subpattern to exclude from the match. Defaults \
1017  to None which means there is no subpattern.
1018  :type exclude_op_pattern: :py:class:`fparser.two.pattern_tools.Pattern`
1019 
1020  :returns: a tuple containing the matched lhs, the operator and \
1021  the matched rhs of the input string or None if there is \
1022  no match.
1023  :rtype: (:py:class:`fparser.two.utils.Base`, str, \
1024  :py:class:`fparser.two.utils.Base`) or NoneType
1025 
1026  """
1027  line, repmap = string_replace_map(string)
1028 
1029  if isinstance(op_pattern, str):
1030  if right:
1031  text_split = line.rsplit(op_pattern, 1)
1032  else:
1033  text_split = line.split(op_pattern, 1)
1034  if len(text_split) != 2:
1035  return None
1036  lhs, rhs = text_split[0].rstrip(), text_split[1].lstrip()
1037  oper = op_pattern
1038  else:
1039  if right:
1040  text_split = op_pattern.rsplit(line)
1041  else:
1042  text_split = op_pattern.lsplit(line)
1043  if not text_split or len(text_split) != 3:
1044  return None
1045  lhs, oper, rhs = text_split
1046  lhs = lhs.rstrip()
1047  rhs = rhs.lstrip()
1048  oper = oper.upper()
1049  if not lhs or not rhs:
1050  return None
1051  if exclude_op_pattern and exclude_op_pattern.match(oper):
1052  return None
1053 
1054  # Matching the shorter text first can be much more efficient
1055  # for complex expressions.
1056  if right:
1057  # The split is closest to the right so try to match the
1058  # RHS first.
1059  rhs_obj = rhs_cls(repmap(rhs))
1060  lhs_obj = lhs_cls(repmap(lhs))
1061  else:
1062  # The split is closest to the left so try to match the LHS
1063  # first.
1064  lhs_obj = lhs_cls(repmap(lhs))
1065  rhs_obj = rhs_cls(repmap(rhs))
1066 
1067  return (lhs_obj, oper.replace(" ", ""), rhs_obj)
1068 
Here is the caller graph for this function:

◆ tostr()

def fparser.two.utils.BinaryOpBase.tostr (   self)
Return the string representation of this object. Uses join() which
is efficient and can make a big performance difference for
complex expressions.

:returns: the string representation of this object.
:rtype: str

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

1069  def tostr(self):
1070  """Return the string representation of this object. Uses join() which
1071  is efficient and can make a big performance difference for
1072  complex expressions.
1073 
1074  :returns: the string representation of this object.
1075  :rtype: str
1076 
1077  """
1078  return " ".join([str(self.items[0]), str(self.items[1]), str(self.items[2])])
1079 
1080 
Here is the caller graph for this function:

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