diff mbox

[v2,2/2] qapi: Add a primitive to include other files from a QAPI schema file

Message ID 20140210195828.3495.73534.stgit@fimbulvetr.bsc.es
State New
Headers show

Commit Message

Lluís Vilanova Feb. 10, 2014, 7:58 p.m. UTC
Adds the "include(...)" primitive to the syntax of QAPI schema files.

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
---
 scripts/qapi.py |   24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

Comments

Eric Blake Feb. 10, 2014, 8:30 p.m. UTC | #1
On 02/10/2014 12:58 PM, Lluís Vilanova wrote:
> Adds the "include(...)" primitive to the syntax of QAPI schema files.
> 
> Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
> ---
>  scripts/qapi.py |   24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 

> +            elif self.tok == 'i':
> +                include_src = self.src[self.cursor-1:]
> +                include_match = include_cre.match(include_src)
> +                if include_match is not None:
> +                    include_path = os.path.join(self.input_dir,
> +                                                include_match.group(1))
> +                    if not os.path.isfile(include_path):
> +                        raise QAPISchemaError(
> +                            self,
> +                            'Non-existing included file "%s"' % include_path)

Should you also check against loops, and gracefully fail if a file tries
to include itself, or if file A includes B while B includes A?

This changes the code generator, but not the documentation.  For this
patch to be complete, you need to edit somewhere in the docs tree,
probably docs/qapi-code-gen.txt, to describe how to use includes.
diff mbox

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 9fc6fb2..80fb594 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -11,9 +11,13 @@ 
 # This work is licensed under the terms of the GNU GPLv2.
 # See the COPYING.LIB file in the top-level directory.
 
+import os
+import re
 from ordereddict import OrderedDict
 import sys
 
+include_cre = re.compile("include\(\"([^\"]*)\"\)")
+
 builtin_types = [
     'str', 'int', 'number', 'bool',
     'int8', 'int16', 'int32', 'int64',
@@ -54,8 +58,9 @@  class QAPISchemaError(Exception):
 
 class QAPISchema:
 
-    def __init__(self, fp):
+    def __init__(self, fp, input_dir):
         self.fp = fp
+        self.input_dir = input_dir
         self.src = fp.read()
         if self.src == '' or self.src[-1] != '\n':
             self.src += '\n'
@@ -100,6 +105,20 @@  class QAPISchema:
                 if self.cursor == len(self.src):
                     self.tok = None
                     return
+            elif self.tok == 'i':
+                include_src = self.src[self.cursor-1:]
+                include_match = include_cre.match(include_src)
+                if include_match is not None:
+                    include_path = os.path.join(self.input_dir,
+                                                include_match.group(1))
+                    if not os.path.isfile(include_path):
+                        raise QAPISchemaError(
+                            self,
+                            'Non-existing included file "%s"' % include_path)
+                    include_schema = QAPISchema(open(include_path),
+                                                self.input_dir)
+                    self.exprs += include_schema.exprs
+                    self.cursor += include_match.span()[1] - 1
             elif not self.tok.isspace():
                 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
 
@@ -159,8 +178,9 @@  class QAPISchema:
         return expr
 
 def parse_schema(input_path):
+    input_dir = os.path.dirname(input_path)
     try:
-        schema = QAPISchema(open(input_path, "r"))
+        schema = QAPISchema(open(input_path, "r"), input_dir)
     except QAPISchemaError, e:
         print >>sys.stderr, e
         exit(1)