[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 06/15] qapi/parser: enforce all top-level expressions must be
From: |
John Snow |
Subject: |
[PATCH v3 06/15] qapi/parser: enforce all top-level expressions must be dict in _parse() |
Date: |
Wed, 19 May 2021 14:39:42 -0400 |
Instead of using get_expr nested=False, allow get_expr to always return
any expression. In exchange, add a new error message to the top-level
parser that explains the semantic error: Top-level expressions must
always be JSON objects.
This helps mypy understand the rest of this function which assumes that
get_expr did indeed return a dict.
The exception type changes from QAPIParseError to QAPISemError as a
result, and the error message in two tests now changes.
Signed-off-by: John Snow <jsnow@redhat.com>
---
Thanks Markus, I like this quite a bit better. I think I swung the
pendulum back too far away from "try not to change anything". This is
cleaner.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 14 ++++++++------
tests/qapi-schema/non-objects.err | 2 +-
tests/qapi-schema/quoted-structural-chars.err | 2 +-
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 0bc852eda72..ffdd4298b6b 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -78,7 +78,11 @@ def _parse(self):
self.docs.append(cur_doc)
continue
- expr = self.get_expr(False)
+ expr = self.get_expr()
+ if not isinstance(expr, dict):
+ raise QAPISemError(
+ info, "top-level expression must be an object")
+
if 'include' in expr:
self.reject_expr_doc(cur_doc)
if len(expr) != 1:
@@ -251,7 +255,7 @@ def get_members(self):
self.accept()
if key in expr:
raise QAPIParseError(self, "duplicate key '%s'" % key)
- expr[key] = self.get_expr(True)
+ expr[key] = self.get_expr()
if self.tok == '}':
self.accept()
return expr
@@ -270,7 +274,7 @@ def get_values(self):
raise QAPIParseError(
self, "expected '{', '[', ']', string, or boolean")
while True:
- expr.append(self.get_expr(True))
+ expr.append(self.get_expr())
if self.tok == ']':
self.accept()
return expr
@@ -278,9 +282,7 @@ def get_values(self):
raise QAPIParseError(self, "expected ',' or ']'")
self.accept()
- def get_expr(self, nested):
- if self.tok != '{' and not nested:
- raise QAPIParseError(self, "expected '{'")
+ def get_expr(self):
if self.tok == '{':
self.accept()
expr = self.get_members()
diff --git a/tests/qapi-schema/non-objects.err
b/tests/qapi-schema/non-objects.err
index 3a4ea36966e..23bdb69c711 100644
--- a/tests/qapi-schema/non-objects.err
+++ b/tests/qapi-schema/non-objects.err
@@ -1 +1 @@
-non-objects.json:1:1: expected '{'
+non-objects.json:1: top-level expression must be an object
diff --git a/tests/qapi-schema/quoted-structural-chars.err
b/tests/qapi-schema/quoted-structural-chars.err
index 07d1561d1f7..af6c1e173db 100644
--- a/tests/qapi-schema/quoted-structural-chars.err
+++ b/tests/qapi-schema/quoted-structural-chars.err
@@ -1 +1 @@
-quoted-structural-chars.json:1:1: expected '{'
+quoted-structural-chars.json:1: top-level expression must be an object
--
2.30.2
- [PATCH v3 07/15] qapi/parser: assert object keys are strings, (continued)
- [PATCH v3 07/15] qapi/parser: assert object keys are strings, John Snow, 2021/05/19
- [PATCH v3 15/15] qapi/parser: add docstrings, John Snow, 2021/05/19
- [PATCH v3 02/15] qapi: Add test for nonexistent schema file, John Snow, 2021/05/19
- [PATCH v3 04/15] qapi/parser: factor parsing routine into method, John Snow, 2021/05/19
- [PATCH v3 03/15] qapi/source: Remove line number from QAPISourceInfo initializer, John Snow, 2021/05/19
- [PATCH v3 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, John Snow, 2021/05/19
- [PATCH v3 10/15] qapi/parser: Fix token membership tests when token can be None, John Snow, 2021/05/19
- [PATCH v3 14/15] qapi/parser: allow 'ch' variable name, John Snow, 2021/05/19
- [PATCH v3 09/15] qapi: add must_match helper, John Snow, 2021/05/19
- [PATCH v3 08/15] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/05/19
- [PATCH v3 06/15] qapi/parser: enforce all top-level expressions must be dict in _parse(),
John Snow <=
- [PATCH v3 12/15] qapi/parser: add type hint annotations, John Snow, 2021/05/19
- [PATCH v3 13/15] qapi/parser: Remove superfluous list comprehension, John Snow, 2021/05/19
- Re: [PATCH v3 00/15] qapi: static typing conversion, pt5a, Markus Armbruster, 2021/05/20