qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 15/21] qapi/parser: add docstrings


From: Markus Armbruster
Subject: Re: [PATCH v2 15/21] qapi/parser: add docstrings
Date: Wed, 19 May 2021 08:41:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)

John Snow <jsnow@redhat.com> writes:

> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  scripts/qapi/parser.py | 68 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 68 insertions(+)
>
> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
> index e80e0a7d965..ed543a2b7a4 100644
> --- a/scripts/qapi/parser.py
> +++ b/scripts/qapi/parser.py
> @@ -47,7 +47,27 @@ def __init__(self, parser: 'QAPISchemaParser', msg: str):
>  
>  
>  class QAPISchemaParser:
> +    """
> +    Performs syntactic parsing of a QAPI schema source file.

"Syntactic parsing" makes me wonder what non-syntactic parsing could be.

Also, PEP 257 wants imperative mood: "Perform X", not "Performs X".

What about a laconic "Parse QAPI schema source"?

>  
> +    Parses a JSON-esque schema file, See qapi-code-gen.txt section

Imperative mood, please.  Period, not comma.

> +    "Schema Syntax" for more information. Grammatical validation
> +    is handled later by `expr.check_exprs()`.

We could mention the processing of directives.  Perhaps:

       Parse a JSON-esque schema file.  See qapi-code-gen.txt section
       "Schema Syntax" for the exact syntax.  Also process directives.
       Grammatical validation is handled later by `expr.check_exprs()`.

What do you think?

> +
> +    :param fname: Source filename.
> +    :param previously_included:
> +        The absolute pathnames of previously included source files,

Either file name / filename (either spelling, but let's pick one), or
pathname, but not both, please.

Possible resolution:

       :param fname: Source file name.
       :param previously_included:
           The absolute names of previously included source files,

> +        if being invoked from another parser.
> +    :param incl_info:
> +       `QAPISourceInfo` belonging to the parent module.
> +       ``None`` implies this is the root module.
> +
> +    :ivar exprs: Resulting parsed expressions.
> +    :ivar docs: Resulting parsed documentation blocks.
> +
> +    :raise OSError: For problems opening the root schema document.

Hardly matters, but here we go: its both for open() and .read().  We
could say "reading" instead of "opening".

> +    :raise QAPIError: For syntactic or semantic parsing errors.

"Semantic parsing errors" sounds like "triangular squares" :)

I figure you wrote this because we're using both QAPIParseError and
QAPISemError.  The latter gets raised where we do more than just parse,
e.g. in directive processing.  It hardly matters, as we don't really
care for the difference between these error classes anywhere, and
pragmatically use whatever class is convenient.

Perhaps we should have a single class with multiple constructors
instead.  Even if yes, not now.

I recommend to gloss over (irrelevant) details and say "For parse
errors".  Yes, some of the errors aren't parse errors in the theory of
parsing sense, but I doubt readers care.  If *you* care, then maybe "For
errors in the schema source".  And then you might want to tweak the
OSError explanation to "For problems reading the root schema source
file".

> +    """
>      def __init__(self,
>                   fname: str,
>                   previously_included: Optional[Set[str]] = None,
> @@ -73,6 +93,11 @@ def __init__(self,
>          self._parse()
>  
>      def _parse(self) -> None:
> +        """
> +        Parse the QAPI schema document.
> +
> +        :return: None. Results are stored in ``.exprs`` and ``.docs``.
> +        """
>          cur_doc = None
>  
>          # May raise OSError; allow the caller to handle it.
> @@ -199,6 +224,49 @@ def check_list_str(name: str, value: object) -> 
> List[str]:
>              raise QAPISemError(info, "unknown pragma '%s'" % name)
>  
>      def accept(self, skip_comment: bool = True) -> None:
> +        """Read and store the next token.
> +
> +        :param skip_comment:
> +            When false, return COMMENT tokens ("#").
> +            This is used when reading documentation blocks.
> +
> +        :return:
> +            None. Several instance attributes are updated instead:
> +
> +            - ``.tok`` represents the token type. See below for values.
> +            - ``.info`` describes the token's source location.
> +            - ``.val`` is the token's value, if any. See below.
> +            - ``.pos`` is the buffer index of the first character of
> +              the token.
> +
> +        * Single-character tokens:
> +
> +            These are "{", "}", ":", ",", "[", and "]". ``.tok`` holds
> +            the single character and ``.val`` is None.
> +
> +        * Multi-character tokens:
> +
> +          * COMMENT:
> +
> +            This token is not normally returned by the lexer, but it can
> +            be when ``skip_comment`` is False. ``.tok`` is "#", and
> +            ``.val`` is a string including all chars until end-of-line,
> +            including the "#" itself.
> +
> +          * STRING:
> +
> +            ``.tok`` is "'", the single quote. ``.val`` contains the
> +            string, excluding the surrounding quotes.
> +
> +          * TRUE and FALSE:
> +
> +            ``.tok`` is either "t" or "f", ``.val`` will be the
> +            corresponding bool value.
> +
> +          * EOF:
> +
> +            ``.tok`` and ``.val`` will both be None at EOF.
> +        """
>          while True:
>              self.tok = self.src[self.cursor]
>              self.pos = self.cursor

This doc string is much better now, thanks!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]