diff options
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | docs/HowToUsePyparsing.rst | 21 | ||||
-rw-r--r-- | pyparsing/core.py | 6 |
3 files changed, 20 insertions, 10 deletions
@@ -80,6 +80,9 @@ help from Devin J. Pohly in structuring the code to enable this peaceful transit - Multiple added and corrected type annotations. With much help from Stephen Rosen, thanks! +- Some documentation and error message clarifications on pyparsing's + keyword logic, cited by Basil Peace. + - General docstring cleanup for Sphinx doc generation, PRs submitted by Devin J. Pohly. A dirty job, but someone has to do it - much appreciated! diff --git a/docs/HowToUsePyparsing.rst b/docs/HowToUsePyparsing.rst index 8301857..57e5a03 100644 --- a/docs/HowToUsePyparsing.rst +++ b/docs/HowToUsePyparsing.rst @@ -543,16 +543,21 @@ Basic ParserElement subclasses - ``max`` - indicating a maximum length of matching characters - - ``exact`` - indicating an exact length of matching characters + - ``exact`` - indicating an exact length of matching characters; + if ``exact`` is specified, it will override any values for ``min`` or ``max`` - If ``exact`` is specified, it will override any values for ``min`` or ``max``. + - ``as_keyword`` - indicating that preceding and following characters must + be whitespace or non-keyword characters - Sometimes you want to define a word using all - characters in a range except for one or two of them; you can do this - with the new ``exclude_chars`` argument. This is helpful if you want to define - a word with all ``printables`` except for a single delimiter character, such - as '.'. Previously, you would have to create a custom string to pass to Word. - With this change, you can just create ``Word(printables, exclude_chars='.')``. + - ``exclude_chars`` - a string of characters that should be excluded from + init_chars and body_chars + + Sometimes you want to define a word using all + characters in a range except for one or two of them; you can do this + with the ``exclude_chars`` argument. This is helpful if you want to define + a word with all ``printables`` except for a single delimiter character, such + as '.'. Previously, you would have to create a custom string to pass to Word. + With this change, you can just create ``Word(printables, exclude_chars='.')``. - ``Char`` - a convenience form of ``Word`` that will match just a single character from a string of matching characters:: diff --git a/pyparsing/core.py b/pyparsing/core.py index fecebb4..c466dbc 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -2449,8 +2449,8 @@ ParserElement._literalStringClass = Literal class Keyword(Token): """ Token to exactly match a specified string as a keyword, that is, - it must be immediately followed by a non-keyword character. Compare - with :class:`Literal`: + it must be immediately preceded and followed by whitespace or + non-keyword characters. Compare with :class:`Literal`: - ``Literal("if")`` will match the leading ``'if'`` in ``'ifAndOnlyIf'``. @@ -2838,6 +2838,8 @@ class Word(Token): self.errmsg = "Expected " + self.name self.mayIndexError = False self.asKeyword = asKeyword + if self.asKeyword: + self.errmsg += " as a keyword" # see if we can make a regex for this Word if " " not in (self.initChars | self.bodyChars): |