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.
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 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. 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. 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 \ 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 1649 if isinstance(keyword, (tuple, list)):
1650 for child
in keyword:
1652 obj = WORDClsBase.match(
1653 child, cls, string, colons=colons, require_cls=require_cls
1655 except NoMatchError:
1661 if isinstance(keyword, str):
1662 line = string.lstrip()
1663 if line[: len(keyword)].upper() != keyword.upper():
1665 line = line[len(keyword) :]
1666 pattern_value = keyword
1668 my_match = keyword.match(string)
1669 if my_match
is None:
1671 line = string[len(my_match.group()) :]
1672 pattern_value = keyword.value
1678 return pattern_value,
None 1679 if isalnum(line[0]):
1681 line = line.lstrip()
1683 if colons
and line.startswith(
"::"):
1685 line = line[2:].lstrip()
1687 if has_colons
or require_cls:
1690 return pattern_value,
None 1693 return pattern_value, cls(line)