diff mbox

[RFC,v2,2/7] qapi: Allow true, false and null in schema json

Message ID 1400576881-6954-3-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng May 20, 2014, 9:07 a.m. UTC
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 scripts/qapi.py | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

Comments

Eric Blake May 20, 2014, 7:20 p.m. UTC | #1
On 05/20/2014 03:07 AM, Fam Zheng wrote:
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  scripts/qapi.py | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)

I applied this patch and tried to break things.

Pre-patch,
{ 'random': tru } => foo.json:1:13: Stray "t"
{ 'random': true } => foo.json:1:13: Stray "t"
{ 'random': truest } => foo.json:1:13: Stray "t"

Post-patch,
{ 'random': tru } => foo.json:1:14: Stray "r"
{ 'random': true } => accepted
{ 'random': truest } => foo.json:1:17: Stray "s"

Not ideal that you are reporting a different stray character based on
how much (1 or 4 bytes) that you tentatively already consumed.  Better
might be to report a stray word (all characters until the next
whitespace, starting at the first bare character rather than mid-word).

Are these cases tested in 3/7?
diff mbox

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 4c945ad..0f275f3 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -171,7 +171,20 @@  class QAPISchema:
                     return
                 except Exception, e:
                     raise QAPISchemaError(self, 'Invalid number "%s": %s' % (val, e))
-
+            elif self.tok in "tfn":
+                val = self.src[self.cursor - 1:]
+                if val.startswith("true"):
+                    self.val = True
+                    self.cursor += 3
+                    return
+                elif val.startswith("false"):
+                    self.val = False
+                    self.cursor += 4
+                    return
+                elif val.startswith("null"):
+                    self.val = None
+                    self.cursor += 3
+                    return
             elif self.tok == '\n':
                 if self.cursor == len(self.src):
                     self.tok = None
@@ -211,8 +224,8 @@  class QAPISchema:
         if self.tok == ']':
             self.accept()
             return expr
-        if not self.tok in "{['-0123456789":
-            raise QAPISchemaError(self, 'Expected "{", "[", "]", string or number')
+        if not self.tok in "{['-0123456789tfn":
+            raise QAPISchemaError(self, 'Expected "{", "[", "]", string, number, boolean or "null"')
         while True:
             expr.append(self.get_expr(True))
             if self.tok == ']':
@@ -231,10 +244,7 @@  class QAPISchema:
         elif self.tok == '[':
             self.accept()
             expr = self.get_values()
-        elif self.tok == "'":
-            expr = self.val
-            self.accept()
-        elif self.tok in "-0123456789":
+        elif self.tok in "'0123456789-tfn":
             expr = self.val
             self.accept()
         else: