22#
33# grammar.py
44"""
5- The expression grammar for ``coverage_pyver_pragma``.
6-
75.. versionadded:: 0.2.0
86
9- Each expression consists of one or more tags (``VERSION_TAG``, ``PLATFORM_TAG`` or ``IMPLEMENTATION_TAG``).
7+ As with ``coverage.py``, lines are marked with comments in the form::
8+
9+ # pragma: no cover
10+
11+ With ``coverage_pyver_pragma``, the comment may be followed with an expression enclosed in parentheses::
12+
13+ # pragma: no cover (<=py38 and !Windows)
14+
15+ Each expression consists of one or more tags
16+ (:py:data:`VERSION_TAG`, :py:data:`PLATFORM_TAG` or :py:data:`IMPLEMENTATION_TAG`).
1017The tags can be joined with the keywords ``AND``, ``OR`` and ``NOT``, with the exclamation mark ``!`` implying ``NOT``.
1118Parentheses can be used to group sub expressions.
19+ A series of tags without keywords in between them are evaluated with ``AND``.
1220
13- ``VERSION_TAG``
14- -------------------
21+ .. py:data:: VERSION_TAG
1522
1623A ``VERSION_TAG`` comprises an optional comparator (one of ``<=``, ``<``, ``>=``, ``>``),
1724a version specifier in the form ``pyXX``, and an optional ``+`` to indicate ``>=``.
2734 py34+ # equivalent to >=py34
2835
2936
30- ``PLATFORM_TAG``
31- -------------------
37+ .. py:data:: PLATFORM_TAG
3238
3339A ``PLATFORM_TAG`` comprises a single word which will be compared (ignoring case)
3440with the output of :func:`platform.system`.
4551If the current platform cannot be determined all strings are treated as :py:obj:`True`.
4652
4753
48- ``IMPLEMENTATION_TAG``
49- -----------------------
54+ .. py:data:: IMPLEMENTATION_TAG
5055
5156An ``IMPLEMENTATION_TAG`` comprises a single word which will be compared (ignoring case)
5257with the output of :func:`platform.python_implementation`.
6368Examples
6469-----------
6570
66- .. parsed-literal::
71+ ignore if the Python version is less than or equal to 3.7::
72+
73+ # pragma: no cover (<=py37)
74+
75+ ignore if running on Python 3.9::
76+
77+ # pragma: no cover (py39)
78+
79+ Ignore if the Python version is greater than 3.6 and it's not running on PyPy::
80+
81+ # pragma: no cover (>py36 and !PyPy)
6782
68- >py36 and !PyPy
69- Windows and <py38
83+ Ignore if the Python version is less than 3.8 and it's running on Windows::
84+
85+ # pragma: no cover (Windows and <py38)
86+
87+ Ignore when not running on macOS (Darwin)::
88+
89+ # pragma: no cover (!Darwin)
90+
91+ Ignore when not running on CPython::
92+
93+ # pragma: no cover (!CPython)
94+
95+ API Reference
96+ ----------------
7097
7198"""
7299#
106133 Literal ,
107134 OneOrMore ,
108135 Optional ,
136+ ParserElement ,
109137 ParseResults ,
110138 Word ,
111139 infixNotation ,
@@ -200,8 +228,8 @@ class PlatformTag(str):
200228
201229 __slots__ = ()
202230
203- def __new__ (cls , token : ParseResults ): # noqa: D102
204- return super ().__new__ (cls , str (token ["platform" ]))
231+ def __new__ (cls , tokens : ParseResults ): # noqa: D102
232+ return super ().__new__ (cls , str (tokens ["platform" ]))
205233
206234 def __repr__ (self ) -> str : # pragma: no cover
207235 return f"<{ self .__class__ .__name__ } ({ str (self )!r} )>"
@@ -353,7 +381,7 @@ def __bool__(self):
353381
354382ELEMENTS = VERSION_TAG | PLATFORM_TAG | IMPLEMENTATION_TAG
355383
356- GRAMMAR = OneOrMore (
384+ GRAMMAR : ParserElement = OneOrMore (
357385 infixNotation (
358386 ELEMENTS ,
359387 [
@@ -363,3 +391,8 @@ def __bool__(self):
363391 ]
364392 )
365393 )
394+ """
395+ The ``coverage_pyver_pragma`` expression grammar.
396+
397+ This can be used to parse an expression outside of the coverage context.
398+ """
0 commit comments