From patchwork Fri Oct 12 21:11:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 191225 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A48E42C008B for ; Sat, 13 Oct 2012 08:28:58 +1100 (EST) Received: from localhost ([::1]:38851 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMmXI-0007ul-N4 for incoming@patchwork.ozlabs.org; Fri, 12 Oct 2012 17:12:48 -0400 Received: from eggs.gnu.org ([208.118.235.92]:42734) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMmWV-0006e0-4x for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:12:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TMmWT-00069p-0G for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:11:59 -0400 Received: from mail-ie0-f173.google.com ([209.85.223.173]:36593) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMmWS-0005iK-S3 for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:11:56 -0400 Received: by mail-ie0-f173.google.com with SMTP id 17so5335456iea.4 for ; Fri, 12 Oct 2012 14:11:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=GL56EO1U2HHFRsZXE+rOt+UKgU1RG/0YcvAJ8VG1AK0=; b=ap3NKzGvjRUraB4OAmxOmhL0GZRFwqTPyGLEZy6dRxG4HCn5MLnra7D2JJ0tzvU6wT KxHUmVL4xT9XRpXsM4oEVzt37RrVKsP77QAUDukzQ6MYmlqWisRmGfgU+4hvpDxLaddg RkP1n9nm6SFwfX3fYVUR/+NmG4Vp2VcaYCltc1WycSFXCY+sgacAZr0tQGvXY9Vc8e2a qk+BdNIr9jXUYoa6vGWWCL6XwymjgKA2TFUIn6ELhxKAww8rkCTmblNnU0AQg+UOpL/e uqO5rpxkZqd0A4n6d8UrUN52WHS7wF6A1vaugLILe/vRS2xC3cKa2MwQ+lm8zK4N23WK i8Tw== Received: by 10.50.219.233 with SMTP id pr9mr3354956igc.68.1350076316657; Fri, 12 Oct 2012 14:11:56 -0700 (PDT) Received: from loki.morrigu.org (cpe-72-179-62-111.austin.res.rr.com. [72.179.62.111]) by mx.google.com with ESMTPS id uj11sm2454777igb.15.2012.10.12.14.11.54 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 12 Oct 2012 14:11:55 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Fri, 12 Oct 2012 16:11:05 -0500 Message-Id: <1350076268-18461-24-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1350076268-18461-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1350076268-18461-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.223.173 Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com, blauwirbel@gmail.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH v4 23/26] qidl: add C parser (based on QC parser) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This introduces the QIDL parser to process QIDL annotations in C files. This code is mostly a straight import from qc.git, with some reworking to handle the declaration/annotation format and lexer we're using for QEMU. Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth --- scripts/qidl_parser.py | 269 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 scripts/qidl_parser.py diff --git a/scripts/qidl_parser.py b/scripts/qidl_parser.py new file mode 100644 index 0000000..7e8573f --- /dev/null +++ b/scripts/qidl_parser.py @@ -0,0 +1,269 @@ +# +# QEMU IDL Parser +# +# Copyright IBM, Corp. 2012 +# +# Authors: +# Anthony Liguori +# Michael Roth +# +# This work is licensed under the terms of the GNU GPLv2 or later. +# See the COPYING file in the top-level directory. + +import sys, json +from lexer import Input, CLexer + +def process_annotation(node, params): + annotation_type = params[0] + if annotation_type == "derived": + node['is_derived'] = True + elif annotation_type == 'immutable': + node['is_immutable'] = True + elif annotation_type == 'elsewhere': + node['is_elsewhere'] = True + elif annotation_type == 'broken': + node['is_broken'] = True + elif annotation_type == 'size_is': + node['is_array'] = True + expression = params[1] + if expression.isdigit(): + expression = int(expression) + node['array_size'] = expression + elif annotation_type == 'optional': + node['is_optional'] = True + elif annotation_type == 'property': + node['is_property'] = True + if node.has_key('property_fields'): + node['property_fields'].append(params[1:]) + else: + node['property_fields'] = [params[1:]] + + return node + +def parse_annotations(l, node): + while l.check_token('symbol', 'QIDL'): + params = [] + l.pop() + + l.pop_expected('operator', '(') + open_parens = 1 + param = "" + while open_parens: + if l.check_token('operator', ','): + params.append(param) + param = "" + l.pop() + continue + + if l.check_token('operator', '('): + open_parens += 1 + elif l.check_token('operator', ')'): + open_parens -= 1 + + if open_parens > 0: + param += l.peek() + + l.pop() + + if param != "": + params.append(param) + + node = process_annotation(node, params) + + return node + +def parse_type(l): + node = {} + + typename = '' + if l.check_token('const', 'const'): + typename += l.pop() + ' ' + + if l.check_token('struct', 'struct'): + typename += l.pop() + ' ' + + if l.check_token('unsigned', 'unsigned'): + typename += l.pop() + ' ' + + if l.check_token('union', 'union'): + typename += l.pop() + ' ' + + if l.check_token('enum', 'enum'): + typename += l.pop() + ' ' + + # we don't currently handle embedded struct declarations, skip them for now + if l.check_token('operator', '{'): + open_braces = 1 + while open_braces: + l.pop() + if l.check_token('operator', '{'): + open_braces += 1 + elif l.check_token('operator', '}'): + open_braces -= 1 + l.pop() + typename += "" + node['is_nested_decl'] = True + else: + if l.check_token('operator', '*'): + l.pop() + node['is_pointer'] = True + else: + typename += l.pop_expected('symbol') + + node['type'] = typename + + node = parse_annotations(l, node) + + if l.check_token('operator', '*'): + l.pop() + node['is_pointer'] = True + + return node + +def parse_var_decl(l, repeating_type=None): + if repeating_type == None: + node = parse_type(l) + else: + node = { 'type': repeating_type } + + if l.check_token('operator', '('): + node['is_function'] = True + l.pop() + l.pop_expected('operator', '*') + variable = l.pop_expected('symbol') + l.pop_expected('operator', ')') + + # skip the param list since we don't use it currently + l.pop_expected('operator', '(') + open_parens = 1 + while open_parens: + if l.check_token('operator', '('): + open_parens += 1 + elif l.check_token('operator', ')'): + open_parens -= 1 + l.pop() + else: + variable = l.pop_expected('symbol') + node['variable'] = variable + + if l.check_token('operator', '['): + l.pop() + expression = "" + while not l.check_token('operator', ']'): + expression += l.pop() + l.pop_expected('operator', ']') + + if not node.has_key('is_array'): + node['is_array'] = True + if expression.isdigit(): + expression = int(expression) + node['array_size'] = expression + else: + if expression.isdigit(): + expression = int(expression) + node['array_capacity'] = expression + + node = parse_annotations(l, node) + + return node + +def parse_struct(l): + l.pop_expected('struct', 'struct') + + name = None + if l.check_token('symbol'): + name = l.pop() + + l.pop_expected('operator', '{') + + nodes = [] + + while not l.check_token('operator', '}'): + node = parse_var_decl(l) + nodes.append(node) + while l.check_token('operator', ','): + l.pop() + node = parse_var_decl(l, node['type']) + nodes.append(node) + + l.pop_expected('operator', ';') + + l.pop() + + return { 'struct': name, 'fields': nodes } + +def parse_typedef(l): + l.pop_expected('typedef', 'typedef') + + node = parse_struct(l) + typename = l.pop_expected('symbol') + + return { 'typedef': typename, 'type': node } + +def parse_declaration_params(l): + declaration_info = {} + params = [] + arg_string = "" + parens = 0 + l.pop_expected('symbol', 'QIDL_START') + while not l.eof(): + if l.check_token('operator', '('): + parens += 1 + elif l.check_token('operator', ')'): + parens -= 1 + if parens == 0: + break + elif parens > 0: + if not l.check_token('operator', ','): + params.append(l.peek()) + l.pop() + l.pop_expected('operator', ')') + if parens != 0: + raise Exception("unmatched parenthesis in QIDL macro") + + declaration_info['id'] = params[0] + declaration_info['do_state'] = True + declaration_info['do_properties'] = True + if "skip_state" in params: + declaration_info['do_state'] = False + if "skip_properties" in params: + declaration_info['do_properties'] = False + + return declaration_info + +def parse_declaration(l): + declaration_info = parse_declaration_params(l) + + if l.check_token('typedef'): + node = parse_typedef(l) + elif l.check_token('struct'): + node = parse_struct(l) + else: + raise Exception("unsupported QIDL declaration") + + l.pop_expected('operator', ';') + node['id'] = declaration_info['id'] + node['do_state'] = declaration_info['do_state'] + node['do_properties'] = declaration_info['do_properties'] + + return node + +def parse_file(f): + nodes = [] + filtered_tokens = ['whitespace', 'comment', 'directive'] + l = CLexer(Input(f), filtered_tokens) + while not l.eof(): + line = l.peek_line() + if line.startswith("QIDL_START("): + node = parse_declaration(l) + nodes.append(node) + else: + l.pop_line() + return nodes + +def main(): + nodes = parse_file(sys.stdin) + print json.dumps(nodes, sort_keys=True, indent=2) + +if __name__ == '__main__': + main()