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.
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 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 \ 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 \ 1237 :rtype: 'NoneType', ( `str`, `NoneType`, `str`) or ( `str`, \ 1241 if not cls
and require_cls:
1245 string_strip = string.strip()
1248 brackets_nospc = brackets.replace(
" ",
"")
1249 if not brackets_nospc:
1251 if len(brackets_nospc) % 2 == 1:
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:
1259 if not (string_strip.startswith(left)
and string_strip.endswith(right)):
1263 line = string_strip[bracket_len:-bracket_len].lstrip()
1264 if (
not line
and cls
and require_cls)
or (line
and not cls):
1266 if not line
and (
not cls
or not require_cls):
1267 return left,
None, right
1268 return left, cls(line), right
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.
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. 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))
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" 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" 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])