qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH 0/9] Our QAPI parser is a hack, replace it


From: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH 0/9] Our QAPI parser is a hack, replace it
Date: Fri, 26 Jul 2013 17:36:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux)

Anthony Liguori <address@hidden> writes:

> Markus Armbruster <address@hidden> writes:
>
>> If you think I'm exaggerating, check out the list of issues in PATCH
>> 3/9.
>
> You are not.
>
> However, I think we can drop the whole thing and just use the JSON
> module in Python.  The bit below seems to work:
>
>   import json.decoder, re
>   from ordereddict import OrderedDict
>   
>   WHITESPACE = re.compile(r'(#.*\n|[ \r\t\n]*)*', re.MULTILINE)
>   
>   def make_object(pairs):
>       return OrderedDict(pairs)
>   
>   def qapi_parse(data):
>       _w = WHITESPACE.match
>       idx = 0
>       while idx < len(data):
>           idx = _w(data, idx).end()
>           if idx == len(data):
>               break
>           decoder = json.decoder.JSONDecoder(object_pairs_hook=make_object)
>           obj, idx = decoder.raw_decode(data, idx)
>           yield obj
>   
>   if __name__ == '__main__':
>       with open('qapi-schema.json', 'r') as fp:
>           data = fp.read().replace("'", '"')
>   
>       exprs = list(qapi_parse(data))
>       print exprs

I tried to find a way to use JSONDecoder, but not hard enough,
apparently.

The fp.read().replace("'", '"') is no good, because it blindly replaces
within strings, such as 'the cat\'s meow'.

Can your code handle comments between arbitrary tokens?  I suspect they
work only between top-level expressions, but I could be wrong; Python
isn't my strongest language, and I didn't test this.



reply via email to

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